Skip to content

Commit d33660c

Browse files
authored
KCL: Parser should allow space after negative (#7766)
`x = - 1` didn't parse previously. Now it does.
1 parent 436dd2b commit d33660c

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

rust/kcl-lib/src/parsing/parser.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2526,6 +2526,7 @@ fn unary_expression(i: &mut TokenSlice) -> ModalResult<Node<UnaryExpression>> {
25262526
})
25272527
.context(expected("a unary expression, e.g. -x or -3"))
25282528
.parse_next(i)?;
2529+
ignore_whitespace(i);
25292530
let argument = operand.parse_next(i)?;
25302531
Ok(Node::new_node(
25312532
op_token.start,
@@ -5582,6 +5583,7 @@ my14 = 4 ^ 2 - 3 ^ 2 * 2
55825583
r#"incl = [1..10]
55835584
excl = [0..<10]"#
55845585
);
5586+
snapshot_test!(space_between_unary_and_operand, r#"x = - 1"#);
55855587
}
55865588

55875589
#[allow(unused)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
source: kcl-lib/src/parsing/parser.rs
3+
expression: actual
4+
---
5+
{
6+
"body": [
7+
{
8+
"commentStart": 0,
9+
"declaration": {
10+
"commentStart": 0,
11+
"end": 7,
12+
"id": {
13+
"commentStart": 0,
14+
"end": 1,
15+
"moduleId": 0,
16+
"name": "x",
17+
"start": 0,
18+
"type": "Identifier"
19+
},
20+
"init": {
21+
"argument": {
22+
"commentStart": 6,
23+
"end": 7,
24+
"moduleId": 0,
25+
"raw": "1",
26+
"start": 6,
27+
"type": "Literal",
28+
"type": "Literal",
29+
"value": {
30+
"value": 1.0,
31+
"suffix": "None"
32+
}
33+
},
34+
"commentStart": 4,
35+
"end": 7,
36+
"moduleId": 0,
37+
"operator": "-",
38+
"start": 4,
39+
"type": "UnaryExpression",
40+
"type": "UnaryExpression"
41+
},
42+
"moduleId": 0,
43+
"start": 0,
44+
"type": "VariableDeclarator"
45+
},
46+
"end": 7,
47+
"kind": "const",
48+
"moduleId": 0,
49+
"start": 0,
50+
"type": "VariableDeclaration",
51+
"type": "VariableDeclaration"
52+
}
53+
],
54+
"commentStart": 0,
55+
"end": 7,
56+
"moduleId": 0,
57+
"start": 0
58+
}

0 commit comments

Comments
 (0)