@@ -56,9 +56,6 @@ fn parse_expr_with_min_bp<S: TokenStream>(
56
56
57
57
// `expr<generic_param_args>()`.
58
58
SyntaxKind :: Lt => {
59
- //let is_call_expr =
60
- // parser.dry_run(|parser| parser.parse(CallExprScope::default(),
61
- // None).0);
62
59
if is_call_expr ( parser) {
63
60
parser. parse ( CallExprScope :: default ( ) , Some ( checkpoint) ) ;
64
61
continue ;
@@ -83,16 +80,17 @@ fn parse_expr_with_min_bp<S: TokenStream>(
83
80
break ;
84
81
}
85
82
86
- if !match kind {
87
- // Method call is already handled as the postfix operator.
88
- SyntaxKind :: Dot => parser. parse ( FieldExprScope :: default ( ) , Some ( checkpoint) ) . 0 ,
89
- _ => {
90
- // 1. Try to parse the expression as an augmented assignment expression.
91
- // 2. If 1. fails, try to parse the expression as an assignment expression.
92
- // 3. If 2. fails, try to parse the expression as a binary expression.
93
- parser. parse ( BinExprScope :: default ( ) , Some ( checkpoint) ) . 0
94
- }
95
- } {
83
+ let ( ok, _) = if kind == SyntaxKind :: Dot {
84
+ parser. parse ( FieldExprScope :: default ( ) , Some ( checkpoint) )
85
+ } else if is_asn ( parser) {
86
+ parser. parse ( AssignExprScope :: default ( ) , Some ( checkpoint) )
87
+ } else if is_aug ( parser) {
88
+ parser. parse ( AugAssignExprScope :: default ( ) , Some ( checkpoint) )
89
+ } else {
90
+ parser. parse ( BinExprScope :: default ( ) , Some ( checkpoint) )
91
+ } ;
92
+
93
+ if !ok {
96
94
return false ;
97
95
}
98
96
@@ -202,21 +200,28 @@ impl super::Parse for BinExprScope {
202
200
fn parse < S : TokenStream > ( & mut self , parser : & mut Parser < S > ) {
203
201
parser. set_newline_as_trivia ( false ) ;
204
202
let ( _, rbp) = infix_binding_power ( parser) . unwrap ( ) ;
205
- if is_aug ( parser) {
206
- self . set_kind ( SyntaxKind :: AugAssignExpr ) ;
207
- bump_aug_assign_op ( parser) ;
208
- parser. bump_expected ( SyntaxKind :: Eq ) ;
209
- parse_expr_with_min_bp ( parser, rbp, true ) ;
210
- } else if is_asn ( parser) {
211
- self . set_kind ( SyntaxKind :: AssignExpr ) ;
212
- parser. set_newline_as_trivia ( false ) ;
213
- let ( _, rbp) = infix_binding_power ( parser) . unwrap ( ) ;
214
- parser. bump_expected ( SyntaxKind :: Eq ) ;
215
- parse_expr_with_min_bp ( parser, rbp, true ) ;
216
- } else {
217
- bump_bin_op ( parser) ;
218
- parse_expr_with_min_bp ( parser, rbp, false ) ;
219
- }
203
+ bump_bin_op ( parser) ;
204
+ parse_expr_with_min_bp ( parser, rbp, false ) ;
205
+ }
206
+ }
207
+
208
+ define_scope ! { AugAssignExprScope , AugAssignExpr , Inheritance }
209
+ impl super :: Parse for AugAssignExprScope {
210
+ fn parse < S : TokenStream > ( & mut self , parser : & mut Parser < S > ) {
211
+ parser. set_newline_as_trivia ( false ) ;
212
+ let ( _, rbp) = infix_binding_power ( parser) . unwrap ( ) ;
213
+ bump_aug_assign_op ( parser) ;
214
+ parse_expr_with_min_bp ( parser, rbp, false ) ;
215
+ }
216
+ }
217
+
218
+ define_scope ! { AssignExprScope , AssignExpr , Inheritance }
219
+ impl super :: Parse for AssignExprScope {
220
+ fn parse < S : TokenStream > ( & mut self , parser : & mut Parser < S > ) {
221
+ parser. set_newline_as_trivia ( false ) ;
222
+ let ( _, rbp) = infix_binding_power ( parser) . unwrap ( ) ;
223
+ parser. bump_expected ( SyntaxKind :: Eq ) ;
224
+ parse_expr_with_min_bp ( parser, rbp, true ) ;
220
225
}
221
226
}
222
227
@@ -305,72 +310,69 @@ impl super::Parse for FieldExprScope {
305
310
define_scope ! { pub ( super ) LShiftScope , LShift , Inheritance }
306
311
impl super :: Parse for LShiftScope {
307
312
fn parse < S : TokenStream > ( & mut self , parser : & mut Parser < S > ) {
308
- parser. bump_or_recover ( SyntaxKind :: Lt , "expected `<<`" , None ) ;
309
- parser. bump_or_recover ( SyntaxKind :: Lt , "expected `<<`" , None ) ;
313
+ parser. bump_expected ( SyntaxKind :: Lt ) ;
314
+ parser. bump_expected ( SyntaxKind :: Lt ) ;
310
315
}
311
316
}
312
317
313
318
define_scope ! { pub ( super ) RShiftScope , RShift , Inheritance }
314
319
impl super :: Parse for RShiftScope {
315
320
fn parse < S : TokenStream > ( & mut self , parser : & mut Parser < S > ) {
316
- parser. bump_or_recover ( SyntaxKind :: Gt , "expected `>>`" , None ) ;
317
- parser. bump_or_recover ( SyntaxKind :: Gt , "expected `>>`" , None ) ;
321
+ parser. bump_expected ( SyntaxKind :: Gt ) ;
322
+ parser. bump_expected ( SyntaxKind :: Gt ) ;
318
323
}
319
324
}
320
325
321
326
define_scope ! { pub ( super ) LtEqScope , LtEq , Inheritance }
322
327
impl super :: Parse for LtEqScope {
323
328
fn parse < S : TokenStream > ( & mut self , parser : & mut Parser < S > ) {
324
- parser. bump_or_recover ( SyntaxKind :: Lt , "expected `<=`" , None ) ;
325
- parser. bump_or_recover ( SyntaxKind :: Eq , "expected `<=`" , None ) ;
329
+ parser. bump_expected ( SyntaxKind :: Lt ) ;
330
+ parser. bump_expected ( SyntaxKind :: Eq ) ;
326
331
}
327
332
}
328
333
329
334
define_scope ! { pub ( super ) GtEqScope , GtEq , Inheritance }
330
335
impl super :: Parse for GtEqScope {
331
336
fn parse < S : TokenStream > ( & mut self , parser : & mut Parser < S > ) {
332
- parser. bump_or_recover ( SyntaxKind :: Gt , "expected `>=`" , None ) ;
333
- parser. bump_or_recover ( SyntaxKind :: Eq , "expected `>=`" , None ) ;
337
+ parser. bump_expected ( SyntaxKind :: Gt ) ;
338
+ parser. bump_expected ( SyntaxKind :: Eq ) ;
334
339
}
335
340
}
336
341
337
342
pub ( crate ) fn is_lshift < S : TokenStream > ( parser : & mut Parser < S > ) -> bool {
338
- parser. dry_run ( |parser| parser . parse ( LShiftScope :: default ( ) , None ) . 0 )
343
+ parser. peek_two ( ) == ( Some ( SyntaxKind :: Lt ) , Some ( SyntaxKind :: Lt ) )
339
344
}
340
345
341
346
pub ( crate ) fn is_rshift < S : TokenStream > ( parser : & mut Parser < S > ) -> bool {
342
- parser. dry_run ( |parser| parser . parse ( RShiftScope :: default ( ) , None ) . 0 )
347
+ parser. peek_two ( ) == ( Some ( SyntaxKind :: Gt ) , Some ( SyntaxKind :: Gt ) )
343
348
}
344
349
345
350
fn is_lt_eq < S : TokenStream > ( parser : & mut Parser < S > ) -> bool {
346
- parser. dry_run ( |parser| parser . parse ( LtEqScope :: default ( ) , None ) . 0 )
351
+ parser. peek_two ( ) == ( Some ( SyntaxKind :: Lt ) , Some ( SyntaxKind :: Eq ) )
347
352
}
348
353
349
354
fn is_gt_eq < S : TokenStream > ( parser : & mut Parser < S > ) -> bool {
350
- parser. dry_run ( |parser| parser . parse ( GtEqScope :: default ( ) , None ) . 0 )
355
+ parser. peek_two ( ) == ( Some ( SyntaxKind :: Gt ) , Some ( SyntaxKind :: Eq ) )
351
356
}
352
357
353
358
fn is_aug < S : TokenStream > ( parser : & mut Parser < S > ) -> bool {
354
- parser. dry_run ( |parser| {
355
- if !bump_aug_assign_op ( parser) {
356
- return false ;
357
- }
358
-
359
- parser. set_newline_as_trivia ( false ) ;
360
- parser. current_kind ( ) == Some ( SyntaxKind :: Eq )
361
- } )
359
+ use SyntaxKind :: * ;
360
+ matches ! (
361
+ parser. peek_three( ) ,
362
+ (
363
+ Some ( Pipe | Hat | Amp | Plus | Minus | Star | Slash | Percent | Star2 ) ,
364
+ Some ( Eq ) ,
365
+ _
366
+ ) | ( Some ( Lt ) , Some ( Lt ) , Some ( Eq ) )
367
+ | ( Some ( Gt ) , Some ( Gt ) , Some ( Eq ) )
368
+ )
362
369
}
363
370
364
371
fn is_asn < S : TokenStream > ( parser : & mut Parser < S > ) -> bool {
365
- parser. dry_run ( |parser| {
366
- parser. set_newline_as_trivia ( false ) ;
367
- if parser. current_kind ( ) == Some ( SyntaxKind :: Eq ) {
368
- parser. bump ( ) ;
369
- true
370
- } else {
371
- false
372
- }
373
- } )
372
+ let nt = parser. set_newline_as_trivia ( false ) ;
373
+ let is_asn = parser. current_kind ( ) == Some ( SyntaxKind :: Eq ) ;
374
+ parser. set_newline_as_trivia ( nt) ;
375
+ is_asn
374
376
}
375
377
376
378
fn bump_bin_op < S : TokenStream > ( parser : & mut Parser < S > ) {
@@ -401,26 +403,21 @@ fn bump_bin_op<S: TokenStream>(parser: &mut Parser<S>) {
401
403
402
404
fn bump_aug_assign_op < S : TokenStream > ( parser : & mut Parser < S > ) -> bool {
403
405
use SyntaxKind :: * ;
404
- match parser. current_kind ( ) {
405
- Some ( Pipe | Hat | Amp | Plus | Minus | Star | Slash | Percent | Star2 ) => {
406
+ match parser. peek_three ( ) {
407
+ ( Some ( Pipe | Hat | Amp | Plus | Minus | Star | Slash | Percent | Star2 ) , Some ( Eq ) , _) => {
408
+ parser. bump ( ) ;
406
409
parser. bump ( ) ;
407
410
true
408
411
}
409
- Some ( Lt ) => {
410
- if is_lshift ( parser) {
411
- parser. parse ( LShiftScope :: default ( ) , None ) ;
412
- true
413
- } else {
414
- false
415
- }
412
+ ( Some ( Lt ) , Some ( Lt ) , Some ( Eq ) ) => {
413
+ parser. parse ( LShiftScope :: default ( ) , None ) ;
414
+ parser. bump_expected ( SyntaxKind :: Eq ) ;
415
+ true
416
416
}
417
- Some ( Gt ) => {
418
- if is_rshift ( parser) {
419
- parser. parse ( RShiftScope :: default ( ) , None ) ;
420
- true
421
- } else {
422
- false
423
- }
417
+ ( Some ( Gt ) , Some ( Gt ) , Some ( Eq ) ) => {
418
+ parser. parse ( RShiftScope :: default ( ) , None ) ;
419
+ parser. bump_expected ( SyntaxKind :: Eq ) ;
420
+ true
424
421
}
425
422
_ => false ,
426
423
}
0 commit comments