Skip to content

Commit da215d4

Browse files
committed
refactor(ast): better literal type wrapping.
1 parent 28d5c9a commit da215d4

File tree

50 files changed

+211
-205
lines changed

Some content is hidden

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

50 files changed

+211
-205
lines changed

crates/fuse-ast/src/ast.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@ pub struct Atom(pub Rc<str>);
100100
#[serializable]
101101
#[derive(Debug, PartialEq)]
102102
pub enum Expression {
103-
NumberLiteral(Box<NumberLiteral>),
104-
StringLiteral(Box<StringLiteral>),
105-
BooleanLiteral(Box<BooleanLiteral>),
103+
Literal(Box<Literal>),
106104
Identifier(Box<Identifier>),
107105
Function(Box<Function>),
108106
If(Box<If>),
@@ -116,6 +114,14 @@ pub enum Expression {
116114
StructConstructionExpression(Box<StructConstructionExpression>),
117115
}
118116

117+
#[serializable]
118+
#[derive(Debug, PartialEq)]
119+
pub enum Literal {
120+
Number(NumberLiteral),
121+
String(StringLiteral),
122+
Boolean(BooleanLiteral),
123+
}
124+
119125
#[serializable]
120126
#[derive(Debug, PartialEq)]
121127
pub struct BooleanLiteral {

crates/fuse-ast/src/ast_factory.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,16 @@ impl AstFactory {
112112
}
113113
}
114114

115-
pub fn boolean_expression(&self, literal: BooleanLiteral) -> Expression {
116-
Expression::BooleanLiteral(Box::from(literal))
115+
pub fn boolean_literal_expression(&self, literal: BooleanLiteral) -> Expression {
116+
Expression::Literal(Box::from(Literal::Boolean(literal)))
117117
}
118118

119-
pub fn number_expression(&self, literal: NumberLiteral) -> Expression {
120-
Expression::NumberLiteral(Box::from(literal))
119+
pub fn number_literal_expression(&self, literal: NumberLiteral) -> Expression {
120+
Expression::Literal(Box::from(Literal::Number(literal)))
121121
}
122122

123-
pub fn string_expression(&self, literal: StringLiteral) -> Expression {
124-
Expression::StringLiteral(Box::from(literal))
123+
pub fn string_literal_expression(&self, literal: StringLiteral) -> Expression {
124+
Expression::Literal(Box::from(Literal::String(literal)))
125125
}
126126

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

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,24 @@ impl<'a> Parser<'a> {
2424
let expr = match self.cur_kind() {
2525
True => {
2626
let token = self.consume();
27-
Ok(self.ast.boolean_expression(BooleanLiteral {
27+
Ok(self.ast.boolean_literal_expression(BooleanLiteral {
2828
span: token.span(),
2929
value: true,
3030
}))
3131
}
3232
False => {
3333
let token = self.consume();
34-
Ok(self.ast.boolean_expression(BooleanLiteral {
34+
Ok(self.ast.boolean_literal_expression(BooleanLiteral {
3535
span: token.span(),
3636
value: false,
3737
}))
3838
}
3939
NumberLiteral => self
4040
.parse_number_literal()
41-
.map(|expr| self.ast.number_expression(expr)),
41+
.map(|expr| self.ast.number_literal_expression(expr)),
4242
StringLiteral | InterpolatedStringHead => self
4343
.parse_string_literal()
44-
.map(|expr| self.ast.string_expression(expr)),
44+
.map(|expr| self.ast.string_literal_expression(expr)),
4545
Identifier => self
4646
.parse_identifier()
4747
.map(|id| self.ast.identifier_expression(id)),

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ Some(Chunk(
2929
type_annotation: None,
3030
optional: false,
3131
),
32-
expression: Some(NumberLiteral(NumberLiteral(
32+
expression: Some(Literal(Number(NumberLiteral(
3333
span: Span(
3434
start: 13,
3535
end: 16,
3636
),
3737
raw: Atom("123"),
3838
value: 123.0,
3939
kind: Decimal,
40-
))),
40+
)))),
4141
)),
4242
],
4343
),

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(NumberLiteral(NumberLiteral(
20+
Expression(Literal(Number(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(NumberLiteral(NumberLiteral(
28+
)))),
29+
Expression(Literal(Number(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(NumberLiteral(NumberLiteral(
37+
)))),
38+
Expression(Literal(Number(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(NumberLiteral(NumberLiteral(
46+
)))),
47+
Expression(Literal(Number(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(NumberLiteral(NumberLiteral(
55+
)))),
56+
Expression(Literal(Number(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(NumberLiteral(NumberLiteral(
20+
Expression(Literal(Number(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(NumberLiteral(NumberLiteral(
28+
)))),
29+
Expression(Literal(Number(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(NumberLiteral(NumberLiteral(
37+
)))),
38+
Expression(Literal(Number(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(NumberLiteral(NumberLiteral(
46+
)))),
47+
Expression(Literal(Number(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(NumberLiteral(NumberLiteral(
67+
Expression(Literal(Number(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(NumberLiteral(NumberLiteral(
75+
)))),
76+
Expression(Literal(Number(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(NumberLiteral(NumberLiteral(
84+
)))),
85+
Expression(Literal(Number(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(NumberLiteral(NumberLiteral(
105+
Expression(Literal(Number(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(NumberLiteral(NumberLiteral(
113+
)))),
114+
Expression(Literal(Number(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: NumberLiteral(NumberLiteral(
36+
lhs: Literal(Number(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: NumberLiteral(NumberLiteral(
50+
lhs: Literal(Number(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: NumberLiteral(NumberLiteral(
58+
))),
59+
rhs: Literal(Number(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: NumberLiteral(NumberLiteral(
75+
lhs: Literal(Number(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: NumberLiteral(NumberLiteral(
83+
))),
84+
rhs: Literal(Number(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)