@@ -228,15 +228,15 @@ def rule(parser):
228
228
while True :
229
229
result = separator_rule (parser )
230
230
if result is unmatched :
231
- results .trailing_comma = False
231
+ results .trailing_comma = None
232
232
return results
233
233
234
- result = inner_rule (parser )
235
- if result is unmatched :
236
- results .trailing_comma = True
234
+ result_1 = inner_rule (parser )
235
+ if result_1 is unmatched :
236
+ results .trailing_comma = result
237
237
return results
238
238
else :
239
- results .append (result )
239
+ results .append (result_1 )
240
240
return rule
241
241
242
242
# Python AST specific parser combinators
@@ -365,7 +365,7 @@ def decorated(self, decorators, classfuncdef):
365
365
return classfuncdef
366
366
367
367
@action (Seq (Loc ('def' ), Tok ('ident' ), Rule ('parameters' ), Loc (':' ), Rule ('suite' )))
368
- def funcdef (def_loc , ident_tok , args , colon_loc , suite ):
368
+ def funcdef (self , def_loc , ident_tok , args , colon_loc , suite ):
369
369
"""funcdef: 'def' NAME parameters ':' suite"""
370
370
return ast .FunctionDef (name = ident_tok .value , args = args , body = suite , decorator_list = [],
371
371
keyword_loc = def_loc , name_loc = ident_tok .value , colon_loc = colon_loc )
@@ -457,13 +457,17 @@ def expr_stmt(self, lhs, rhs):
457
457
458
458
@action (List (Rule ('test' ), ',' , trailing = True ))
459
459
def print_stmt_1 (self , values ):
460
- return ast .Print (dest = None , values = values , nl = values .trailing ,
461
- dest_loc = None )
460
+ loc = values .trailing_comma .loc if values .trailing_comma else values [- 1 ].loc
461
+ nl = True if values .trailing_comma else False
462
+ return ast .Print (dest = None , values = values , nl = nl ,
463
+ dest_loc = None , loc = loc )
462
464
463
465
@action (Seq (Loc ('>>' ), Rule ('test' ), Tok (',' ), List (Rule ('test' ), ',' , trailing = True )))
464
466
def print_stmt_2 (self , dest_loc , dest , comma_tok , values ):
465
- return ast .Print (dest = dest , values = values , nl = values .trailing ,
466
- dest_loc = dest_loc )
467
+ loc = values .trailing_comma .loc if values .trailing_comma else values [- 1 ].loc
468
+ nl = True if values .trailing_comma else False
469
+ return ast .Print (dest = dest , values = values , nl = nl ,
470
+ dest_loc = dest_loc , loc = loc )
467
471
468
472
@action (Seq (Loc ('print' ), Alt (print_stmt_1 , print_stmt_2 )))
469
473
def print_stmt (self , print_loc , stmt ):
@@ -473,12 +477,13 @@ def print_stmt(self, print_loc, stmt):
473
477
'>>' test [ (',' test)+ [','] ] )
474
478
"""
475
479
stmt .keyword_loc = print_loc
480
+ stmt .loc = print_loc .join (stmt .loc )
476
481
return stmt
477
482
478
483
@action (Seq (Loc ('del' ), Rule ('exprlist' )))
479
484
def del_stmt (self , stmt_loc , exprs ):
480
485
"""del_stmt: 'del' exprlist"""
481
- return ast .Delete (targets = list ( map ( self ._assignable , exprs ) ),
486
+ return ast .Delete (targets = self ._assignable ( exprs ),
482
487
loc = stmt_loc .join (exprs .loc ), keyword_loc = stmt_loc )
483
488
484
489
@action (Loc ('pass' ))
@@ -503,23 +508,34 @@ def continue_stmt(self, stmt_loc):
503
508
@action (Seq (Loc ('return' ), Opt (Rule ('testlist' ))))
504
509
def return_stmt (self , stmt_loc , values ):
505
510
"""return_stmt: 'return' [testlist]"""
506
- return ast .Return (loc = stmt_loc .join (values .loc ), keyword_loc = stmt_loc )
511
+ loc = stmt_loc
512
+ if values :
513
+ loc = loc .join (values .loc )
514
+ return ast .Return (value = values ,
515
+ loc = loc , keyword_loc = stmt_loc )
507
516
508
- yield_stmt = Rule ('yield_expr' )
509
- """yield_stmt: yield_expr"""
517
+ @action (Rule ('yield_expr' ))
518
+ def yield_stmt (self , expr ):
519
+ """yield_stmt: yield_expr"""
520
+ return ast .Expr (value = expr , loc = expr .loc )
510
521
511
522
@action (Seq (Loc ('raise' ), Opt (Seq (Rule ('test' ),
512
523
Opt (Seq (Tok (',' ), Rule ('test' ),
513
524
Opt (SeqN (1 , Tok (',' ), Rule ('test' )))))))))
514
525
def raise_stmt (self , raise_loc , type_opt ):
515
526
"""raise_stmt: 'raise' [test [',' test [',' test]]]"""
516
527
type_ = inst = tback = None
528
+ loc = raise_loc
517
529
if type_opt :
518
- _ , type_ , inst_opt = type_opt
530
+ type_ , inst_opt = type_opt
531
+ loc = loc .join (type_ .loc )
519
532
if inst_opt :
520
- inst , tback = inst_opt
533
+ _ , inst , tback = inst_opt
534
+ loc = loc .join (inst .loc )
535
+ if tback :
536
+ loc = loc .join (tback .loc )
521
537
return ast .Raise (type = type_ , inst = inst , tback = tback ,
522
- raise_loc = raise_loc )
538
+ keyword_loc = raise_loc , loc = loc )
523
539
524
540
import_stmt = Alt (Rule ('import_name' ), Rule ('import_from' ))
525
541
"""import_stmt: import_name | import_from"""
@@ -593,27 +609,36 @@ def dotted_name(self, idents):
593
609
'.' .join (list (map (lambda x : x .value , idents )))
594
610
595
611
@action (Seq (Loc ('global' ), List (Tok ('ident' ), ',' , trailing = False )))
596
- def global_stmt (self , keyword_loc , names ):
612
+ def global_stmt (self , global_loc , names ):
597
613
"""global_stmt: 'global' NAME (',' NAME)*"""
598
- return ast .Global (names = list (map (ast .Name , names )),
599
- keyword_loc = keyword_loc )
614
+ return ast .Global (names = list (map (lambda x : x .value , names )),
615
+ name_locs = list (map (lambda x : x .loc , names )),
616
+ keyword_loc = global_loc , loc = global_loc .join (names [- 1 ].loc ))
600
617
601
618
@action (Seq (Loc ('exec' ), Rule ('expr' ),
602
619
Opt (Seq (Loc ('in' ), Rule ('test' ),
603
620
Opt (SeqN (1 , Loc (',' ), Rule ('test' )))))))
604
- def exec_stmt (self , keyword_loc , body , in_opt ):
621
+ def exec_stmt (self , exec_loc , body , in_opt ):
605
622
"""exec_stmt: 'exec' expr ['in' test [',' test]]"""
606
- in_loc , locals , globals_opt = None , None , None
623
+ in_loc , locals , globals = None , None , None
624
+ loc = exec_loc .join (body .loc )
607
625
if in_opt :
608
- in_loc , locals , globals_opt = in_opt
626
+ in_loc , locals , globals = in_opt
627
+ if globals :
628
+ loc = loc .join (globals .loc )
629
+ else :
630
+ loc = loc .join (locals .loc )
609
631
return ast .Exec (body = body , locals = locals , globals = globals ,
610
- keyword_loc = keyword_loc , in_loc = in_loc )
632
+ loc = loc , keyword_loc = exec_loc , in_loc = in_loc )
611
633
612
634
@action (Seq (Loc ('assert' ), Rule ('test' ), Opt (SeqN (1 , Tok (',' ), Rule ('test' )))))
613
- def assert_stmt (self , keyword_loc , test , msg ):
635
+ def assert_stmt (self , assert_loc , test , msg ):
614
636
"""assert_stmt: 'assert' test [',' test]"""
637
+ loc = assert_loc .join (test .loc )
638
+ if msg :
639
+ loc = loc .join (msg .loc )
615
640
return ast .Assert (test = test , msg = msg ,
616
- keyword_loc = keyword_loc )
641
+ loc = loc , keyword_loc = assert_loc )
617
642
618
643
@action (Alt (Rule ('if_stmt' ), Rule ('while_stmt' ), Rule ('for_stmt' ),
619
644
Rule ('try_stmt' ), Rule ('with_stmt' ), Rule ('funcdef' ),
@@ -637,38 +662,42 @@ def if_stmt(self, if_loc, test, if_colon_loc, body, elifs, else_opt):
637
662
for elif_ in elifs :
638
663
stmt .keyword_loc , stmt .test , stmt .if_colon_loc , stmt .body = elif_
639
664
stmt .loc = stmt .keyword_loc .join (stmt .body [- 1 ].loc )
640
- if stmt .orelse : stmt .loc = stmt .loc .join (stmt .orelse [- 1 ])
665
+ if stmt .orelse : stmt .loc = stmt .loc .join (stmt .orelse [- 1 ]. loc )
641
666
stmt = ast .If (orelse = [stmt ],
642
667
else_loc = None , else_colon_loc = None )
643
668
644
669
stmt .keyword_loc , stmt .test , stmt .if_colon_loc , stmt .body = \
645
670
if_loc , test , if_colon_loc , body
646
671
stmt .loc = stmt .keyword_loc .join (stmt .body [- 1 ].loc )
647
- if stmt .orelse : stmt .loc = stmt .loc .join (stmt .orelse [- 1 ])
672
+ if stmt .orelse : stmt .loc = stmt .loc .join (stmt .orelse [- 1 ]. loc )
648
673
return stmt
649
674
650
675
@action (Seq (Loc ('while' ), Rule ('test' ), Loc (':' ), Rule ('suite' ),
651
676
Opt (Seq (Loc ('else' ), Loc (':' ), Rule ('suite' )))))
652
- def while_stmt (while_loc , test , while_colon_loc , body , else_opt ):
677
+ def while_stmt (self , while_loc , test , while_colon_loc , body , else_opt ):
653
678
"""while_stmt: 'while' test ':' suite ['else' ':' suite]"""
654
679
stmt = ast .While (test = test , body = body , orelse = [],
655
- while_loc = while_loc , while_colon_loc = while_colon_loc ,
656
- else_loc = None , else_colon_loc = None )
680
+ keyword_loc = while_loc , while_colon_loc = while_colon_loc ,
681
+ else_loc = None , else_colon_loc = None ,
682
+ loc = while_loc .join (body [- 1 ].loc ))
657
683
if else_opt :
658
- stmt .else_loc , stmt .else_colon_loc , orelse = else_opt
684
+ stmt .else_loc , stmt .else_colon_loc , stmt .orelse = else_opt
685
+ stmt .loc = stmt .loc .join (stmt .orelse [- 1 ].loc )
659
686
660
687
return stmt
661
688
662
689
@action (Seq (Loc ('for' ), Rule ('exprlist' ), Loc ('in' ), Rule ('testlist' ),
663
690
Loc (':' ), Rule ('suite' ),
664
691
Opt (Seq (Loc ('else' ), Loc (':' ), Rule ('suite' )))))
665
- def for_stmt (for_loc , target , in_loc , iter , for_colon_loc , body , else_opt ):
692
+ def for_stmt (self , for_loc , target , in_loc , iter , for_colon_loc , body , else_opt ):
666
693
"""for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]"""
667
694
stmt = ast .For (target = self ._assignable (target ), iter = iter , body = body , orelse = [],
668
- for_loc = for_loc , in_loc = in_loc , for_colon_loc = for_colon_loc ,
669
- else_loc = None , else_colon_loc = None )
695
+ keyword_loc = for_loc , in_loc = in_loc , for_colon_loc = for_colon_loc ,
696
+ else_loc = None , else_colon_loc = None ,
697
+ loc = for_loc .join (body [- 1 ].loc ))
670
698
if else_opt :
671
- stmt .else_loc , stmt .else_colon_loc , orelse = else_opt
699
+ stmt .else_loc , stmt .else_colon_loc , stmt .orelse = else_opt
700
+ stmt .loc = stmt .loc .join (stmt .orelse [- 1 ].loc )
672
701
673
702
return stmt
674
703
@@ -716,19 +745,22 @@ def try_stmt(self, try_loc, try_colon_loc, body, stmt):
716
745
return stmt
717
746
718
747
@action (Seq (Loc ('with' ), Rule ('test' ), Opt (Rule ('with_var' )), Loc (':' ), Rule ('suite' )))
719
- def with_stmt (with_loc , context , with_var , colon_loc , body ):
748
+ def with_stmt (self , with_loc , context , with_var , colon_loc , body ):
720
749
"""with_stmt: 'with' test [ with_var ] ':' suite"""
721
- as_loc , optional_vars = with_var
722
- return ast .With (context = context , optional_vars = optional_vars , body = body ,
723
- keyword_loc = with_loc , as_loc = as_loc , colon_loc = colon_loc )
750
+ as_loc = optional_vars = None
751
+ if with_var :
752
+ as_loc , optional_vars = with_var
753
+ return ast .With (context_expr = context , optional_vars = optional_vars , body = body ,
754
+ keyword_loc = with_loc , as_loc = as_loc , colon_loc = colon_loc ,
755
+ loc = with_loc .join (body [- 1 ].loc ))
724
756
725
757
with_var = Seq (Loc ('as' ), Rule ('expr' ))
726
758
"""with_var: 'as' expr"""
727
759
728
760
@action (Seq (Loc ('except' ),
729
761
Opt (Seq (Rule ('test' ),
730
762
Opt (Seq (Alt (Loc ('as' ), Loc (',' )), Rule ('test' )))))))
731
- def except_clause (except_loc , exc_opt ):
763
+ def except_clause (self , except_loc , exc_opt ):
732
764
"""except_clause: 'except' [test [('as' | ',') test]]"""
733
765
type_ = name = as_loc = None
734
766
if exc_opt :
@@ -738,8 +770,12 @@ def except_clause(except_loc, exc_opt):
738
770
return ast .ExceptHandler (type = type_ , name = name ,
739
771
except_loc = except_loc , as_loc = as_loc )
740
772
773
+ @action (Plus (Rule ('stmt' )))
774
+ def suite_1 (self , stmts ):
775
+ return reduce (list .__add__ , stmts , [])
776
+
741
777
suite = Alt (Rule ('simple_stmt' ),
742
- SeqN (2 , Tok ('newline' ), Tok ('indent' ), Plus ( Rule ( 'stmt' )) , Tok ('dedent' )))
778
+ SeqN (2 , Tok ('newline' ), Tok ('indent' ), suite_1 , Tok ('dedent' )))
743
779
"""suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT"""
744
780
745
781
# 2.x-only backwards compatibility start
@@ -1031,17 +1067,19 @@ def dictmaker(self, elts):
1031
1067
loc = None )
1032
1068
1033
1069
@action (Seq (Loc ('class' ), Tok ('ident' ),
1034
- Opt (BeginEnd ( '(' , Rule ('testlist ' ), ')' )),
1070
+ Opt (Seq ( Loc ( '(' ), List ( Rule ('test ' ), ',' , trailing = True ), Loc ( ')' ) )),
1035
1071
Loc (':' ), Rule ('suite' )))
1036
- def classdef (class_loc , name_tok , bases_opt , colon_loc , body ):
1072
+ def classdef (self , class_loc , name_tok , bases_opt , colon_loc , body ):
1037
1073
"""classdef: 'class' NAME ['(' [testlist] ')'] ':' suite"""
1038
- bases = lparen_loc = rparen_loc = None
1074
+ bases , lparen_loc , rparen_loc = [], None , None
1039
1075
if bases_opt :
1040
- lparen_loc , bases , rparen_loc = \
1041
- bases_opt .begin_loc , bases_opt .elts , bases_opt .end_loc
1076
+ lparen_loc , bases , rparen_loc = bases_opt
1042
1077
1043
- return ast .ClassDef (name = name_tok .value , bases = bases , body = body , decorator_list = [],
1044
- keyword_loc = class_loc , lparen_loc = lparen_loc , rparen_loc = rparen_loc )
1078
+ return ast .ClassDef (name = name_tok .value , bases = bases , body = body ,
1079
+ decorator_list = [], at_locs = [],
1080
+ keyword_loc = class_loc , lparen_loc = lparen_loc , rparen_loc = rparen_loc ,
1081
+ name_loc = name_tok .loc , colon_loc = colon_loc ,
1082
+ loc = class_loc .join (body [- 1 ].loc ))
1045
1083
1046
1084
@action (Seq (Loc ('*' ), Rule ('test' ), Star (SeqN (1 , Tok (',' ), Rule ('argument' ))),
1047
1085
Opt (Seq (Tok (',' ), Loc ('**' ), Rule ('test' )))))
0 commit comments