@@ -336,10 +336,7 @@ CompoundStatement: ast::LocatedStatement = {
336
336
IfStatement: ast::LocatedStatement = {
337
337
<loc:@L> "if" <t:Test> ":" <s1:Suite> <s2:(@L "elif" Test ":" Suite)*> <s3:("else" ":" Suite)?> => {
338
338
// Determine last else:
339
- let mut last = match s3 {
340
- Some(s) => Some(s.2),
341
- None => None,
342
- };
339
+ let mut last = s3.map(|s| s.2);
343
340
344
341
// handle elif:
345
342
for i in s2.into_iter().rev() {
@@ -359,10 +356,7 @@ IfStatement: ast::LocatedStatement = {
359
356
360
357
WhileStatement: ast::LocatedStatement = {
361
358
<loc:@L> "while" <e:Test> ":" <s:Suite> <s2:("else" ":" Suite)?> => {
362
- let or_else = match s2 {
363
- Some(s) => Some(s.2),
364
- None => None,
365
- };
359
+ let or_else = s2.map(|s| s.2);
366
360
ast::LocatedStatement {
367
361
location: loc,
368
362
node: ast::Statement::While { test: e, body: s, orelse: or_else },
@@ -372,10 +366,7 @@ WhileStatement: ast::LocatedStatement = {
372
366
373
367
ForStatement: ast::LocatedStatement = {
374
368
<loc:@L> "for" <e:ExpressionList> "in" <t:TestList> ":" <s:Suite> <s2:("else" ":" Suite)?> => {
375
- let or_else = match s2 {
376
- Some(s) => Some(s.2),
377
- None => None,
378
- };
369
+ let or_else = s2.map(|s| s.2);
379
370
ast::LocatedStatement {
380
371
location: loc,
381
372
node: ast::Statement::For {
@@ -388,14 +379,8 @@ ForStatement: ast::LocatedStatement = {
388
379
389
380
TryStatement: ast::LocatedStatement = {
390
381
<loc:@L> "try" ":" <body:Suite> <handlers:ExceptClause*> <else_suite:("else" ":" Suite)?> <finally:("finally" ":" Suite)?> => {
391
- let or_else = match else_suite {
392
- Some(s) => Some(s.2),
393
- None => None,
394
- };
395
- let finalbody = match finally {
396
- Some(s) => Some(s.2),
397
- None => None,
398
- };
382
+ let or_else = else_suite.map(|s| s.2);
383
+ let finalbody = finally.map(|s| s.2);
399
384
ast::LocatedStatement {
400
385
location: loc,
401
386
node: ast::Statement::Try {
@@ -436,10 +421,7 @@ WithStatement: ast::LocatedStatement = {
436
421
437
422
WithItem: ast::WithItem = {
438
423
<t:Test> <n:("as" Expression)?> => {
439
- let optional_vars = match n {
440
- Some(val) => Some(val.1),
441
- None => None,
442
- };
424
+ let optional_vars = n.map(|val| val.1);
443
425
ast::WithItem { context_expr: t, optional_vars }
444
426
},
445
427
};
@@ -460,27 +442,19 @@ FuncDef: ast::LocatedStatement = {
460
442
};
461
443
462
444
Parameters: ast::Parameters = {
463
- "(" <a: (TypedArgsList<TypedParameter>)?> ")" => {
464
- match a {
465
- Some(a) => a,
466
- None => Default::default(),
467
- }
445
+ "(" <a: (ParameterList<TypedParameter>)?> ")" => {
446
+ a.unwrap_or_else(Default::default)
468
447
},
469
448
};
470
449
471
- // parameters are (String, None), kwargs are (String, Some(Test)) where Test is
472
- // the default
473
450
// Note that this is a macro which is used once for function defs, and
474
451
// once for lambda defs.
475
- TypedArgsList <ArgType>: ast::Parameters = {
476
- <param1:TypedParameters <ArgType>> <args2:("," ParameterListStarArgs<ArgType>)?> => {
452
+ ParameterList <ArgType>: ast::Parameters = {
453
+ <param1:ParameterDefs <ArgType>> <args2:("," ParameterListStarArgs<ArgType>)?> ","? => {
477
454
let (names, default_elements) = param1;
478
455
479
456
// Now gather rest of parameters:
480
- let (vararg, kwonlyargs, kw_defaults, kwarg) = match args2 {
481
- Some((_, x)) => x,
482
- None => (None, vec![], vec![], None),
483
- };
457
+ let (vararg, kwonlyargs, kw_defaults, kwarg) = args2.map_or((None, vec![], vec![], None), |x| x.1);
484
458
485
459
ast::Parameters {
486
460
args: names,
@@ -491,7 +465,7 @@ TypedArgsList<ArgType>: ast::Parameters = {
491
465
kw_defaults: kw_defaults,
492
466
}
493
467
},
494
- <param1:TypedParameters <ArgType>> <kw:("," KwargParameter<ArgType>)> => {
468
+ <param1:ParameterDefs <ArgType>> <kw:("," KwargParameter<ArgType>)> ","? => {
495
469
let (names, default_elements) = param1;
496
470
497
471
// Now gather rest of parameters:
@@ -509,7 +483,7 @@ TypedArgsList<ArgType>: ast::Parameters = {
509
483
kw_defaults: kw_defaults,
510
484
}
511
485
},
512
- <params:ParameterListStarArgs<ArgType>> => {
486
+ <params:ParameterListStarArgs<ArgType>> ","? => {
513
487
let (vararg, kwonlyargs, kw_defaults, kwarg) = params;
514
488
ast::Parameters {
515
489
args: vec![],
@@ -520,7 +494,7 @@ TypedArgsList<ArgType>: ast::Parameters = {
520
494
kw_defaults: kw_defaults,
521
495
}
522
496
},
523
- <kw:KwargParameter<ArgType>> => {
497
+ <kw:KwargParameter<ArgType>> ","? => {
524
498
ast::Parameters {
525
499
args: vec![],
526
500
kwonlyargs: vec![],
@@ -534,8 +508,8 @@ TypedArgsList<ArgType>: ast::Parameters = {
534
508
535
509
// Use inline here to make sure the "," is not creating an ambiguity.
536
510
#[inline]
537
- TypedParameters <ArgType>: (Vec<ast::Parameter>, Vec<ast::Expression>) = {
538
- <param1:TypedParameterDef <ArgType>> <param2:("," TypedParameterDef <ArgType>)*> => {
511
+ ParameterDefs <ArgType>: (Vec<ast::Parameter>, Vec<ast::Expression>) = {
512
+ <param1:ParameterDef <ArgType>> <param2:("," ParameterDef <ArgType>)*> => {
539
513
// Combine first parameters:
540
514
let mut args = vec![param1];
541
515
args.extend(param2.into_iter().map(|x| x.1));
@@ -563,7 +537,7 @@ TypedParameters<ArgType>: (Vec<ast::Parameter>, Vec<ast::Expression>) = {
563
537
}
564
538
};
565
539
566
- TypedParameterDef <ArgType>: (ast::Parameter, Option<ast::Expression>) = {
540
+ ParameterDef <ArgType>: (ast::Parameter, Option<ast::Expression>) = {
567
541
<i:ArgType> => (i, None),
568
542
<i:ArgType> "=" <e:Test> => (i, Some(e)),
569
543
};
@@ -579,8 +553,11 @@ TypedParameter: ast::Parameter = {
579
553
},
580
554
};
581
555
556
+ // Use inline here to make sure the "," is not creating an ambiguity.
557
+ // TODO: figure out another grammar that makes this inline no longer required.
558
+ #[inline]
582
559
ParameterListStarArgs<ArgType>: (Option<Option<ast::Parameter>>, Vec<ast::Parameter>, Vec<Option<ast::Expression>>, Option<Option<ast::Parameter>>) = {
583
- "*" <va:ArgType?> <kw:("," TypedParameterDef <ArgType>)*> <kwarg:("," KwargParameter<ArgType>)?> => {
560
+ "*" <va:ArgType?> <kw:("," ParameterDef <ArgType>)*> <kwarg:("," KwargParameter<ArgType>)?> => {
584
561
// Extract keyword arguments:
585
562
let mut kwonlyargs = vec![];
586
563
let mut kw_defaults = vec![];
@@ -589,10 +566,7 @@ ParameterListStarArgs<ArgType>: (Option<Option<ast::Parameter>>, Vec<ast::Parame
589
566
kw_defaults.push(value);
590
567
}
591
568
592
- let kwarg = match kwarg {
593
- Some((_, name)) => Some(name),
594
- None => None,
595
- };
569
+ let kwarg = kwarg.map(|n| n.1);
596
570
597
571
(Some(va), kwonlyargs, kw_defaults, kwarg)
598
572
}
@@ -683,7 +657,7 @@ Test: ast::Expression = {
683
657
};
684
658
685
659
LambdaDef: ast::Expression = {
686
- "lambda" <p:TypedArgsList <UntypedParameter>?> ":" <b:Test> =>
660
+ "lambda" <p:ParameterList <UntypedParameter>?> ":" <b:Test> =>
687
661
ast::Expression::Lambda {
688
662
args: p.unwrap_or(Default::default()),
689
663
body:Box::new(b)
0 commit comments