| 1 |
# Grammar for 2to3. This grammar supports Python 2.x and 3.x. |
| 2 |
|
| 3 |
# NOTE WELL: You should also follow all the steps listed at |
| 4 |
# https://devguide.python.org/grammar/ |
| 5 |
|
| 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 |
| 14 |
|
| 15 |
typevar: NAME [':' test] ['=' test] |
| 16 |
paramspec: '**' NAME ['=' test] |
| 17 |
typevartuple: '*' NAME ['=' (test|star_expr)] |
| 18 |
typeparam: typevar | paramspec | typevartuple |
| 19 |
typeparams: '[' typeparam (',' typeparam)* [','] ']' |
| 20 |
|
| 21 |
decorator: '@' namedexpr_test NEWLINE |
| 22 |
decorators: decorator+ |
| 23 |
decorated: decorators (classdef | funcdef | async_funcdef) |
| 24 |
async_funcdef: ASYNC funcdef |
| 25 |
funcdef: 'def' NAME [typeparams] parameters ['->' test] ':' suite |
| 26 |
parameters: '(' [typedargslist] ')' |
| 27 |
|
| 28 |
# The following definition for typedarglist is equivalent to this set of rules: |
| 29 |
# |
| 30 |
# arguments = argument (',' argument)* |
| 31 |
# argument = tfpdef ['=' test] |
| 32 |
# kwargs = '**' tname [','] |
| 33 |
# args = '*' [tname_star] |
| 34 |
# kwonly_kwargs = (',' argument)* [',' [kwargs]] |
| 35 |
# args_kwonly_kwargs = args kwonly_kwargs | kwargs |
| 36 |
# poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]] |
| 37 |
# typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs |
| 38 |
# typedarglist = arguments ',' '/' [',' [typedargslist_no_posonly]])|(typedargslist_no_posonly)" |
| 39 |
# |
| 40 |
# It needs to be fully expanded to allow our LL(1) parser to work on it. |
| 41 |
|
| 42 |
typedargslist: tfpdef ['=' test] (',' tfpdef ['=' test])* ',' '/' [ |
| 43 |
',' [((tfpdef ['=' test] ',')* ('*' [tname_star] (',' tname ['=' test])* |
| 44 |
[',' ['**' tname [',']]] | '**' tname [',']) |
| 45 |
| tfpdef ['=' test] (',' tfpdef ['=' test])* [','])] |
| 46 |
] | ((tfpdef ['=' test] ',')* ('*' [tname_star] (',' tname ['=' test])* |
| 47 |
[',' ['**' tname [',']]] | '**' tname [',']) |
| 48 |
| tfpdef ['=' test] (',' tfpdef ['=' test])* [',']) |
| 49 |
|
| 50 |
tname: NAME [':' test] |
| 51 |
tname_star: NAME [':' (test|star_expr)] |
| 52 |
tfpdef: tname | '(' tfplist ')' |
| 53 |
tfplist: tfpdef (',' tfpdef)* [','] |
| 54 |
|
| 55 |
# The following definition for varargslist is equivalent to this set of rules: |
| 56 |
# |
| 57 |
# arguments = argument (',' argument )* |
| 58 |
# argument = vfpdef ['=' test] |
| 59 |
# kwargs = '**' vname [','] |
| 60 |
# args = '*' [vname] |
| 61 |
# kwonly_kwargs = (',' argument )* [',' [kwargs]] |
| 62 |
# args_kwonly_kwargs = args kwonly_kwargs | kwargs |
| 63 |
# poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]] |
| 64 |
# vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs |
| 65 |
# varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] | (vararglist_no_posonly) |
| 66 |
# |
| 67 |
# It needs to be fully expanded to allow our LL(1) parser to work on it. |
| 68 |
|
| 69 |
varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ |
| 70 |
((vfpdef ['=' test] ',')* ('*' [vname] (',' vname ['=' test])* |
| 71 |
[',' ['**' vname [',']]] | '**' vname [',']) |
| 72 |
| vfpdef ['=' test] (',' vfpdef ['=' test])* [',']) |
| 73 |
]] | ((vfpdef ['=' test] ',')* |
| 74 |
('*' [vname] (',' vname ['=' test])* [',' ['**' vname [',']]]| '**' vname [',']) |
| 75 |
| vfpdef ['=' test] (',' vfpdef ['=' test])* [',']) |
| 76 |
|
| 77 |
vname: NAME |
| 78 |
vfpdef: vname | '(' vfplist ')' |
| 79 |
vfplist: vfpdef (',' vfpdef)* [','] |
| 80 |
|
| 81 |
stmt: simple_stmt | compound_stmt |
| 82 |
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
| 83 |
small_stmt: (type_stmt | expr_stmt | del_stmt | pass_stmt | flow_stmt | |
| 84 |
import_stmt | global_stmt | assert_stmt) |
| 85 |
expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | |
| 86 |
('=' (yield_expr|testlist_star_expr))*) |
| 87 |
annassign: ':' test ['=' (yield_expr|testlist_star_expr)] |
| 88 |
testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] |
| 89 |
augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
| 90 |
'<<=' | '>>=' | '**=' | '//=') |
| 91 |
# For normal and annotated assignments, additional restrictions enforced by the interpreter |
| 92 |
del_stmt: 'del' exprlist |
| 93 |
pass_stmt: 'pass' |
| 94 |
flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt |
| 95 |
break_stmt: 'break' |
| 96 |
continue_stmt: 'continue' |
| 97 |
return_stmt: 'return' [testlist_star_expr] |
| 98 |
yield_stmt: yield_expr |
| 99 |
raise_stmt: 'raise' [test ['from' test | ',' test [',' test]]] |
| 100 |
import_stmt: import_name | import_from | lazy_import |
| 101 |
lazy_import: LAZY (import_name | import_from) |
| 102 |
import_name: 'import' dotted_as_names |
| 103 |
import_from: ('from' ('.'* dotted_name | '.'+) |
| 104 |
'import' ('*' | '(' import_as_names ')' | import_as_names)) |
| 105 |
import_as_name: NAME ['as' NAME] |
| 106 |
dotted_as_name: dotted_name ['as' NAME] |
| 107 |
import_as_names: import_as_name (',' import_as_name)* [','] |
| 108 |
dotted_as_names: dotted_as_name (',' dotted_as_name)* |
| 109 |
dotted_name: NAME ('.' NAME)* |
| 110 |
global_stmt: ('global' | 'nonlocal') NAME (',' NAME)* |
| 111 |
assert_stmt: 'assert' test [',' test] |
| 112 |
type_stmt: "type" NAME [typeparams] '=' test |
| 113 |
|
| 114 |
compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt | match_stmt |
| 115 |
async_stmt: ASYNC (funcdef | with_stmt | for_stmt) |
| 116 |
if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* ['else' ':' suite] |
| 117 |
while_stmt: 'while' namedexpr_test ':' suite ['else' ':' suite] |
| 118 |
for_stmt: 'for' exprlist 'in' testlist_star_expr ':' suite ['else' ':' suite] |
| 119 |
try_stmt: ('try' ':' suite |
| 120 |
((except_clause ':' suite)+ |
| 121 |
['else' ':' suite] |
| 122 |
['finally' ':' suite] | |
| 123 |
'finally' ':' suite)) |
| 124 |
with_stmt: 'with' asexpr_test (',' asexpr_test)* ':' suite |
| 125 |
|
| 126 |
# NB compile.c makes sure that the default except clause is last |
| 127 |
except_clause: 'except' ['*'] [testlist ['as' test]] |
| 128 |
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT |
| 129 |
|
| 130 |
# Backward compatibility cruft to support: |
| 131 |
# [ x for x in lambda: True, lambda: False if x() ] |
| 132 |
# even while also allowing: |
| 133 |
# lambda x: 5 if x else 2 |
| 134 |
# (But not a mix of the two) |
| 135 |
testlist_safe: old_test [(',' old_test)+ [',']] |
| 136 |
old_test: or_test | old_lambdef |
| 137 |
old_lambdef: 'lambda' [varargslist] ':' old_test |
| 138 |
|
| 139 |
namedexpr_test: asexpr_test [':=' asexpr_test] |
| 140 |
|
| 141 |
# This is actually not a real rule, though since the parser is very |
| 142 |
# limited in terms of the strategy about match/case rules, we are inserting |
| 143 |
# a virtual case (<expr> as <expr>) as a valid expression. Unless a better |
| 144 |
# approach is thought, the only side effect of this seem to be just allowing |
| 145 |
# more stuff to be parser (which would fail on the ast). |
| 146 |
asexpr_test: test ['as' test] |
| 147 |
|
| 148 |
test: or_test ['if' or_test 'else' test] | lambdef |
| 149 |
or_test: and_test ('or' and_test)* |
| 150 |
and_test: not_test ('and' not_test)* |
| 151 |
not_test: 'not' not_test | comparison |
| 152 |
comparison: expr (comp_op expr)* |
| 153 |
comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' |
| 154 |
star_expr: '*' expr |
| 155 |
expr: xor_expr ('|' xor_expr)* |
| 156 |
xor_expr: and_expr ('^' and_expr)* |
| 157 |
and_expr: shift_expr ('&' shift_expr)* |
| 158 |
shift_expr: arith_expr (('<<'|'>>') arith_expr)* |
| 159 |
arith_expr: term (('+'|'-') term)* |
| 160 |
term: factor (('*'|'@'|'/'|'%'|'//') factor)* |
| 161 |
factor: ('+'|'-'|'~') factor | power |
| 162 |
power: [AWAIT] atom trailer* ['**' factor] |
| 163 |
atom: ('(' [yield_expr|testlist_gexp] ')' | |
| 164 |
'[' [listmaker] ']' | |
| 165 |
'{' [dictsetmaker] '}' | |
| 166 |
'`' testlist1 '`' | |
| 167 |
NAME | NUMBER | (STRING | fstring | tstring)+ | '.' '.' '.') |
| 168 |
listmaker: (namedexpr_test|star_expr) ( old_comp_for | (',' (namedexpr_test|star_expr))* [','] ) |
| 169 |
testlist_gexp: (namedexpr_test|star_expr) ( old_comp_for | (',' (namedexpr_test|star_expr))* [','] ) |
| 170 |
lambdef: 'lambda' [varargslist] ':' test |
| 171 |
trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
| 172 |
subscriptlist: (subscript|star_expr) (',' (subscript|star_expr))* [','] |
| 173 |
subscript: test [':=' test] | [test] ':' [test] [sliceop] |
| 174 |
sliceop: ':' [test] |
| 175 |
exprlist: (expr|star_expr) (',' (expr|star_expr))* [','] |
| 176 |
testlist: test (',' test)* [','] |
| 177 |
dictsetmaker: ( ((test ':' asexpr_test | '**' expr) |
| 178 |
(comp_for | (',' (test ':' asexpr_test | '**' expr))* [','])) | |
| 179 |
((test [':=' test] | star_expr) |
| 180 |
(comp_for | (',' (test [':=' test] | star_expr))* [','])) ) |
| 181 |
|
| 182 |
classdef: 'class' NAME [typeparams] ['(' [arglist] ')'] ':' suite |
| 183 |
|
| 184 |
arglist: argument (',' argument)* [','] |
| 185 |
|
| 186 |
# "test '=' test" is really "keyword '=' test", but we have no such token. |
| 187 |
# These need to be in a single rule to avoid grammar that is ambiguous |
| 188 |
# to our LL(1) parser. Even though 'test' includes '*expr' in star_expr, |
| 189 |
# we explicitly match '*' here, too, to give it proper precedence. |
| 190 |
# Illegal combinations and orderings are blocked in ast.c: |
| 191 |
# multiple (test comp_for) arguments are blocked; keyword unpackings |
| 192 |
# that precede iterable unpackings are blocked; etc. |
| 193 |
argument: ( test [comp_for] | |
| 194 |
test ':=' test [comp_for] | |
| 195 |
test 'as' test | |
| 196 |
test '=' asexpr_test | |
| 197 |
'**' test | |
| 198 |
'*' test ) |
| 199 |
|
| 200 |
comp_iter: comp_for | comp_if |
| 201 |
comp_for: [ASYNC] 'for' exprlist 'in' or_test [comp_iter] |
| 202 |
comp_if: 'if' old_test [comp_iter] |
| 203 |
|
| 204 |
# As noted above, testlist_safe extends the syntax allowed in list |
| 205 |
# comprehensions and generators. We can't use it indiscriminately in all |
| 206 |
# derivations using a comp_for-like pattern because the testlist_safe derivation |
| 207 |
# contains comma which clashes with trailing comma in arglist. |
| 208 |
# |
| 209 |
# This was an issue because the parser would not follow the correct derivation |
| 210 |
# when parsing syntactically valid Python code. Since testlist_safe was created |
| 211 |
# specifically to handle list comprehensions and generator expressions enclosed |
| 212 |
# with parentheses, it's safe to only use it in those. That avoids the issue; we |
| 213 |
# can parse code like set(x for x in [],). |
| 214 |
# |
| 215 |
# The syntax supported by this set of rules is not a valid Python 3 syntax, |
| 216 |
# hence the prefix "old". |
| 217 |
# |
| 218 |
# See https://bugs.python.org/issue27494 |
| 219 |
old_comp_iter: old_comp_for | old_comp_if |
| 220 |
old_comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [old_comp_iter] |
| 221 |
old_comp_if: 'if' old_test [old_comp_iter] |
| 222 |
|
| 223 |
testlist1: test (',' test)* |
| 224 |
|
| 225 |
# not used in grammar, but may appear in "node" passed from Parser to Compiler |
| 226 |
encoding_decl: NAME |
| 227 |
|
| 228 |
yield_expr: 'yield' [yield_arg] |
| 229 |
yield_arg: 'from' test | testlist_star_expr |
| 230 |
|
| 231 |
|
| 232 |
# 3.10 match statement definition |
| 233 |
|
| 234 |
# PS: normally the grammar is much much more restricted, but |
| 235 |
# at this moment for not trying to bother much with encoding the |
| 236 |
# exact same DSL in a LL(1) parser, we will just accept an expression |
| 237 |
# and let the ast.parse() step of the safe mode to reject invalid |
| 238 |
# grammar. |
| 239 |
|
| 240 |
# The reason why it is more restricted is that, patterns are some |
| 241 |
# sort of a DSL (more advanced than our LHS on assignments, but |
| 242 |
# still in a very limited python subset). They are not really |
| 243 |
# expressions, but who cares. If we can parse them, that is enough |
| 244 |
# to reformat them. |
| 245 |
|
| 246 |
match_stmt: "match" subject_expr ':' NEWLINE INDENT case_block+ DEDENT |
| 247 |
|
| 248 |
# This is more permissive than the actual version. For example it |
| 249 |
# accepts `match *something:`, even though single-item starred expressions |
| 250 |
# are forbidden. |
| 251 |
subject_expr: (namedexpr_test|star_expr) (',' (namedexpr_test|star_expr))* [','] |
| 252 |
|
| 253 |
# cases |
| 254 |
case_block: "case" patterns [guard] ':' suite |
| 255 |
guard: 'if' namedexpr_test |
| 256 |
patterns: pattern (',' pattern)* [','] |
| 257 |
pattern: (expr|star_expr) ['as' expr] |
| 258 |
|
| 259 |
fstring: FSTRING_START fstring_middle* FSTRING_END |
| 260 |
fstring_middle: fstring_replacement_field | FSTRING_MIDDLE |
| 261 |
fstring_replacement_field: '{' (yield_expr | testlist_star_expr) ['='] [ "!" NAME ] [ ':' fstring_format_spec* ] '}' |
| 262 |
fstring_format_spec: FSTRING_MIDDLE | fstring_replacement_field |
| 263 |
|
| 264 |
tstring: TSTRING_START tstring_middle* TSTRING_END |
| 265 |
tstring_middle: tstring_replacement_field | TSTRING_MIDDLE |
| 266 |
tstring_replacement_field: '{' (yield_expr | testlist_star_expr) ['='] [ "!" NAME ] [ ':' tstring_format_spec* ] '}' |
| 267 |
tstring_format_spec: TSTRING_MIDDLE | tstring_replacement_field |