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