Skip to content

Commit d4ff0bc

Browse files
committed
feat: add type to ast (not perfect solution, needs another process on js side)
1 parent 25bc90c commit d4ff0bc

12 files changed

+305
-308
lines changed

parser/ast.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ use core::fmt;
22
use core::fmt::Result;
33
use std::fmt::Formatter;
44
use lexer::token::{Token, TokenKind, Span};
5-
use serde::{Deserialize, Serialize};
5+
use serde::{Deserialize, Serialize, Deserializer, Serializer};
6+
use serde::ser::SerializeStruct;
67

8+
// still wait for https://github.com/serde-rs/serde/issues/1402
9+
// or https://github.com/serde-rs/serde/issues/760
710
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
811
pub enum Node {
912
Program(Program),
@@ -21,11 +24,24 @@ impl fmt::Display for Node {
2124
}
2225
}
2326

24-
#[derive(Clone, Debug, Eq, Serialize, Deserialize, Hash, PartialEq)]
27+
#[derive(Clone, Debug, Eq, Deserialize, Hash, PartialEq)]
2528
pub struct Program {
2629
pub body: Vec<Statement>,
2730
}
2831

32+
// remove this if serde resolve resolve https://github.com/serde-rs/serde/issues/760
33+
impl Serialize for Program {
34+
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
35+
where
36+
S: Serializer,
37+
{
38+
let mut state = serializer.serialize_struct("Program", 2)?;
39+
state.serialize_field("body", &self.body)?;
40+
state.serialize_field("type", "Program")?;
41+
state.end()
42+
}
43+
}
44+
2945
impl Program {
3046
pub fn new() -> Self {
3147
Program { body: vec![] }
@@ -46,7 +62,7 @@ pub struct Let {
4662
}
4763

4864
#[derive(Clone, Debug, Eq, Serialize, Deserialize, Hash, PartialEq)]
49-
#[serde(tag = "type")]
65+
#[serde(tag = "stmt_type")]
5066
pub enum Statement {
5167
Let(Let),
5268
Return(Expression),
@@ -84,6 +100,7 @@ impl fmt::Display for BlockStatement {
84100
}
85101

86102
#[derive(Clone, Debug, Serialize, Deserialize, Eq, Hash, PartialEq)]
103+
#[serde(tag = "expr_type")]
87104
pub enum Expression {
88105
IDENTIFIER(IDENTIFIER),
89106
LITERAL(Literal),
Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,40 @@
11
---
22
source: parser/ast_tree_test.rs
3-
expression: let_ast
3+
expression: "[1, true]"
44
---
55
{
66
"Program": {
77
"body": [
88
{
9-
"type": "Expr",
10-
"LITERAL": {
11-
"type": "Array",
12-
"elements": [
13-
{
14-
"LITERAL": {
15-
"type": "Integer",
16-
"raw": 1,
17-
"span": {
18-
"start": 1,
19-
"end": 2
20-
}
21-
}
22-
},
23-
{
24-
"LITERAL": {
25-
"type": "Boolean",
26-
"raw": true,
27-
"span": {
28-
"start": 4,
29-
"end": 8
30-
}
31-
}
9+
"stmt_type": "Expr",
10+
"expr_type": "LITERAL",
11+
"type": "Array",
12+
"elements": [
13+
{
14+
"expr_type": "LITERAL",
15+
"type": "Integer",
16+
"raw": 1,
17+
"span": {
18+
"start": 1,
19+
"end": 2
20+
}
21+
},
22+
{
23+
"expr_type": "LITERAL",
24+
"type": "Boolean",
25+
"raw": true,
26+
"span": {
27+
"start": 4,
28+
"end": 8
3229
}
33-
],
34-
"span": {
35-
"start": 0,
36-
"end": 9
3730
}
31+
],
32+
"span": {
33+
"start": 0,
34+
"end": 9
3835
}
3936
}
40-
]
37+
],
38+
"type": "Program"
4139
}
4240
}
Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,71 @@
11
---
22
source: parser/ast_tree_test.rs
3-
expression: let_ast
3+
expression: 1 + 2 * 3
44
---
55
{
66
"Program": {
77
"body": [
88
{
9-
"type": "Expr",
10-
"INFIX": {
9+
"stmt_type": "Expr",
10+
"expr_type": "INFIX",
11+
"op": {
12+
"span": {
13+
"start": 2,
14+
"end": 3
15+
},
16+
"kind": {
17+
"type": "PLUS"
18+
}
19+
},
20+
"left": {
21+
"expr_type": "LITERAL",
22+
"type": "Integer",
23+
"raw": 1,
24+
"span": {
25+
"start": 0,
26+
"end": 1
27+
}
28+
},
29+
"right": {
30+
"expr_type": "INFIX",
1131
"op": {
1232
"span": {
13-
"start": 2,
14-
"end": 3
33+
"start": 6,
34+
"end": 7
1535
},
1636
"kind": {
17-
"type": "PLUS"
37+
"type": "ASTERISK"
1838
}
1939
},
2040
"left": {
21-
"LITERAL": {
22-
"type": "Integer",
23-
"raw": 1,
24-
"span": {
25-
"start": 0,
26-
"end": 1
27-
}
41+
"expr_type": "LITERAL",
42+
"type": "Integer",
43+
"raw": 2,
44+
"span": {
45+
"start": 4,
46+
"end": 5
2847
}
2948
},
3049
"right": {
31-
"INFIX": {
32-
"op": {
33-
"span": {
34-
"start": 6,
35-
"end": 7
36-
},
37-
"kind": {
38-
"type": "ASTERISK"
39-
}
40-
},
41-
"left": {
42-
"LITERAL": {
43-
"type": "Integer",
44-
"raw": 2,
45-
"span": {
46-
"start": 4,
47-
"end": 5
48-
}
49-
}
50-
},
51-
"right": {
52-
"LITERAL": {
53-
"type": "Integer",
54-
"raw": 3,
55-
"span": {
56-
"start": 8,
57-
"end": 9
58-
}
59-
}
60-
},
61-
"span": {
62-
"start": 4,
63-
"end": 9
64-
}
50+
"expr_type": "LITERAL",
51+
"type": "Integer",
52+
"raw": 3,
53+
"span": {
54+
"start": 8,
55+
"end": 9
6556
}
6657
},
6758
"span": {
68-
"start": 0,
59+
"start": 4,
6960
"end": 9
7061
}
62+
},
63+
"span": {
64+
"start": 0,
65+
"end": 9
7166
}
7267
}
73-
]
68+
],
69+
"type": "Program"
7470
}
7571
}
Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,47 @@
11
---
22
source: parser/ast_tree_test.rs
3-
expression: let_ast
3+
expression: "add(1, 2)"
44
---
55
{
66
"Program": {
77
"body": [
88
{
9-
"type": "Expr",
10-
"FunctionCall": {
11-
"callee": {
12-
"IDENTIFIER": {
13-
"name": "add",
14-
"span": {
15-
"start": 0,
16-
"end": 3
17-
}
9+
"stmt_type": "Expr",
10+
"expr_type": "FunctionCall",
11+
"callee": {
12+
"expr_type": "IDENTIFIER",
13+
"name": "add",
14+
"span": {
15+
"start": 0,
16+
"end": 3
17+
}
18+
},
19+
"arguments": [
20+
{
21+
"expr_type": "LITERAL",
22+
"type": "Integer",
23+
"raw": 1,
24+
"span": {
25+
"start": 4,
26+
"end": 5
1827
}
1928
},
20-
"arguments": [
21-
{
22-
"LITERAL": {
23-
"type": "Integer",
24-
"raw": 1,
25-
"span": {
26-
"start": 4,
27-
"end": 5
28-
}
29-
}
30-
},
31-
{
32-
"LITERAL": {
33-
"type": "Integer",
34-
"raw": 2,
35-
"span": {
36-
"start": 7,
37-
"end": 8
38-
}
39-
}
29+
{
30+
"expr_type": "LITERAL",
31+
"type": "Integer",
32+
"raw": 2,
33+
"span": {
34+
"start": 7,
35+
"end": 8
4036
}
41-
],
42-
"span": {
43-
"start": 3,
44-
"end": 9
4537
}
38+
],
39+
"span": {
40+
"start": 3,
41+
"end": 9
4642
}
4743
}
48-
]
44+
],
45+
"type": "Program"
4946
}
5047
}
Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
---
22
source: parser/ast_tree_test.rs
3-
expression: let_ast
3+
expression: "fn(x) { x };"
44
---
55
{
66
"Program": {
77
"body": [
88
{
9-
"type": "Expr",
10-
"FUNCTION": {
11-
"params": [
12-
"x"
13-
],
14-
"body": [
15-
{
16-
"type": "Expr",
17-
"IDENTIFIER": {
18-
"name": "x",
19-
"span": {
20-
"start": 8,
21-
"end": 9
22-
}
23-
}
9+
"stmt_type": "Expr",
10+
"expr_type": "FUNCTION",
11+
"params": [
12+
"x"
13+
],
14+
"body": [
15+
{
16+
"stmt_type": "Expr",
17+
"expr_type": "IDENTIFIER",
18+
"name": "x",
19+
"span": {
20+
"start": 8,
21+
"end": 9
2422
}
25-
],
26-
"span": {
27-
"start": 0,
28-
"end": 11
2923
}
24+
],
25+
"span": {
26+
"start": 0,
27+
"end": 11
3028
}
3129
}
32-
]
30+
],
31+
"type": "Program"
3332
}
3433
}

0 commit comments

Comments
 (0)