All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 # Grammar for 2to3. This grammar supports Python 2.x and 3.x.
3 # NOTE WELL: You should also follow all the steps listed at
4 # https://devguide.python.org/grammar/
6 # Start symbols for the grammar:
7 # file_input is a module or sequence of commands read from an input file;
8 # single_input is a single interactive statement;
9 # eval_input is the input for the eval() and input() functions.
10 # NB: compound_stmt in single_input is followed by extra NEWLINE!
11 file_input: (NEWLINE | stmt)* ENDMARKER
12 single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
13 eval_input: testlist NEWLINE* ENDMARKER
15 decorator: '@' namedexpr_test NEWLINE
16 decorators: decorator+
17 decorated: decorators (classdef | funcdef | async_funcdef)
18 async_funcdef: ASYNC funcdef
19 funcdef: 'def' NAME parameters ['->' test] ':' suite
20 parameters: '(' [typedargslist] ')'
22 # The following definition for typedarglist is equivalent to this set of rules:
24 # arguments = argument (',' argument)*
25 # argument = tfpdef ['=' test]
26 # kwargs = '**' tname [',']
28 # kwonly_kwargs = (',' argument)* [',' [kwargs]]
29 # args_kwonly_kwargs = args kwonly_kwargs | kwargs
30 # poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]
31 # typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
32 # typedarglist = arguments ',' '/' [',' [typedargslist_no_posonly]])|(typedargslist_no_posonly)"
34 # It needs to be fully expanded to allow our LL(1) parser to work on it.
36 typedargslist: tfpdef ['=' test] (',' tfpdef ['=' test])* ',' '/' [
37 ',' [((tfpdef ['=' test] ',')* ('*' [tname] (',' tname ['=' test])*
38 [',' ['**' tname [',']]] | '**' tname [','])
39 | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])]
40 ] | ((tfpdef ['=' test] ',')* ('*' [tname] (',' tname ['=' test])*
41 [',' ['**' tname [',']]] | '**' tname [','])
42 | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
44 tname: NAME [':' test]
45 tfpdef: tname | '(' tfplist ')'
46 tfplist: tfpdef (',' tfpdef)* [',']
48 # The following definition for varargslist is equivalent to this set of rules:
50 # arguments = argument (',' argument )*
51 # argument = vfpdef ['=' test]
52 # kwargs = '**' vname [',']
54 # kwonly_kwargs = (',' argument )* [',' [kwargs]]
55 # args_kwonly_kwargs = args kwonly_kwargs | kwargs
56 # poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]
57 # vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
58 # varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] | (vararglist_no_posonly)
60 # It needs to be fully expanded to allow our LL(1) parser to work on it.
62 varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [
63 ((vfpdef ['=' test] ',')* ('*' [vname] (',' vname ['=' test])*
64 [',' ['**' vname [',']]] | '**' vname [','])
65 | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
66 ]] | ((vfpdef ['=' test] ',')*
67 ('*' [vname] (',' vname ['=' test])* [',' ['**' vname [',']]]| '**' vname [','])
68 | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
71 vfpdef: vname | '(' vfplist ')'
72 vfplist: vfpdef (',' vfpdef)* [',']
74 stmt: simple_stmt | compound_stmt
75 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
76 small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
77 import_stmt | global_stmt | exec_stmt | assert_stmt)
78 expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
79 ('=' (yield_expr|testlist_star_expr))*)
80 annassign: ':' test ['=' (yield_expr|testlist_star_expr)]
81 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
82 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
83 '<<=' | '>>=' | '**=' | '//=')
84 # For normal and annotated assignments, additional restrictions enforced by the interpreter
85 print_stmt: 'print' ( [ test (',' test)* [','] ] |
86 '>>' test [ (',' test)+ [','] ] )
87 del_stmt: 'del' exprlist
89 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
91 continue_stmt: 'continue'
92 return_stmt: 'return' [testlist_star_expr]
93 yield_stmt: yield_expr
94 raise_stmt: 'raise' [test ['from' test | ',' test [',' test]]]
95 import_stmt: import_name | import_from
96 import_name: 'import' dotted_as_names
97 import_from: ('from' ('.'* dotted_name | '.'+)
98 'import' ('*' | '(' import_as_names ')' | import_as_names))
99 import_as_name: NAME ['as' NAME]
100 dotted_as_name: dotted_name ['as' NAME]
101 import_as_names: import_as_name (',' import_as_name)* [',']
102 dotted_as_names: dotted_as_name (',' dotted_as_name)*
103 dotted_name: NAME ('.' NAME)*
104 global_stmt: ('global' | 'nonlocal') NAME (',' NAME)*
105 exec_stmt: 'exec' expr ['in' test [',' test]]
106 assert_stmt: 'assert' test [',' test]
108 compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt | match_stmt
109 async_stmt: ASYNC (funcdef | with_stmt | for_stmt)
110 if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* ['else' ':' suite]
111 while_stmt: 'while' namedexpr_test ':' suite ['else' ':' suite]
112 for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
113 try_stmt: ('try' ':' suite
114 ((except_clause ':' suite)+
116 ['finally' ':' suite] |
117 'finally' ':' suite))
118 with_stmt: 'with' asexpr_test (',' asexpr_test)* ':' suite
120 # NB compile.c makes sure that the default except clause is last
121 except_clause: 'except' [test [(',' | 'as') test]]
122 suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
124 # Backward compatibility cruft to support:
125 # [ x for x in lambda: True, lambda: False if x() ]
126 # even while also allowing:
127 # lambda x: 5 if x else 2
128 # (But not a mix of the two)
129 testlist_safe: old_test [(',' old_test)+ [',']]
130 old_test: or_test | old_lambdef
131 old_lambdef: 'lambda' [varargslist] ':' old_test
133 namedexpr_test: asexpr_test [':=' asexpr_test]
135 # This is actually not a real rule, though since the parser is very
136 # limited in terms of the strategy about match/case rules, we are inserting
137 # a virtual case (<expr> as <expr>) as a valid expression. Unless a better
138 # approach is thought, the only side effect of this seem to be just allowing
139 # more stuff to be parser (which would fail on the ast).
140 asexpr_test: test ['as' test]
142 test: or_test ['if' or_test 'else' test] | lambdef
143 or_test: and_test ('or' and_test)*
144 and_test: not_test ('and' not_test)*
145 not_test: 'not' not_test | comparison
146 comparison: expr (comp_op expr)*
147 comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
149 expr: xor_expr ('|' xor_expr)*
150 xor_expr: and_expr ('^' and_expr)*
151 and_expr: shift_expr ('&' shift_expr)*
152 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
153 arith_expr: term (('+'|'-') term)*
154 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
155 factor: ('+'|'-'|'~') factor | power
156 power: [AWAIT] atom trailer* ['**' factor]
157 atom: ('(' [yield_expr|testlist_gexp] ')' |
158 '[' [listmaker] ']' |
159 '{' [dictsetmaker] '}' |
161 NAME | NUMBER | STRING+ | '.' '.' '.')
162 listmaker: (namedexpr_test|star_expr) ( old_comp_for | (',' (namedexpr_test|star_expr))* [','] )
163 testlist_gexp: (namedexpr_test|star_expr) ( old_comp_for | (',' (namedexpr_test|star_expr))* [','] )
164 lambdef: 'lambda' [varargslist] ':' test
165 trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
166 subscriptlist: subscript (',' subscript)* [',']
167 subscript: test [':=' test] | [test] ':' [test] [sliceop]
169 exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
170 testlist: test (',' test)* [',']
171 dictsetmaker: ( ((test ':' test | '**' expr)
172 (comp_for | (',' (test ':' test | '**' expr))* [','])) |
173 ((test [':=' test] | star_expr)
174 (comp_for | (',' (test [':=' test] | star_expr))* [','])) )
176 classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
178 arglist: argument (',' argument)* [',']
180 # "test '=' test" is really "keyword '=' test", but we have no such token.
181 # These need to be in a single rule to avoid grammar that is ambiguous
182 # to our LL(1) parser. Even though 'test' includes '*expr' in star_expr,
183 # we explicitly match '*' here, too, to give it proper precedence.
184 # Illegal combinations and orderings are blocked in ast.c:
185 # multiple (test comp_for) arguments are blocked; keyword unpackings
186 # that precede iterable unpackings are blocked; etc.
187 argument: ( test [comp_for] |
194 comp_iter: comp_for | comp_if
195 comp_for: [ASYNC] 'for' exprlist 'in' or_test [comp_iter]
196 comp_if: 'if' old_test [comp_iter]
198 # As noted above, testlist_safe extends the syntax allowed in list
199 # comprehensions and generators. We can't use it indiscriminately in all
200 # derivations using a comp_for-like pattern because the testlist_safe derivation
201 # contains comma which clashes with trailing comma in arglist.
203 # This was an issue because the parser would not follow the correct derivation
204 # when parsing syntactically valid Python code. Since testlist_safe was created
205 # specifically to handle list comprehensions and generator expressions enclosed
206 # with parentheses, it's safe to only use it in those. That avoids the issue; we
207 # can parse code like set(x for x in [],).
209 # The syntax supported by this set of rules is not a valid Python 3 syntax,
210 # hence the prefix "old".
212 # See https://bugs.python.org/issue27494
213 old_comp_iter: old_comp_for | old_comp_if
214 old_comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [old_comp_iter]
215 old_comp_if: 'if' old_test [old_comp_iter]
217 testlist1: test (',' test)*
219 # not used in grammar, but may appear in "node" passed from Parser to Compiler
222 yield_expr: 'yield' [yield_arg]
223 yield_arg: 'from' test | testlist_star_expr
226 # 3.10 match statement definition
228 # PS: normally the grammar is much much more restricted, but
229 # at this moment for not trying to bother much with encoding the
230 # exact same DSL in a LL(1) parser, we will just accept an expression
231 # and let the ast.parse() step of the safe mode to reject invalid
234 # The reason why it is more restricted is that, patterns are some
235 # sort of a DSL (more advanced than our LHS on assignments, but
236 # still in a very limited python subset). They are not really
237 # expressions, but who cares. If we can parse them, that is enough
240 match_stmt: "match" subject_expr ':' NEWLINE INDENT case_block+ DEDENT
242 # This is more permissive than the actual version. For example it
243 # accepts `match *something:`, even though single-item starred expressions
245 subject_expr: (namedexpr_test|star_expr) (',' (namedexpr_test|star_expr))* [',']
248 case_block: "case" patterns [guard] ':' suite
249 guard: 'if' namedexpr_test
250 patterns: pattern ['as' pattern]
251 pattern: (expr|star_expr) (',' (expr|star_expr))* [',']