Skip to content

Commit 910619d

Browse files
committed
feat: add location info to root node
1 parent d4ff0bc commit 910619d

13 files changed

+52
-2
lines changed

parser/ast.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ impl fmt::Display for Node {
2727
#[derive(Clone, Debug, Eq, Deserialize, Hash, PartialEq)]
2828
pub struct Program {
2929
pub body: Vec<Statement>,
30+
pub span: Span,
3031
}
3132

3233
// remove this if serde resolve resolve https://github.com/serde-rs/serde/issues/760
@@ -37,14 +38,18 @@ impl Serialize for Program {
3738
{
3839
let mut state = serializer.serialize_struct("Program", 2)?;
3940
state.serialize_field("body", &self.body)?;
41+
state.serialize_field("span", &self.span)?;
4042
state.serialize_field("type", "Program")?;
4143
state.end()
4244
}
4345
}
4446

4547
impl Program {
4648
pub fn new() -> Self {
47-
Program { body: vec![] }
49+
Program { body: vec![], span: Span {
50+
start: 0,
51+
end: 0,
52+
} }
4853
}
4954
}
5055

@@ -103,7 +108,7 @@ impl fmt::Display for BlockStatement {
103108
#[serde(tag = "expr_type")]
104109
pub enum Expression {
105110
IDENTIFIER(IDENTIFIER),
106-
LITERAL(Literal),
111+
LITERAL(Literal), // need to flatten
107112
PREFIX(UnaryExpression),
108113
INFIX(BinaryExpression),
109114
IF(IF),

parser/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ impl<'a> Parser<'a> {
7575
}
7676
self.next_token();
7777
}
78+
program.span.end = self.current_token.span.end;
7879

7980
if self.errors.is_empty() {
8081
return Ok(program);

parser/snapshots/parser__ast_tree_test__tests__test_array.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ expression: "[1, true]"
3535
}
3636
}
3737
],
38+
"span": {
39+
"start": 0,
40+
"end": 10
41+
},
3842
"type": "Program"
3943
}
4044
}

parser/snapshots/parser__ast_tree_test__tests__test_binary.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ expression: 1 + 2 * 3
6666
}
6767
}
6868
],
69+
"span": {
70+
"start": 0,
71+
"end": 10
72+
},
6973
"type": "Program"
7074
}
7175
}

parser/snapshots/parser__ast_tree_test__tests__test_func_call.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ expression: "add(1, 2)"
4242
}
4343
}
4444
],
45+
"span": {
46+
"start": 0,
47+
"end": 10
48+
},
4549
"type": "Program"
4650
}
4751
}

parser/snapshots/parser__ast_tree_test__tests__test_func_declaration.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ expression: "fn(x) { x };"
2828
}
2929
}
3030
],
31+
"span": {
32+
"start": 0,
33+
"end": 13
34+
},
3135
"type": "Program"
3236
}
3337
}

parser/snapshots/parser__ast_tree_test__tests__test_hash.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ expression: "{\"a\": 1}"
3737
}
3838
}
3939
],
40+
"span": {
41+
"start": 0,
42+
"end": 9
43+
},
4044
"type": "Program"
4145
}
4246
}

parser/snapshots/parser__ast_tree_test__tests__test_if.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ expression: "if (x < y) { x } else { y }"
6868
}
6969
}
7070
],
71+
"span": {
72+
"start": 0,
73+
"end": 28
74+
},
7175
"type": "Program"
7276
}
7377
}

parser/snapshots/parser__ast_tree_test__tests__test_index.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ expression: "a[1]"
3131
}
3232
}
3333
],
34+
"span": {
35+
"start": 0,
36+
"end": 5
37+
},
3438
"type": "Program"
3539
}
3640
}

parser/snapshots/parser__ast_tree_test__tests__test_let.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ expression: let a = 3
3232
}
3333
}
3434
],
35+
"span": {
36+
"start": 0,
37+
"end": 10
38+
},
3539
"type": "Program"
3640
}
3741
}

0 commit comments

Comments
 (0)