Skip to content

Commit 77f57fb

Browse files
committed
Box callee on ExprCall
1 parent 787e1b0 commit 77f57fb

4 files changed

Lines changed: 41 additions & 20 deletions

File tree

USAGE.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,31 @@ pub enum Expr {
5555
}
5656
```
5757

58+
#### Span Information
59+
60+
`Expr` that contain sub expressions e.g. `ExprCall` store those expressions as `(Expr, Range<usize>)`.
61+
62+
The expression `(and true false)` would parse to this.
63+
64+
```rust
65+
Expr::Call(ExprCall {
66+
callee: (Expr::identifier("and"), 1..4).into(),
67+
args: vec![
68+
(Expr::bool(true), 5..9),
69+
(Expr::bool(false), 10..15)
70+
]
71+
}.into());
72+
```
73+
74+
Span information `1..4`, `5..9`, `10..15` is stored next to the subexpressions `and`, `true`, and `false`.
75+
5876
#### ExprCall
5977

6078
A call to a builtin (referenced by identifier) with N expressions passed as arguments.`
6179

6280
```rust
6381
pub struct ExprCall {
64-
pub callee: ExprS,
82+
pub callee: Box<ExprS>,
6583
pub args: Vec<ExprS>,
6684
}
6785
```

src/ast.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ impl Expr {
3636
}
3737

3838
pub fn call(callee: ExprS, args: Vec<ExprS>) -> Self {
39-
Self::Call(Box::new(ExprCall { callee, args }))
39+
Self::Call(Box::new(ExprCall {
40+
callee: callee.into(),
41+
args,
42+
}))
4043
}
4144

4245
pub fn bool(value: bool) -> Self {
@@ -146,7 +149,7 @@ impl ExprString {
146149

147150
#[derive(Debug, PartialEq)]
148151
pub struct ExprCall {
149-
pub callee: ExprS,
152+
pub callee: Box<ExprS>,
150153
pub args: Vec<ExprS>,
151154
}
152155

src/grammar.lalrpop

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ CallCallee = ExprS;
3737
CallArgs = Args<ExprS>;
3838
ExprCall: ast::Expr = {
3939
"(" <callee:CallCallee> <args:CallArgs> ")" => ast::Expr::Call(ast::ExprCall {
40-
callee,
40+
callee: callee.into(),
4141
args
4242
}.into())
4343
}

tests/integration_tests.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ mod valid {
937937

938938
ast should be: Ok(
939939
Expr::Call(ExprCall {
940-
callee: (Expr::identifier("not"), 1..4),
940+
callee: (Expr::identifier("not"), 1..4).into(),
941941
args: vec![(
942942
Expr::bool(false),
943943
5..10
@@ -982,7 +982,7 @@ mod valid {
982982

983983
ast should be: Ok(
984984
Expr::Call(ExprCall {
985-
callee: (Expr::identifier("and"), 1..4),
985+
callee: (Expr::identifier("and"), 1..4).into(),
986986
args: vec![
987987
(Expr::bool(true), 5..9),
988988
(Expr::bool(false), 10..15)
@@ -1028,7 +1028,7 @@ mod valid {
10281028

10291029
ast should be: Ok(
10301030
Expr::Call(ExprCall {
1031-
callee: (Expr::identifier("and"), 1..4),
1031+
callee: (Expr::identifier("and"), 1..4).into(),
10321032
args: vec![
10331033
(Expr::bool(true), 5..9),
10341034
(Expr::bool(true), 10..14)
@@ -1074,7 +1074,7 @@ mod valid {
10741074

10751075
ast should be: Ok(
10761076
Expr::Call(ExprCall {
1077-
callee: (Expr::identifier("and"), 1..4),
1077+
callee: (Expr::identifier("and"), 1..4).into(),
10781078
args: vec![
10791079
(Expr::bool(false), 5..10),
10801080
(Expr::bool(true), 11..15)
@@ -1119,7 +1119,7 @@ mod valid {
11191119

11201120
ast should be: Ok(
11211121
Expr::Call(ExprCall {
1122-
callee: (Expr::identifier("or"), 1..3),
1122+
callee: (Expr::identifier("or"), 1..3).into(),
11231123
args: vec![
11241124
(Expr::bool(true), 4..8),
11251125
(Expr::bool(false), 9..14)
@@ -1164,7 +1164,7 @@ mod valid {
11641164

11651165
ast should be: Ok(
11661166
Expr::Call(ExprCall {
1167-
callee: (Expr::identifier("or"), 1..3),
1167+
callee: (Expr::identifier("or"), 1..3).into(),
11681168
args: vec![
11691169
(Expr::bool(true), 4..8),
11701170
(Expr::bool(true), 9..13)
@@ -1209,7 +1209,7 @@ mod valid {
12091209

12101210
ast should be: Ok(
12111211
Expr::Call(ExprCall {
1212-
callee: (Expr::identifier("or"), 1..3),
1212+
callee: (Expr::identifier("or"), 1..3).into(),
12131213
args: vec![
12141214
(Expr::bool(false), 4..9),
12151215
(Expr::bool(true), 10..14)
@@ -1256,7 +1256,7 @@ mod valid {
12561256

12571257
ast should be: Ok(
12581258
Expr::Call(ExprCall {
1259-
callee: (Expr::identifier("cond"), 1..5),
1259+
callee: (Expr::identifier("cond"), 1..5).into(),
12601260
args: vec![
12611261
(Expr::bool(true), 6..10),
12621262
(Expr::string("foo"), 11..16),
@@ -1308,7 +1308,7 @@ mod valid {
13081308

13091309
ast should be: Ok(
13101310
Expr::Call(ExprCall {
1311-
callee: (Expr::identifier("cond"), 1..5),
1311+
callee: (Expr::identifier("cond"), 1..5).into(),
13121312
args: vec![
13131313
(Expr::bool(false), 6..11),
13141314
(Expr::string("foo"), 12..17),
@@ -1585,7 +1585,7 @@ mod valid {
15851585

15861586
ast should be: Ok(
15871587
Expr::Call(ExprCall {
1588-
callee: (Expr::identifier("contains"), 1..9),
1588+
callee: (Expr::identifier("contains"), 1..9).into(),
15891589
args: vec![
15901590
(Expr::Identifier(ExprIdentifier(":a".to_string(), IdentifierKind::Var, Some(Type::String)).into()), 10..12),
15911591
(Expr::identifier_with_type(":b", Type::String), 13..15)
@@ -2442,7 +2442,7 @@ mod invalid {
24422442
];
24432443

24442444
ast should be: Ok(Expr::Call(ExprCall {
2445-
callee: (Expr::identifier("concat"), 1..7),
2445+
callee: (Expr::identifier("concat"), 1..7).into(),
24462446
args: vec![
24472447
(
24482448
Expr::identifier("foo"),
@@ -2509,7 +2509,7 @@ mod invalid {
25092509
];
25102510

25112511
ast should be: Ok(Expr::Call(ExprCall {
2512-
callee: (Expr::identifier("not"), 1..4),
2512+
callee: (Expr::identifier("not"), 1..4).into(),
25132513
args: vec![]
25142514
}.into()));
25152515

@@ -2548,7 +2548,7 @@ mod invalid {
25482548
];
25492549

25502550
ast should be: Ok(Expr::Call(ExprCall {
2551-
callee: (Expr::identifier("not"), 1..4),
2551+
callee: (Expr::identifier("not"), 1..4).into(),
25522552
args: vec![
25532553
(Expr::bool(true), 5..9),
25542554
(Expr::bool(false), 10..15),
@@ -2589,7 +2589,7 @@ mod invalid {
25892589
];
25902590

25912591
ast should be: Ok(Expr::Call(ExprCall {
2592-
callee: (Expr::identifier("not"), 1..4),
2592+
callee: (Expr::identifier("not"), 1..4).into(),
25932593
args: vec![
25942594
(Expr::string("true"), 5..11),
25952595
]
@@ -2630,7 +2630,7 @@ mod invalid {
26302630
];
26312631

26322632
ast should be: Ok(Expr::Call(ExprCall {
2633-
callee: (Expr::identifier("not"), 1..4),
2633+
callee: (Expr::identifier("not"), 1..4).into(),
26342634
args: vec![
26352635
(Expr::bool(true), 5..9),
26362636
(Expr::string("true"), 10..16),
@@ -2672,7 +2672,7 @@ mod invalid {
26722672
];
26732673

26742674
ast should be: Ok(Expr::Call(ExprCall {
2675-
callee: (Expr::identifier("not"), 1..4),
2675+
callee: (Expr::identifier("not"), 1..4).into(),
26762676
args: vec![
26772677
(Expr::string("true"), 5..11),
26782678
(Expr::bool(true), 12..16),

0 commit comments

Comments
 (0)