-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathgrammar.y
1987 lines (1837 loc) · 35.7 KB
/
grammar.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
%{
package parser
// Grammar for Python
import (
"fmt"
"github.com/go-python/gpython/py"
"github.com/go-python/gpython/ast"
)
// NB can put code blocks in not just at the end
// Returns a Tuple if > 1 items or a trailing comma, otherwise returns
// the first item in elts
func tupleOrExpr(pos ast.Pos, elts []ast.Expr, optional_comma bool) ast.Expr {
if optional_comma || len(elts) > 1 {
return &ast.Tuple{ExprBase: ast.ExprBase{Pos: pos}, Elts: elts, Ctx: ast.Load}
} else {
return elts[0]
}
}
// Apply trailers (if any) to expr
//
// trailers are half made Call, Subscript or Attribute
func applyTrailers(expr ast.Expr, trailers []ast.Expr) ast.Expr {
//trailers := $1
for _, trailer := range trailers {
switch x := trailer.(type) {
case *ast.Call:
x.Func, expr = expr, x
case *ast.Subscript:
x.Value, expr = expr, x
case *ast.Attribute:
x.Value, expr = expr, x
default:
panic(fmt.Sprintf("Unknown trailer type: %T", expr))
}
}
return expr
}
// Set the context for expr
func setCtx(yylex yyLexer, expr ast.Expr, ctx ast.ExprContext) {
setctxer, ok := expr.(ast.SetCtxer)
if !ok {
expr_name := ""
switch expr.(type) {
case *ast.Lambda:
expr_name = "lambda"
case *ast.Call:
expr_name = "function call"
case *ast.BoolOp, *ast.BinOp, *ast.UnaryOp:
expr_name = "operator"
case *ast.GeneratorExp:
expr_name = "generator expression"
case *ast.Yield, *ast.YieldFrom:
expr_name = "yield expression"
case *ast.ListComp:
expr_name = "list comprehension"
case *ast.SetComp:
expr_name = "set comprehension"
case *ast.DictComp:
expr_name = "dict comprehension"
case *ast.Dict, *ast.Set, *ast.Num, *ast.Str, *ast.Bytes:
expr_name = "literal"
case *ast.NameConstant:
expr_name = "keyword"
case *ast.Ellipsis:
expr_name = "Ellipsis"
case *ast.Compare:
expr_name = "comparison"
case *ast.IfExp:
expr_name = "conditional expression"
default:
expr_name = fmt.Sprintf("unexpected %T", expr)
}
action := "assign to"
if ctx == ast.Del {
action = "delete"
}
yylex.(*yyLex).SyntaxErrorf("can't %s %s", action, expr_name)
return
}
setctxer.SetCtx(ctx)
}
// Set the context for all the items in exprs
func setCtxs(yylex yyLexer, exprs []ast.Expr, ctx ast.ExprContext) {
for i := range exprs {
setCtx(yylex, exprs[i], ctx)
}
}
%}
%union {
pos ast.Pos // kept up to date by the lexer
str string
obj py.Object
mod ast.Mod
stmt ast.Stmt
stmts []ast.Stmt
expr ast.Expr
exprs []ast.Expr
op ast.OperatorNumber
cmpop ast.CmpOp
comma bool
comprehensions []ast.Comprehension
isExpr bool
slice ast.Slicer
call *ast.Call
level int
alias *ast.Alias
aliases []*ast.Alias
identifiers []ast.Identifier
ifstmt *ast.If
lastif *ast.If
exchandlers []*ast.ExceptHandler
withitem *ast.WithItem
withitems []*ast.WithItem
arg *ast.Arg
args []*ast.Arg
arguments *ast.Arguments
}
%type <obj> strings
%type <mod> inputs file_input single_input eval_input
%type <stmts> simple_stmt stmt nl_or_stmt small_stmts stmts suite optional_else
%type <stmt> compound_stmt small_stmt expr_stmt del_stmt pass_stmt flow_stmt import_stmt global_stmt nonlocal_stmt assert_stmt break_stmt continue_stmt return_stmt raise_stmt yield_stmt import_name import_from while_stmt if_stmt for_stmt try_stmt with_stmt funcdef classdef classdef_or_funcdef decorated
%type <op> augassign
%type <expr> expr_or_star_expr expr star_expr xor_expr and_expr shift_expr arith_expr term factor power trailer atom test_or_star_expr test not_test lambdef test_nocond lambdef_nocond or_test and_test comparison testlist testlist_star_expr yield_expr_or_testlist yield_expr yield_expr_or_testlist_star_expr dictorsetmaker sliceop except_clause optional_return_type decorator
%type <exprs> exprlist testlistraw comp_if comp_iter expr_or_star_exprs test_or_star_exprs tests test_colon_tests trailers equals_yield_expr_or_testlist_star_expr decorators
%type <cmpop> comp_op
%type <comma> optional_comma
%type <comprehensions> comp_for
%type <slice> subscript subscriptlist subscripts
%type <call> argument arguments optional_arguments arguments2 arglist optional_arglist_call optional_arglist
%type <level> dot dots
%type <str> dotted_name from_arg
%type <identifiers> names
%type <alias> dotted_as_name import_as_name
%type <aliases> dotted_as_names import_as_names import_from_arg
%type <ifstmt> elifs
%type <exchandlers> except_clauses
%type <withitem> with_item
%type <withitems> with_items
%type <arg> vfpdeftest vfpdef optional_vfpdef tfpdeftest tfpdef optional_tfpdef
%type <args> vfpdeftests vfpdeftests1 tfpdeftests tfpdeftests1
%type <arguments> varargslist parameters optional_typedargslist typedargslist
%token NEWLINE
%token ENDMARKER
%token <str> NAME
%token INDENT
%token DEDENT
%token <obj> STRING
%token <obj> NUMBER
%token PLINGEQ // !=
%token PERCEQ // %=
%token ANDEQ // &=
%token STARSTAR // **
%token STARSTAREQ // **=
%token STAREQ // *=
%token PLUSEQ // +=
%token MINUSEQ // -=
%token MINUSGT // ->
%token ELIPSIS // ...
%token DIVDIV // //
%token DIVDIVEQ // //=
%token DIVEQ // /=
%token LTLT // <<
%token LTLTEQ // <<=
%token LTEQ // <=
%token LTGT // <>
%token EQEQ // ==
%token GTEQ // >=
%token GTGT // >>
%token GTGTEQ // >>=
%token HATEQ // ^=
%token PIPEEQ // |=
%token FALSE // False
%token NONE // None
%token TRUE // True
%token AND // and
%token AS // as
%token ASSERT // assert
%token BREAK // break
%token CLASS // class
%token CONTINUE // continue
%token DEF // def
%token DEL // del
%token ELIF // elif
%token ELSE // else
%token EXCEPT // except
%token FINALLY // finally
%token FOR // for
%token FROM // from
%token GLOBAL // global
%token IF // if
%token IMPORT // import
%token IN // in
%token IS // is
%token LAMBDA // lambda
%token NONLOCAL // nonlocal
%token NOT // not
%token OR // or
%token PASS // pass
%token RAISE // raise
%token RETURN // return
%token TRY // try
%token WHILE // while
%token WITH // with
%token YIELD // yield
%token '(' ')' '[' ']' ':' ',' ';' '+' '-' '*' '/' '|' '&' '<' '>' '=' '.' '%' '{' '}' '^' '~' '@'
%token SINGLE_INPUT FILE_INPUT EVAL_INPUT
// Note: Changing the grammar specified in this file will most likely
// require corresponding changes in the parser module
// (../Modules/parsermodule.c). If you can't make the changes to
// that module yourself, please co-ordinate the required changes
// with someone who can; ask around on python-dev for help. Fred
// Drake <[email protected]> will probably be listening there.
// NOTE WELL: You should also follow all the steps listed in PEP 306,
// "How to Change Python's Grammar"
// Start symbols for the grammar:
// single_input is a single interactive statement;
// file_input is a module or sequence of commands read from an input file;
// eval_input is the input for the eval() functions.
// NB: compound_stmt in single_input is followed by extra NEWLINE!
%%
// Start of grammar. This has 3 pseudo tokens which say which
// direction through the rest of the grammar we take.
inputs:
SINGLE_INPUT single_input
{
yylex.(*yyLex).mod = $2
return 0
}
| FILE_INPUT file_input
{
yylex.(*yyLex).mod = $2
return 0
}
| EVAL_INPUT eval_input
{
yylex.(*yyLex).mod = $2
return 0
}
single_input:
/* NEWLINE
{
// This is in the python grammar, but the interpreter
// just gives "unexpected EOF while parsing" when you
// give it a \n
$$ = &ast.Interactive{ModBase: ast.ModBase{Pos: $<pos>$}}
}
|*/ simple_stmt
{
$$ = &ast.Interactive{ModBase: ast.ModBase{Pos: $<pos>$}, Body: $1}
}
| compound_stmt NEWLINE
{
// NB: compound_stmt in single_input is followed by extra NEWLINE!
$$ = &ast.Interactive{ModBase: ast.ModBase{Pos: $<pos>$}, Body: []ast.Stmt{$1}}
}
//file_input: (NEWLINE | stmt)* ENDMARKER
file_input:
nl_or_stmt ENDMARKER
{
$$ = &ast.Module{ModBase: ast.ModBase{Pos: $<pos>$}, Body: $1}
}
// (NEWLINE | stmt)*
nl_or_stmt:
{
$$ = nil
}
| nl_or_stmt NEWLINE
{
}
| nl_or_stmt stmt
{
$$ = append($$, $2...)
}
//eval_input: testlist NEWLINE* ENDMARKER
eval_input:
testlist nls ENDMARKER
{
$$ = &ast.Expression{ModBase: ast.ModBase{Pos: $<pos>$}, Body: $1}
}
// NEWLINE*
nls:
| nls NEWLINE
optional_arglist:
{
$$ = &ast.Call{ExprBase: ast.ExprBase{Pos: $<pos>$}}
}
| arglist
{
$$ = $1
}
optional_arglist_call:
{
$$ = nil
}
| '(' optional_arglist ')'
{
$$ = $2
}
decorator:
'@' dotted_name optional_arglist_call NEWLINE
{
fn := &ast.Name{ExprBase: ast.ExprBase{Pos: $<pos>$}, Id: ast.Identifier($2), Ctx: ast.Load}
if $3 == nil {
$$ = fn
} else {
call := *$3
call.Func = fn
$$ = &call
}
}
decorators:
decorator
{
$$ = nil
$$ = append($$, $1)
}
| decorators decorator
{
$$ = append($$, $2)
}
classdef_or_funcdef:
classdef
{
$$ = $1
}
| funcdef
{
$$ = $1
}
decorated:
decorators classdef_or_funcdef
{
switch x := ($2).(type) {
case *ast.ClassDef:
x.DecoratorList = $1
$$ = x
case *ast.FunctionDef:
x.DecoratorList = $1
$$ = x
default:
panic("bad type for decorated")
}
}
optional_return_type:
{
$$ = nil
}
| MINUSGT test
{
$$ = $2
}
funcdef:
DEF NAME parameters optional_return_type ':' suite
{
$$ = &ast.FunctionDef{StmtBase: ast.StmtBase{Pos: $<pos>$}, Name: ast.Identifier($2), Args: $3, Body: $6, Returns: $4}
}
parameters:
'(' optional_typedargslist ')'
{
$$ = $2
}
optional_typedargslist:
{
$$ = &ast.Arguments{Pos: $<pos>$}
}
| typedargslist
{
$$ = $1
}
// (',' tfpdef ['=' test])*
tfpdeftest:
tfpdef
{
$$ = $1
$<expr>$ = nil
}
| tfpdef '=' test
{
$$ = $1
$<expr>$ = $3
}
tfpdeftests:
{
$$ = nil
$<exprs>$ = nil
}
| tfpdeftests ',' tfpdeftest
{
$$ = append($$, $3)
if $<expr>3 != nil {
$<exprs>$ = append($<exprs>$, $<expr>3)
}
}
tfpdeftests1:
tfpdeftest
{
$$ = nil
$$ = append($$, $1)
$<exprs>$ = nil
if $<expr>1 != nil {
$<exprs>$ = append($<exprs>$, $<expr>1)
}
}
| tfpdeftests1 ',' tfpdeftest
{
$$ = append($$, $3)
if $<expr>3 != nil {
$<exprs>$ = append($<exprs>$, $<expr>3)
}
}
optional_tfpdef:
{
$$ = nil
}
| tfpdef
{
$$ = $1
}
// FIXME this isn't checking all the python rules for args before kwargs etc
typedargslist:
tfpdeftests1 optional_comma
{
$$ = &ast.Arguments{Pos: $<pos>$, Args: $1, Defaults: $<exprs>1}
}
| tfpdeftests1 ',' '*' optional_tfpdef tfpdeftests
{
$$ = &ast.Arguments{Pos: $<pos>$, Args: $1, Defaults: $<exprs>1, Vararg: $4, Kwonlyargs: $5, KwDefaults: $<exprs>5}
}
| tfpdeftests1 ',' '*' optional_tfpdef tfpdeftests ',' STARSTAR tfpdef
{
$$ = &ast.Arguments{Pos: $<pos>$, Args: $1, Defaults: $<exprs>1, Vararg: $4, Kwonlyargs: $5, KwDefaults: $<exprs>5, Kwarg: $8}
}
| tfpdeftests1 ',' STARSTAR tfpdef
{
$$ = &ast.Arguments{Pos: $<pos>$, Args: $1, Defaults: $<exprs>1, Kwarg: $4}
}
| '*' optional_tfpdef tfpdeftests
{
$$ = &ast.Arguments{Pos: $<pos>$, Vararg: $2, Kwonlyargs: $3, KwDefaults: $<exprs>3}
}
| '*' optional_tfpdef tfpdeftests ',' STARSTAR tfpdef
{
$$ = &ast.Arguments{Pos: $<pos>$, Vararg: $2, Kwonlyargs: $3, KwDefaults: $<exprs>3, Kwarg: $6}
}
| STARSTAR tfpdef
{
$$ = &ast.Arguments{Pos: $<pos>$, Kwarg: $2}
}
tfpdef:
NAME
{
$$ = &ast.Arg{Pos: $<pos>$, Arg: ast.Identifier($1)}
}
| NAME ':' test
{
$$ = &ast.Arg{Pos: $<pos>$, Arg: ast.Identifier($1), Annotation: $3}
}
vfpdeftest:
vfpdef
{
$$ = $1
$<expr>$ = nil
}
| vfpdef '=' test
{
$$ = $1
$<expr>$ = $3
}
vfpdeftests:
{
$$ = nil
$<exprs>$ = nil
}
| vfpdeftests ',' vfpdeftest
{
$$ = append($$, $3)
if $<expr>3 != nil {
$<exprs>$ = append($<exprs>$, $<expr>3)
}
}
vfpdeftests1:
vfpdeftest
{
$$ = nil
$$ = append($$, $1)
$<exprs>$ = nil
if $<expr>1 != nil {
$<exprs>$ = append($<exprs>$, $<expr>1)
}
}
| vfpdeftests1 ',' vfpdeftest
{
$$ = append($$, $3)
if $<expr>3 != nil {
$<exprs>$ = append($<exprs>$, $<expr>3)
}
}
optional_vfpdef:
{
$$ = nil
}
| vfpdef
{
$$ = $1
}
// FIXME this isn't checking all the python rules for args before kwargs etc
varargslist:
vfpdeftests1 optional_comma
{
$$ = &ast.Arguments{Pos: $<pos>$, Args: $1, Defaults: $<exprs>1}
}
| vfpdeftests1 ',' '*' optional_vfpdef vfpdeftests
{
$$ = &ast.Arguments{Pos: $<pos>$, Args: $1, Defaults: $<exprs>1, Vararg: $4, Kwonlyargs: $5, KwDefaults: $<exprs>5}
}
| vfpdeftests1 ',' '*' optional_vfpdef vfpdeftests ',' STARSTAR vfpdef
{
$$ = &ast.Arguments{Pos: $<pos>$, Args: $1, Defaults: $<exprs>1, Vararg: $4, Kwonlyargs: $5, KwDefaults: $<exprs>5, Kwarg: $8}
}
| vfpdeftests1 ',' STARSTAR vfpdef
{
$$ = &ast.Arguments{Pos: $<pos>$, Args: $1, Defaults: $<exprs>1, Kwarg: $4}
}
| '*' optional_vfpdef vfpdeftests
{
$$ = &ast.Arguments{Pos: $<pos>$, Vararg: $2, Kwonlyargs: $3, KwDefaults: $<exprs>3}
}
| '*' optional_vfpdef vfpdeftests ',' STARSTAR vfpdef
{
$$ = &ast.Arguments{Pos: $<pos>$, Vararg: $2, Kwonlyargs: $3, KwDefaults: $<exprs>3, Kwarg: $6}
}
| STARSTAR vfpdef
{
$$ = &ast.Arguments{Pos: $<pos>$, Kwarg: $2}
}
vfpdef:
NAME
{
$$ = &ast.Arg{Pos: $<pos>$, Arg: ast.Identifier($1)}
}
stmt:
simple_stmt
{
$$ = $1
}
| compound_stmt
{
$$ = []ast.Stmt{$1}
}
optional_semicolon: | ';'
small_stmts:
small_stmt
{
$$ = nil
$$ = append($$, $1)
}
| small_stmts ';' small_stmt
{
$$ = append($$, $3)
}
simple_stmt:
small_stmts optional_semicolon NEWLINE
{
$$ = $1
}
small_stmt:
expr_stmt
{
$$ = $1
}
| del_stmt
{
$$ = $1
}
| pass_stmt
{
$$ = $1
}
| flow_stmt
{
$$ = $1
}
| import_stmt
{
$$ = $1
}
| global_stmt
{
$$ = $1
}
| nonlocal_stmt
{
$$ = $1
}
| assert_stmt
{
$$ = $1
}
/*
expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) |
('=' (yield_expr|testlist_star_expr))*)
expr_stmt:
testlist_star_expr (
augassign (
yield_expr|testlist
) | (
'=' (
yield_expr|testlist_star_expr
)
)*
)
expr_stmt: testlist_star_expr augassign yield_expr
expr_stmt: testlist_star_expr augassign testlist
expr_stmt: testlist_star_expr ('=' (yield_expr|testlist_star_expr))*
*/
expr_stmt:
testlist_star_expr augassign yield_expr_or_testlist
{
target := $1
setCtx(yylex, target, ast.Store)
$$ = &ast.AugAssign{StmtBase: ast.StmtBase{Pos: $<pos>$}, Target: target, Op: $2, Value: $3}
}
| testlist_star_expr equals_yield_expr_or_testlist_star_expr
{
targets := []ast.Expr{$1}
targets = append(targets, $2...)
value := targets[len(targets)-1]
targets = targets[:len(targets)-1]
setCtxs(yylex, targets, ast.Store)
$$ = &ast.Assign{StmtBase: ast.StmtBase{Pos: $<pos>$}, Targets: targets, Value: value}
}
| testlist_star_expr
{
$$ = &ast.ExprStmt{StmtBase: ast.StmtBase{Pos: $<pos>$}, Value: $1}
}
yield_expr_or_testlist:
yield_expr
{
$$ = $1
}
| testlist
{
$$ = $1
}
yield_expr_or_testlist_star_expr:
yield_expr
{
$$ = $1
}
| testlist_star_expr
{
$$ = $1
}
equals_yield_expr_or_testlist_star_expr:
'=' yield_expr_or_testlist_star_expr
{
$$ = nil
$$ = append($$, $2)
}
| equals_yield_expr_or_testlist_star_expr '=' yield_expr_or_testlist_star_expr
{
$$ = append($$, $3)
}
test_or_star_exprs:
test_or_star_expr
{
$$ = nil
$$ = append($$, $1)
}
| test_or_star_exprs ',' test_or_star_expr
{
$$ = append($$, $3)
}
test_or_star_expr:
test
{
$$ = $1
}
| star_expr
{
$$ = $1
}
optional_comma:
{
$$ = false
}
| ','
{
$$ = true
}
testlist_star_expr:
test_or_star_exprs optional_comma
{
$$ = tupleOrExpr($<pos>$, $1, $2)
}
augassign:
PLUSEQ
{
$$ = ast.Add
}
| MINUSEQ
{
$$ = ast.Sub
}
| STAREQ
{
$$ = ast.Mult
}
| DIVEQ
{
$$ = ast.Div
}
| PERCEQ
{
$$ = ast.Modulo
}
| ANDEQ
{
$$ = ast.BitAnd
}
| PIPEEQ
{
$$ = ast.BitOr
}
| HATEQ
{
$$ = ast.BitXor
}
| LTLTEQ
{
$$ = ast.LShift
}
| GTGTEQ
{
$$ = ast.RShift
}
| STARSTAREQ
{
$$ = ast.Pow
}
| DIVDIVEQ
{
$$ = ast.FloorDiv
}
// For normal assignments, additional restrictions enforced by the interpreter
del_stmt:
DEL exprlist
{
setCtxs(yylex, $2, ast.Del)
$$ = &ast.Delete{StmtBase: ast.StmtBase{Pos: $<pos>$}, Targets: $2}
}
pass_stmt:
PASS
{
$$ = &ast.Pass{StmtBase: ast.StmtBase{Pos: $<pos>$}}
}
flow_stmt:
break_stmt
{
$$ = $1
}
| continue_stmt
{
$$ = $1
}
| return_stmt
{
$$ = $1
}
| raise_stmt
{
$$ = $1
}
| yield_stmt
{
$$ = $1
}
break_stmt:
BREAK
{
$$ = &ast.Break{StmtBase: ast.StmtBase{Pos: $<pos>$}}
}
continue_stmt:
CONTINUE
{
$$ = &ast.Continue{StmtBase: ast.StmtBase{Pos: $<pos>$}}
}
return_stmt:
RETURN
{
$$ = &ast.Return{StmtBase: ast.StmtBase{Pos: $<pos>$}}
}
| RETURN testlist
{
$$ = &ast.Return{StmtBase: ast.StmtBase{Pos: $<pos>$}, Value: $2}
}
yield_stmt:
yield_expr
{
$$ = &ast.ExprStmt{StmtBase: ast.StmtBase{Pos: $<pos>$}, Value: $1}
}
raise_stmt:
RAISE
{
$$ = &ast.Raise{StmtBase: ast.StmtBase{Pos: $<pos>$}}
}
| RAISE test
{
$$ = &ast.Raise{StmtBase: ast.StmtBase{Pos: $<pos>$}, Exc: $2}
}
| RAISE test FROM test
{
$$ = &ast.Raise{StmtBase: ast.StmtBase{Pos: $<pos>$}, Exc: $2, Cause: $4}
}
import_stmt:
import_name
{
$$ = $1
}
| import_from
{
$$ = $1
}
import_name:
IMPORT dotted_as_names
{
$$ = &ast.Import{StmtBase: ast.StmtBase{Pos: $<pos>$}, Names: $2}
}
// note below: the '.' | ELIPSIS is necessary because '...' is tokenized as ELIPSIS
dot:
'.'
{
$$ = 1
}
| ELIPSIS
{
$$ = 3
}
dots:
dot
{
$$ = $1
}
| dots dot
{
$$ += $2
}
from_arg:
dotted_name
{
$<level>$ = 0
$$ = $1
}
| dots dotted_name
{
$<level>$ = $1
$$ = $2
}
| dots
{
$<level>$ = $1
$$ = ""
}
import_from_arg:
'*'
{
$$ = []*ast.Alias{&ast.Alias{Pos: $<pos>$, Name: ast.Identifier("*")}}
}
| '(' import_as_names optional_comma ')'
{
$$ = $2
}
| import_as_names optional_comma
{
$$ = $1
}
import_from:
FROM from_arg IMPORT import_from_arg
{
$$ = &ast.ImportFrom{StmtBase: ast.StmtBase{Pos: $<pos>$}, Module: ast.Identifier($2), Names: $4, Level: $<level>2}
}
import_as_name:
NAME
{
$$ = &ast.Alias{Pos: $<pos>$, Name: ast.Identifier($1)}
}
| NAME AS NAME
{
$$ = &ast.Alias{Pos: $<pos>$, Name: ast.Identifier($1), AsName: ast.Identifier($3)}
}
dotted_as_name:
dotted_name
{
$$ = &ast.Alias{Pos: $<pos>$, Name: ast.Identifier($1)}
}
| dotted_name AS NAME
{
$$ = &ast.Alias{Pos: $<pos>$, Name: ast.Identifier($1), AsName: ast.Identifier($3)}
}
import_as_names:
import_as_name
{
$$ = nil
$$ = append($$, $1)
}
| import_as_names ',' import_as_name
{
$$ = append($$, $3)
}
dotted_as_names:
dotted_as_name