forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpr.rs
More file actions
395 lines (347 loc) · 12.4 KB
/
Copy pathexpr.rs
File metadata and controls
395 lines (347 loc) · 12.4 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
use crate::SyntaxKind;
use super::{
define_scope, expr_atom,
param::{CallArgListScope, GenericArgListScope},
token_stream::{LexicalToken, TokenStream},
Checkpoint, Parser,
};
/// Parses expression.
pub fn parse_expr<S: TokenStream>(parser: &mut Parser<S>) -> bool {
parse_expr_with_min_bp(parser, 0, true)
}
/// Parses expression except for `struct` initialization expression.
pub fn parse_expr_no_struct<S: TokenStream>(parser: &mut Parser<S>) -> bool {
parse_expr_with_min_bp(parser, 0, false)
}
// Expressions are parsed in Pratt's top-down operator precedence style.
// <https://matklad.github.io/2020/04/13/simple-but-powerful-pratt-parsing.html>
/// Parse an expression, stopping if/when we reach an operator that binds less
/// tightly than given binding power.
///
/// Returns `true` if parsing succeeded, `false` otherwise.
fn parse_expr_with_min_bp<S: TokenStream>(
parser: &mut Parser<S>,
min_bp: u8,
allow_struct_init: bool,
) -> bool {
let (ok, checkpoint) = parse_expr_atom(parser, allow_struct_init);
if !ok {
return false;
}
loop {
let Some(kind) = parser.current_kind() else {
break;
};
// Parse postfix operators.
match postfix_binding_power(kind) {
Some(lbp) if lbp < min_bp => break,
Some(_) => {
match kind {
SyntaxKind::LBracket => {
parser.parse(IndexExprScope::default(), Some(checkpoint));
continue;
}
SyntaxKind::LParen => {
if parser.parse(CallExprScope::default(), Some(checkpoint)).0 {
continue;
}
}
// `expr<generic_param_args>()`.
SyntaxKind::Lt => {
//let is_call_expr =
// parser.dry_run(|parser| parser.parse(CallExprScope::default(),
// None).0);
if is_call_expr(parser) {
parser.parse(CallExprScope::default(), Some(checkpoint));
continue;
}
}
// `expr.method<T, i32>()`
SyntaxKind::Dot => {
if is_method_call(parser) {
parser.parse(MethodExprScope::default(), Some(checkpoint));
continue;
}
}
_ => unreachable!(),
}
}
None => {}
}
if let Some((lbp, _)) = infix_binding_power(parser) {
if lbp < min_bp {
break;
}
if !match kind {
// Method call is already handled as the postfix operator.
SyntaxKind::Dot => parser.parse(FieldExprScope::default(), Some(checkpoint)).0,
_ => parser.parse(BinExprScope::default(), Some(checkpoint)).0,
} {
return false;
}
continue;
}
break;
}
true
}
fn parse_expr_atom<S: TokenStream>(
parser: &mut Parser<S>,
allow_struct_init: bool,
) -> (bool, Checkpoint) {
match parser.current_kind() {
Some(kind) if prefix_binding_power(kind).is_some() => {
parser.parse(UnExprScope::default(), None)
}
Some(_) => expr_atom::parse_expr_atom(parser, allow_struct_init),
None => {
parser.error_and_recover("expected expression", None);
(false, parser.checkpoint())
}
}
}
/// Specifies how tightly a prefix unary operator binds to its operand.
fn prefix_binding_power(kind: SyntaxKind) -> Option<u8> {
use SyntaxKind::*;
match kind {
Not | Plus | Minus | Tilde => Some(145),
_ => None,
}
}
/// Specifies how tightly a postfix operator binds to its operand.
fn postfix_binding_power(kind: SyntaxKind) -> Option<u8> {
use SyntaxKind::*;
match kind {
LBracket | LParen | Lt => Some(147),
Dot => Some(151),
_ => None,
}
}
/// Specifies how tightly does an infix operator bind to its left and right
/// operands.
fn infix_binding_power<S: TokenStream>(parser: &mut Parser<S>) -> Option<(u8, u8)> {
use SyntaxKind::*;
let bp = match parser.current_kind()? {
Pipe2 => (50, 51),
Amp2 => (60, 61),
NotEq | Eq2 => (70, 71),
Lt => {
if is_lshift(parser) {
(110, 111)
} else {
// `LT` and `LtEq` has the same binding power.
(70, 71)
}
}
Gt => {
if is_rshift(parser) {
(110, 111)
} else {
// `Gt` and `GtEq` has the same binding power.
(70, 71)
}
}
Pipe => (80, 81),
Hat => (90, 91),
Amp => (100, 101),
LShift | RShift => (110, 111),
Plus | Minus => (120, 121),
Star | Slash | Percent => (130, 131),
Star2 => (141, 140),
Dot => (151, 150),
_ => return None,
};
Some(bp)
}
define_scope! { UnExprScope, UnExpr, Inheritance }
impl super::Parse for UnExprScope {
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) {
parser.set_newline_as_trivia(false);
let kind = parser.current_kind().unwrap();
let bp = prefix_binding_power(kind).unwrap();
parser.bump();
parse_expr_with_min_bp(parser, bp, true);
}
}
define_scope! { BinExprScope, BinExpr, Inheritance }
impl super::Parse for BinExprScope {
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) {
parser.set_newline_as_trivia(false);
let (_, rbp) = infix_binding_power(parser).unwrap();
bump_bin_op(parser);
parse_expr_with_min_bp(parser, rbp, true);
}
}
define_scope! { IndexExprScope, IndexExpr, Override(RBracket) }
impl super::Parse for IndexExprScope {
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) {
parser.set_newline_as_trivia(false);
parser.bump_expected(SyntaxKind::LBracket);
parser.with_next_expected_tokens(parse_expr, &[SyntaxKind::RBracket]);
parser.bump_or_recover(SyntaxKind::RBracket, "expected `]`", None);
}
}
define_scope! { CallExprScope, CallExpr, Inheritance }
impl super::Parse for CallExprScope {
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) {
parser.set_newline_as_trivia(false);
if parser.current_kind() == Some(SyntaxKind::Lt) {
parser.with_next_expected_tokens(
|parser| {
parser.parse(GenericArgListScope::default(), None);
},
&[SyntaxKind::LParen],
);
}
if parser.current_kind() != Some(SyntaxKind::LParen) {
parser.error_and_recover("expected `(`", None);
return;
}
parser.parse(CallArgListScope::default(), None);
}
}
define_scope! { MethodExprScope, MethodCallExpr, Inheritance }
impl super::Parse for MethodExprScope {
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) {
parser.set_newline_as_trivia(false);
parser.bump_expected(SyntaxKind::Dot);
parser.bump_or_recover(SyntaxKind::Ident, "expected identifier", None);
parser.with_next_expected_tokens(
|parser| {
if parser.current_kind() == Some(SyntaxKind::Lt) {
parser.parse(GenericArgListScope::default(), None);
}
},
&[SyntaxKind::LParen],
);
if parser.current_kind() != Some(SyntaxKind::LParen) {
parser.error_and_recover("expected `(`", None);
return;
}
parser.parse(CallArgListScope::default(), None);
}
}
define_scope! { FieldExprScope, FieldExpr, Inheritance }
impl super::Parse for FieldExprScope {
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) {
parser.set_newline_as_trivia(false);
parser.bump_expected(SyntaxKind::Dot);
match parser.current_token() {
Some(token) if token.syntax_kind() == SyntaxKind::Ident => {
parser.bump();
}
Some(token) if token.syntax_kind() == SyntaxKind::Int => {
let text = token.text();
if !text.chars().all(|c| c.is_ascii_digit()) {
parser
.error_and_recover("expected integer decimal literal without prefix", None);
return;
}
parser.bump();
}
_ => {
parser.error_and_recover("expected identifier or integer literal", None);
}
}
}
}
define_scope! { pub(super) LShiftScope, LShift, Inheritance }
impl super::Parse for LShiftScope {
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) {
parser.bump_or_recover(SyntaxKind::Lt, "expected `<<`", None);
parser.bump_or_recover(SyntaxKind::Lt, "expected `<<`", None);
}
}
define_scope! { pub(super) RShiftScope, RShift, Inheritance }
impl super::Parse for RShiftScope {
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) {
parser.bump_or_recover(SyntaxKind::Gt, "expected `>>`", None);
parser.bump_or_recover(SyntaxKind::Gt, "expected `>>`", None);
}
}
define_scope! { pub(super) LtEqScope, LtEq, Inheritance }
impl super::Parse for LtEqScope {
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) {
parser.bump_or_recover(SyntaxKind::Lt, "expected `<=`", None);
parser.bump_or_recover(SyntaxKind::Eq, "expected `<=`", None);
}
}
define_scope! { pub(super) GtEqScope, GtEq, Inheritance }
impl super::Parse for GtEqScope {
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) {
parser.bump_or_recover(SyntaxKind::Gt, "expected `>=`", None);
parser.bump_or_recover(SyntaxKind::Eq, "expected `>=`", None);
}
}
pub(crate) fn is_lshift<S: TokenStream>(parser: &mut Parser<S>) -> bool {
parser.dry_run(|parser| parser.parse(LShiftScope::default(), None).0)
}
pub(crate) fn is_rshift<S: TokenStream>(parser: &mut Parser<S>) -> bool {
parser.dry_run(|parser| parser.parse(RShiftScope::default(), None).0)
}
fn is_lt_eq<S: TokenStream>(parser: &mut Parser<S>) -> bool {
parser.dry_run(|parser| parser.parse(LtEqScope::default(), None).0)
}
fn is_gt_eq<S: TokenStream>(parser: &mut Parser<S>) -> bool {
parser.dry_run(|parser| parser.parse(GtEqScope::default(), None).0)
}
fn bump_bin_op<S: TokenStream>(parser: &mut Parser<S>) {
match parser.current_kind() {
Some(SyntaxKind::Lt) => {
if is_lshift(parser) {
parser.parse(LShiftScope::default(), None);
} else if is_lt_eq(parser) {
parser.parse(LtEqScope::default(), None);
} else {
parser.bump();
}
}
Some(SyntaxKind::Gt) => {
if is_rshift(parser) {
parser.parse(RShiftScope::default(), None);
} else if is_gt_eq(parser) {
parser.parse(GtEqScope::default(), None);
} else {
parser.bump();
}
}
_ => {
parser.bump();
}
}
}
fn is_call_expr<S: TokenStream>(parser: &mut Parser<S>) -> bool {
parser.dry_run(|parser| {
parser.set_newline_as_trivia(false);
let mut is_call = true;
if parser.current_kind() == Some(SyntaxKind::Lt) {
is_call &= parser.parse(GenericArgListScope::default(), None).0;
}
if parser.current_kind() != Some(SyntaxKind::LParen) {
false
} else {
is_call && parser.parse(CallArgListScope::default(), None).0
}
})
}
fn is_method_call<S: TokenStream>(parser: &mut Parser<S>) -> bool {
parser.dry_run(|parser| {
parser.set_newline_as_trivia(false);
if !parser.bump_if(SyntaxKind::Dot) {
return false;
}
if !parser.bump_if(SyntaxKind::Ident) {
return false;
}
if parser.current_kind() == Some(SyntaxKind::Lt)
&& !parser.parse(GenericArgListScope::default(), None).0
{
return false;
}
if parser.current_kind() != Some(SyntaxKind::LParen) {
false
} else {
parser.parse(CallArgListScope::default(), None).0
}
})
}