Add ? token
This commit is contained in:
@ -2801,4 +2801,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) }");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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": []
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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,
|
||||||
|
|||||||
@ -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()))
|
||||||
|
|||||||
Reference in New Issue
Block a user