Add ? token

This commit is contained in:
Adam Chalmers
2023-11-17 15:26:55 -06:00
parent 361500058c
commit 2693c93bb7
4 changed files with 104 additions and 0 deletions

View File

@ -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) }");
}

View File

@ -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": []
}
}

View File

@ -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<TokenType> 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,

View File

@ -21,6 +21,7 @@ pub fn token(i: &mut Located<&str>) -> PResult<Token> {
'{' | '(' | '[' => 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<Token> {
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> {
let (value, range) = ':'.with_span().parse_next(i)?;
Ok(Token::from_range(range, TokenType::Colon, value.to_string()))