Skip to content

Commit 3c894b8

Browse files
committed
chore: improve program type generation
1 parent e4a4946 commit 3c894b8

12 files changed

+25
-39
lines changed

parser/ast.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ 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, Serializer};
6-
use serde::ser::SerializeStruct;
5+
use serde::{Deserialize, Serialize};
76

87
// still wait for https://github.com/serde-rs/serde/issues/1402
98
// or https://github.com/serde-rs/serde/issues/760
@@ -24,26 +23,13 @@ impl fmt::Display for Node {
2423
}
2524
}
2625

27-
#[derive(Clone, Debug, Eq, Deserialize, Hash, PartialEq)]
26+
#[derive(Clone, Debug, Eq, Serialize, Deserialize, Hash, PartialEq)]
27+
#[serde(tag = "type")]
2828
pub struct Program {
2929
pub body: Vec<Statement>,
3030
pub span: Span,
3131
}
3232

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

parser/snapshots/parser__ast_tree_test__tests__test_array.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ expression: "[1, true]"
44
---
55
{
66
"Program": {
7+
"type": "Program",
78
"body": [
89
{
910
"stmt_type": "Expr",
@@ -38,7 +39,6 @@ expression: "[1, true]"
3839
"span": {
3940
"start": 0,
4041
"end": 10
41-
},
42-
"type": "Program"
42+
}
4343
}
4444
}

parser/snapshots/parser__ast_tree_test__tests__test_binary.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ expression: 1 + 2 * 3
44
---
55
{
66
"Program": {
7+
"type": "Program",
78
"body": [
89
{
910
"stmt_type": "Expr",
@@ -69,7 +70,6 @@ expression: 1 + 2 * 3
6970
"span": {
7071
"start": 0,
7172
"end": 10
72-
},
73-
"type": "Program"
73+
}
7474
}
7575
}

parser/snapshots/parser__ast_tree_test__tests__test_func_call.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ expression: "add(1, 2)"
44
---
55
{
66
"Program": {
7+
"type": "Program",
78
"body": [
89
{
910
"stmt_type": "Expr",
@@ -45,7 +46,6 @@ expression: "add(1, 2)"
4546
"span": {
4647
"start": 0,
4748
"end": 10
48-
},
49-
"type": "Program"
49+
}
5050
}
5151
}

parser/snapshots/parser__ast_tree_test__tests__test_func_declaration.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ expression: "fn(x) { x };"
44
---
55
{
66
"Program": {
7+
"type": "Program",
78
"body": [
89
{
910
"stmt_type": "Expr",
@@ -31,7 +32,6 @@ expression: "fn(x) { x };"
3132
"span": {
3233
"start": 0,
3334
"end": 13
34-
},
35-
"type": "Program"
35+
}
3636
}
3737
}

parser/snapshots/parser__ast_tree_test__tests__test_hash.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ expression: "{\"a\": 1}"
44
---
55
{
66
"Program": {
7+
"type": "Program",
78
"body": [
89
{
910
"stmt_type": "Expr",
@@ -40,7 +41,6 @@ expression: "{\"a\": 1}"
4041
"span": {
4142
"start": 0,
4243
"end": 9
43-
},
44-
"type": "Program"
44+
}
4545
}
4646
}

parser/snapshots/parser__ast_tree_test__tests__test_if.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ expression: "if (x < y) { x } else { y }"
44
---
55
{
66
"Program": {
7+
"type": "Program",
78
"body": [
89
{
910
"stmt_type": "Expr",
@@ -71,7 +72,6 @@ expression: "if (x < y) { x } else { y }"
7172
"span": {
7273
"start": 0,
7374
"end": 28
74-
},
75-
"type": "Program"
75+
}
7676
}
7777
}

parser/snapshots/parser__ast_tree_test__tests__test_index.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ expression: "a[1]"
44
---
55
{
66
"Program": {
7+
"type": "Program",
78
"body": [
89
{
910
"stmt_type": "Expr",
@@ -34,7 +35,6 @@ expression: "a[1]"
3435
"span": {
3536
"start": 0,
3637
"end": 5
37-
},
38-
"type": "Program"
38+
}
3939
}
4040
}

parser/snapshots/parser__ast_tree_test__tests__test_let.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ expression: let a = 3
44
---
55
{
66
"Program": {
7+
"type": "Program",
78
"body": [
89
{
910
"stmt_type": "Let",
@@ -35,7 +36,6 @@ expression: let a = 3
3536
"span": {
3637
"start": 0,
3738
"end": 10
38-
},
39-
"type": "Program"
39+
}
4040
}
4141
}

parser/snapshots/parser__ast_tree_test__tests__test_return.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ expression: return 3
44
---
55
{
66
"Program": {
7+
"type": "Program",
78
"body": [
89
{
910
"stmt_type": "Return",
@@ -19,7 +20,6 @@ expression: return 3
1920
"span": {
2021
"start": 0,
2122
"end": 9
22-
},
23-
"type": "Program"
23+
}
2424
}
2525
}

0 commit comments

Comments
 (0)