1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
use super::*;
// test expr_literals
// fn foo() {
// let _ = true;
// let _ = false;
// let _ = 1;
// let _ = 2.0;
// let _ = b'a';
// let _ = 'b';
// let _ = "c";
// let _ = r"d";
// let _ = b"e";
// let _ = br"f";
// }
pub(super) fn literal(p: &mut Parser) -> Option<CompletedMarker> {
match p.current() {
TRUE_KW | FALSE_KW | INT_NUMBER | FLOAT_NUMBER | BYTE | CHAR | STRING | RAW_STRING
| BYTE_STRING | RAW_BYTE_STRING => {
let m = p.start();
p.bump();
Some(m.complete(p, LITERAL))
}
_ => None,
}
}
pub(super) fn expr(p: &mut Parser) {
let mut lhs = match prefix_expr(p) {
Some(lhs) => lhs,
None => return,
};
loop {
lhs = match p.current() {
L_PAREN => call_expr(p, lhs),
DOT if p.nth(1) == IDENT =>
if p.nth(2) == L_PAREN {
method_call_expr(p, lhs)
} else {
field_expr(p, lhs)
}
_ => break,
}
}
}
fn prefix_expr(p: &mut Parser) -> Option<CompletedMarker> {
match p.current() {
AMPERSAND => Some(ref_expr(p)),
_ => atom_expr(p),
}
}
// test ref_expr
// fn foo() {
// let _ = &1;
// let _ = &mut &f();
// }
fn ref_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(AMPERSAND));
let m = p.start();
p.bump();
p.eat(MUT_KW);
expr(p);
m.complete(p, REF_EXPR)
}
fn atom_expr(p: &mut Parser) -> Option<CompletedMarker> {
match literal(p) {
Some(m) => return Some(m),
None => (),
}
if paths::is_path_start(p) {
return Some(path_expr(p));
}
match p.current() {
L_PAREN => Some(tuple_expr(p)),
_ => {
p.err_and_bump("expected expression");
None
}
}
}
fn tuple_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(L_PAREN));
let m = p.start();
p.expect(L_PAREN);
p.expect(R_PAREN);
m.complete(p, TUPLE_EXPR)
}
// test call_expr
// fn foo() {
// let _ = f();
// let _ = f()(1)(1, 2,);
// }
fn call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
assert!(p.at(L_PAREN));
let m = lhs.precede(p);
arg_list(p);
m.complete(p, CALL_EXPR)
}
// test method_call_expr
// fn foo() {
// x.foo();
// y.bar(1, 2,);
// }
fn method_call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
assert!(p.at(DOT) && p.nth(1) == IDENT && p.nth(2) == L_PAREN);
let m = lhs.precede(p);
p.bump();
p.bump();
arg_list(p);
m.complete(p, METHOD_CALL_EXPR)
}
// test field_expr
// fn foo() {
// x.foo.bar;
// }
fn field_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
assert!(p.at(DOT) && p.nth(1) == IDENT);
let m = lhs.precede(p);
p.bump();
p.bump();
m.complete(p, FIELD_EXPR)
}
fn arg_list(p: &mut Parser) {
assert!(p.at(L_PAREN));
let m = p.start();
p.bump();
while !p.at(R_PAREN) && !p.at(EOF) {
expr(p);
if !p.at(R_PAREN) && !p.expect(COMMA) {
break;
}
}
p.eat(R_PAREN);
m.complete(p, ARG_LIST);
}
// test path_expr
// fn foo() {
// let _ = a;
// let _ = a::b;
// let _ = ::a::<b>;
// }
fn path_expr(p: &mut Parser) -> CompletedMarker {
assert!(paths::is_path_start(p));
let m = p.start();
paths::expr_path(p);
if p.at(L_CURLY) {
struct_lit(p);
m.complete(p, STRUCT_LIT)
} else {
m.complete(p, PATH_EXPR)
}
}
// test struct_lit
// fn foo() {
// S {};
// S { x, y: 32, };
// S { x, y: 32, ..Default::default() };
// }
fn struct_lit(p: &mut Parser) {
assert!(p.at(L_CURLY));
p.bump();
while !p.at(EOF) && !p.at(R_CURLY) {
match p.current() {
IDENT => {
let m = p.start();
name_ref(p);
if p.eat(COLON) {
expr(p);
}
m.complete(p, STRUCT_LIT_FIELD);
},
DOTDOT => {
p.bump();
expr(p);
},
_ => p.err_and_bump("expected identifier"),
}
if !p.at(R_CURLY) {
p.expect(COMMA);
}
}
p.expect(R_CURLY);
}
|