Compare commits

...

4 Commits

Author SHA1 Message Date
12e8cd6a5e Remove enums that only existed for optional tags 2023-11-19 20:38:45 -06:00
5a0043c959 Fix up tests 2023-11-17 17:27:26 -06:00
4eeb41ba06 Parameters can now be optional 2023-11-17 17:10:18 -06:00
2693c93bb7 Add ? token 2023-11-17 15:27:21 -06:00
12 changed files with 431 additions and 341 deletions

View File

@ -169,16 +169,24 @@ describe('testing function declaration', () => {
end: 39, end: 39,
params: [ params: [
{ {
type: 'Identifier', type: 'Parameter',
start: 12, identifier: {
end: 13, type: 'Identifier',
name: 'a', start: 12,
end: 13,
name: 'a',
},
optional: false,
}, },
{ {
type: 'Identifier', type: 'Parameter',
start: 15, identifier: {
end: 16, type: 'Identifier',
name: 'b', start: 15,
end: 16,
name: 'b',
},
optional: false,
}, },
], ],
body: { body: {
@ -244,16 +252,24 @@ const myVar = funcN(1, 2)`
end: 37, end: 37,
params: [ params: [
{ {
type: 'Identifier', type: 'Parameter',
start: 12, identifier: {
end: 13, type: 'Identifier',
name: 'a', start: 12,
end: 13,
name: 'a',
},
optional: false,
}, },
{ {
type: 'Identifier', type: 'Parameter',
start: 15, identifier: {
end: 16, type: 'Identifier',
name: 'b', start: 15,
end: 16,
name: 'b',
},
optional: false,
}, },
], ],
body: { body: {

View File

@ -1,5 +1,5 @@
import { getNodePathFromSourceRange, getNodeFromPath } from './queryAst' import { getNodePathFromSourceRange, getNodeFromPath } from './queryAst'
import { Identifier, parse, initPromise } from './wasm' import { Identifier, parse, initPromise, Parameter } from './wasm'
beforeAll(() => initPromise) beforeAll(() => initPromise)
@ -46,7 +46,7 @@ const b1 = cube([0,0], 10)`
const ast = parse(code) const ast = parse(code)
const nodePath = getNodePathFromSourceRange(ast, sourceRange) const nodePath = getNodePathFromSourceRange(ast, sourceRange)
const node = getNodeFromPath<Identifier>(ast, nodePath).node const node = getNodeFromPath<Parameter>(ast, nodePath).node
expect(nodePath).toEqual([ expect(nodePath).toEqual([
['body', ''], ['body', ''],
@ -57,8 +57,8 @@ const b1 = cube([0,0], 10)`
['params', 'FunctionExpression'], ['params', 'FunctionExpression'],
[0, 'index'], [0, 'index'],
]) ])
expect(node.type).toBe('Identifier') expect(node.type).toBe('Parameter')
expect(node.name).toBe('pos') expect(node.identifier.name).toBe('pos')
}) })
it('gets path right for deep within function definition body', () => { it('gets path right for deep within function definition body', () => {
const code = `fn cube = (pos, scale) => { const code = `fn cube = (pos, scale) => {

View File

@ -247,10 +247,10 @@ function moreNodePathFromSourceRange(
if (_node.type === 'FunctionExpression' && isInRange) { if (_node.type === 'FunctionExpression' && isInRange) {
for (let i = 0; i < _node.params.length; i++) { for (let i = 0; i < _node.params.length; i++) {
const param = _node.params[i] const param = _node.params[i]
if (param.start <= start && param.end >= end) { if (param.identifier.start <= start && param.identifier.end >= end) {
path.push(['params', 'FunctionExpression']) path.push(['params', 'FunctionExpression'])
path.push([i, 'index']) path.push([i, 'index'])
return moreNodePathFromSourceRange(param, sourceRange, path) return moreNodePathFromSourceRange(param.identifier, sourceRange, path)
} }
} }
if (_node.body.start <= start && _node.body.end >= end) { if (_node.body.start <= start && _node.body.end >= end) {

View File

@ -20,6 +20,7 @@ export type { ObjectExpression } from '../wasm-lib/kcl/bindings/ObjectExpression
export type { MemberExpression } from '../wasm-lib/kcl/bindings/MemberExpression' export type { MemberExpression } from '../wasm-lib/kcl/bindings/MemberExpression'
export type { PipeExpression } from '../wasm-lib/kcl/bindings/PipeExpression' export type { PipeExpression } from '../wasm-lib/kcl/bindings/PipeExpression'
export type { VariableDeclaration } from '../wasm-lib/kcl/bindings/VariableDeclaration' export type { VariableDeclaration } from '../wasm-lib/kcl/bindings/VariableDeclaration'
export type { Parameter } from '../wasm-lib/kcl/bindings/Parameter'
export type { PipeSubstitution } from '../wasm-lib/kcl/bindings/PipeSubstitution' export type { PipeSubstitution } from '../wasm-lib/kcl/bindings/PipeSubstitution'
export type { Identifier } from '../wasm-lib/kcl/bindings/Identifier' export type { Identifier } from '../wasm-lib/kcl/bindings/Identifier'
export type { UnaryExpression } from '../wasm-lib/kcl/bindings/UnaryExpression' export type { UnaryExpression } from '../wasm-lib/kcl/bindings/UnaryExpression'

View File

@ -221,11 +221,11 @@ impl Program {
if let Some(Value::FunctionExpression(ref mut function_expression)) = &mut value { if let Some(Value::FunctionExpression(ref mut function_expression)) = &mut value {
// Check if the params to the function expression contain the position. // Check if the params to the function expression contain the position.
for param in &mut function_expression.params { for param in &mut function_expression.params {
let param_source_range: SourceRange = param.clone().into(); let param_source_range: SourceRange = (&param.identifier).into();
if param_source_range.contains(pos) { if param_source_range.contains(pos) {
let old_name = param.name.clone(); let old_name = param.identifier.name.clone();
// Rename the param. // Rename the param.
param.rename(&old_name, new_name); param.identifier.rename(&old_name, new_name);
// Now rename all the identifiers in the rest of the program. // Now rename all the identifiers in the rest of the program.
function_expression.body.rename_identifiers(&old_name, new_name); function_expression.body.rename_identifiers(&old_name, new_name);
return; return;
@ -1014,7 +1014,11 @@ impl CallExpression {
// Add the arguments to the memory. // Add the arguments to the memory.
let mut fn_memory = memory.clone(); let mut fn_memory = memory.clone();
for (index, param) in function_expression.params.iter().enumerate() { for (index, param) in function_expression.params.iter().enumerate() {
fn_memory.add(&param.name, fn_args.get(index).unwrap().clone(), param.into())?; fn_memory.add(
&param.identifier.name,
fn_args.get(index).unwrap().clone(),
param.identifier.clone().into(),
)?;
} }
// Call the stdlib function // Call the stdlib function
@ -1249,10 +1253,10 @@ impl VariableDeclaration {
symbol_kind = SymbolKind::FUNCTION; symbol_kind = SymbolKind::FUNCTION;
let mut children = vec![]; let mut children = vec![];
for param in &function_expression.params { for param in &function_expression.params {
let param_source_range: SourceRange = param.into(); let param_source_range: SourceRange = (&param.identifier).into();
#[allow(deprecated)] #[allow(deprecated)]
children.push(DocumentSymbol { children.push(DocumentSymbol {
name: param.name.clone(), name: param.identifier.name.clone(),
detail: None, detail: None,
kind: SymbolKind::VARIABLE, kind: SymbolKind::VARIABLE,
range: param_source_range.to_lsp_range(code), range: param_source_range.to_lsp_range(code),
@ -1442,7 +1446,7 @@ impl From<&Box<Literal>> for MemoryItem {
} }
} }
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake, Eq)]
#[databake(path = kcl_lib::ast::types)] #[databake(path = kcl_lib::ast::types)]
#[ts(export)] #[ts(export)]
#[serde(tag = "type")] #[serde(tag = "type")]
@ -2621,6 +2625,18 @@ async fn execute_pipe_body(
} }
} }
/// Parameter of a KCL function.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib::ast::types)]
#[ts(export)]
#[serde(tag = "type")]
pub struct Parameter {
/// The parameter's label or name.
pub identifier: Identifier,
/// Is the parameter optional?
pub optional: bool,
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib::ast::types)] #[databake(path = kcl_lib::ast::types)]
#[ts(export)] #[ts(export)]
@ -2628,7 +2644,7 @@ async fn execute_pipe_body(
pub struct FunctionExpression { pub struct FunctionExpression {
pub start: usize, pub start: usize,
pub end: usize, pub end: usize,
pub params: Vec<Identifier>, pub params: Vec<Parameter>,
pub body: Program, pub body: Program,
} }
@ -2654,7 +2670,7 @@ impl FunctionExpression {
"({}) => {{\n{}{}\n}}", "({}) => {{\n{}{}\n}}",
self.params self.params
.iter() .iter()
.map(|param| param.name.clone()) .map(|param| param.identifier.name.clone())
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join(", "), .join(", "),
options.get_indentation(indentation_level + 1), options.get_indentation(indentation_level + 1),

View File

@ -908,9 +908,9 @@ pub async fn execute(
// Add the arguments to the memory. // Add the arguments to the memory.
for (index, param) in function_expression.params.iter().enumerate() { for (index, param) in function_expression.params.iter().enumerate() {
fn_memory.add( fn_memory.add(
&param.name, &param.identifier.name,
args.get(index).unwrap().clone(), args.get(index).unwrap().clone(),
param.into(), (&param.identifier).into(),
)?; )?;
} }

View File

@ -12,7 +12,7 @@ use crate::{
ArrayExpression, BinaryExpression, BinaryOperator, BinaryPart, BodyItem, CallExpression, CommentStyle, ArrayExpression, BinaryExpression, BinaryOperator, BinaryPart, BodyItem, CallExpression, CommentStyle,
ExpressionStatement, FunctionExpression, Identifier, Literal, LiteralIdentifier, LiteralValue, ExpressionStatement, FunctionExpression, Identifier, Literal, LiteralIdentifier, LiteralValue,
MemberExpression, MemberObject, NonCodeMeta, NonCodeNode, NonCodeValue, ObjectExpression, ObjectProperty, MemberExpression, MemberObject, NonCodeMeta, NonCodeNode, NonCodeValue, ObjectExpression, ObjectProperty,
PipeExpression, PipeSubstitution, Program, ReturnStatement, UnaryExpression, UnaryOperator, Value, Parameter, PipeExpression, PipeSubstitution, Program, ReturnStatement, UnaryExpression, UnaryOperator, Value,
VariableDeclaration, VariableDeclarator, VariableKind, VariableDeclaration, VariableDeclarator, VariableKind,
}, },
errors::{KclError, KclErrorDetails}, errors::{KclError, KclErrorDetails},
@ -1208,26 +1208,62 @@ fn arguments(i: TokenSlice) -> PResult<Vec<Value>> {
.parse_next(i) .parse_next(i)
} }
fn not_close_paren(i: TokenSlice) -> PResult<Token> { fn required_param(i: TokenSlice) -> PResult<Token> {
any.verify(|token: &Token| !matches!(token.token_type, TokenType::Brace) || token.value != ")") any.verify(|token: &Token| !matches!(token.token_type, TokenType::Brace) || token.value != ")")
.parse_next(i) .parse_next(i)
} }
fn optional_param(i: TokenSlice) -> PResult<Token> {
let token = required_param.parse_next(i)?;
let _question_mark = one_of(TokenType::QuestionMark).parse_next(i)?;
Ok(token)
}
/// Parameters are declared in a function signature, and used within a function. /// Parameters are declared in a function signature, and used within a function.
fn parameters(i: TokenSlice) -> PResult<Vec<Identifier>> { fn parameters(i: TokenSlice) -> PResult<Vec<Parameter>> {
// Get all tokens until the next ), because that ends the parameter list. // Get all tokens until the next ), because that ends the parameter list.
let candidates: Vec<Token> = separated(0.., not_close_paren, comma_sep) let candidates: Vec<_> = separated(
.context(expected("function parameters")) 0..,
.parse_next(i)?; alt((optional_param.map(|t| (t, true)), required_param.map(|t| (t, false)))),
comma_sep,
)
.context(expected("function parameters"))
.parse_next(i)?;
// Make sure all those tokens are valid parameters. // Make sure all those tokens are valid parameters.
let params = candidates let params: Vec<Parameter> = candidates
.into_iter() .into_iter()
.map(|token| Identifier::try_from(token).and_then(Identifier::into_valid_binding_name)) .map(|(token, optional)| {
let identifier = Identifier::try_from(token).and_then(Identifier::into_valid_binding_name)?;
Ok(Parameter { identifier, optional })
})
.collect::<Result<_, _>>() .collect::<Result<_, _>>()
.map_err(|e| ErrMode::Backtrack(ContextError::from(e)))?; .map_err(|e: KclError| ErrMode::Backtrack(ContextError::from(e)))?;
// Make sure optional parameters are last.
if let Err(e) = optional_after_required(&params) {
return Err(ErrMode::Cut(ContextError::from(e)));
}
Ok(params) Ok(params)
} }
fn optional_after_required(params: &[Parameter]) -> Result<(), KclError> {
let mut found_optional = false;
for p in params {
if p.optional {
found_optional = true;
}
if !p.optional && found_optional {
let e = KclError::Syntax(KclErrorDetails {
source_ranges: vec![(&p.identifier).into()],
message: "mandatory parameters must be declared before optional parameters".to_owned(),
});
return Err(e);
}
}
Ok(())
}
impl Identifier { impl Identifier {
fn into_valid_binding_name(self) -> Result<Identifier, KclError> { fn into_valid_binding_name(self) -> Result<Identifier, KclError> {
// Make sure they are not assigning a variable to a stdlib function. // Make sure they are not assigning a variable to a stdlib function.
@ -1895,7 +1931,7 @@ const mySk1 = startSketchAt([0, 0])"#;
let tokens = crate::token::lexer(input); let tokens = crate::token::lexer(input);
let actual = parameters.parse(&tokens); let actual = parameters.parse(&tokens);
assert!(actual.is_ok(), "could not parse test {i}"); assert!(actual.is_ok(), "could not parse test {i}");
let actual_ids: Vec<_> = actual.unwrap().into_iter().map(|id| id.name).collect(); let actual_ids: Vec<_> = actual.unwrap().into_iter().map(|p| p.identifier.name).collect();
assert_eq!(actual_ids, expected); assert_eq!(actual_ids, expected);
} }
} }
@ -2322,6 +2358,82 @@ e
assert!(result.err().unwrap().to_string().contains("Unexpected token")); assert!(result.err().unwrap().to_string().contains("Unexpected token"));
} }
#[test]
fn test_optional_param_order() {
for (i, (params, expect_ok)) in [
(
vec![Parameter {
identifier: Identifier {
start: 0,
end: 0,
name: "a".to_owned(),
},
optional: true,
}],
true,
),
(
vec![Parameter {
identifier: Identifier {
start: 0,
end: 0,
name: "a".to_owned(),
},
optional: false,
}],
true,
),
(
vec![
Parameter {
identifier: Identifier {
start: 0,
end: 0,
name: "a".to_owned(),
},
optional: false,
},
Parameter {
identifier: Identifier {
start: 0,
end: 0,
name: "b".to_owned(),
},
optional: true,
},
],
true,
),
(
vec![
Parameter {
identifier: Identifier {
start: 0,
end: 0,
name: "a".to_owned(),
},
optional: true,
},
Parameter {
identifier: Identifier {
start: 0,
end: 0,
name: "b".to_owned(),
},
optional: false,
},
],
false,
),
]
.into_iter()
.enumerate()
{
let actual = optional_after_required(&params);
assert_eq!(actual.is_ok(), expect_ok, "failed test {i}");
}
}
#[test] #[test]
fn test_parse_expand_array() { fn test_parse_expand_array() {
let code = "const myArray = [0..10]"; let code = "const myArray = [0..10]";
@ -2801,4 +2913,5 @@ mod snapshot_tests {
snapshot_test!(ar, r#"5 + "a""#); snapshot_test!(ar, r#"5 + "a""#);
snapshot_test!(at, "line([0, l], %)"); snapshot_test!(at, "line([0, l], %)");
snapshot_test!(au, include_str!("../../../tests/executor/inputs/cylinder.kcl")); snapshot_test!(au, include_str!("../../../tests/executor/inputs/cylinder.kcl"));
snapshot_test!(av, "fn f = (angle?) => { return default(angle, 360) }");
} }

View File

@ -29,10 +29,13 @@ expression: actual
"end": 49, "end": 49,
"params": [ "params": [
{ {
"type": "Identifier", "identifier": {
"start": 12, "type": "Identifier",
"end": 17, "start": 12,
"name": "param" "end": 17,
"name": "param"
},
"optional": false
} }
], ],
"body": { "body": {

View File

@ -0,0 +1,97 @@
---
source: kcl/src/parser/parser_impl.rs
expression: actual
---
{
"start": 0,
"end": 49,
"body": [
{
"type": "VariableDeclaration",
"type": "VariableDeclaration",
"start": 0,
"end": 49,
"declarations": [
{
"type": "VariableDeclarator",
"start": 3,
"end": 49,
"id": {
"type": "Identifier",
"start": 3,
"end": 4,
"name": "f"
},
"init": {
"type": "FunctionExpression",
"type": "FunctionExpression",
"start": 7,
"end": 49,
"params": [
{
"identifier": {
"type": "Identifier",
"start": 8,
"end": 13,
"name": "angle"
},
"optional": true
}
],
"body": {
"start": 19,
"end": 49,
"body": [
{
"type": "ReturnStatement",
"type": "ReturnStatement",
"start": 21,
"end": 47,
"argument": {
"type": "CallExpression",
"type": "CallExpression",
"start": 28,
"end": 47,
"callee": {
"type": "Identifier",
"start": 28,
"end": 35,
"name": "default"
},
"arguments": [
{
"type": "Identifier",
"type": "Identifier",
"start": 36,
"end": 41,
"name": "angle"
},
{
"type": "Literal",
"type": "Literal",
"start": 43,
"end": 46,
"value": 360,
"raw": "360"
}
],
"optional": false
}
}
],
"nonCodeMeta": {
"nonCodeNodes": {},
"start": []
}
}
}
}
],
"kind": "fn"
}
],
"nonCodeMeta": {
"nonCodeNodes": {},
"start": []
}
}

View File

@ -20,17 +20,12 @@ use crate::{
/// Data to draw a line to a point. /// Data to draw a line to a point.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)] #[ts(export)]
#[serde(rename_all = "camelCase", untagged)] #[serde(rename_all = "camelCase")]
pub enum LineToData { pub struct LineToData {
/// A point with a tag. /// The to point.
PointWithTag { to: [f64; 2],
/// The to point. /// The tag.
to: [f64; 2], tag: Option<String>,
/// The tag.
tag: String,
},
/// A point.
Point([f64; 2]),
} }
/// Draw a line to a point. /// Draw a line to a point.
@ -51,10 +46,8 @@ async fn inner_line_to(
args: Args, args: Args,
) -> Result<Box<SketchGroup>, KclError> { ) -> Result<Box<SketchGroup>, KclError> {
let from = sketch_group.get_coords_from_paths()?; let from = sketch_group.get_coords_from_paths()?;
let to = match data { let to = data.to;
LineToData::PointWithTag { to, .. } => to, let name = data.tag.unwrap_or_default();
LineToData::Point(to) => to,
};
let id = uuid::Uuid::new_v4(); let id = uuid::Uuid::new_v4();
@ -63,11 +56,7 @@ async fn inner_line_to(
ModelingCmd::ExtendPath { ModelingCmd::ExtendPath {
path: sketch_group.id, path: sketch_group.id,
segment: kittycad::types::PathSegment::Line { segment: kittycad::types::PathSegment::Line {
end: Point3D { end: point3d(to),
x: to[0],
y: to[1],
z: 0.0,
},
relative: false, relative: false,
}, },
}, },
@ -78,11 +67,7 @@ async fn inner_line_to(
base: BasePath { base: BasePath {
from: from.into(), from: from.into(),
to, to,
name: if let LineToData::PointWithTag { tag, .. } = data { name,
tag.to_string()
} else {
"".to_string()
},
geo_meta: GeoMeta { geo_meta: GeoMeta {
id, id,
metadata: args.source_range.into(), metadata: args.source_range.into(),
@ -99,17 +84,12 @@ async fn inner_line_to(
/// Data to draw a line to a point on an axis. /// Data to draw a line to a point on an axis.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)] #[ts(export)]
#[serde(rename_all = "camelCase", untagged)] #[serde(rename_all = "camelCase")]
pub enum AxisLineToData { pub struct AxisLineToData {
/// A point with a tag. /// The to point.
PointWithTag { to: f64,
/// The to point. /// The tag.
to: f64, tag: Option<String>,
/// The tag.
tag: String,
},
/// A point.
Point(f64),
} }
/// Draw a line to a point on the x-axis. /// Draw a line to a point on the x-axis.
@ -131,9 +111,9 @@ async fn inner_x_line_to(
) -> Result<Box<SketchGroup>, KclError> { ) -> Result<Box<SketchGroup>, KclError> {
let from = sketch_group.get_coords_from_paths()?; let from = sketch_group.get_coords_from_paths()?;
let line_to_data = match data { let line_to_data = LineToData {
AxisLineToData::PointWithTag { to, tag } => LineToData::PointWithTag { to: [to, from.y], tag }, to: [data.to, from.y],
AxisLineToData::Point(data) => LineToData::Point([data, from.y]), tag: data.tag,
}; };
let new_sketch_group = inner_line_to(line_to_data, sketch_group, args).await?; let new_sketch_group = inner_line_to(line_to_data, sketch_group, args).await?;
@ -160,9 +140,9 @@ async fn inner_y_line_to(
) -> Result<Box<SketchGroup>, KclError> { ) -> Result<Box<SketchGroup>, KclError> {
let from = sketch_group.get_coords_from_paths()?; let from = sketch_group.get_coords_from_paths()?;
let line_to_data = match data { let line_to_data = LineToData {
AxisLineToData::PointWithTag { to, tag } => LineToData::PointWithTag { to: [from.x, to], tag }, to: [from.x, data.to],
AxisLineToData::Point(data) => LineToData::Point([from.x, data]), tag: data.tag,
}; };
let new_sketch_group = inner_line_to(line_to_data, sketch_group, args).await?; let new_sketch_group = inner_line_to(line_to_data, sketch_group, args).await?;
@ -172,17 +152,12 @@ async fn inner_y_line_to(
/// Data to draw a line. /// Data to draw a line.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)] #[ts(export)]
#[serde(rename_all = "camelCase", untagged)] #[serde(rename_all = "camelCase")]
pub enum LineData { pub struct LineData {
/// A point with a tag. /// The to point.
PointWithTag { to: [f64; 2],
/// The to point. /// The tag.
to: [f64; 2], tag: Option<String>,
/// The tag.
tag: String,
},
/// A point.
Point([f64; 2]),
} }
/// Draw a line. /// Draw a line.
@ -199,10 +174,7 @@ pub async fn line(args: Args) -> Result<MemoryItem, KclError> {
}] }]
async fn inner_line(data: LineData, sketch_group: Box<SketchGroup>, args: Args) -> Result<Box<SketchGroup>, KclError> { async fn inner_line(data: LineData, sketch_group: Box<SketchGroup>, args: Args) -> Result<Box<SketchGroup>, KclError> {
let from = sketch_group.get_coords_from_paths()?; let from = sketch_group.get_coords_from_paths()?;
let inner_args = match &data { let inner_args = data.to;
LineData::PointWithTag { to, .. } => *to,
LineData::Point(to) => *to,
};
let delta = inner_args; let delta = inner_args;
let to = [from.x + inner_args[0], from.y + inner_args[1]]; let to = [from.x + inner_args[0], from.y + inner_args[1]];
@ -214,11 +186,7 @@ async fn inner_line(data: LineData, sketch_group: Box<SketchGroup>, args: Args)
ModelingCmd::ExtendPath { ModelingCmd::ExtendPath {
path: sketch_group.id, path: sketch_group.id,
segment: kittycad::types::PathSegment::Line { segment: kittycad::types::PathSegment::Line {
end: Point3D { end: point3d(delta),
x: delta[0],
y: delta[1],
z: 0.0,
},
relative: true, relative: true,
}, },
}, },
@ -229,11 +197,7 @@ async fn inner_line(data: LineData, sketch_group: Box<SketchGroup>, args: Args)
base: BasePath { base: BasePath {
from: from.into(), from: from.into(),
to, to,
name: if let LineData::PointWithTag { tag, .. } = data { name: data.tag.unwrap_or_default(),
tag.to_string()
} else {
"".to_string()
},
geo_meta: GeoMeta { geo_meta: GeoMeta {
id, id,
metadata: args.source_range.into(), metadata: args.source_range.into(),
@ -250,17 +214,12 @@ async fn inner_line(data: LineData, sketch_group: Box<SketchGroup>, args: Args)
/// Data to draw a line on an axis. /// Data to draw a line on an axis.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)] #[ts(export)]
#[serde(rename_all = "camelCase", untagged)] #[serde(rename_all = "camelCase")]
pub enum AxisLineData { pub struct AxisLineData {
/// The length with a tag. /// The length of the line.
LengthWithTag { length: f64,
/// The length of the line. /// The tag.
length: f64, tag: Option<String>,
/// The tag.
tag: String,
},
/// The length.
Length(f64),
} }
/// Draw a line on the x-axis. /// Draw a line on the x-axis.
@ -280,9 +239,9 @@ async fn inner_x_line(
sketch_group: Box<SketchGroup>, sketch_group: Box<SketchGroup>,
args: Args, args: Args,
) -> Result<Box<SketchGroup>, KclError> { ) -> Result<Box<SketchGroup>, KclError> {
let line_data = match data { let line_data = LineData {
AxisLineData::LengthWithTag { length, tag } => LineData::PointWithTag { to: [length, 0.0], tag }, to: [data.length, 0.0],
AxisLineData::Length(length) => LineData::Point([length, 0.0]), tag: data.tag,
}; };
let new_sketch_group = inner_line(line_data, sketch_group, args).await?; let new_sketch_group = inner_line(line_data, sketch_group, args).await?;
@ -306,9 +265,9 @@ async fn inner_y_line(
sketch_group: Box<SketchGroup>, sketch_group: Box<SketchGroup>,
args: Args, args: Args,
) -> Result<Box<SketchGroup>, KclError> { ) -> Result<Box<SketchGroup>, KclError> {
let line_data = match data { let line_data = LineData {
AxisLineData::LengthWithTag { length, tag } => LineData::PointWithTag { to: [0.0, length], tag }, to: [0.0, data.length],
AxisLineData::Length(length) => LineData::Point([0.0, length]), tag: data.tag,
}; };
let new_sketch_group = inner_line(line_data, sketch_group, args).await?; let new_sketch_group = inner_line(line_data, sketch_group, args).await?;
@ -318,28 +277,19 @@ async fn inner_y_line(
/// Data to draw an angled line. /// Data to draw an angled line.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)] #[ts(export)]
#[serde(rename_all = "camelCase", untagged)] #[serde(rename_all = "camelCase")]
pub enum AngledLineData { pub struct AngledLineData {
/// An angle and length with a tag. /// The angle of the line.
AngleWithTag { angle: f64,
/// The angle of the line. /// The length of the line.
angle: f64, length: f64,
/// The length of the line. /// The tag.
length: f64, tag: Option<String>,
/// The tag.
tag: String,
},
/// An angle and length.
AngleAndLength([f64; 2]),
} }
impl AngledLineData { impl AngledLineData {
pub fn into_inner_line(self, to: [f64; 2]) -> LineData { pub fn into_inner_line(self, to: [f64; 2]) -> LineData {
if let AngledLineData::AngleWithTag { tag, .. } = self { LineData { to, tag: self.tag }
LineData::PointWithTag { to, tag }
} else {
LineData::Point(to)
}
} }
} }
@ -361,10 +311,7 @@ async fn inner_angled_line(
args: Args, args: Args,
) -> Result<Box<SketchGroup>, KclError> { ) -> Result<Box<SketchGroup>, KclError> {
let from = sketch_group.get_coords_from_paths()?; let from = sketch_group.get_coords_from_paths()?;
let (angle, length) = match &data { let AngledLineData { angle, length, tag } = data;
AngledLineData::AngleWithTag { angle, length, .. } => (*angle, *length),
AngledLineData::AngleAndLength(angle_and_length) => (angle_and_length[0], angle_and_length[1]),
};
//double check me on this one - mike //double check me on this one - mike
let delta: [f64; 2] = [ let delta: [f64; 2] = [
@ -381,11 +328,7 @@ async fn inner_angled_line(
base: BasePath { base: BasePath {
from: from.into(), from: from.into(),
to, to,
name: if let AngledLineData::AngleWithTag { tag, .. } = data { name: tag.unwrap_or_default(),
tag.to_string()
} else {
"".to_string()
},
geo_meta: GeoMeta { geo_meta: GeoMeta {
id, id,
metadata: args.source_range.into(), metadata: args.source_range.into(),
@ -398,11 +341,7 @@ async fn inner_angled_line(
ModelingCmd::ExtendPath { ModelingCmd::ExtendPath {
path: sketch_group.id, path: sketch_group.id,
segment: kittycad::types::PathSegment::Line { segment: kittycad::types::PathSegment::Line {
end: Point3D { end: point3d(delta),
x: delta[0],
y: delta[1],
z: 0.0,
},
relative, relative,
}, },
}, },
@ -431,12 +370,7 @@ async fn inner_angled_line_of_x_length(
sketch_group: Box<SketchGroup>, sketch_group: Box<SketchGroup>,
args: Args, args: Args,
) -> Result<Box<SketchGroup>, KclError> { ) -> Result<Box<SketchGroup>, KclError> {
let (angle, length) = match &data { let to = get_y_component(Angle::from_degrees(data.angle), data.length);
AngledLineData::AngleWithTag { angle, length, .. } => (*angle, *length),
AngledLineData::AngleAndLength(angle_and_length) => (angle_and_length[0], angle_and_length[1]),
};
let to = get_y_component(Angle::from_degrees(angle), length);
let new_sketch_group = inner_line(data.into_inner_line(to.into()), sketch_group, args).await?; let new_sketch_group = inner_line(data.into_inner_line(to.into()), sketch_group, args).await?;
@ -446,28 +380,20 @@ async fn inner_angled_line_of_x_length(
/// Data to draw an angled line to a point. /// Data to draw an angled line to a point.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)] #[ts(export)]
#[serde(rename_all = "camelCase", untagged)] #[serde(rename_all = "camelCase")]
pub enum AngledLineToData { pub struct AngledLineToData {
/// An angle and point with a tag. /// The angle of the line.
AngleWithTag { angle: f64,
/// The angle of the line. /// The point to draw to.
angle: f64, to: f64,
/// The point to draw to. /// The tag.
to: f64, tag: Option<String>,
/// The tag.
tag: String,
},
/// An angle and point to draw to.
AngleAndPoint([f64; 2]),
} }
impl AngledLineToData { impl AngledLineToData {
pub fn into_inner_line(self, x_to: f64, y_to: f64) -> LineToData { pub fn into_inner_line(self, x_to: f64, y_to: f64) -> LineToData {
if let AngledLineToData::AngleWithTag { tag, .. } = self { let tag = self.tag;
LineToData::PointWithTag { to: [x_to, y_to], tag } LineToData { to: [x_to, y_to], tag }
} else {
LineToData::Point([x_to, y_to])
}
} }
} }
@ -489,10 +415,7 @@ async fn inner_angled_line_to_x(
args: Args, args: Args,
) -> Result<Box<SketchGroup>, KclError> { ) -> Result<Box<SketchGroup>, KclError> {
let from = sketch_group.get_coords_from_paths()?; let from = sketch_group.get_coords_from_paths()?;
let (angle, x_to) = match &data { let (angle, x_to) = (data.angle, data.to);
AngledLineToData::AngleWithTag { angle, to, .. } => (*angle, *to),
AngledLineToData::AngleAndPoint(angle_and_to) => (angle_and_to[0], angle_and_to[1]),
};
let x_component = x_to - from.x; let x_component = x_to - from.x;
let y_component = x_component * f64::tan(angle.to_radians()); let y_component = x_component * f64::tan(angle.to_radians());
@ -520,10 +443,7 @@ async fn inner_angled_line_of_y_length(
sketch_group: Box<SketchGroup>, sketch_group: Box<SketchGroup>,
args: Args, args: Args,
) -> Result<Box<SketchGroup>, KclError> { ) -> Result<Box<SketchGroup>, KclError> {
let (angle, length) = match &data { let (angle, length) = (data.angle, data.length);
AngledLineData::AngleWithTag { angle, length, .. } => (*angle, *length),
AngledLineData::AngleAndLength(angle_and_length) => (angle_and_length[0], angle_and_length[1]),
};
let to = get_x_component(Angle::from_degrees(angle), length); let to = get_x_component(Angle::from_degrees(angle), length);
@ -550,10 +470,7 @@ async fn inner_angled_line_to_y(
args: Args, args: Args,
) -> Result<Box<SketchGroup>, KclError> { ) -> Result<Box<SketchGroup>, KclError> {
let from = sketch_group.get_coords_from_paths()?; let from = sketch_group.get_coords_from_paths()?;
let (angle, y_to) = match &data { let (angle, y_to) = (data.angle, data.to);
AngledLineToData::AngleWithTag { angle, to, .. } => (*angle, *to),
AngledLineToData::AngleAndPoint(angle_and_to) => (angle_and_to[0], angle_and_to[1]),
};
let y_component = y_to - from.y; let y_component = y_to - from.y;
let x_component = y_component / f64::tan(angle.to_radians()); let x_component = y_component / f64::tan(angle.to_radians());
@ -616,10 +533,9 @@ async fn inner_angled_line_that_intersects(
from, from,
); );
let line_to_data = if let Some(tag) = data.tag { let line_to_data = LineToData {
LineToData::PointWithTag { to: to.into(), tag } to: to.into(),
} else { tag: data.tag,
LineToData::Point(to.into())
}; };
let new_sketch_group = inner_line_to(line_to_data, sketch_group, args).await?; let new_sketch_group = inner_line_to(line_to_data, sketch_group, args).await?;
@ -830,10 +746,7 @@ pub async fn start_profile_at(args: Args) -> Result<MemoryItem, KclError> {
name = "startProfileAt", name = "startProfileAt",
}] }]
async fn inner_start_profile_at(data: LineData, plane: Box<Plane>, args: Args) -> Result<Box<SketchGroup>, KclError> { async fn inner_start_profile_at(data: LineData, plane: Box<Plane>, args: Args) -> Result<Box<SketchGroup>, KclError> {
let to = match &data { let to = data.to;
LineData::PointWithTag { to, .. } => *to,
LineData::Point(to) => *to,
};
let id = uuid::Uuid::new_v4(); let id = uuid::Uuid::new_v4();
let path_id = uuid::Uuid::new_v4(); let path_id = uuid::Uuid::new_v4();
@ -843,11 +756,7 @@ async fn inner_start_profile_at(data: LineData, plane: Box<Plane>, args: Args) -
id, id,
ModelingCmd::MovePathPen { ModelingCmd::MovePathPen {
path: path_id, path: path_id,
to: Point3D { to: point3d(to),
x: to[0],
y: to[1],
z: 0.0,
},
}, },
) )
.await?; .await?;
@ -855,11 +764,7 @@ async fn inner_start_profile_at(data: LineData, plane: Box<Plane>, args: Args) -
let current_path = BasePath { let current_path = BasePath {
from: to, from: to,
to, to,
name: if let LineData::PointWithTag { tag, .. } = data { name: data.tag.unwrap_or_default(),
tag.to_string()
} else {
"".to_string()
},
geo_meta: GeoMeta { geo_meta: GeoMeta {
id, id,
metadata: args.source_range.into(), metadata: args.source_range.into(),
@ -943,16 +848,7 @@ pub enum ArcData {
/// The radius. /// The radius.
radius: f64, radius: f64,
/// The tag. /// The tag.
tag: String, tag: Option<String>,
},
/// Angles and radius.
AnglesAndRadius {
/// The start angle.
angle_start: f64,
/// The end angle.
angle_end: f64,
/// The radius.
radius: f64,
}, },
/// Center, to and radius with a tag. /// Center, to and radius with a tag.
CenterToRadiusWithTag { CenterToRadiusWithTag {
@ -963,16 +859,7 @@ pub enum ArcData {
/// The radius. /// The radius.
radius: f64, radius: f64,
/// The tag. /// The tag.
tag: String, tag: Option<String>,
},
/// Center, to and radius.
CenterToRadius {
/// The center.
center: [f64; 2],
/// The to point.
to: [f64; 2],
/// The radius.
radius: f64,
}, },
} }
@ -1003,24 +890,10 @@ async fn inner_arc(data: ArcData, sketch_group: Box<SketchGroup>, args: Args) ->
let (center, end) = arc_center_and_end(from, a_start, a_end, *radius); let (center, end) = arc_center_and_end(from, a_start, a_end, *radius);
(center, a_start, a_end, *radius, end) (center, a_start, a_end, *radius, end)
} }
ArcData::AnglesAndRadius {
angle_start,
angle_end,
radius,
} => {
let a_start = Angle::from_degrees(*angle_start);
let a_end = Angle::from_degrees(*angle_end);
let (center, end) = arc_center_and_end(from, a_start, a_end, *radius);
(center, a_start, a_end, *radius, end)
}
ArcData::CenterToRadiusWithTag { center, to, radius, .. } => { ArcData::CenterToRadiusWithTag { center, to, radius, .. } => {
let (angle_start, angle_end) = arc_angles(from, center.into(), to.into(), *radius, args.source_range)?; let (angle_start, angle_end) = arc_angles(from, center.into(), to.into(), *radius, args.source_range)?;
(center.into(), angle_start, angle_end, *radius, to.into()) (center.into(), angle_start, angle_end, *radius, to.into())
} }
ArcData::CenterToRadius { center, to, radius } => {
let (angle_start, angle_end) = arc_angles(from, center.into(), to.into(), *radius, args.source_range)?;
(center.into(), angle_start, angle_end, *radius, to.into())
}
}; };
let id = uuid::Uuid::new_v4(); let id = uuid::Uuid::new_v4();
@ -1047,10 +920,8 @@ async fn inner_arc(data: ArcData, sketch_group: Box<SketchGroup>, args: Args) ->
from: from.into(), from: from.into(),
to: end.into(), to: end.into(),
name: match data { name: match data {
ArcData::AnglesAndRadiusWithTag { tag, .. } => tag.to_string(), ArcData::AnglesAndRadiusWithTag { tag, .. } => tag.unwrap_or_default(),
ArcData::AnglesAndRadius { .. } => "".to_string(), ArcData::CenterToRadiusWithTag { tag, .. } => tag.unwrap_or_default(),
ArcData::CenterToRadiusWithTag { tag, .. } => tag.to_string(),
ArcData::CenterToRadius { .. } => "".to_string(),
}, },
geo_meta: GeoMeta { geo_meta: GeoMeta {
id, id,
@ -1082,10 +953,8 @@ pub enum TangentialArcData {
/// Where the arc should end. Must lie in the same plane as the current path pen position. Must not be colinear with current path pen position. /// Where the arc should end. Must lie in the same plane as the current path pen position. Must not be colinear with current path pen position.
to: [f64; 2], to: [f64; 2],
/// The tag. /// The tag.
tag: String, tag: Option<String>,
}, },
/// A point where the arc should end. Must lie in the same plane as the current path pen position. Must not be colinear with current path pen position.
Point([f64; 2]),
} }
/// Draw a tangential arc. /// Draw a tangential arc.
@ -1135,11 +1004,6 @@ async fn inner_tangential_arc(
TangentialArcData::PointWithTag { to, .. } => { TangentialArcData::PointWithTag { to, .. } => {
args.send_modeling_cmd(id, tan_arc_to(&sketch_group, to)).await?; args.send_modeling_cmd(id, tan_arc_to(&sketch_group, to)).await?;
*to
}
TangentialArcData::Point(to) => {
args.send_modeling_cmd(id, tan_arc_to(&sketch_group, to)).await?;
*to *to
} }
}; };
@ -1169,11 +1033,7 @@ fn tan_arc_to(sketch_group: &SketchGroup, to: &[f64; 2]) -> ModelingCmd {
path: sketch_group.id, path: sketch_group.id,
segment: kittycad::types::PathSegment::TangentialArcTo { segment: kittycad::types::PathSegment::TangentialArcTo {
angle_snap_increment: None, angle_snap_increment: None,
to: Point3D { to: point3d(*to),
x: to[0],
y: to[1],
z: 0.0,
},
}, },
} }
} }
@ -1181,17 +1041,12 @@ fn tan_arc_to(sketch_group: &SketchGroup, to: &[f64; 2]) -> ModelingCmd {
/// Data to draw a tangential arc to a specific point. /// Data to draw a tangential arc to a specific point.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, JsonSchema, ts_rs::TS)] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, JsonSchema, ts_rs::TS)]
#[ts(export)] #[ts(export)]
#[serde(rename_all = "camelCase", untagged)] #[serde(rename_all = "camelCase")]
pub enum TangentialArcToData { pub struct TangentialArcToData {
/// A point with a tag. /// Where the arc should end. Must lie in the same plane as the current path pen position. Must not be colinear with current path pen position.
PointWithTag { to: [f64; 2],
/// Where the arc should end. Must lie in the same plane as the current path pen position. Must not be colinear with current path pen position. /// The tag.
to: [f64; 2], tag: Option<String>,
/// The tag.
tag: String,
},
/// A point where the arc should end. Must lie in the same plane as the current path pen position. Must not be colinear with current path pen position.
Point([f64; 2]),
} }
/// Draw a tangential arc to a specific point. /// Draw a tangential arc to a specific point.
@ -1212,10 +1067,8 @@ async fn inner_tangential_arc_to(
args: Args, args: Args,
) -> Result<Box<SketchGroup>, KclError> { ) -> Result<Box<SketchGroup>, KclError> {
let from: Point2d = sketch_group.get_coords_from_paths()?; let from: Point2d = sketch_group.get_coords_from_paths()?;
let to = match &data { let to = data.to;
TangentialArcToData::PointWithTag { to, .. } => to, let name = data.tag.unwrap_or_default().to_owned();
TangentialArcToData::Point(to) => to,
};
let delta = [to[0] - from.x, to[1] - from.y]; let delta = [to[0] - from.x, to[1] - from.y];
let id = uuid::Uuid::new_v4(); let id = uuid::Uuid::new_v4();
@ -1224,12 +1077,8 @@ async fn inner_tangential_arc_to(
let current_path = Path::ToPoint { let current_path = Path::ToPoint {
base: BasePath { base: BasePath {
from: from.into(), from: from.into(),
to: *to, to,
name: if let TangentialArcToData::PointWithTag { tag, .. } = data { name,
tag.to_string()
} else {
"".to_string()
},
geo_meta: GeoMeta { geo_meta: GeoMeta {
id, id,
metadata: args.source_range.into(), metadata: args.source_range.into(),
@ -1246,28 +1095,16 @@ async fn inner_tangential_arc_to(
/// Data to draw a bezier curve. /// Data to draw a bezier curve.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)] #[ts(export)]
#[serde(rename_all = "camelCase", untagged)] #[serde(rename_all = "camelCase")]
pub enum BezierData { pub struct BezierData {
/// Points with a tag. /// The to point.
PointsWithTag { to: [f64; 2],
/// The to point. /// The first control point.
to: [f64; 2], control1: [f64; 2],
/// The first control point. /// The second control point.
control1: [f64; 2], control2: [f64; 2],
/// The second control point. /// The tag.
control2: [f64; 2], tag: Option<String>,
/// The tag.
tag: String,
},
/// Points.
Points {
/// The to point.
to: [f64; 2],
/// The first control point.
control1: [f64; 2],
/// The second control point.
control2: [f64; 2],
},
} }
/// Draw a bezier curve. /// Draw a bezier curve.
@ -1289,12 +1126,13 @@ async fn inner_bezier_curve(
) -> Result<Box<SketchGroup>, KclError> { ) -> Result<Box<SketchGroup>, KclError> {
let from = sketch_group.get_coords_from_paths()?; let from = sketch_group.get_coords_from_paths()?;
let (to, control1, control2) = match &data { let BezierData {
BezierData::PointsWithTag { to,
to, control1, control2, .. control1,
} => (to, control1, control2), control2,
BezierData::Points { to, control1, control2 } => (to, control1, control2), tag,
}; } = data;
let name = tag.unwrap_or_default();
let relative = true; let relative = true;
let delta = to; let delta = to;
@ -1307,21 +1145,9 @@ async fn inner_bezier_curve(
ModelingCmd::ExtendPath { ModelingCmd::ExtendPath {
path: sketch_group.id, path: sketch_group.id,
segment: kittycad::types::PathSegment::Bezier { segment: kittycad::types::PathSegment::Bezier {
control1: Point3D { control1: point3d(control1),
x: control1[0], control2: point3d(control2),
y: control1[1], end: point3d(delta),
z: 0.0,
},
control2: Point3D {
x: control2[0],
y: control2[1],
z: 0.0,
},
end: Point3D {
x: delta[0],
y: delta[1],
z: 0.0,
},
relative, relative,
}, },
}, },
@ -1332,11 +1158,7 @@ async fn inner_bezier_curve(
base: BasePath { base: BasePath {
from: from.into(), from: from.into(),
to, to,
name: if let BezierData::PointsWithTag { tag, .. } = data { name,
tag.to_string()
} else {
"".to_string()
},
geo_meta: GeoMeta { geo_meta: GeoMeta {
id, id,
metadata: args.source_range.into(), metadata: args.source_range.into(),
@ -1394,6 +1216,10 @@ async fn inner_hole(
Ok(sketch_group) Ok(sketch_group)
} }
fn point3d([x, y]: [f64; 2]) -> Point3D {
Point3D { x, y, z: 0.0 }
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -1403,21 +1229,30 @@ mod tests {
#[test] #[test]
fn test_deserialize_line_data() { fn test_deserialize_line_data() {
let data = LineData::Point([0.0, 1.0]); let data = LineData {
to: [0.0, 1.0],
tag: None,
};
let mut str_json = serde_json::to_string(&data).unwrap(); let mut str_json = serde_json::to_string(&data).unwrap();
assert_eq!(str_json, "[0.0,1.0]"); assert_eq!(str_json, "[0.0,1.0]");
str_json = "[0, 1]".to_string(); str_json = "[0, 1]".to_string();
let data: LineData = serde_json::from_str(&str_json).unwrap(); let data: LineData = serde_json::from_str(&str_json).unwrap();
assert_eq!(data, LineData::Point([0.0, 1.0])); assert_eq!(
data,
LineData {
to: [0.0, 1.0],
tag: None
}
);
str_json = "{ \"to\": [0.0, 1.0], \"tag\": \"thing\" }".to_string(); str_json = "{ \"to\": [0.0, 1.0], \"tag\": \"thing\" }".to_string();
let data: LineData = serde_json::from_str(&str_json).unwrap(); let data: LineData = serde_json::from_str(&str_json).unwrap();
assert_eq!( assert_eq!(
data, data,
LineData::PointWithTag { LineData {
to: [0.0, 1.0], to: [0.0, 1.0],
tag: "thing".to_string() tag: Some("thing".to_string()),
} }
); );
} }

View File

@ -47,6 +47,8 @@ pub enum TokenType {
Function, Function,
/// Unknown lexemes. /// Unknown lexemes.
Unknown, Unknown,
/// The ? symbol, used for optional values.
QuestionMark,
} }
/// Most KCL tokens correspond to LSP semantic tokens (but not all). /// Most KCL tokens correspond to LSP semantic tokens (but not all).
@ -58,6 +60,7 @@ impl TryFrom<TokenType> for SemanticTokenType {
TokenType::Word => Self::VARIABLE, TokenType::Word => Self::VARIABLE,
TokenType::Keyword => Self::KEYWORD, TokenType::Keyword => Self::KEYWORD,
TokenType::Operator => Self::OPERATOR, TokenType::Operator => Self::OPERATOR,
TokenType::QuestionMark => Self::OPERATOR,
TokenType::String => Self::STRING, TokenType::String => Self::STRING,
TokenType::LineComment => Self::COMMENT, TokenType::LineComment => Self::COMMENT,
TokenType::BlockComment => Self::COMMENT, TokenType::BlockComment => Self::COMMENT,

View File

@ -21,6 +21,7 @@ pub fn token(i: &mut Located<&str>) -> PResult<Token> {
'{' | '(' | '[' => brace_start, '{' | '(' | '[' => brace_start,
'}' | ')' | ']' => brace_end, '}' | ')' | ']' => brace_end,
',' => comma, ',' => comma,
'?' => question_mark,
'0'..='9' => number, '0'..='9' => number,
':' => colon, ':' => colon,
'.' => alt((number, double_period, period)), '.' => alt((number, double_period, period)),
@ -108,6 +109,11 @@ fn comma(i: &mut Located<&str>) -> PResult<Token> {
Ok(Token::from_range(range, TokenType::Comma, value.to_string())) Ok(Token::from_range(range, TokenType::Comma, value.to_string()))
} }
fn question_mark(i: &mut Located<&str>) -> PResult<Token> {
let (value, range) = '?'.with_span().parse_next(i)?;
Ok(Token::from_range(range, TokenType::QuestionMark, value.to_string()))
}
fn colon(i: &mut Located<&str>) -> PResult<Token> { fn colon(i: &mut Located<&str>) -> PResult<Token> {
let (value, range) = ':'.with_span().parse_next(i)?; let (value, range) = ':'.with_span().parse_next(i)?;
Ok(Token::from_range(range, TokenType::Colon, value.to_string())) Ok(Token::from_range(range, TokenType::Colon, value.to_string()))