| 1 |
# PEG grammar for Python, actions in RPython |
| 2 |
# forked from the python.gram of the pegen repo |
| 3 |
|
| 4 |
@class PythonParser |
| 5 |
|
| 6 |
@subheader''' |
| 7 |
|
| 8 |
from pypy.interpreter.pyparser.baserpypeg import * |
| 9 |
''' |
| 10 |
|
| 11 |
# ========================= START OF THE GRAMMAR ========================= |
| 12 |
|
| 13 |
# General grammatical elements and rules: |
| 14 |
# |
| 15 |
# * Strings with double quotes (") denote SOFT KEYWORDS |
| 16 |
# * Strings with single quotes (') denote KEYWORDS |
| 17 |
# * Upper case names (NAME) denote tokens in the Grammar/Tokens file |
| 18 |
# * Rule names starting with "invalid_" are used for specialized syntax errors |
| 19 |
# - These rules are NOT used in the first pass of the parser. |
| 20 |
# - Only if the first pass fails to parse, a second pass including the invalid |
| 21 |
# rules will be executed. |
| 22 |
# - If the parser fails in the second phase with a generic syntax error, the |
| 23 |
# location of the generic failure of the first pass will be used (this avoids |
| 24 |
# reporting incorrect locations due to the invalid rules). |
| 25 |
# - The order of the alternatives involving invalid rules matter |
| 26 |
# (like any rule in PEG). |
| 27 |
# |
| 28 |
# Grammar Syntax (see PEP 617 for more information): |
| 29 |
# |
| 30 |
# rule_name: expression |
| 31 |
# Optionally, a type can be included right after the rule name, which |
| 32 |
# specifies the return type of the C or Python function corresponding to the |
| 33 |
# rule: |
| 34 |
# rule_name[return_type]: expression |
| 35 |
# If the return type is omitted, then a void * is returned in C and an Any in |
| 36 |
# Python. |
| 37 |
# e1 e2 |
| 38 |
# Match e1, then match e2. |
| 39 |
# e1 | e2 |
| 40 |
# Match e1 or e2. |
| 41 |
# The first alternative can also appear on the line after the rule name for |
| 42 |
# formatting purposes. In that case, a | must be used before the first |
| 43 |
# alternative, like so: |
| 44 |
# rule_name[return_type]: |
| 45 |
# | first_alt |
| 46 |
# | second_alt |
| 47 |
# ( e ) |
| 48 |
# Match e (allows also to use other operators in the group like '(e)*') |
| 49 |
# [ e ] or e? |
| 50 |
# Optionally match e. |
| 51 |
# e* |
| 52 |
# Match zero or more occurrences of e. |
| 53 |
# e+ |
| 54 |
# Match one or more occurrences of e. |
| 55 |
# s.e+ |
| 56 |
# Match one or more occurrences of e, separated by s. The generated parse tree |
| 57 |
# does not include the separator. This is otherwise identical to (e (s e)*). |
| 58 |
# &e |
| 59 |
# Succeed if e can be parsed, without consuming any input. |
| 60 |
# !e |
| 61 |
# Fail if e can be parsed, without consuming any input. |
| 62 |
# ~ |
| 63 |
# Commit to the current alternative, even if it fails to parse. |
| 64 |
# |
| 65 |
|
| 66 |
# STARTING RULES |
| 67 |
# ============== |
| 68 |
|
| 69 |
start: file |
| 70 |
|
| 71 |
file[ast.Module]: a=[statements] ENDMARKER { ast.Module(body=a, type_ignores=self.make_type_ignores()) } |
| 72 |
interactive[ast.Interactive]: a=statement_newline { ast.Interactive(body=a) } |
| 73 |
eval[ast.Expression]: a=expressions NEWLINE* ENDMARKER { ast.Expression(body=a) } |
| 74 |
func_type[ast.FunctionType]: '(' a=[type_expressions] ')' '->' b=expression NEWLINE* ENDMARKER { ast.FunctionType(argtypes=a, returns=b) } |
| 75 |
fstring[ast.Expr]: star_expressions |
| 76 |
|
| 77 |
# TYPING ELEMENTS |
| 78 |
# --------------- |
| 79 |
|
| 80 |
# type_expressions allow */** but ignore them |
| 81 |
type_expressions[list]: |
| 82 |
| a=','.expression+ ',' '*' b=expression ',' '**' c=expression { a + [b, c] } |
| 83 |
| a=','.expression+ ',' '*' b=expression { a + [b] } |
| 84 |
| a=','.expression+ ',' '**' b=expression { a + [b] } |
| 85 |
| '*' a=expression ',' '**' b=expression { [a, b] } |
| 86 |
| '*' a=expression { [a] } |
| 87 |
| '**' a=expression { [a] } |
| 88 |
| a=','.expression+ {a} |
| 89 |
|
| 90 |
|
| 91 |
# GENERAL STATEMENTS |
| 92 |
# ================== |
| 93 |
|
| 94 |
statements[list]: a=statement+ { [x for l in a for x in l] } |
| 95 |
|
| 96 |
statement[list]: a=compound_stmt { [a] } | a=simple_stmts { a } |
| 97 |
|
| 98 |
statement_newline[list]: # PyPy modification: all rules need to end with ENDMARKER` |
| 99 |
| a=compound_stmt [NEWLINE] ENDMARKER { [a] } |
| 100 |
| a=simple_stmts ENDMARKER { a } |
| 101 |
| NEWLINE ENDMARKER { [ast.Pass(LOCATIONS)] } |
| 102 |
| ENDMARKER { None } |
| 103 |
|
| 104 |
simple_stmts[list]: |
| 105 |
| a=simple_stmt !';' NEWLINE { [a] } # Not needed, there for speedup |
| 106 |
| a=';'.simple_stmt+ [';'] NEWLINE { a } |
| 107 |
|
| 108 |
# NOTE: assignment MUST precede expression, else parsing a simple assignment |
| 109 |
# will throw a SyntaxError. |
| 110 |
simple_stmt (memo): |
| 111 |
| assignment |
| 112 |
| e=star_expressions { ast.Expr(value=e, LOCATIONS) } |
| 113 |
| &'return' return_stmt |
| 114 |
| &('import' | 'from') import_stmt |
| 115 |
| &'raise' raise_stmt |
| 116 |
| 'pass' { ast.Pass(LOCATIONS) } |
| 117 |
| &'del' del_stmt |
| 118 |
| &'yield' yield_stmt |
| 119 |
| &'assert' assert_stmt |
| 120 |
| 'break' { ast.Break(LOCATIONS) } |
| 121 |
| 'continue' { ast.Continue(LOCATIONS) } |
| 122 |
| &'global' global_stmt |
| 123 |
| &'nonlocal' nonlocal_stmt |
| 124 |
|
| 125 |
compound_stmt: |
| 126 |
| &('def' | '@' | ASYNC) function_def |
| 127 |
| &'if' if_stmt |
| 128 |
| &('class' | '@') class_def |
| 129 |
| &('with' | ASYNC) with_stmt |
| 130 |
| &('for' | ASYNC) for_stmt |
| 131 |
| &'try' try_stmt |
| 132 |
| &'while' while_stmt |
| 133 |
| match_stmt |
| 134 |
|
| 135 |
# SIMPLE STATEMENTS |
| 136 |
# ================= |
| 137 |
|
| 138 |
# NOTE: annotated_rhs may start with 'yield'; yield_expr must start with 'yield' |
| 139 |
assignment: |
| 140 |
| a=NAME ':' b=expression c=['=' d=annotated_rhs { d }] { |
| 141 |
self.check_version( |
| 142 |
(3, 6), |
| 143 |
"Variable annotation syntax is", |
| 144 |
ast.AnnAssign( |
| 145 |
target=self.set_expr_context(a, Store), |
| 146 |
annotation=b, |
| 147 |
value=c, |
| 148 |
simple=1, |
| 149 |
LOCATIONS, |
| 150 |
) |
| 151 |
) } |
| 152 |
| a=('(' b=single_target ')' { b } |
| 153 |
| single_subscript_attribute_target) ':' b=expression c=['=' d=annotated_rhs { d }] { |
| 154 |
self.check_version( |
| 155 |
(3, 6), |
| 156 |
"Variable annotation syntax is", |
| 157 |
ast.AnnAssign( |
| 158 |
target=a, |
| 159 |
annotation=b, |
| 160 |
value=c, |
| 161 |
simple=0, |
| 162 |
LOCATIONS, |
| 163 |
) |
| 164 |
) |
| 165 |
} |
| 166 |
| a=(z=star_targets '=' { z })+ b=(yield_expr | star_expressions) !'=' tc=[TYPE_COMMENT] { |
| 167 |
ast.Assign(targets=a, value=b, type_comment=tc, LOCATIONS) |
| 168 |
} |
| 169 |
| a=single_target b=augassign ~ c=(yield_expr | star_expressions) { |
| 170 |
ast.AugAssign(target = a, op=b[0], value=c, LOCATIONS) |
| 171 |
} |
| 172 |
| invalid_assignment |
| 173 |
|
| 174 |
annotated_rhs: yield_expr | star_expressions |
| 175 |
|
| 176 |
augassign: # rpython hack: to make the numbers compatible with None, wrap in a list |
| 177 |
| '+=' { [ast.Add] } |
| 178 |
| '-=' { [ast.Sub] } |
| 179 |
| '*=' { [ast.Mult] } |
| 180 |
| '@=' { self.check_version((3, 5), "The '@' operator is", [ast.MatMult]) } |
| 181 |
| '/=' { [ast.Div] } |
| 182 |
| '%=' { [ast.Mod] } |
| 183 |
| '&=' { [ast.BitAnd] } |
| 184 |
| '|=' { [ast.BitOr] } |
| 185 |
| '^=' { [ast.BitXor] } |
| 186 |
| '<<=' { [ast.LShift] } |
| 187 |
| '>>=' { [ast.RShift] } |
| 188 |
| '**=' { [ast.Pow] } |
| 189 |
| '//=' { [ast.FloorDiv] } |
| 190 |
|
| 191 |
return_stmt[ast.Return]: |
| 192 |
| 'return' a=[star_expressions] { ast.Return(value=a, LOCATIONS) } |
| 193 |
|
| 194 |
raise_stmt[ast.Raise]: |
| 195 |
| 'raise' a=expression b=['from' z=expression { z }] { ast.Raise(exc=a, cause=b, LOCATIONS) } |
| 196 |
| 'raise' { ast.Raise(exc=None, cause=None, LOCATIONS) } |
| 197 |
|
| 198 |
global_stmt[ast.Global]: 'global' a=','.NAME+ { |
| 199 |
ast.Global(names=[n.id for n in a], LOCATIONS) |
| 200 |
} |
| 201 |
|
| 202 |
nonlocal_stmt[ast.Nonlocal]: 'nonlocal' a=','.NAME+ { |
| 203 |
ast.Nonlocal(names=[n.id for n in a], LOCATIONS) |
| 204 |
} |
| 205 |
|
| 206 |
del_stmt[ast.Delete]: |
| 207 |
| 'del' a=del_targets &(';' | NEWLINE) { ast.Delete(targets=a, LOCATIONS) } |
| 208 |
| invalid_del_stmt |
| 209 |
|
| 210 |
yield_stmt[ast.Expr]: y=yield_expr { ast.Expr(value=y, LOCATIONS) } |
| 211 |
|
| 212 |
assert_stmt[ast.Assert]: 'assert' a=expression b=[',' z=expression { z }] { |
| 213 |
ast.Assert(test=a, msg=b, LOCATIONS) |
| 214 |
} |
| 215 |
|
| 216 |
import_stmt[ast.Import]: import_name | import_from |
| 217 |
|
| 218 |
# Import statements |
| 219 |
# ----------------- |
| 220 |
|
| 221 |
import_name[ast.Import]: 'import' a=dotted_as_names { ast.Import(names=a, LOCATIONS) } |
| 222 |
|
| 223 |
# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS |
| 224 |
import_from[ast.ImportFrom]: |
| 225 |
| 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets { |
| 226 |
ast.ImportFrom(module=b, names=c, level=self.extract_import_level(a), LOCATIONS) |
| 227 |
} |
| 228 |
| 'from' a=('.' | '...')+ 'import' b=import_from_targets { |
| 229 |
ast.ImportFrom(module=None, names=b, level=self.extract_import_level(a), LOCATIONS) |
| 230 |
} |
| 231 |
import_from_targets[List[ast.alias]]: |
| 232 |
| '(' a=import_from_as_names [','] ')' { a } |
| 233 |
| import_from_as_names !',' |
| 234 |
| '*' { [ast.alias(name="*", asname=None, LOCATIONS)] } |
| 235 |
| invalid_import_from_targets |
| 236 |
import_from_as_names[List[ast.alias]]: |
| 237 |
| a=','.import_from_as_name+ { a } |
| 238 |
import_from_as_name[ast.alias]: |
| 239 |
| a=NAME b=['as' z=NAME { z }] { ast.alias(name=self.extract_id(a), asname=self.extract_id(b), LOCATIONS) } |
| 240 |
dotted_as_names[List[ast.alias]]: |
| 241 |
| a=','.dotted_as_name+ { a } |
| 242 |
dotted_as_name[ast.alias]: |
| 243 |
| a=dotted_name b=['as' z=NAME { z }] { ast.alias(name=a, asname=self.extract_id(b), LOCATIONS) } |
| 244 |
dotted_name[str]: |
| 245 |
| a=NAME ! '.' { self.extract_id(a) } # single name without following dots |
| 246 |
| a=dotted_name '.' b=NAME { a + "." + b.id } |
| 247 |
| a=NAME { a.id } |
| 248 |
|
| 249 |
# COMPOUND STATEMENTS |
| 250 |
# =================== |
| 251 |
|
| 252 |
# Common elements |
| 253 |
# --------------- |
| 254 |
|
| 255 |
block[list] (memo): |
| 256 |
| NEWLINE INDENT a=statements DEDENT { a } |
| 257 |
| simple_stmts |
| 258 |
| invalid_block |
| 259 |
|
| 260 |
decorators: decorator+ |
| 261 |
decorator: |
| 262 |
| a=('@' f=dec_maybe_call NEWLINE { f }) { a } |
| 263 |
| a=('@' f=named_expression NEWLINE { f }) { |
| 264 |
self.check_version((3, 9), "Generic decorator are", a) |
| 265 |
} |
| 266 |
dec_maybe_call: |
| 267 |
| dn=dec_primary '(' z=[arguments] ')' { |
| 268 |
ast.Call(func=dn, args=z.args if z and z.args else None, keywords=z.keywords if z and z.keywords else None, LOCATIONS) |
| 269 |
} |
| 270 |
| dec_primary |
| 271 |
dec_primary: |
| 272 |
| a=dec_primary '.' b=NAME { ast.Attribute(value=a, attr=b.id, ctx=Load, LOCATIONS) } |
| 273 |
| a=NAME { a } |
| 274 |
|
| 275 |
# Class definitions # XXX this section moved in cpy 3.11 |
| 276 |
# ----------------- |
| 277 |
|
| 278 |
class_def[ast.ClassDef]: |
| 279 |
| a=decorators b=class_def_raw { self.set_decorators(b, a) } |
| 280 |
| class_def_raw |
| 281 |
|
| 282 |
class_def_raw[ast.ClassDef]: |
| 283 |
| invalid_class_def_raw |
| 284 |
| 'class' a=NAME b=['(' z=[arguments] ')' { z }] ':' c=block { |
| 285 |
ast.ClassDef( |
| 286 |
self.extract_id(a), |
| 287 |
bases=b.args if b else None, |
| 288 |
keywords=b.keywords if b else None, |
| 289 |
body=c, |
| 290 |
decorator_list=None, |
| 291 |
LOCATIONS, |
| 292 |
) |
| 293 |
} |
| 294 |
|
| 295 |
# Function definitions |
| 296 |
# -------------------- |
| 297 |
|
| 298 |
function_def[Union[ast.FunctionDef, ast.AsyncFunctionDef]]: |
| 299 |
| d=decorators f=function_def_raw { self.set_decorators(f, d) } |
| 300 |
| f=function_def_raw {self.set_decorators(f, None)} |
| 301 |
|
| 302 |
function_def_raw[Union[ast.FunctionDef, ast.AsyncFunctionDef]]: |
| 303 |
| invalid_def_raw |
| 304 |
| 'def' n=NAME &&'(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block { |
| 305 |
ast.FunctionDef( |
| 306 |
name=self.extract_id(n), |
| 307 |
args=params or self.make_arguments(None, None, None, [], None), |
| 308 |
returns=a, |
| 309 |
body=b, |
| 310 |
decorator_list=None, |
| 311 |
type_comment=tc, |
| 312 |
LOCATIONS, |
| 313 |
) |
| 314 |
} |
| 315 |
| ASYNC 'def' n=NAME &&'(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block { |
| 316 |
self.check_version( |
| 317 |
(3, 5), |
| 318 |
"Async functions are", |
| 319 |
ast.AsyncFunctionDef( |
| 320 |
name=self.extract_id(n), |
| 321 |
args=params or self.make_arguments(None, None, None, [], None), |
| 322 |
returns=a, |
| 323 |
body=b, |
| 324 |
decorator_list=None, |
| 325 |
type_comment=tc, |
| 326 |
LOCATIONS, |
| 327 |
) |
| 328 |
) |
| 329 |
} |
| 330 |
|
| 331 |
func_type_comment: |
| 332 |
| NEWLINE t=TYPE_COMMENT &(NEWLINE INDENT) { t } # Must be followed by indented block |
| 333 |
| invalid_double_type_comments |
| 334 |
| TYPE_COMMENT |
| 335 |
|
| 336 |
|
| 337 |
# Function parameters |
| 338 |
# ------------------- |
| 339 |
|
| 340 |
params: |
| 341 |
| invalid_parameters |
| 342 |
| parameters |
| 343 |
|
| 344 |
parameters[ast.arguments]: |
| 345 |
| a=slash_no_default b=param_no_default* c=param_with_default* d=[star_etc] { |
| 346 |
self.make_arguments(a.plain_names, None, b, c, d) |
| 347 |
} |
| 348 |
| a=slash_with_default b=param_with_default* c=[star_etc] { |
| 349 |
self.make_arguments(None, a, None, b, c) |
| 350 |
} |
| 351 |
| a=param_no_default+ b=param_with_default* c=[star_etc] { |
| 352 |
self.make_arguments(None, None, a, b, c) |
| 353 |
} |
| 354 |
| a=param_with_default+ b=[star_etc] { |
| 355 |
self.make_arguments(None, None, None, a, b) |
| 356 |
} |
| 357 |
| a=star_etc { self.make_arguments(None, None, None, None, a) } |
| 358 |
|
| 359 |
# Some duplication here because we can't write (',' | &')'), |
| 360 |
# which is because we don't support empty alternatives (yet). |
| 361 |
# |
| 362 |
|
| 363 |
slash_no_default[List[ast.arg]]: |
| 364 |
| a=param_no_default+ '/' ',' { self.make_slash_with_default(a, None) } |
| 365 |
| a=param_no_default+ '/' &')' { self.make_slash_with_default(a, None) } |
| 366 |
slash_with_default[SlashWithDefault]: |
| 367 |
| a=param_no_default* b=param_with_default+ '/' ',' { self.make_slash_with_default(a, b) } |
| 368 |
| a=param_no_default* b=param_with_default+ '/' &')' { self.make_slash_with_default(a, b) } |
| 369 |
|
| 370 |
star_etc[StarEtc]: |
| 371 |
| invalid_star_etc |
| 372 |
| '*' a=param_no_default b=param_maybe_default* c=[kwds] { self.make_star_etc(a, b, c) } |
| 373 |
| '*' a=param_no_default_star_annotation b=param_maybe_default* c=[kwds] { |
| 374 |
self.make_star_etc(a, b, c) } |
| 375 |
| '*' ',' b=param_maybe_default+ c=[kwds] { self.make_star_etc(None, b, c) } |
| 376 |
| a=kwds { self.make_star_etc(None, [], a) } |
| 377 |
|
| 378 |
|
| 379 |
kwds[arg_ty]: |
| 380 |
| invalid_kwds |
| 381 |
| '**' a=param_no_default { a } |
| 382 |
|
| 383 |
# One parameter. This *includes* a following comma and type comment. |
| 384 |
# |
| 385 |
# There are three styles: |
| 386 |
# - No default |
| 387 |
# - With default |
| 388 |
# - Maybe with default |
| 389 |
# |
| 390 |
# There are two alternative forms of each, to deal with type comments: |
| 391 |
# - Ends in a comma followed by an optional type comment |
| 392 |
# - No comma, optional type comment, must be followed by close paren |
| 393 |
# The latter form is for a final parameter without trailing comma. |
| 394 |
# |
| 395 |
|
| 396 |
param_no_default[ast.arg]: |
| 397 |
| a=param ',' tc=TYPE_COMMENT? { self.set_arg_type_comment(a, tc) } |
| 398 |
| a=param tc=TYPE_COMMENT? &')' { self.set_arg_type_comment(a, tc) } |
| 399 |
param_no_default_star_annotation[ast.arg]: |
| 400 |
| a=param_star_annotation ',' tc=TYPE_COMMENT? { self.set_arg_type_comment(a, tc) } |
| 401 |
| a=param_star_annotation tc=TYPE_COMMENT? &')' { self.set_arg_type_comment(a, tc) } |
| 402 |
param_with_default[NameDefaultPair]: |
| 403 |
| a=param c=default ',' tc=TYPE_COMMENT? { self.name_default_pair(a, c, tc) } |
| 404 |
| a=param c=default tc=TYPE_COMMENT? &')' { self.name_default_pair(a, c, tc) } |
| 405 |
param_maybe_default[NameDefaultPair]: |
| 406 |
| a=param c=default? ',' tc=TYPE_COMMENT? { self.name_default_pair(a, c, tc) } |
| 407 |
| a=param c=default? tc=TYPE_COMMENT? &')' { self.name_default_pair(a, c, tc) } |
| 408 |
param[ast.arg]: a=NAME b=annotation? { ast.arg(arg=self.extract_id(a), annotation=b, type_comment=None, LOCATIONS) } |
| 409 |
param_star_annotation[ast.arg]: a=NAME b=star_annotation { ast.arg(arg=self.extract_id(a), annotation=b, type_comment=None, LOCATIONS) } |
| 410 |
annotation: ':' a=expression { a } |
| 411 |
star_annotation: ':' a=star_expression { a } |
| 412 |
default: '=' a=expression { a } | invalid_default |
| 413 |
|
| 414 |
# If statement |
| 415 |
# ------------ |
| 416 |
|
| 417 |
if_stmt[ast.If]: |
| 418 |
| invalid_if_stmt |
| 419 |
| 'if' a=named_expression ':' b=block c=elif_stmt { ast.If(test=a, body=b, orelse=c, LOCATIONS) } |
| 420 |
| 'if' a=named_expression ':' b=block c=[else_block] { ast.If(test=a, body=b, orelse=c, LOCATIONS) } |
| 421 |
elif_stmt[List[ast.If]]: |
| 422 |
| invalid_elif_stmt |
| 423 |
| 'elif' a=named_expression ':' b=block c=elif_stmt { [ast.If(test=a, body=b, orelse=c, LOCATIONS)] } |
| 424 |
| 'elif' a=named_expression ':' b=block c=[else_block] { [ast.If(test=a, body=b, orelse=c, LOCATIONS)] } |
| 425 |
else_block[list]: |
| 426 |
| invalid_else_stmt |
| 427 |
| 'else' &&':' b=block { b } |
| 428 |
|
| 429 |
# While statement |
| 430 |
# --------------- |
| 431 |
|
| 432 |
while_stmt[ast.While]: |
| 433 |
| invalid_while_stmt |
| 434 |
| 'while' a=named_expression ':' b=block c=[else_block] { |
| 435 |
ast.While(test=a, body=b, orelse=c, LOCATIONS) |
| 436 |
} |
| 437 |
|
| 438 |
# For statement |
| 439 |
# ------------- |
| 440 |
|
| 441 |
for_stmt[Union[ast.For, ast.AsyncFor]]: |
| 442 |
| invalid_for_stmt |
| 443 |
| 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] { |
| 444 |
ast.For(target=t, iter=ex, body=b, orelse=el, type_comment=tc, LOCATIONS) } |
| 445 |
| ASYNC 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] { |
| 446 |
self.check_version( |
| 447 |
(3, 5), |
| 448 |
"Async for loops are", |
| 449 |
ast.AsyncFor(target=t, iter=ex, body=b, orelse=el, type_comment=tc, LOCATIONS)) } |
| 450 |
| invalid_for_target |
| 451 |
|
| 452 |
# With statement |
| 453 |
# -------------- |
| 454 |
|
| 455 |
with_stmt[Union[ast.With, ast.AsyncWith]]: |
| 456 |
| invalid_with_stmt_indent |
| 457 |
| 'with' '(' a=','.with_item+ opt=','? ')' ':' b=block { |
| 458 |
self.check_version_for_parenthesized_with( |
| 459 |
a, opt, ast.With(items=a, body=b, type_comment=None, LOCATIONS)) |
| 460 |
} |
| 461 |
| 'with' a=','.with_item+ ':' tc=[TYPE_COMMENT] b=block { |
| 462 |
ast.With(items=a, body=b, type_comment=tc, LOCATIONS) |
| 463 |
} |
| 464 |
| ASYNC 'with' '(' a=','.with_item+ opt=','? ')' ':' b=block { |
| 465 |
self.check_version_for_parenthesized_with( |
| 466 |
a, opt, ast.AsyncWith(items=a, body=b, type_comment=None, LOCATIONS)) |
| 467 |
} |
| 468 |
| ASYNC 'with' a=','.with_item+ ':' tc=[TYPE_COMMENT] b=block { |
| 469 |
self.check_version( |
| 470 |
(3, 5), |
| 471 |
"Async with statements are", |
| 472 |
ast.AsyncWith(items=a, body=b, type_comment=tc, LOCATIONS) |
| 473 |
) |
| 474 |
} |
| 475 |
| invalid_with_stmt |
| 476 |
|
| 477 |
with_item[ast.withitem]: |
| 478 |
| e=expression 'as' t=star_target &(',' | ')' | ':') { |
| 479 |
ast.withitem(context_expr=e, optional_vars=t) |
| 480 |
} |
| 481 |
| invalid_with_item |
| 482 |
| e=expression { ast.withitem(context_expr=e, optional_vars=None) } |
| 483 |
|
| 484 |
# Try statement |
| 485 |
# ------------- |
| 486 |
|
| 487 |
try_stmt[ast.Try]: |
| 488 |
| invalid_try_stmt |
| 489 |
| 'try' &&':' b=block f=finally_block { |
| 490 |
ast.Try(body=b, handlers=[], orelse=None, finalbody=f, LOCATIONS) |
| 491 |
} |
| 492 |
| 'try' &&':' b=block ex=except_block+ el=[else_block] f=[finally_block] { |
| 493 |
ast.Try(body=b, handlers=ex, orelse=el, finalbody=f, LOCATIONS) |
| 494 |
} |
| 495 |
| 'try' &&':' b=block ex=except_star_block+ el=[else_block] f=[finally_block] { |
| 496 |
self.check_version((3, 11), "Exception groups are", |
| 497 |
ast.TryStar(b, ex, el, f, LOCATIONS)) } |
| 498 |
|
| 499 |
|
| 500 |
# Except statement |
| 501 |
# ---------------- |
| 502 |
|
| 503 |
except_block[ast.ExceptHandler]: |
| 504 |
| invalid_except_stmt_indent |
| 505 |
| 'except' e=expression t=['as' z=NAME { z }] ':' b=block { |
| 506 |
ast.ExceptHandler(type=e, name=self.extract_id(t), body=b, LOCATIONS) } |
| 507 |
| 'except' ':' b=block { ast.ExceptHandler(type=None, name=None, body=b, LOCATIONS) } |
| 508 |
| invalid_except_stmt |
| 509 |
except_star_block[excepthandler_ty]: |
| 510 |
| invalid_except_star_stmt_indent |
| 511 |
| 'except' '*' e=expression t=['as' z=NAME { z }] ':' b=block { |
| 512 |
ast.ExceptHandler(e, self.extract_id(t), b, LOCATIONS) } |
| 513 |
| invalid_except_stmt |
| 514 |
finally_block[list]: |
| 515 |
| invalid_finally_stmt |
| 516 |
| 'finally' &&':' a=block { a } |
| 517 |
|
| 518 |
# Match statement |
| 519 |
# --------------- |
| 520 |
|
| 521 |
# We cannot do version checks here since the production will occur after any other |
| 522 |
# production which will have failed since the ast module does not have the right nodes. |
| 523 |
match_stmt["ast.Match"]: |
| 524 |
| "match" subject=subject_expr ':' NEWLINE INDENT cases=case_block+ DEDENT { |
| 525 |
ast.Match(subject=subject, cases=cases, LOCATIONS) |
| 526 |
} |
| 527 |
| invalid_match_stmt |
| 528 |
|
| 529 |
# Version checking here allows to avoid tracking down every single possible production |
| 530 |
subject_expr: |
| 531 |
| value=star_named_expression ',' values=star_named_expressions? { |
| 532 |
self.check_version( |
| 533 |
(3, 10), |
| 534 |
"Pattern matching is", |
| 535 |
ast.Tuple(elts=[value] + (values or []), ctx=Load, LOCATIONS) |
| 536 |
) |
| 537 |
} |
| 538 |
| e=named_expression { self.check_version((3, 10), "Pattern matching is", e)} |
| 539 |
|
| 540 |
case_block["ast.match_case"]: |
| 541 |
| invalid_case_block |
| 542 |
| "case" pattern=patterns guard=guard? ':' body=block { |
| 543 |
ast.match_case(pattern=pattern, guard=guard, body=body) |
| 544 |
} |
| 545 |
|
| 546 |
guard: 'if' guard=named_expression { guard } |
| 547 |
|
| 548 |
patterns: |
| 549 |
| patterns=open_sequence_pattern { |
| 550 |
ast.MatchSequence(patterns=patterns, LOCATIONS) |
| 551 |
} |
| 552 |
| pattern |
| 553 |
|
| 554 |
pattern: |
| 555 |
| as_pattern |
| 556 |
| or_pattern |
| 557 |
|
| 558 |
as_pattern["ast.MatchAs"]: |
| 559 |
| pattern=or_pattern 'as' target=pattern_capture_target { |
| 560 |
ast.MatchAs(pattern=pattern, name=target, LOCATIONS) |
| 561 |
} |
| 562 |
| invalid_as_pattern |
| 563 |
|
| 564 |
or_pattern["ast.MatchOr"]: |
| 565 |
| patterns='|'.closed_pattern+ { |
| 566 |
ast.MatchOr(patterns=patterns, LOCATIONS) if len(patterns) > 1 else patterns[0] |
| 567 |
} |
| 568 |
|
| 569 |
closed_pattern (memo): |
| 570 |
| literal_pattern |
| 571 |
| capture_pattern |
| 572 |
| wildcard_pattern |
| 573 |
| value_pattern |
| 574 |
| group_pattern |
| 575 |
| sequence_pattern |
| 576 |
| mapping_pattern |
| 577 |
| class_pattern |
| 578 |
|
| 579 |
# Literal patterns are used for equality and identity constraints |
| 580 |
literal_pattern: |
| 581 |
| value=signed_number !('+' | '-') { ast.MatchValue(value=value, LOCATIONS) } |
| 582 |
| value=complex_number { ast.MatchValue(value=value, LOCATIONS) } |
| 583 |
| value=strings { ast.MatchValue(value=value, LOCATIONS) } |
| 584 |
| 'None' { ast.MatchSingleton(value=self.space.w_None, LOCATIONS) } |
| 585 |
| 'True' { ast.MatchSingleton(value=self.space.w_True, LOCATIONS) } |
| 586 |
| 'False' { ast.MatchSingleton(value=self.space.w_False, LOCATIONS) } |
| 587 |
|
| 588 |
# Literal expressions are used to restrict permitted mapping pattern keys |
| 589 |
literal_expr: |
| 590 |
| signed_number !('+' | '-') |
| 591 |
| complex_number |
| 592 |
| strings |
| 593 |
| 'None' { ast.Constant(self.space.w_None, None, LOCATIONS) } |
| 594 |
| 'True' { ast.Constant(self.space.w_True, None, LOCATIONS) } |
| 595 |
| 'False' { ast.Constant(self.space.w_False, None, LOCATIONS) } |
| 596 |
|
| 597 |
complex_number: |
| 598 |
| real=signed_real_number '+' imag=imaginary_number { |
| 599 |
ast.BinOp(left=real, op=ast.Add, right=imag, LOCATIONS) |
| 600 |
} |
| 601 |
| real=signed_real_number '-' imag=imaginary_number { |
| 602 |
ast.BinOp(left=real, op=ast.Sub, right=imag, LOCATIONS) |
| 603 |
} |
| 604 |
|
| 605 |
signed_number: |
| 606 |
| a=NUMBER { ast.Constant(value=self.parse_number(a), kind=None, LOCATIONS) } |
| 607 |
| '-' a=NUMBER { |
| 608 |
ast.UnaryOp( |
| 609 |
op=ast.USub, |
| 610 |
operand=ast.Constant( |
| 611 |
value=self.parse_number(a), |
| 612 |
kind=None, |
| 613 |
lineno=a.lineno, |
| 614 |
col_offset=a.column, |
| 615 |
end_lineno=a.end_lineno, |
| 616 |
end_col_offset=a.end_column |
| 617 |
), |
| 618 |
LOCATIONS, |
| 619 |
) |
| 620 |
} |
| 621 |
|
| 622 |
signed_real_number: |
| 623 |
| real_number |
| 624 |
| '-' real=real_number { ast.UnaryOp(op=ast.USub, operand=real, LOCATIONS) } |
| 625 |
|
| 626 |
real_number[ast.Constant]: |
| 627 |
| real=NUMBER { ast.Constant(value=self.ensure_real(real), kind=None, LOCATIONS) } |
| 628 |
|
| 629 |
imaginary_number[ast.Constant]: |
| 630 |
| imag=NUMBER { ast.Constant(value=self.ensure_imaginary(imag), kind=None, LOCATIONS) } |
| 631 |
|
| 632 |
capture_pattern: |
| 633 |
| target=pattern_capture_target { |
| 634 |
ast.MatchAs(pattern=None, name=target, LOCATIONS) |
| 635 |
} |
| 636 |
|
| 637 |
pattern_capture_target[str]: |
| 638 |
| !"_" name=NAME !('.' | '(' | '=') { name.id } |
| 639 |
|
| 640 |
wildcard_pattern["ast.MatchAs"]: |
| 641 |
| "_" { ast.MatchAs(pattern=None, name=None, LOCATIONS) } |
| 642 |
|
| 643 |
value_pattern["ast.MatchValue"]: |
| 644 |
| attr=attr !('.' | '(' | '=') { ast.MatchValue(value=attr, LOCATIONS) } |
| 645 |
|
| 646 |
attr[ast.Attribute]: |
| 647 |
| value=name_or_attr '.' attr=NAME { |
| 648 |
ast.Attribute(value=value, attr=attr.id, ctx=Load, LOCATIONS) |
| 649 |
} |
| 650 |
|
| 651 |
name_or_attr: |
| 652 |
| attr |
| 653 |
| NAME |
| 654 |
|
| 655 |
group_pattern: |
| 656 |
| '(' pattern=pattern ')' { pattern } |
| 657 |
|
| 658 |
sequence_pattern["ast.MatchSequence"]: |
| 659 |
| '[' patterns=maybe_sequence_pattern? ']' { ast.MatchSequence(patterns=patterns, LOCATIONS) } |
| 660 |
| '(' patterns=open_sequence_pattern? ')' { ast.MatchSequence(patterns=patterns, LOCATIONS) } |
| 661 |
|
| 662 |
open_sequence_pattern: |
| 663 |
| pattern=maybe_star_pattern ',' patterns=maybe_sequence_pattern? { |
| 664 |
[pattern] + (patterns or []) |
| 665 |
} |
| 666 |
|
| 667 |
maybe_sequence_pattern: |
| 668 |
| patterns=','.maybe_star_pattern+ ','? { patterns } |
| 669 |
|
| 670 |
maybe_star_pattern: |
| 671 |
| star_pattern |
| 672 |
| pattern |
| 673 |
|
| 674 |
star_pattern (memo): |
| 675 |
| '*' target=pattern_capture_target { ast.MatchStar(name=target, LOCATIONS) } |
| 676 |
| '*' wildcard_pattern { ast.MatchStar(name=None, LOCATIONS) } |
| 677 |
|
| 678 |
mapping_pattern: |
| 679 |
| '{' '}' { ast.MatchMapping(keys=None, patterns=None, rest=None, LOCATIONS) } |
| 680 |
| '{' rest=double_star_pattern ','? '}' { |
| 681 |
ast.MatchMapping(keys=None, patterns=None, rest=rest, LOCATIONS) } |
| 682 |
| '{' items=items_pattern ',' rest=double_star_pattern ','? '}' { |
| 683 |
ast.MatchMapping( |
| 684 |
keys=self.get_pattern_keys(items), |
| 685 |
patterns=self.get_patterns(items), |
| 686 |
rest=rest, |
| 687 |
LOCATIONS, |
| 688 |
) |
| 689 |
} |
| 690 |
| '{' items=items_pattern ','? '}' { |
| 691 |
ast.MatchMapping( |
| 692 |
keys=self.get_pattern_keys(items), |
| 693 |
patterns=self.get_patterns(items), |
| 694 |
rest=None, |
| 695 |
LOCATIONS, |
| 696 |
) |
| 697 |
} |
| 698 |
|
| 699 |
items_pattern: |
| 700 |
| ','.key_value_pattern+ |
| 701 |
|
| 702 |
key_value_pattern: |
| 703 |
| key=(literal_expr | attr) ':' pattern=pattern { self.key_pattern_pair(key, pattern) } |
| 704 |
|
| 705 |
double_star_pattern: |
| 706 |
| '**' target=pattern_capture_target { target } |
| 707 |
|
| 708 |
class_pattern["ast.MatchClass"]: |
| 709 |
| cls=name_or_attr '(' ')' { |
| 710 |
ast.MatchClass(cls=cls, patterns=None, kwd_attrs=None, kwd_patterns=None, LOCATIONS) |
| 711 |
} |
| 712 |
| cls=name_or_attr '(' patterns=positional_patterns ','? ')' { |
| 713 |
ast.MatchClass(cls=cls, patterns=patterns, kwd_attrs=None, kwd_patterns=None, LOCATIONS) |
| 714 |
} |
| 715 |
| cls=name_or_attr '(' keywords=keyword_patterns ','? ')' { |
| 716 |
ast.MatchClass( |
| 717 |
cls=cls, |
| 718 |
patterns=None, |
| 719 |
kwd_attrs=self.get_pattern_names(keywords), |
| 720 |
kwd_patterns=self.get_patterns(keywords), |
| 721 |
LOCATIONS, |
| 722 |
) |
| 723 |
} |
| 724 |
| cls=name_or_attr '(' patterns=positional_patterns ',' keywords=keyword_patterns ','? ')' { |
| 725 |
ast.MatchClass( |
| 726 |
cls=cls, |
| 727 |
patterns=patterns, |
| 728 |
kwd_attrs=self.get_pattern_names(keywords), |
| 729 |
kwd_patterns=self.get_patterns(keywords), |
| 730 |
LOCATIONS, |
| 731 |
) |
| 732 |
} |
| 733 |
| invalid_class_pattern |
| 734 |
|
| 735 |
positional_patterns: |
| 736 |
| args=','.pattern+ { args } |
| 737 |
|
| 738 |
keyword_patterns: |
| 739 |
| ','.keyword_pattern+ |
| 740 |
|
| 741 |
keyword_pattern: |
| 742 |
| arg=NAME '=' value=pattern { self.key_pattern_pair(arg, value) } |
| 743 |
|
| 744 |
# EXPRESSIONS |
| 745 |
# ----------- |
| 746 |
|
| 747 |
star_expressions: |
| 748 |
| a=star_expression b=(',' c=star_expression { c })+ [','] { |
| 749 |
ast.Tuple(elts=[a] + b, ctx=Load, LOCATIONS) } |
| 750 |
| a=star_expression ',' { ast.Tuple(elts=[a], ctx=Load, LOCATIONS) } |
| 751 |
| star_expression |
| 752 |
|
| 753 |
star_expression (memo): |
| 754 |
| '*' a=bitwise_or { ast.Starred(value=a, ctx=Load, LOCATIONS) } |
| 755 |
| expression |
| 756 |
|
| 757 |
star_named_expressions: a=','.star_named_expression+ [','] { a } |
| 758 |
|
| 759 |
star_named_expression: |
| 760 |
| '*' a=bitwise_or { ast.Starred(value=a, ctx=Load, LOCATIONS) } |
| 761 |
| named_expression |
| 762 |
assignment_expression: |
| 763 |
| a=NAME ':=' ~ b=expression { |
| 764 |
self.check_version( |
| 765 |
(3, 8), |
| 766 |
"The ':=' operator is", |
| 767 |
ast.NamedExpr( |
| 768 |
target=self.set_expr_context(a, Store), |
| 769 |
value=b, |
| 770 |
LOCATIONS, |
| 771 |
) |
| 772 |
) |
| 773 |
} |
| 774 |
|
| 775 |
named_expression: |
| 776 |
| assignment_expression |
| 777 |
| invalid_named_expression |
| 778 |
| a=expression !':=' { a } |
| 779 |
|
| 780 |
expressions: |
| 781 |
| a=expression b=(',' c=expression { c })+ [','] { |
| 782 |
ast.Tuple(elts=[a] + b, ctx=Load, LOCATIONS) } |
| 783 |
| a=expression ',' { ast.Tuple(elts=[a], ctx=Load, LOCATIONS) } |
| 784 |
| expression |
| 785 |
|
| 786 |
expression (memo): |
| 787 |
| invalid_expression |
| 788 |
| invalid_legacy_expression |
| 789 |
| a=disjunction 'if' b=disjunction 'else' c=expression { |
| 790 |
ast.IfExp(body=a, test=b, orelse=c, LOCATIONS) |
| 791 |
} |
| 792 |
| disjunction |
| 793 |
| lambdef |
| 794 |
|
| 795 |
yield_expr: |
| 796 |
| 'yield' 'from' a=expression { ast.YieldFrom(value=a, LOCATIONS) } |
| 797 |
| 'yield' a=[star_expressions] { ast.Yield(value=a, LOCATIONS) } |
| 798 |
|
| 799 |
disjunction (memo): |
| 800 |
| a=conjunction b=('or' c=conjunction { c })+ { ast.BoolOp(op=ast.Or, values=[a] + b, LOCATIONS) } |
| 801 |
| conjunction |
| 802 |
|
| 803 |
conjunction (memo): |
| 804 |
| a=inversion b=('and' c=inversion { c })+ { ast.BoolOp(op=ast.And, values=[a] + b, LOCATIONS) } |
| 805 |
| inversion |
| 806 |
|
| 807 |
inversion (memo): |
| 808 |
| 'not' a=inversion { ast.UnaryOp(op=ast.Not, operand=a, LOCATIONS) } |
| 809 |
| comparison |
| 810 |
|
| 811 |
# Comparisons operators |
| 812 |
# --------------------- |
| 813 |
|
| 814 |
comparison: |
| 815 |
| a=bitwise_or b=compare_op_bitwise_or_pair+ { |
| 816 |
ast.Compare(left=a, ops=self.get_comparison_ops(b), comparators=self.get_comparators(b), LOCATIONS) |
| 817 |
} |
| 818 |
| bitwise_or |
| 819 |
|
| 820 |
# Make a tuple of operator and comparator |
| 821 |
compare_op_bitwise_or_pair: |
| 822 |
| eq_bitwise_or |
| 823 |
| noteq_bitwise_or |
| 824 |
| lte_bitwise_or |
| 825 |
| lt_bitwise_or |
| 826 |
| gte_bitwise_or |
| 827 |
| gt_bitwise_or |
| 828 |
| notin_bitwise_or |
| 829 |
| in_bitwise_or |
| 830 |
| isnot_bitwise_or |
| 831 |
| is_bitwise_or |
| 832 |
|
| 833 |
eq_bitwise_or: '==' a=bitwise_or { self.cmpop_expr_pair(ast.Eq, a) } |
| 834 |
noteq_bitwise_or: |
| 835 |
| tok='!=' a=bitwise_or { self.check_barry(tok) and self.cmpop_expr_pair(ast.NotEq, a) } |
| 836 |
lte_bitwise_or: '<=' a=bitwise_or { self.cmpop_expr_pair(ast.LtE, a) } |
| 837 |
lt_bitwise_or: '<' a=bitwise_or { self.cmpop_expr_pair(ast.Lt, a) } |
| 838 |
gte_bitwise_or: '>=' a=bitwise_or { self.cmpop_expr_pair(ast.GtE, a) } |
| 839 |
gt_bitwise_or: '>' a=bitwise_or { self.cmpop_expr_pair(ast.Gt, a) } |
| 840 |
notin_bitwise_or: 'not' 'in' a=bitwise_or { self.cmpop_expr_pair(ast.NotIn, a) } |
| 841 |
in_bitwise_or: 'in' a=bitwise_or { self.cmpop_expr_pair(ast.In, a) } |
| 842 |
isnot_bitwise_or: 'is' 'not' a=bitwise_or { self.cmpop_expr_pair(ast.IsNot, a) } |
| 843 |
is_bitwise_or: 'is' a=bitwise_or { self.cmpop_expr_pair(ast.Is, a) } |
| 844 |
|
| 845 |
# Bitwise operators |
| 846 |
# ----------------- |
| 847 |
|
| 848 |
bitwise_or: |
| 849 |
| a=bitwise_or '|' b=bitwise_xor { ast.BinOp(left=a, op=ast.BitOr, right=b, LOCATIONS) } |
| 850 |
| bitwise_xor |
| 851 |
|
| 852 |
bitwise_xor: |
| 853 |
| a=bitwise_xor '^' b=bitwise_and { ast.BinOp(left=a, op=ast.BitXor, right=b, LOCATIONS) } |
| 854 |
| bitwise_and |
| 855 |
|
| 856 |
bitwise_and: |
| 857 |
| a=bitwise_and '&' b=shift_expr { ast.BinOp(left=a, op=ast.BitAnd, right=b, LOCATIONS) } |
| 858 |
| shift_expr |
| 859 |
|
| 860 |
shift_expr: |
| 861 |
| a=shift_expr '<<' b=sum { ast.BinOp(left=a, op=ast.LShift, right=b, LOCATIONS) } |
| 862 |
| a=shift_expr '>>' b=sum { ast.BinOp(left=a, op=ast.RShift, right=b, LOCATIONS) } |
| 863 |
| sum |
| 864 |
|
| 865 |
# Arithmetic operators |
| 866 |
# -------------------- |
| 867 |
|
| 868 |
sum: |
| 869 |
| a=sum '+' b=term { ast.BinOp(left=a, op=ast.Add, right=b, LOCATIONS) } |
| 870 |
| a=sum '-' b=term { ast.BinOp(left=a, op=ast.Sub, right=b, LOCATIONS) } |
| 871 |
| term |
| 872 |
|
| 873 |
term: |
| 874 |
| a=term '*' b=factor { ast.BinOp(left=a, op=ast.Mult, right=b, LOCATIONS) } |
| 875 |
| a=term '/' b=factor { ast.BinOp(left=a, op=ast.Div, right=b, LOCATIONS) } |
| 876 |
| a=term '//' b=factor { ast.BinOp(left=a, op=ast.FloorDiv, right=b, LOCATIONS) } |
| 877 |
| a=term '%' b=factor { ast.BinOp(left=a, op=ast.Mod, right=b, LOCATIONS) } |
| 878 |
| a=term '@' b=factor { |
| 879 |
self.check_version((3, 5), "The '@' operator is", ast.BinOp(left=a, op=ast.MatMult, right=b, LOCATIONS)) |
| 880 |
} |
| 881 |
| factor |
| 882 |
|
| 883 |
factor (memo): |
| 884 |
| '+' a=factor { ast.UnaryOp(op=ast.UAdd, operand=a, LOCATIONS) } |
| 885 |
| '-' a=factor { ast.UnaryOp(op=ast.USub, operand=a, LOCATIONS) } |
| 886 |
| '~' a=factor { ast.UnaryOp(op=ast.Invert, operand=a, LOCATIONS) } |
| 887 |
| power |
| 888 |
|
| 889 |
power: |
| 890 |
| a=await_primary '**' b=factor { ast.BinOp(left=a, op=ast.Pow, right=b, LOCATIONS) } |
| 891 |
| await_primary |
| 892 |
|
| 893 |
# Primary elements |
| 894 |
# ---------------- |
| 895 |
|
| 896 |
# Primary elements are things like "obj.something.something", "obj[something]", "obj(something)", "obj" ... |
| 897 |
|
| 898 |
await_primary (memo): |
| 899 |
| AWAIT a=primary { self.check_version((3, 5), "Await expressions are", ast.Await(a, LOCATIONS)) } |
| 900 |
| primary |
| 901 |
|
| 902 |
primary: |
| 903 |
| a=primary '.' b=NAME { ast.Attribute(value=a, attr=b.id, ctx=Load, LOCATIONS) } |
| 904 |
| a=primary b=genexp { ast.Call(func=a, args=[b], keywords=None, LOCATIONS) } |
| 905 |
| a=primary '(' b=[arguments] ')' { |
| 906 |
ast.Call( |
| 907 |
func=a, |
| 908 |
args=b.args if b and b.args else None, |
| 909 |
keywords=b.keywords if b and b.keywords else None, |
| 910 |
LOCATIONS, |
| 911 |
) |
| 912 |
} |
| 913 |
| a=primary '[' b=slices ']' { ast.Subscript(value=a, slice=b, ctx=Load, LOCATIONS) } |
| 914 |
| atom |
| 915 |
|
| 916 |
slices: |
| 917 |
| a=slice !',' { a } |
| 918 |
| a=','.(slice | starred_expression)+ [','] { |
| 919 |
ast.Tuple(elts=a, ctx=Load, LOCATIONS) |
| 920 |
} |
| 921 |
|
| 922 |
slice: |
| 923 |
| a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { |
| 924 |
ast.Slice(lower=a, upper=b, step=c, LOCATIONS) |
| 925 |
} |
| 926 |
| a=named_expression { |
| 927 |
a |
| 928 |
} |
| 929 |
|
| 930 |
atom: |
| 931 |
| NAME |
| 932 |
| 'True' { |
| 933 |
ast.Constant(value=self.space.w_True, kind=None, LOCATIONS) |
| 934 |
} |
| 935 |
| 'False' { |
| 936 |
ast.Constant(value=self.space.w_False, kind=None, LOCATIONS) |
| 937 |
} |
| 938 |
| 'None' { |
| 939 |
ast.Constant(value=self.space.w_None, kind=None, LOCATIONS) |
| 940 |
} |
| 941 |
| &STRING strings |
| 942 |
| a=NUMBER { |
| 943 |
ast.Constant(value=self.parse_number(a), kind=None, LOCATIONS) |
| 944 |
} |
| 945 |
| a='$NUM' { |
| 946 |
self.revdbmetavar(int(tok.value[1:]), LOCATIONS) |
| 947 |
} |
| 948 |
| &'(' (tuple | group | genexp) |
| 949 |
| &'[' (list | listcomp) |
| 950 |
| &'{' (dict | set | dictcomp | setcomp) |
| 951 |
| '...' { |
| 952 |
ast.Constant(value=self.space.w_Ellipsis, kind=None, LOCATIONS) |
| 953 |
} |
| 954 |
|
| 955 |
group: |
| 956 |
| '(' a=(yield_expr | named_expression) ')' { a } |
| 957 |
| invalid_group |
| 958 |
|
| 959 |
|
| 960 |
# Lambda functions |
| 961 |
# ---------------- |
| 962 |
|
| 963 |
lambdef: |
| 964 |
| 'lambda' a=[lambda_params] ':' b=expression { |
| 965 |
ast.Lambda(args=a or self.make_arguments(None, None, None, [], None), body=b, LOCATIONS) |
| 966 |
} |
| 967 |
|
| 968 |
lambda_params: |
| 969 |
| invalid_lambda_parameters |
| 970 |
| lambda_parameters |
| 971 |
|
| 972 |
# lambda_parameters etc. duplicates parameters but without annotations |
| 973 |
# or type comments, and if there's no comma after a parameter, we expect |
| 974 |
# a colon, not a close parenthesis. (For more, see parameters above.) |
| 975 |
# |
| 976 |
lambda_parameters[ast.arguments]: |
| 977 |
| a=lambda_slash_no_default b=lambda_param_no_default* c=lambda_param_with_default* d=[lambda_star_etc] { |
| 978 |
self.make_arguments(a.plain_names, None, b, c, d) |
| 979 |
} |
| 980 |
| a=lambda_slash_with_default b=lambda_param_with_default* c=[lambda_star_etc] { |
| 981 |
self.make_arguments(None, a, None, b, c) |
| 982 |
} |
| 983 |
| a=lambda_param_no_default+ b=lambda_param_with_default* c=[lambda_star_etc] { |
| 984 |
self.make_arguments(None, None, a, b, c) |
| 985 |
} |
| 986 |
| a=lambda_param_with_default+ b=[lambda_star_etc] { |
| 987 |
self.make_arguments(None, None, None, a, b) |
| 988 |
} |
| 989 |
| a=lambda_star_etc { self.make_arguments(None, None, None, [], a) } |
| 990 |
|
| 991 |
lambda_slash_no_default: |
| 992 |
| a=lambda_param_no_default+ '/' ',' { self.make_slash_with_default(a, None) } |
| 993 |
| a=lambda_param_no_default+ '/' &':' { self.make_slash_with_default(a, None) } |
| 994 |
|
| 995 |
lambda_slash_with_default: |
| 996 |
| a=lambda_param_no_default* b=lambda_param_with_default+ '/' ',' { self.make_slash_with_default(a, b) } |
| 997 |
| a=lambda_param_no_default* b=lambda_param_with_default+ '/' &':' { self.make_slash_with_default(a, b) } |
| 998 |
|
| 999 |
lambda_star_etc[StarEtc]: |
| 1000 |
| invalid_lambda_star_etc |
| 1001 |
| '*' a=lambda_param_no_default b=lambda_param_maybe_default* c=[lambda_kwds] { |
| 1002 |
self.make_star_etc(a, b, c) } |
| 1003 |
| '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] { |
| 1004 |
self.make_star_etc(None, b, c) } |
| 1005 |
| a=lambda_kwds { self.make_star_etc(None, None, a) } |
| 1006 |
|
| 1007 |
lambda_kwds[ast.arg]: |
| 1008 |
| invalid_lambda_kwds |
| 1009 |
| '**' a=lambda_param_no_default { a } |
| 1010 |
|
| 1011 |
lambda_param_no_default[ast.arg]: |
| 1012 |
| a=lambda_param ',' { a } |
| 1013 |
| a=lambda_param &':' { a } |
| 1014 |
|
| 1015 |
lambda_param_with_default[NameDefaultPair]: |
| 1016 |
| a=lambda_param c=default ',' { self.name_default_pair(a, c, None) } |
| 1017 |
| a=lambda_param c=default &':' { self.name_default_pair(a, c, None) } |
| 1018 |
lambda_param_maybe_default[NameDefaultPair]: |
| 1019 |
| a=lambda_param c=default? ',' { self.name_default_pair(a, c, None) } |
| 1020 |
| a=lambda_param c=default? &':' { self.name_default_pair(a, c, None) } |
| 1021 |
lambda_param[ast.arg]: a=NAME { |
| 1022 |
ast.arg(arg=self.extract_id(a), annotation=None, type_comment=None, LOCATIONS) |
| 1023 |
} |
| 1024 |
|
| 1025 |
# LITERALS |
| 1026 |
# ======== |
| 1027 |
|
| 1028 |
strings[ast.Str] (memo): a=STRING+ { self.generate_ast_for_string(a) } |
| 1029 |
|
| 1030 |
list[ast.List]: |
| 1031 |
| '[' a=[star_named_expressions] ']' { ast.List(elts=a, ctx=Load, LOCATIONS) } |
| 1032 |
|
| 1033 |
tuple[ast.Tuple]: |
| 1034 |
| '(' a=[y=star_named_expression ',' z=[star_named_expressions] { [y] + (z or []) } ] ')' { |
| 1035 |
ast.Tuple(elts=a, ctx=Load, LOCATIONS) |
| 1036 |
} |
| 1037 |
|
| 1038 |
set[ast.Set]: '{' a=star_named_expressions '}' { ast.Set(elts=a, LOCATIONS) } |
| 1039 |
|
| 1040 |
# Dicts |
| 1041 |
# ----- |
| 1042 |
|
| 1043 |
dict[ast.Dict]: |
| 1044 |
| '{' a=[double_starred_kvpairs] '}' { |
| 1045 |
ast.Dict(keys=[kv.key for kv in a] if a else None, values=[kv.value for kv in a] if a else None, LOCATIONS) |
| 1046 |
} |
| 1047 |
| '{' invalid_double_starred_kvpairs '}' |
| 1048 |
|
| 1049 |
double_starred_kvpairs[list]: a=','.double_starred_kvpair+ [','] { a } |
| 1050 |
|
| 1051 |
double_starred_kvpair: |
| 1052 |
| '**' a=bitwise_or { self.dict_display_entry(None, a) } |
| 1053 |
| kvpair |
| 1054 |
|
| 1055 |
kvpair: a=expression ':' b=expression { self.dict_display_entry(a, b) } |
| 1056 |
|
| 1057 |
# Comprehensions & Generators |
| 1058 |
# --------------------------- |
| 1059 |
|
| 1060 |
for_if_clauses[List[ast.comprehension]]: |
| 1061 |
| a=for_if_clause+ { a } |
| 1062 |
|
| 1063 |
for_if_clause[ast.comprehension]: |
| 1064 |
| x=ASYNC 'for' a=star_targets 'in' ~ b=disjunction c=('if' z=disjunction { z })* { |
| 1065 |
self.check_version( |
| 1066 |
(3, 6), |
| 1067 |
"Async comprehensions are", |
| 1068 |
x |
| 1069 |
) and ast.comprehension(target=a, iter=b, ifs=c if c else None, is_async=True) |
| 1070 |
} |
| 1071 |
| 'for' a=star_targets 'in' ~ b=disjunction c=('if' z=disjunction { z })* { |
| 1072 |
ast.comprehension(target=a, iter=b, ifs=c if c else None, is_async=False) } |
| 1073 |
| invalid_for_target |
| 1074 |
|
| 1075 |
listcomp[ast.ListComp]: |
| 1076 |
| '[' a=named_expression b=for_if_clauses ']' { ast.ListComp(elt=a, generators=b, LOCATIONS) } |
| 1077 |
| invalid_comprehension |
| 1078 |
|
| 1079 |
setcomp[ast.SetComp]: |
| 1080 |
| '{' a=named_expression b=for_if_clauses '}' { ast.SetComp(elt=a, generators=b, LOCATIONS) } |
| 1081 |
| invalid_comprehension |
| 1082 |
|
| 1083 |
genexp[ast.GeneratorExp]: |
| 1084 |
| '(' a=( assignment_expression | expression !':=') b=for_if_clauses ')' { |
| 1085 |
ast.GeneratorExp(elt=a, generators=b, LOCATIONS) |
| 1086 |
} |
| 1087 |
| invalid_comprehension |
| 1088 |
|
| 1089 |
dictcomp[ast.DictComp]: |
| 1090 |
| '{' a=kvpair b=for_if_clauses '}' { ast.DictComp(key=a.key, value=a.value, generators=b, LOCATIONS) } |
| 1091 |
| invalid_dict_comprehension |
| 1092 |
|
| 1093 |
# FUNCTION CALL ARGUMENTS |
| 1094 |
# ======================= |
| 1095 |
|
| 1096 |
arguments[ast.Call] (memo): |
| 1097 |
| a=args [','] &')' { a } |
| 1098 |
| invalid_arguments |
| 1099 |
|
| 1100 |
args[ast.Call]: |
| 1101 |
| a=','.(starred_expression | ( assignment_expression | expression !':=') !'=')+ b=[',' k=kwargs {k}] { |
| 1102 |
ast.Call( |
| 1103 |
func=self.dummy_name(), |
| 1104 |
args=a + ([e for e in b if isinstance(e, ast.Starred)] if b else []), |
| 1105 |
keywords=([e for e in b if isinstance(e, ast.keyword)] if b else []), |
| 1106 |
LOCATIONS |
| 1107 |
) |
| 1108 |
} |
| 1109 |
| a=kwargs { |
| 1110 |
ast.Call( |
| 1111 |
func=self.dummy_name(), |
| 1112 |
args=[e for e in a if isinstance(e, ast.Starred)], |
| 1113 |
keywords=[e for e in a if isinstance(e, ast.keyword)], |
| 1114 |
LOCATIONS |
| 1115 |
) |
| 1116 |
} |
| 1117 |
|
| 1118 |
kwargs[list]: |
| 1119 |
| a=','.kwarg_or_starred+ ',' b=','.kwarg_or_double_starred+ { a + b } |
| 1120 |
| ','.kwarg_or_starred+ |
| 1121 |
| ','.kwarg_or_double_starred+ |
| 1122 |
|
| 1123 |
starred_expression: |
| 1124 |
| '*' a=expression { ast.Starred(value=a, ctx=Load, LOCATIONS) } |
| 1125 |
| '*' { self.raise_syntax_error("Invalid star expression") } |
| 1126 |
|
| 1127 |
kwarg_or_starred: |
| 1128 |
| invalid_kwarg |
| 1129 |
| a=NAME '=' b=expression { ast.keyword(arg=self.extract_id(a), value=b, LOCATIONS) } |
| 1130 |
| a=starred_expression { a } |
| 1131 |
|
| 1132 |
kwarg_or_double_starred: |
| 1133 |
| invalid_kwarg |
| 1134 |
| a=NAME '=' b=expression { ast.keyword(arg=self.extract_id(a), value=b, LOCATIONS) } |
| 1135 |
| '**' a=expression { ast.keyword(arg=None, value=a, LOCATIONS) } |
| 1136 |
|
| 1137 |
# ASSIGNMENT TARGETS |
| 1138 |
# ================== |
| 1139 |
|
| 1140 |
# Generic targets |
| 1141 |
# --------------- |
| 1142 |
|
| 1143 |
# NOTE: star_targets may contain *bitwise_or, targets may not. |
| 1144 |
star_targets: |
| 1145 |
| a=star_target !',' { a } |
| 1146 |
| a=star_target b=(',' c=star_target { c })* [','] { |
| 1147 |
ast.Tuple(elts=[a] + b, ctx=Store, LOCATIONS) |
| 1148 |
} |
| 1149 |
|
| 1150 |
star_targets_list_seq[list]: a=','.star_target+ [','] { a } |
| 1151 |
|
| 1152 |
star_targets_tuple_seq[list]: |
| 1153 |
| a=star_target b=(',' c=star_target { c })+ [','] { [a] + b } |
| 1154 |
| a=star_target ',' { [a] } |
| 1155 |
|
| 1156 |
star_target (memo): |
| 1157 |
| '*' a=(!'*' star_target) { |
| 1158 |
ast.Starred(value=self.set_expr_context(a, Store), ctx=Store, LOCATIONS) |
| 1159 |
} |
| 1160 |
| target_with_star_atom |
| 1161 |
|
| 1162 |
target_with_star_atom (memo): |
| 1163 |
| a=t_primary '.' b=NAME !t_lookahead { ast.Attribute(value=a, attr=b.id, ctx=Store, LOCATIONS) } |
| 1164 |
| a=t_primary '[' b=slices ']' !t_lookahead { ast.Subscript(value=a, slice=b, ctx=Store, LOCATIONS) } |
| 1165 |
| star_atom |
| 1166 |
|
| 1167 |
star_atom: |
| 1168 |
| a=NAME { self.set_expr_context(a, Store) } |
| 1169 |
| '(' a=target_with_star_atom ')' { self.set_expr_context(a, Store) } |
| 1170 |
| '(' a=[star_targets_tuple_seq] ')' { ast.Tuple(elts=a, ctx=Store, LOCATIONS) } |
| 1171 |
| '[' a=[star_targets_list_seq] ']' { ast.List(elts=a, ctx=Store, LOCATIONS) } |
| 1172 |
|
| 1173 |
single_target: |
| 1174 |
| single_subscript_attribute_target |
| 1175 |
| a=NAME { self.set_expr_context(a, Store) } |
| 1176 |
| '(' a=single_target ')' { a } |
| 1177 |
|
| 1178 |
single_subscript_attribute_target: |
| 1179 |
| a=t_primary '.' b=NAME !t_lookahead { ast.Attribute(value=a, attr=b.id, ctx=Store, LOCATIONS) } |
| 1180 |
| a=t_primary '[' b=slices ']' !t_lookahead { ast.Subscript(value=a, slice=b, ctx=Store, LOCATIONS) } |
| 1181 |
|
| 1182 |
|
| 1183 |
t_primary: |
| 1184 |
| a=t_primary '.' b=NAME &t_lookahead { ast.Attribute(value=a, attr=b.id, ctx=Load, LOCATIONS) } |
| 1185 |
| a=t_primary '[' b=slices ']' &t_lookahead { ast.Subscript(value=a, slice=b, ctx=Load, LOCATIONS) } |
| 1186 |
| a=t_primary b=genexp &t_lookahead { ast.Call(func=a, args=[b], keywords=None, LOCATIONS) } |
| 1187 |
| a=t_primary '(' b=[arguments] ')' &t_lookahead { |
| 1188 |
ast.Call( |
| 1189 |
func=a, |
| 1190 |
args=b.args if b else None, |
| 1191 |
keywords=b.keywords if b and b.keywords else None, |
| 1192 |
LOCATIONS, |
| 1193 |
) |
| 1194 |
} |
| 1195 |
| a=atom &t_lookahead { a } |
| 1196 |
|
| 1197 |
t_lookahead: '(' | '[' | '.' |
| 1198 |
|
| 1199 |
# Targets for del statements |
| 1200 |
# -------------------------- |
| 1201 |
|
| 1202 |
del_targets: a=','.del_target+ [','] { a } |
| 1203 |
|
| 1204 |
del_target (memo): |
| 1205 |
| a=t_primary '.' b=NAME !t_lookahead { ast.Attribute(value=a, attr=b.id, ctx=Del, LOCATIONS) } |
| 1206 |
| a=t_primary '[' b=slices ']' !t_lookahead { ast.Subscript(value=a, slice=b, ctx=Del, LOCATIONS) } |
| 1207 |
| del_t_atom |
| 1208 |
|
| 1209 |
del_t_atom: |
| 1210 |
| a=NAME { self.set_expr_context(a, Del) } |
| 1211 |
| '(' a=del_target ')' { self.set_expr_context(a, Del) } |
| 1212 |
| '(' a=[del_targets] ')' { ast.Tuple(elts=a, ctx=Del, LOCATIONS) } |
| 1213 |
| '[' a=[del_targets] ']' { ast.List(elts=a, ctx=Del, LOCATIONS) } |
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
# ========================= END OF THE GRAMMAR =========================== |
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
# ========================= START OF INVALID RULES ======================= |
| 1224 |
|
| 1225 |
# From here on, there are rules for invalid syntax with specialised error messages |
| 1226 |
invalid_arguments[NoReturn]: |
| 1227 |
# pypy change: split this rule into two for typing reasons |
| 1228 |
| ','.(starred_expression | ( assignment_expression | expression !':=') !'=')+ ',' kwargs a=',' ','.(starred_expression !'=')+ { |
| 1229 |
self.raise_syntax_error_starting_from( |
| 1230 |
"iterable argument unpacking follows keyword argument unpacking", |
| 1231 |
a |
| 1232 |
) |
| 1233 |
} |
| 1234 |
| kwargs a=',' ','.(starred_expression !'=')+ { |
| 1235 |
self.raise_syntax_error_starting_from( |
| 1236 |
"iterable argument unpacking follows keyword argument unpacking", |
| 1237 |
a |
| 1238 |
) |
| 1239 |
} |
| 1240 |
| a=expression b=for_if_clauses ',' [args | expression for_if_clauses] { |
| 1241 |
self.raise_syntax_error_known_range( |
| 1242 |
"Generator expression must be parenthesized", a, self.get_last_comprehension_item(self.get_last(b)) |
| 1243 |
) |
| 1244 |
} |
| 1245 |
| a=NAME b='=' expression for_if_clauses { |
| 1246 |
self.raise_syntax_error_known_range( |
| 1247 |
"invalid syntax. Maybe you meant '==' or ':=' instead of '='?", a, b |
| 1248 |
) |
| 1249 |
} |
| 1250 |
| a=args b=for_if_clauses { |
| 1251 |
self.check_nonparen_genexp_in_call( |
| 1252 |
"Generator expression must be parenthesized", |
| 1253 |
a, b) |
| 1254 |
} |
| 1255 |
| args ',' a=expression b=for_if_clauses { |
| 1256 |
self.raise_syntax_error_known_range( |
| 1257 |
"Generator expression must be parenthesized", |
| 1258 |
a, |
| 1259 |
self.get_last_comprehension_item(self.get_last(b)), |
| 1260 |
) |
| 1261 |
} |
| 1262 |
| a=args ',' args { |
| 1263 |
self.raise_syntax_error( # XXX looks wrong? |
| 1264 |
"positional argument follows keyword argument unpacking" |
| 1265 |
if self.check_last_keyword_no_arg(a) else |
| 1266 |
"positional argument follows keyword argument", |
| 1267 |
) |
| 1268 |
} |
| 1269 |
invalid_kwarg[NoReturn]: |
| 1270 |
| a=NAME b='=' expression for_if_clauses { |
| 1271 |
self.raise_syntax_error_known_range( |
| 1272 |
"invalid syntax. Maybe you meant '==' or ':=' instead of '='?", a, b |
| 1273 |
) |
| 1274 |
} |
| 1275 |
| !(NAME '=') a=expression b='=' { |
| 1276 |
self.kwarg_illegal_assignment(a, b) |
| 1277 |
} |
| 1278 |
|
| 1279 |
expression_without_invalid[ast.AST]: |
| 1280 |
| a=disjunction 'if' b=disjunction 'else' c=expression { ast.IfExp(body=b, test=a, orelse=c, LOCATIONS) } |
| 1281 |
| disjunction |
| 1282 |
| lambdef |
| 1283 |
invalid_legacy_expression: |
| 1284 |
| a=NAME !'(' b=star_expressions { |
| 1285 |
self.raise_syntax_error_known_range( |
| 1286 |
"Missing parentheses in call to '%s'. Did you mean %s(...)?" % (a.id, a.id), a, b, |
| 1287 |
) if self.check_legacy_stmt(a) else |
| 1288 |
None |
| 1289 |
} |
| 1290 |
invalid_expression[NoReturn]: |
| 1291 |
# !(NAME STRING) is not matched so we don't show this error with some invalid string prefixes like: kf"dsfsdf" |
| 1292 |
# Soft keywords need to also be ignored because they can be parsed as NAME NAME |
| 1293 |
| !(NAME STRING | SOFT_KEYWORD) a=disjunction b=expression_without_invalid { |
| 1294 |
(None if self.check_legacy_stmt(a) or self._tokens[mark - 1].level == 0 else |
| 1295 |
self.raise_syntax_error_known_range("invalid syntax. Perhaps you forgot a comma?", a, b)) |
| 1296 |
} |
| 1297 |
| a=disjunction 'if' b=disjunction !('else'|':') { |
| 1298 |
self.raise_syntax_error_known_range("expected 'else' after 'if' expression", a, b) |
| 1299 |
} |
| 1300 |
invalid_named_expression[NoReturn]: |
| 1301 |
| a=expression ':=' expression { |
| 1302 |
self.raise_syntax_error_known_location( |
| 1303 |
"cannot use assignment expressions with %s" % (self.get_expr_name(a), ), a |
| 1304 |
) |
| 1305 |
} |
| 1306 |
# Use in_raw_rule |
| 1307 |
| a=NAME '=' b=bitwise_or !('='|':=') { |
| 1308 |
( |
| 1309 |
None |
| 1310 |
if self.in_recursive_rule else |
| 1311 |
self.raise_syntax_error_known_range( |
| 1312 |
"invalid syntax. Maybe you meant '==' or ':=' instead of '='?", a, b |
| 1313 |
) |
| 1314 |
) |
| 1315 |
} |
| 1316 |
| !(list|tuple|genexp|'True'|'None'|'False') a=bitwise_or b='=' bitwise_or !('='|':=') { |
| 1317 |
( |
| 1318 |
None |
| 1319 |
if self.in_recursive_rule else |
| 1320 |
self.raise_syntax_error_known_location( |
| 1321 |
"cannot assign to %s here. Maybe you meant '==' instead of '='?" % (self.get_expr_name(a), ), a |
| 1322 |
) |
| 1323 |
) |
| 1324 |
} |
| 1325 |
|
| 1326 |
invalid_assignment[NoReturn]: |
| 1327 |
| a=invalid_ann_assign_target ':' expression { |
| 1328 |
self.raise_syntax_error_known_location( |
| 1329 |
"only single target (not %s) can be annotated" % (self.get_expr_name(a), ), a |
| 1330 |
) |
| 1331 |
} |
| 1332 |
| a=star_named_expression ',' star_named_expressions* ':' expression { |
| 1333 |
self.raise_syntax_error_known_location("only single target (not tuple) can be annotated", a) } |
| 1334 |
| a=expression ':' expression { |
| 1335 |
self.raise_syntax_error_known_location("illegal target for annotation", a) } |
| 1336 |
| (star_targets '=')* a=star_expressions '=' { |
| 1337 |
self.raise_syntax_error_if_invalid_target(a, "assign") |
| 1338 |
} |
| 1339 |
| (star_targets '=')* a=yield_expr '=' { |
| 1340 |
self.raise_syntax_error_known_location("assignment to yield expression not possible", a) |
| 1341 |
} |
| 1342 |
| a=star_expressions augassign (yield_expr | star_expressions) { |
| 1343 |
self.raise_syntax_error_known_location( |
| 1344 |
"'%s' is an illegal expression for augmented assignment" % (self.get_expr_name(a), ), a |
| 1345 |
) |
| 1346 |
} |
| 1347 |
invalid_ann_assign_target[ast.AST]: |
| 1348 |
| a=list { a } |
| 1349 |
| a=tuple { a } |
| 1350 |
| '(' a=invalid_ann_assign_target ')' { a } |
| 1351 |
invalid_del_stmt[NoReturn]: |
| 1352 |
| 'del' a=star_expressions { |
| 1353 |
self.raise_syntax_error_if_invalid_target(a, "delete") |
| 1354 |
} |
| 1355 |
invalid_block[NoReturn]: |
| 1356 |
| NEWLINE !INDENT { self.raise_indentation_error("expected an indented block") } |
| 1357 |
invalid_comprehension[NoReturn]: |
| 1358 |
| ('[' | '(' | '{') a=starred_expression for_if_clauses { |
| 1359 |
self.raise_syntax_error_known_location("iterable unpacking cannot be used in comprehension", a) |
| 1360 |
} |
| 1361 |
| ('[' | '{') a=star_named_expression ',' b=star_named_expressions for_if_clauses { |
| 1362 |
self.raise_syntax_error_known_range( |
| 1363 |
"did you forget parentheses around the comprehension target?", a, b[-1] |
| 1364 |
) |
| 1365 |
} |
| 1366 |
| ('[' | '{') a=star_named_expression b=',' for_if_clauses { |
| 1367 |
self.raise_syntax_error_known_range( |
| 1368 |
"did you forget parentheses around the comprehension target?", a, b |
| 1369 |
) |
| 1370 |
} |
| 1371 |
invalid_dict_comprehension[NoReturn]: |
| 1372 |
| '{' a='**' bitwise_or for_if_clauses '}' { |
| 1373 |
self.raise_syntax_error_known_location("dict unpacking cannot be used in dict comprehension", a) |
| 1374 |
} |
| 1375 |
invalid_parameters[NoReturn]: |
| 1376 |
| param_no_default* invalid_parameters_helper a=param_no_default { |
| 1377 |
self.raise_syntax_error_known_location("non-default argument follows default argument", a) |
| 1378 |
} |
| 1379 |
| param_no_default* a='(' param_no_default+ ','? b=')' { |
| 1380 |
self.raise_syntax_error_known_range("Function parameters cannot be parenthesized", a, b) } |
| 1381 |
| a="/" ',' { |
| 1382 |
self.raise_syntax_error_known_location("at least one argument must precede /", a) } |
| 1383 |
| (slash_no_default | slash_with_default) param_maybe_default* a='/' { |
| 1384 |
self.raise_syntax_error_known_location("/ may appear only once", a) } |
| 1385 |
| (slash_no_default | slash_with_default)? param_maybe_default* '*' (',' | param_no_default) param_maybe_default* a='/' { |
| 1386 |
self.raise_syntax_error_known_location("/ must be ahead of *", a) } |
| 1387 |
| param_maybe_default+ '/' a='*' { |
| 1388 |
self.raise_syntax_error_known_location("expected comma between / and *", a) } |
| 1389 |
invalid_default: |
| 1390 |
| a='=' &(')'|',') { self.raise_syntax_error_known_location("expected default value expression", a) } |
| 1391 |
invalid_star_etc: |
| 1392 |
| a='*' (')' | ',' (')' | '**')) { self.raise_syntax_error_known_location("named arguments must follow bare *", a) } |
| 1393 |
| '*' ',' TYPE_COMMENT { self.raise_syntax_error("bare * has associated type comment") } |
| 1394 |
| '*' param a='=' { self.raise_syntax_error_known_location("var-positional argument cannot have default value", a) } |
| 1395 |
| '*' (param_no_default | ',') param_maybe_default* a='*' (param_no_default | ',') { |
| 1396 |
self.raise_syntax_error_known_location("* argument may appear only once", a) } |
| 1397 |
invalid_kwds: |
| 1398 |
| '**' param a='=' { self.raise_syntax_error_known_location("var-keyword argument cannot have default value", a) } |
| 1399 |
| '**' param ',' a=param { self.raise_syntax_error_known_location("arguments cannot follow var-keyword argument", a) } |
| 1400 |
| '**' param ',' a[Token*]=('*'|'**'|'/') { self.raise_syntax_error_known_location("arguments cannot follow var-keyword argument", a) } |
| 1401 |
invalid_parameters_helper: # This is only there to avoid type errors |
| 1402 |
| a=slash_with_default { self.dummy_name() } |
| 1403 |
| a=param_with_default+ { self.dummy_name() } |
| 1404 |
invalid_lambda_parameters[NoReturn]: |
| 1405 |
| lambda_param_no_default* invalid_lambda_parameters_helper a=lambda_param_no_default { |
| 1406 |
self.raise_syntax_error_known_location("non-default argument follows default argument", a) |
| 1407 |
} |
| 1408 |
| lambda_param_no_default* a='(' ','.lambda_param+ ','? b=')' { |
| 1409 |
self.raise_syntax_error_known_range("Lambda expression parameters cannot be parenthesized", a, b) } |
| 1410 |
| a="/" ',' { |
| 1411 |
self.raise_syntax_error_known_location("at least one argument must precede /", a) } |
| 1412 |
| (lambda_slash_no_default | lambda_slash_with_default) lambda_param_maybe_default* a='/' { |
| 1413 |
self.raise_syntax_error_known_location("/ may appear only once", a) } |
| 1414 |
| (lambda_slash_no_default | lambda_slash_with_default)? lambda_param_maybe_default* '*' (',' | lambda_param_no_default) lambda_param_maybe_default* a='/' { |
| 1415 |
self.raise_syntax_error_known_location("/ must be ahead of *", a) } |
| 1416 |
| lambda_param_maybe_default+ '/' a='*' { |
| 1417 |
self.raise_syntax_error_known_location("expected comma between / and *", a) } |
| 1418 |
invalid_lambda_parameters_helper: |
| 1419 |
| a=lambda_slash_with_default { self.dummy_name() } |
| 1420 |
| a=lambda_param_with_default+ { self.dummy_name() } |
| 1421 |
invalid_lambda_star_etc[NoReturn]: |
| 1422 |
| '*' (':' | ',' (':' | '**')) { |
| 1423 |
self.raise_syntax_error("named arguments must follow bare *") |
| 1424 |
} |
| 1425 |
| '*' lambda_param a='=' { self.raise_syntax_error_known_location("var-positional argument cannot have default value", a) } |
| 1426 |
| '*' (lambda_param_no_default | ',') lambda_param_maybe_default* a='*' (lambda_param_no_default | ',') { |
| 1427 |
self.raise_syntax_error_known_location("* argument may appear only once", a) } |
| 1428 |
invalid_lambda_kwds: |
| 1429 |
| '**' lambda_param a='=' { self.raise_syntax_error_known_location("var-keyword argument cannot have default value", a) } |
| 1430 |
| '**' lambda_param ',' a=lambda_param { self.raise_syntax_error_known_location("arguments cannot follow var-keyword argument", a) } |
| 1431 |
| '**' lambda_param ',' a[Token*]=('*'|'**'|'/') { self.raise_syntax_error_known_location("arguments cannot follow var-keyword argument", a) } |
| 1432 |
invalid_double_type_comments[NoReturn]: |
| 1433 |
| TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT { |
| 1434 |
self.raise_syntax_error("Cannot have two type comments on def") |
| 1435 |
} |
| 1436 |
invalid_with_item[NoReturn]: |
| 1437 |
| expression 'as' a=expression &(',' | ')' | ':') { |
| 1438 |
self.raise_syntax_error_if_invalid_target(a, "assign") |
| 1439 |
} |
| 1440 |
|
| 1441 |
invalid_for_target[NoReturn]: |
| 1442 |
| ASYNC? 'for' a=star_expressions { |
| 1443 |
self.raise_syntax_error_if_invalid_target(a, "for") |
| 1444 |
} |
| 1445 |
|
| 1446 |
invalid_group[NoReturn]: |
| 1447 |
| '(' a=starred_expression ')' { |
| 1448 |
self.raise_syntax_error_known_location("cannot use starred expression here", a) |
| 1449 |
} |
| 1450 |
| '(' a='**' expression ')' { |
| 1451 |
self.raise_syntax_error_known_location("cannot use double starred expression here", a) |
| 1452 |
} |
| 1453 |
invalid_import_from_targets[NoReturn]: |
| 1454 |
| import_from_as_names a=',' NEWLINE { |
| 1455 |
self.raise_syntax_error_known_location("trailing comma not allowed without surrounding parentheses", a) |
| 1456 |
} |
| 1457 |
|
| 1458 |
invalid_with_stmt[None]: |
| 1459 |
| [ASYNC] 'with' ','.(expression ['as' star_target])+ n=NEWLINE { self.raise_syntax_error_known_location("expected ':'", n) } |
| 1460 |
| [ASYNC] 'with' '(' ','.(expressions ['as' star_target])+ ','? ')' n=NEWLINE { self.raise_syntax_error_known_location("expected ':'", n) } |
| 1461 |
invalid_with_stmt_indent[NoReturn]: |
| 1462 |
| [ASYNC] a='with' ','.(expression ['as' star_target])+ ':' NEWLINE !INDENT { |
| 1463 |
self.raise_indentation_error( |
| 1464 |
"expected an indented block after 'with' statement on line %s" % a.lineno |
| 1465 |
) |
| 1466 |
} |
| 1467 |
| [ASYNC] a='with' '(' ','.(expressions ['as' star_target])+ ','? ')' ':' NEWLINE !INDENT { |
| 1468 |
self.raise_indentation_error( |
| 1469 |
"expected an indented block after 'with' statement on line %s" % a.lineno |
| 1470 |
) |
| 1471 |
} |
| 1472 |
|
| 1473 |
invalid_try_stmt: |
| 1474 |
| a='try' ':' NEWLINE !INDENT { |
| 1475 |
self.raise_indentation_error( |
| 1476 |
"expected an indented block after 'try' statement on line %s" % a.lineno |
| 1477 |
) |
| 1478 |
} |
| 1479 |
| 'try' ':' block !('except' | 'finally') { |
| 1480 |
self.raise_syntax_error("expected 'except' or 'finally' block") |
| 1481 |
} |
| 1482 |
| 'try' ':' block* except_block+ a='except' b='*' expression ['as' NAME] ':' { |
| 1483 |
self.raise_syntax_error_known_range("cannot have both 'except' and 'except*' on the same 'try'", a, b) } |
| 1484 |
| 'try' ':' block* except_star_block+ a='except' [expression ['as' NAME]] ':' { |
| 1485 |
self.raise_syntax_error_known_location("cannot have both 'except' and 'except*' on the same 'try'", a) } |
| 1486 |
invalid_except_stmt[None]: |
| 1487 |
| 'except' '*'? a=expression ',' expressions ['as' NAME ] ':' { |
| 1488 |
self.raise_syntax_error_starting_from("multiple exception types must be parenthesized", a) |
| 1489 |
} |
| 1490 |
| a='except' '*'? expression ['as' NAME ] n=NEWLINE { self.raise_syntax_error_known_location("expected ':'", n) } |
| 1491 |
| a='except' n=NEWLINE { self.raise_syntax_error_known_location("expected ':'", n) } |
| 1492 |
| a='except' '*' b=(NEWLINE | ':') { self.raise_syntax_error_known_location("expected one or more exception types", b) } |
| 1493 |
invalid_finally_stmt[NoReturn]: |
| 1494 |
| a='finally' ':' NEWLINE !INDENT { |
| 1495 |
self.raise_indentation_error( |
| 1496 |
"expected an indented block after 'finally' statement on line %s" % a.lineno |
| 1497 |
) |
| 1498 |
} |
| 1499 |
invalid_except_stmt_indent[NoReturn]: |
| 1500 |
| a='except' expression ['as' NAME ] ':' NEWLINE !INDENT { |
| 1501 |
self.raise_indentation_error( |
| 1502 |
"expected an indented block after 'except' statement on line %s" % a.lineno |
| 1503 |
) |
| 1504 |
} |
| 1505 |
| a='except' ':' NEWLINE !INDENT { |
| 1506 |
self.raise_indentation_error( |
| 1507 |
"expected an indented block after 'except' statement on line %s" % a.lineno |
| 1508 |
) |
| 1509 |
} |
| 1510 |
invalid_except_star_stmt_indent: |
| 1511 |
| a='except' '*' expression ['as' NAME ] ':' NEWLINE !INDENT { |
| 1512 |
self.raise_indentation_error( |
| 1513 |
"expected an indented block after 'except*' statement on line %d" % a.lineno) |
| 1514 |
} |
| 1515 |
invalid_match_stmt[NoReturn]: |
| 1516 |
| "match" subject_expr !':' { |
| 1517 |
self.check_version( |
| 1518 |
(3, 10), |
| 1519 |
"Pattern matching is", |
| 1520 |
self.raise_syntax_error("expected ':'") |
| 1521 |
) |
| 1522 |
} |
| 1523 |
| a="match" subject=subject_expr ':' NEWLINE !INDENT { |
| 1524 |
self.check_version( |
| 1525 |
(3, 10), |
| 1526 |
"Pattern matching is", |
| 1527 |
self.raise_indentation_error( |
| 1528 |
"expected an indented block after 'match' statement on line %s" % a.lineno |
| 1529 |
) |
| 1530 |
) |
| 1531 |
} |
| 1532 |
invalid_case_block[NoReturn]: |
| 1533 |
| "case" patterns guard? !':' { self.raise_syntax_error("expected ':'") } |
| 1534 |
| a="case" patterns guard? ':' NEWLINE !INDENT { |
| 1535 |
self.raise_indentation_error( |
| 1536 |
"expected an indented block after 'case' statement on line %s" % a.lineno |
| 1537 |
) |
| 1538 |
} |
| 1539 |
invalid_as_pattern[NoReturn]: |
| 1540 |
| or_pattern 'as' a="_" { |
| 1541 |
self.raise_syntax_error_known_location("cannot use '_' as a target", a) |
| 1542 |
} |
| 1543 |
| or_pattern 'as' !NAME a=expression { |
| 1544 |
self.raise_syntax_error_known_location("invalid pattern target", a) |
| 1545 |
} |
| 1546 |
invalid_class_pattern[NoReturn]: |
| 1547 |
| name_or_attr '(' a=invalid_class_argument_pattern { |
| 1548 |
self.raise_syntax_error_known_range( |
| 1549 |
"positional patterns follow keyword patterns", a[0], a[-1] |
| 1550 |
) |
| 1551 |
} |
| 1552 |
invalid_class_argument_pattern[list]: |
| 1553 |
| [positional_patterns ','] keyword_patterns ',' a=positional_patterns { a } |
| 1554 |
invalid_if_stmt[NoReturn]: |
| 1555 |
| 'if' named_expression n=NEWLINE { self.raise_syntax_error_known_location("expected ':'", n) } |
| 1556 |
| a='if' a=named_expression ':' NEWLINE !INDENT { |
| 1557 |
self.raise_indentation_error( |
| 1558 |
"expected an indented block after 'if' statement on line %s" % a.lineno |
| 1559 |
) |
| 1560 |
} |
| 1561 |
invalid_elif_stmt[NoReturn]: |
| 1562 |
| 'elif' named_expression NEWLINE { self.raise_syntax_error("expected ':'") } |
| 1563 |
| a='elif' named_expression ':' NEWLINE !INDENT { |
| 1564 |
self.raise_indentation_error( |
| 1565 |
"expected an indented block after 'elif' statement on line %s" % a.lineno |
| 1566 |
) |
| 1567 |
} |
| 1568 |
invalid_else_stmt[NoReturn]: |
| 1569 |
| a='else' ':' NEWLINE !INDENT { |
| 1570 |
self.raise_indentation_error( |
| 1571 |
"expected an indented block after 'else' statement on line %s" % a.lineno |
| 1572 |
) |
| 1573 |
} |
| 1574 |
invalid_while_stmt[NoReturn]: |
| 1575 |
| 'while' named_expression NEWLINE { self.raise_syntax_error("expected ':'") } |
| 1576 |
| a='while' named_expression ':' NEWLINE !INDENT { |
| 1577 |
self.raise_indentation_error( |
| 1578 |
"expected an indented block after 'while' statement on line %s" % a.lineno |
| 1579 |
) |
| 1580 |
} |
| 1581 |
invalid_for_stmt[NoReturn]: |
| 1582 |
| [ASYNC] 'for' star_targets 'in' star_expressions n=NEWLINE { self.raise_syntax_error_known_location("expected ':'", n) } |
| 1583 |
| [ASYNC] a='for' star_targets 'in' star_expressions ':' NEWLINE !INDENT { |
| 1584 |
self.raise_indentation_error( |
| 1585 |
"expected an indented block after 'for' statement on line %s" % a.lineno |
| 1586 |
) |
| 1587 |
} |
| 1588 |
invalid_def_raw[NoReturn]: |
| 1589 |
| [ASYNC] a='def' NAME '(' [params] ')' ['->' expression] ':' NEWLINE !INDENT { |
| 1590 |
self.raise_indentation_error( |
| 1591 |
"expected an indented block after function definition on line %s" % a.lineno |
| 1592 |
) |
| 1593 |
} |
| 1594 |
invalid_class_def_raw[NoReturn]: |
| 1595 |
| 'class' NAME ['(' [arguments] ')'] n=NEWLINE { self.raise_syntax_error_known_location("expected ':'", n) } |
| 1596 |
| a='class' NAME ['(' [arguments] ')'] ':' NEWLINE !INDENT { |
| 1597 |
self.raise_indentation_error( |
| 1598 |
"expected an indented block after class definition on line %s" % a.lineno |
| 1599 |
) |
| 1600 |
} |
| 1601 |
|
| 1602 |
invalid_double_starred_kvpairs[None]: |
| 1603 |
| ','.double_starred_kvpair+ ',' invalid_kvpair |
| 1604 |
| expression ':' a='*' bitwise_or { |
| 1605 |
self.raise_syntax_error_starting_from("cannot use a starred expression in a dictionary value", a) |
| 1606 |
} |
| 1607 |
| expression a=':' &('}'|',') { |
| 1608 |
self.raise_syntax_error_known_location("expression expected after dictionary key and ':'", a) |
| 1609 |
} |
| 1610 |
| a=expression b='=' { |
| 1611 |
self.raise_syntax_error_known_location("':' expected after dictionary key", b) |
| 1612 |
} |
| 1613 |
invalid_kvpair[None]: |
| 1614 |
| a=expression !(':') { |
| 1615 |
self._raise_syntax_error( |
| 1616 |
"':' expected after dictionary key", |
| 1617 |
a.lineno, a.col_offset - 1, |
| 1618 |
a.end_lineno, a.end_col_offset - 1, |
| 1619 |
) |
| 1620 |
} |
| 1621 |
| expression ':' a='*' bitwise_or { |
| 1622 |
self.raise_syntax_error_starting_from("cannot use a starred expression in a dictionary value", a) |
| 1623 |
} |
| 1624 |
| expression a=':' { |
| 1625 |
self.raise_syntax_error_known_location("expression expected after dictionary key and ':'", a) |
| 1626 |
} |