2
2
// See also: https://github.com/antlr/grammars-v4/blob/master/python3/Python3.g4
3
3
// See also: file:///usr/share/doc/python/html/reference/compound_stmts.html#function-definitions
4
4
// See also: https://greentreesnakes.readthedocs.io/en/latest/nodes.html#keyword
5
- #![allow(unknown_lints,clippy)]
6
5
7
6
use std::iter::FromIterator;
8
7
@@ -337,10 +336,7 @@ CompoundStatement: ast::LocatedStatement = {
337
336
IfStatement: ast::LocatedStatement = {
338
337
<loc:@L> "if" <t:Test> ":" <s1:Suite> <s2:(@L "elif" Test ":" Suite)*> <s3:("else" ":" Suite)?> => {
339
338
// Determine last else:
340
- let mut last = match s3 {
341
- Some(s) => Some(s.2),
342
- None => None,
343
- };
339
+ let mut last = s3.map(|s| s.2);
344
340
345
341
// handle elif:
346
342
for i in s2.into_iter().rev() {
@@ -360,10 +356,7 @@ IfStatement: ast::LocatedStatement = {
360
356
361
357
WhileStatement: ast::LocatedStatement = {
362
358
<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
- };
359
+ let or_else = s2.map(|s| s.2);
367
360
ast::LocatedStatement {
368
361
location: loc,
369
362
node: ast::Statement::While { test: e, body: s, orelse: or_else },
@@ -373,10 +366,7 @@ WhileStatement: ast::LocatedStatement = {
373
366
374
367
ForStatement: ast::LocatedStatement = {
375
368
<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
- };
369
+ let or_else = s2.map(|s| s.2);
380
370
ast::LocatedStatement {
381
371
location: loc,
382
372
node: ast::Statement::For {
@@ -389,14 +379,8 @@ ForStatement: ast::LocatedStatement = {
389
379
390
380
TryStatement: ast::LocatedStatement = {
391
381
<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
- };
382
+ let or_else = else_suite.map(|s| s.2);
383
+ let finalbody = finally.map(|s| s.2);
400
384
ast::LocatedStatement {
401
385
location: loc,
402
386
node: ast::Statement::Try {
@@ -437,10 +421,7 @@ WithStatement: ast::LocatedStatement = {
437
421
438
422
WithItem: ast::WithItem = {
439
423
<t:Test> <n:("as" Expression)?> => {
440
- let optional_vars = match n {
441
- Some(val) => Some(val.1),
442
- None => None,
443
- };
424
+ let optional_vars = n.map(|val| val.1);
444
425
ast::WithItem { context_expr: t, optional_vars }
445
426
},
446
427
};
@@ -461,27 +442,19 @@ FuncDef: ast::LocatedStatement = {
461
442
};
462
443
463
444
Parameters: ast::Parameters = {
464
- "(" <a: (TypedArgsList<TypedParameter>)?> ")" => {
465
- match a {
466
- Some(a) => a,
467
- None => Default::default(),
468
- }
445
+ "(" <a: (ParameterList<TypedParameter>)?> ")" => {
446
+ a.unwrap_or_else(Default::default)
469
447
},
470
448
};
471
449
472
- // parameters are (String, None), kwargs are (String, Some(Test)) where Test is
473
- // the default
474
450
// Note that this is a macro which is used once for function defs, and
475
451
// once for lambda defs.
476
- TypedArgsList <ArgType>: ast::Parameters = {
477
- <param1:TypedParameters <ArgType>> <args2:("," ParameterListStarArgs<ArgType>)?> => {
452
+ ParameterList <ArgType>: ast::Parameters = {
453
+ <param1:ParameterDefs <ArgType>> <args2:("," ParameterListStarArgs<ArgType>)?> ","? => {
478
454
let (names, default_elements) = param1;
479
455
480
456
// 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
- };
457
+ let (vararg, kwonlyargs, kw_defaults, kwarg) = args2.map_or((None, vec![], vec![], None), |x| x.1);
485
458
486
459
ast::Parameters {
487
460
args: names,
@@ -492,7 +465,7 @@ TypedArgsList<ArgType>: ast::Parameters = {
492
465
kw_defaults: kw_defaults,
493
466
}
494
467
},
495
- <param1:TypedParameters <ArgType>> <kw:("," KwargParameter<ArgType>)> => {
468
+ <param1:ParameterDefs <ArgType>> <kw:("," KwargParameter<ArgType>)> ","? => {
496
469
let (names, default_elements) = param1;
497
470
498
471
// Now gather rest of parameters:
@@ -510,7 +483,7 @@ TypedArgsList<ArgType>: ast::Parameters = {
510
483
kw_defaults: kw_defaults,
511
484
}
512
485
},
513
- <params:ParameterListStarArgs<ArgType>> => {
486
+ <params:ParameterListStarArgs<ArgType>> ","? => {
514
487
let (vararg, kwonlyargs, kw_defaults, kwarg) = params;
515
488
ast::Parameters {
516
489
args: vec![],
@@ -521,7 +494,7 @@ TypedArgsList<ArgType>: ast::Parameters = {
521
494
kw_defaults: kw_defaults,
522
495
}
523
496
},
524
- <kw:KwargParameter<ArgType>> => {
497
+ <kw:KwargParameter<ArgType>> ","? => {
525
498
ast::Parameters {
526
499
args: vec![],
527
500
kwonlyargs: vec![],
@@ -535,8 +508,8 @@ TypedArgsList<ArgType>: ast::Parameters = {
535
508
536
509
// Use inline here to make sure the "," is not creating an ambiguity.
537
510
#[inline]
538
- TypedParameters <ArgType>: (Vec<ast::Parameter>, Vec<ast::Expression>) = {
539
- <param1:TypedParameterDef <ArgType>> <param2:("," TypedParameterDef <ArgType>)*> => {
511
+ ParameterDefs <ArgType>: (Vec<ast::Parameter>, Vec<ast::Expression>) = {
512
+ <param1:ParameterDef <ArgType>> <param2:("," ParameterDef <ArgType>)*> => {
540
513
// Combine first parameters:
541
514
let mut args = vec![param1];
542
515
args.extend(param2.into_iter().map(|x| x.1));
@@ -564,7 +537,7 @@ TypedParameters<ArgType>: (Vec<ast::Parameter>, Vec<ast::Expression>) = {
564
537
}
565
538
};
566
539
567
- TypedParameterDef <ArgType>: (ast::Parameter, Option<ast::Expression>) = {
540
+ ParameterDef <ArgType>: (ast::Parameter, Option<ast::Expression>) = {
568
541
<i:ArgType> => (i, None),
569
542
<i:ArgType> "=" <e:Test> => (i, Some(e)),
570
543
};
@@ -580,8 +553,11 @@ TypedParameter: ast::Parameter = {
580
553
},
581
554
};
582
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]
583
559
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>)?> => {
560
+ "*" <va:ArgType?> <kw:("," ParameterDef <ArgType>)*> <kwarg:("," KwargParameter<ArgType>)?> => {
585
561
// Extract keyword arguments:
586
562
let mut kwonlyargs = vec![];
587
563
let mut kw_defaults = vec![];
@@ -590,10 +566,7 @@ ParameterListStarArgs<ArgType>: (Option<Option<ast::Parameter>>, Vec<ast::Parame
590
566
kw_defaults.push(value);
591
567
}
592
568
593
- let kwarg = match kwarg {
594
- Some((_, name)) => Some(name),
595
- None => None,
596
- };
569
+ let kwarg = kwarg.map(|n| n.1);
597
570
598
571
(Some(va), kwonlyargs, kw_defaults, kwarg)
599
572
}
@@ -684,7 +657,7 @@ Test: ast::Expression = {
684
657
};
685
658
686
659
LambdaDef: ast::Expression = {
687
- "lambda" <p:TypedArgsList <UntypedParameter>?> ":" <b:Test> =>
660
+ "lambda" <p:ParameterList <UntypedParameter>?> ":" <b:Test> =>
688
661
ast::Expression::Lambda {
689
662
args: p.unwrap_or(Default::default()),
690
663
body:Box::new(b)
0 commit comments