Skip to content

Commit 1b8ab76

Browse files
committed
refactor(ast): better ast structure for binding identifier and literals.
1 parent da215d4 commit 1b8ab76

File tree

55 files changed

+308
-228
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+308
-228
lines changed

crates/fuse-ast/src/ast.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub enum BindingPatternKind {
8282
#[derive(Debug, PartialEq)]
8383
pub struct BindingIdentifier {
8484
pub span: Span,
85-
pub atom: Atom,
85+
pub identifier: Identifier,
8686
pub mutable: bool,
8787
}
8888

@@ -100,7 +100,9 @@ pub struct Atom(pub Rc<str>);
100100
#[serializable]
101101
#[derive(Debug, PartialEq)]
102102
pub enum Expression {
103-
Literal(Box<Literal>),
103+
NumberLiteral(Box<NumberLiteral>),
104+
StringLiteral(Box<StringLiteral>),
105+
BooleanLiteral(Box<BooleanLiteral>),
104106
Identifier(Box<Identifier>),
105107
Function(Box<Function>),
106108
If(Box<If>),
@@ -114,14 +116,6 @@ pub enum Expression {
114116
StructConstructionExpression(Box<StructConstructionExpression>),
115117
}
116118

117-
#[serializable]
118-
#[derive(Debug, PartialEq)]
119-
pub enum Literal {
120-
Number(NumberLiteral),
121-
String(StringLiteral),
122-
Boolean(BooleanLiteral),
123-
}
124-
125119
#[serializable]
126120
#[derive(Debug, PartialEq)]
127121
pub struct BooleanLiteral {

crates/fuse-ast/src/ast_factory.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,15 @@ impl AstFactory {
8585
}
8686
}
8787

88-
pub fn binding_identifier(&self, span: Span, atom: Atom, mutable: bool) -> BindingIdentifier {
88+
pub fn binding_identifier(
89+
&self,
90+
span: Span,
91+
identifier: Identifier,
92+
mutable: bool,
93+
) -> BindingIdentifier {
8994
BindingIdentifier {
9095
span,
91-
atom,
96+
identifier,
9297
mutable,
9398
}
9499
}
@@ -113,15 +118,15 @@ impl AstFactory {
113118
}
114119

115120
pub fn boolean_literal_expression(&self, literal: BooleanLiteral) -> Expression {
116-
Expression::Literal(Box::from(Literal::Boolean(literal)))
121+
Expression::BooleanLiteral(Box::from(literal))
117122
}
118123

119124
pub fn number_literal_expression(&self, literal: NumberLiteral) -> Expression {
120-
Expression::Literal(Box::from(Literal::Number(literal)))
125+
Expression::NumberLiteral(Box::from(literal))
121126
}
122127

123128
pub fn string_literal_expression(&self, literal: StringLiteral) -> Expression {
124-
Expression::Literal(Box::from(Literal::String(literal)))
129+
Expression::StringLiteral(Box::from(literal))
125130
}
126131

127132
pub fn identifier_expression(&self, ident: Identifier) -> Expression {

crates/fuse-parser/src/parsers/binding.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl<'a> Parser<'a> {
3232
return Err(Self::unexpected_error(self.cur_token()));
3333
}
3434

35-
let identifier = self.parse_binding_identifier();
35+
let identifier = self.parse_binding_identifier()?;
3636
let type_annotation = if self.consume_if(TokenKind::Colon).is_some() {
3737
Some(self.parse_type_annotation()?)
3838
} else {
@@ -44,16 +44,13 @@ impl<'a> Parser<'a> {
4444
.binding_identifier_pattern(identifier, type_annotation, false))
4545
}
4646

47-
pub(crate) fn parse_binding_identifier(&mut self) -> BindingIdentifier {
47+
pub(crate) fn parse_binding_identifier(&mut self) -> ParserResult<BindingIdentifier> {
4848
let mut span = self.start_span();
4949
let mutable = self.consume_if(TokenKind::Mut).is_some();
50-
let token = self.consume();
51-
let name = self.view_token(*token);
50+
let identifier = self.parse_identifier()?;
5251

5352
span = self.end_span(span);
5453

55-
let atom = self.ast.atom(name);
56-
57-
self.ast.binding_identifier(span, atom, mutable)
54+
Ok(self.ast.binding_identifier(span, identifier, mutable))
5855
}
5956
}

crates/fuse-parser/tests/cases/fail/variable-declaration-01/ast.snap

+9-3
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,27 @@ Some(Chunk(
2323
start: 6,
2424
end: 10,
2525
),
26-
atom: Atom("name"),
26+
identifier: Identifier(
27+
span: Span(
28+
start: 6,
29+
end: 10,
30+
),
31+
name: Atom("name"),
32+
),
2733
mutable: false,
2834
)),
2935
type_annotation: None,
3036
optional: false,
3137
),
32-
expression: Some(Literal(Number(NumberLiteral(
38+
expression: Some(NumberLiteral(NumberLiteral(
3339
span: Span(
3440
start: 13,
3541
end: 16,
3642
),
3743
raw: Atom("123"),
3844
value: 123.0,
3945
kind: Decimal,
40-
)))),
46+
))),
4147
)),
4248
],
4349
),

crates/fuse-parser/tests/cases/pass/array-initializer-01/ast.snap

+10-10
Original file line numberDiff line numberDiff line change
@@ -17,51 +17,51 @@ Some(Chunk(
1717
end: 15,
1818
),
1919
elements: [
20-
Expression(Literal(Number(NumberLiteral(
20+
Expression(NumberLiteral(NumberLiteral(
2121
span: Span(
2222
start: 1,
2323
end: 2,
2424
),
2525
raw: Atom("1"),
2626
value: 1.0,
2727
kind: Decimal,
28-
)))),
29-
Expression(Literal(Number(NumberLiteral(
28+
))),
29+
Expression(NumberLiteral(NumberLiteral(
3030
span: Span(
3131
start: 4,
3232
end: 5,
3333
),
3434
raw: Atom("2"),
3535
value: 2.0,
3636
kind: Decimal,
37-
)))),
38-
Expression(Literal(Number(NumberLiteral(
37+
))),
38+
Expression(NumberLiteral(NumberLiteral(
3939
span: Span(
4040
start: 7,
4141
end: 8,
4242
),
4343
raw: Atom("3"),
4444
value: 3.0,
4545
kind: Decimal,
46-
)))),
47-
Expression(Literal(Number(NumberLiteral(
46+
))),
47+
Expression(NumberLiteral(NumberLiteral(
4848
span: Span(
4949
start: 10,
5050
end: 11,
5151
),
5252
raw: Atom("4"),
5353
value: 4.0,
5454
kind: Decimal,
55-
)))),
56-
Expression(Literal(Number(NumberLiteral(
55+
))),
56+
Expression(NumberLiteral(NumberLiteral(
5757
span: Span(
5858
start: 13,
5959
end: 14,
6060
),
6161
raw: Atom("5"),
6262
value: 5.0,
6363
kind: Decimal,
64-
)))),
64+
))),
6565
],
6666
))),
6767
],

crates/fuse-parser/tests/cases/pass/array-initializer-02/ast.snap

+18-18
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,42 @@ Some(Chunk(
1717
end: 37,
1818
),
1919
elements: [
20-
Expression(Literal(Number(NumberLiteral(
20+
Expression(NumberLiteral(NumberLiteral(
2121
span: Span(
2222
start: 1,
2323
end: 2,
2424
),
2525
raw: Atom("1"),
2626
value: 1.0,
2727
kind: Decimal,
28-
)))),
29-
Expression(Literal(Number(NumberLiteral(
28+
))),
29+
Expression(NumberLiteral(NumberLiteral(
3030
span: Span(
3131
start: 4,
3232
end: 5,
3333
),
3434
raw: Atom("2"),
3535
value: 2.0,
3636
kind: Decimal,
37-
)))),
38-
Expression(Literal(Number(NumberLiteral(
37+
))),
38+
Expression(NumberLiteral(NumberLiteral(
3939
span: Span(
4040
start: 7,
4141
end: 8,
4242
),
4343
raw: Atom("3"),
4444
value: 3.0,
4545
kind: Decimal,
46-
)))),
47-
Expression(Literal(Number(NumberLiteral(
46+
))),
47+
Expression(NumberLiteral(NumberLiteral(
4848
span: Span(
4949
start: 10,
5050
end: 11,
5151
),
5252
raw: Atom("4"),
5353
value: 4.0,
5454
kind: Decimal,
55-
)))),
55+
))),
5656
Spread(SpreadArgument(
5757
span: Span(
5858
start: 13,
@@ -64,33 +64,33 @@ Some(Chunk(
6464
end: 36,
6565
),
6666
elements: [
67-
Expression(Literal(Number(NumberLiteral(
67+
Expression(NumberLiteral(NumberLiteral(
6868
span: Span(
6969
start: 17,
7070
end: 18,
7171
),
7272
raw: Atom("5"),
7373
value: 5.0,
7474
kind: Decimal,
75-
)))),
76-
Expression(Literal(Number(NumberLiteral(
75+
))),
76+
Expression(NumberLiteral(NumberLiteral(
7777
span: Span(
7878
start: 20,
7979
end: 21,
8080
),
8181
raw: Atom("6"),
8282
value: 6.0,
8383
kind: Decimal,
84-
)))),
85-
Expression(Literal(Number(NumberLiteral(
84+
))),
85+
Expression(NumberLiteral(NumberLiteral(
8686
span: Span(
8787
start: 23,
8888
end: 24,
8989
),
9090
raw: Atom("7"),
9191
value: 7.0,
9292
kind: Decimal,
93-
)))),
93+
))),
9494
Spread(SpreadArgument(
9595
span: Span(
9696
start: 26,
@@ -102,24 +102,24 @@ Some(Chunk(
102102
end: 35,
103103
),
104104
elements: [
105-
Expression(Literal(Number(NumberLiteral(
105+
Expression(NumberLiteral(NumberLiteral(
106106
span: Span(
107107
start: 30,
108108
end: 31,
109109
),
110110
raw: Atom("8"),
111111
value: 8.0,
112112
kind: Decimal,
113-
)))),
114-
Expression(Literal(Number(NumberLiteral(
113+
))),
114+
Expression(NumberLiteral(NumberLiteral(
115115
span: Span(
116116
start: 33,
117117
end: 34,
118118
),
119119
raw: Atom("9"),
120120
value: 9.0,
121121
kind: Decimal,
122-
)))),
122+
))),
123123
],
124124
)),
125125
)),

crates/fuse-parser/tests/cases/pass/binary-operator-04/ast.snap

+10-10
Original file line numberDiff line numberDiff line change
@@ -33,63 +33,63 @@ Some(Chunk(
3333
start: 6,
3434
end: 7,
3535
)),
36-
lhs: Literal(Number(NumberLiteral(
36+
lhs: NumberLiteral(NumberLiteral(
3737
span: Span(
3838
start: 4,
3939
end: 5,
4040
),
4141
raw: Atom("1"),
4242
value: 1.0,
4343
kind: Decimal,
44-
))),
44+
)),
4545
rhs: BinaryOperator(BinaryOperator(
4646
kind: Multiply(Span(
4747
start: 10,
4848
end: 11,
4949
)),
50-
lhs: Literal(Number(NumberLiteral(
50+
lhs: NumberLiteral(NumberLiteral(
5151
span: Span(
5252
start: 8,
5353
end: 9,
5454
),
5555
raw: Atom("2"),
5656
value: 2.0,
5757
kind: Decimal,
58-
))),
59-
rhs: Literal(Number(NumberLiteral(
58+
)),
59+
rhs: NumberLiteral(NumberLiteral(
6060
span: Span(
6161
start: 12,
6262
end: 13,
6363
),
6464
raw: Atom("3"),
6565
value: 3.0,
6666
kind: Decimal,
67-
))),
67+
)),
6868
)),
6969
)),
7070
rhs: BinaryOperator(BinaryOperator(
7171
kind: Division(Span(
7272
start: 18,
7373
end: 19,
7474
)),
75-
lhs: Literal(Number(NumberLiteral(
75+
lhs: NumberLiteral(NumberLiteral(
7676
span: Span(
7777
start: 16,
7878
end: 17,
7979
),
8080
raw: Atom("4"),
8181
value: 4.0,
8282
kind: Decimal,
83-
))),
84-
rhs: Literal(Number(NumberLiteral(
83+
)),
84+
rhs: NumberLiteral(NumberLiteral(
8585
span: Span(
8686
start: 20,
8787
end: 21,
8888
),
8989
raw: Atom("5"),
9090
value: 5.0,
9191
kind: Decimal,
92-
))),
92+
)),
9393
)),
9494
)),
9595
))),

0 commit comments

Comments
 (0)