diff --git a/src/wasm-lib/kcl/src/parser/parser_impl.rs b/src/wasm-lib/kcl/src/parser/parser_impl.rs index 6fc7d566c..21ebd220b 100644 --- a/src/wasm-lib/kcl/src/parser/parser_impl.rs +++ b/src/wasm-lib/kcl/src/parser/parser_impl.rs @@ -2801,4 +2801,5 @@ mod snapshot_tests { snapshot_test!(ar, r#"5 + "a""#); snapshot_test!(at, "line([0, l], %)"); snapshot_test!(au, include_str!("../../../tests/executor/inputs/cylinder.kcl")); + snapshot_test!(av, "fn f = (angle) => { return default(angle, 360) }"); } diff --git a/src/wasm-lib/kcl/src/parser/snapshots/kcl_lib__parser__parser_impl__snapshot_tests__av.snap b/src/wasm-lib/kcl/src/parser/snapshots/kcl_lib__parser__parser_impl__snapshot_tests__av.snap new file mode 100644 index 000000000..567f89421 --- /dev/null +++ b/src/wasm-lib/kcl/src/parser/snapshots/kcl_lib__parser__parser_impl__snapshot_tests__av.snap @@ -0,0 +1,94 @@ +--- +source: kcl/src/parser/parser_impl.rs +expression: actual +--- +{ + "start": 0, + "end": 48, + "body": [ + { + "type": "VariableDeclaration", + "type": "VariableDeclaration", + "start": 0, + "end": 48, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 3, + "end": 48, + "id": { + "type": "Identifier", + "start": 3, + "end": 4, + "name": "f" + }, + "init": { + "type": "FunctionExpression", + "type": "FunctionExpression", + "start": 7, + "end": 48, + "params": [ + { + "type": "Identifier", + "start": 8, + "end": 13, + "name": "angle" + } + ], + "body": { + "start": 18, + "end": 48, + "body": [ + { + "type": "ReturnStatement", + "type": "ReturnStatement", + "start": 20, + "end": 46, + "argument": { + "type": "CallExpression", + "type": "CallExpression", + "start": 27, + "end": 46, + "callee": { + "type": "Identifier", + "start": 27, + "end": 34, + "name": "default" + }, + "arguments": [ + { + "type": "Identifier", + "type": "Identifier", + "start": 35, + "end": 40, + "name": "angle" + }, + { + "type": "Literal", + "type": "Literal", + "start": 42, + "end": 45, + "value": 360, + "raw": "360" + } + ], + "optional": false + } + } + ], + "nonCodeMeta": { + "nonCodeNodes": {}, + "start": [] + } + } + } + } + ], + "kind": "fn" + } + ], + "nonCodeMeta": { + "nonCodeNodes": {}, + "start": [] + } +} diff --git a/src/wasm-lib/kcl/src/token.rs b/src/wasm-lib/kcl/src/token.rs index 04a5a24ee..35367217e 100644 --- a/src/wasm-lib/kcl/src/token.rs +++ b/src/wasm-lib/kcl/src/token.rs @@ -47,6 +47,8 @@ pub enum TokenType { Function, /// Unknown lexemes. Unknown, + /// The ? symbol, used for optional values. + QuestionMark, } /// Most KCL tokens correspond to LSP semantic tokens (but not all). @@ -58,6 +60,7 @@ impl TryFrom for SemanticTokenType { TokenType::Word => Self::VARIABLE, TokenType::Keyword => Self::KEYWORD, TokenType::Operator => Self::OPERATOR, + TokenType::QuestionMark => Self::OPERATOR, TokenType::String => Self::STRING, TokenType::LineComment => Self::COMMENT, TokenType::BlockComment => Self::COMMENT, diff --git a/src/wasm-lib/kcl/src/token/tokeniser.rs b/src/wasm-lib/kcl/src/token/tokeniser.rs index c56d5c226..f44d31bc8 100644 --- a/src/wasm-lib/kcl/src/token/tokeniser.rs +++ b/src/wasm-lib/kcl/src/token/tokeniser.rs @@ -21,6 +21,7 @@ pub fn token(i: &mut Located<&str>) -> PResult { '{' | '(' | '[' => brace_start, '}' | ')' | ']' => brace_end, ',' => comma, + '?' => question_mark, '0'..='9' => number, ':' => colon, '.' => alt((number, double_period, period)), @@ -108,6 +109,11 @@ fn comma(i: &mut Located<&str>) -> PResult { Ok(Token::from_range(range, TokenType::Comma, value.to_string())) } +fn question_mark(i: &mut Located<&str>) -> PResult { + 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 { let (value, range) = ':'.with_span().parse_next(i)?; Ok(Token::from_range(range, TokenType::Colon, value.to_string()))