|
44 | 44 | # children. For example, "call" has precedence 2 so we don't get
|
45 | 45 | # additional the additional parenthesis of: ".. op (call())". However
|
46 | 46 | # for call's children, it parameters, we set the the precedence high,
|
47 |
| -# say to 100, to make sure we avoid additional prenthesis in |
| 47 | +# say to 100, to make sure we avoid additional parenthesis in |
48 | 48 | # call((.. op ..)).
|
49 | 49 |
|
50 | 50 | NO_PARENTHESIS_EVER = 100
|
|
130 | 130 | # Some parse trees created below are used for comparing code
|
131 | 131 | # fragments (like "return None" at the end of functions).
|
132 | 132 |
|
133 |
| -ASSIGN_DOC_STRING = lambda doc_string, doc_load: SyntaxTree( |
| 133 | +ASSIGN_DOC_STRING = lambda doc_string, doc_load: SyntaxTree( # noqa |
134 | 134 | "assign",
|
135 | 135 | [
|
136 | 136 | SyntaxTree(
|
|
247 | 247 | "assert_expr_or": ("%c or %c", 0, 2),
|
248 | 248 | "assert_expr_and": ("%c and %c", 0, 2),
|
249 | 249 |
|
| 250 | + "assign": ( |
| 251 | + "%|%c = %p\n", |
| 252 | + -1, |
| 253 | + (0, ("expr", "branch_op"), PRECEDENCE["tuple_list_starred"] + 1) |
| 254 | + ), |
| 255 | + |
250 | 256 | "attribute": ("%c.%[1]{pattr}", (0, "expr")),
|
251 | 257 |
|
252 | 258 | # This nonterminal we create on the fly in semantic routines
|
253 | 259 | "attribute_w_parens": ("(%c).%[1]{pattr}", (0, "expr")),
|
254 | 260 |
|
255 |
| - "assign": ("%|%c = %p\n", -1, (0, 200)), |
256 | 261 | # The 2nd parameter should have a = suffix.
|
257 | 262 | # There is a rule with a 4th parameter "store"
|
258 | 263 | # which we don't use here.
|
|
268 | 273 | (0, -1, ", ", NO_PARENTHESIS_EVER)
|
269 | 274 | ),
|
270 | 275 |
|
| 276 | + "call_stmt": ( "%|%p\n", |
| 277 | + # When a call statement contains only a named_expr (:=) |
| 278 | + # the named_expr should have parenthesis around it. |
| 279 | + (0, PRECEDENCE["named_expr"]-1)), |
| 280 | + |
| 281 | + # "classdef": (), # handled by n_classdef() |
| 282 | + # A custom rule in n_function def distinguishes whether to call this or |
| 283 | + # function_def_async |
| 284 | + |
271 | 285 | "classdefdeco": ("\n\n%c", 0),
|
272 | 286 | "classdefdeco1": ("%|@%c\n%c", 0, 1),
|
273 | 287 |
|
|
283 | 297 |
|
284 | 298 | "continue": ("%|continue\n",),
|
285 | 299 |
|
286 |
| - # "classdef": (), # handled by n_classdef() |
287 |
| - # A custom rule in n_function def distinguishes whether to call this or |
288 |
| - # function_def_async |
289 |
| - |
290 | 300 | "delete_subscript": (
|
291 | 301 | "%|del %p[%c]\n",
|
292 | 302 | (0, "expr", PRECEDENCE["subscript"]),
|
293 | 303 | (1, "expr"),
|
294 | 304 | ),
|
| 305 | + "designList": ("%c = %c", 0, -1), |
| 306 | + "dict_comp_body": ("%c: %c", 1, 0), |
| 307 | + |
| 308 | + "elifelifstmt": ("%|elif %c:\n%+%c%-%c", 0, 1, 3), |
| 309 | + "elifelsestmt": ("%|elif %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3), |
| 310 | + "elifelsestmtr": ("%|elif %c:\n%+%c%-%|else:\n%+%c%-\n\n", 0, 1, 2), |
| 311 | + "elifelsestmtr2": ( |
| 312 | + "%|elif %c:\n%+%c%-%|else:\n%+%c%-\n\n", |
| 313 | + 0, |
| 314 | + 1, |
| 315 | + 3, |
| 316 | + ), # has COME_FROM |
| 317 | + "elifstmt": ("%|elif %c:\n%+%c%-", 0, 1), |
295 | 318 |
|
296 | 319 | "except": ("%|except:\n%+%c%-", 3),
|
297 | 320 | "except_cond1": ("%|except %c:\n", 1),
|
298 | 321 | "except_cond2": ("%|except %c as %c:\n", (1, "expr"), (5, "store")),
|
299 | 322 | "except_suite": ("%+%c%-%C", 0, (1, maxint, "")),
|
| 323 | + |
300 | 324 | # In Python 3.6+, this is more complicated in the presence of "returns"
|
301 | 325 | "except_suite_finalize": ("%+%c%-%C", 1, (3, maxint, "")),
|
302 | 326 |
|
|
332 | 356 | -2,
|
333 | 357 | ),
|
334 | 358 |
|
335 |
| - "get_iter": ("iter(%c)", (0, "expr"),), |
| 359 | + "function_def": ("\n\n%|def %c\n", -2), # -2 to handle closures |
| 360 | + "function_def_deco": ("\n\n%c", 0), |
336 | 361 |
|
337 |
| - "set_comp_body": ("%c", 0), |
338 | 362 | "gen_comp_body": ("%c", 0),
|
339 |
| - "dict_comp_body": ("%c: %c", 1, 0), |
340 |
| - "designList": ("%c = %c", 0, -1), |
341 |
| - "ret_and": ("%c and %c", 0, 2), |
342 |
| - "or": ("%p or %p", (0, PRECEDENCE["or"]), (1, PRECEDENCE["or"])), |
343 |
| - "ret_or": ("%c or %c", 0, 2), |
| 363 | + "get_iter": ("iter(%c)", (0, "expr"),), |
| 364 | + |
344 | 365 | "if_exp": ("%p if %c else %c", (2, "expr", 27), 0, 4),
|
345 | 366 | "if_exp_lambda": ("%p if %c else %c", (2, "expr", 27), (0, "expr"), 4),
|
346 | 367 | "if_exp_true": ("%p if 1 else %c", (0, "expr", 27), 2),
|
|
353 | 374 | ),
|
354 | 375 | "if_exp_not_lambda": ("%p if not %c else %c", (2, "expr", 27), 0, 4),
|
355 | 376 |
|
356 |
| - "function_def": ("\n\n%|def %c\n", -2), # -2 to handle closures |
357 |
| - "function_def_deco": ("\n\n%c", 0), |
358 |
| - |
359 |
| - # This is only generated by transform |
360 |
| - # it is a string at the beginning of a function that is *not* a docstring |
361 |
| - # 3.7 test_fstring.py tests for this kind of crap. |
362 |
| - # For compatibility with older Python, we'll use "%" instead of |
363 |
| - # a format string. |
364 |
| - "string_at_beginning": ('%|"%%s" %% %c\n', 0), |
365 |
| - "call_stmt": ( "%|%p\n", |
366 |
| - # When a call statement contains only a named_expr (:=) |
367 |
| - # the named_expr should have parenthesis around it. |
368 |
| - (0, PRECEDENCE["named_expr"]-1)), |
369 |
| - |
370 |
| - "ifstmt": ( |
371 |
| - "%|if %c:\n%+%c%-", |
372 |
| - 0, # "testexpr" or "testexpr_then" |
373 |
| - 1, # "_ifstmts_jump" or "return_stmts" |
374 |
| - ), |
375 |
| - "iflaststmt": ("%|if %c:\n%+%c%-", 0, 1), |
376 |
| - "iflaststmtl": ("%|if %c:\n%+%c%-", 0, 1), |
377 |
| - "testtrue": ("not %p", (0, PRECEDENCE["unary_not"])), |
378 | 377 | # Generally the args here are 0: (some sort of) "testexpr",
|
379 | 378 | # 1: (some sort of) "cstmts_opt",
|
380 | 379 | # 2 or 3: "else_suite"
|
|
384 | 383 | "ifelsestmt": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3),
|
385 | 384 | "ifelsestmtc": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3),
|
386 | 385 | "ifelsestmtl": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3),
|
387 |
| - # These are created only via transformation |
| 386 | + |
| 387 | + # This is created only via transformation |
388 | 388 | "ifelifstmt": ("%|if %c:\n%+%c%-%c", 0, 1, 3), # "testexpr" or "testexpr_then"
|
389 |
| - "elifelifstmt": ("%|elif %c:\n%+%c%-%c", 0, 1, 3), |
390 |
| - "elifstmt": ("%|elif %c:\n%+%c%-", 0, 1), |
391 |
| - "elifelsestmt": ("%|elif %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3), |
| 389 | + |
392 | 390 | "ifelsestmtr": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 2),
|
393 | 391 | "ifelsestmtr2": ("%|if %c:\n%+%c%-%|else:\n%+%c%-\n\n", 0, 1, 3), # has COME_FROM
|
394 |
| - "elifelsestmtr": ("%|elif %c:\n%+%c%-%|else:\n%+%c%-\n\n", 0, 1, 2), |
395 |
| - "elifelsestmtr2": ( |
396 |
| - "%|elif %c:\n%+%c%-%|else:\n%+%c%-\n\n", |
397 |
| - 0, |
398 |
| - 1, |
399 |
| - 3, |
400 |
| - ), # has COME_FROM |
| 392 | + "iflaststmt": ("%|if %c:\n%+%c%-", 0, 1), |
| 393 | + |
| 394 | + "iflaststmtl": ("%|if %c:\n%+%c%-", 0, 1), |
| 395 | + |
| 396 | + "ifstmt": ( |
| 397 | + "%|if %c:\n%+%c%-", |
| 398 | + 0, # "testexpr" or "testexpr_then" |
| 399 | + 1, # "_ifstmts_jump" or "return_stmts" |
| 400 | + ), |
401 | 401 |
|
402 | 402 | "import": ("%|import %c\n", 2),
|
403 | 403 | "importlist": ("%C", (0, maxint, ", ")),
|
|
415 | 415 |
|
416 | 416 | "kv": ("%c: %c", 3, 1),
|
417 | 417 | "kv2": ("%c: %c", 1, 2),
|
| 418 | + |
418 | 419 | "kwarg": ("%[0]{pattr}=%c", 1), # Change when Python 2 does LOAD_STR
|
419 | 420 | "kwargs": ("%D", (0, maxint, ", ")),
|
420 | 421 | "kwargs1": ("%D", (0, maxint, ", ")),
|
|
436 | 437 | # and this is added, as a transformation rule.
|
437 | 438 | "newline": ("\n"),
|
438 | 439 |
|
| 440 | + "or": ("%p or %p", (0, PRECEDENCE["or"]), (1, PRECEDENCE["or"])), |
| 441 | + |
| 442 | + "pass": ("%|pass\n",), |
| 443 | + |
439 | 444 | "print_item": (", %c", 0),
|
440 | 445 | "print_items_nl_stmt": ("%|print %c%c\n", 0, 2),
|
441 | 446 | "print_items_stmt": ("%|print %c%c,\n", 0, 2), # Python 2 only
|
|
445 | 450 | "print_to_items": ("%C", (0, 2, ", ")),
|
446 | 451 | "print_to_nl": ("%|print >> %c, %c\n", 0, 1),
|
447 | 452 |
|
448 |
| - "pass": ("%|pass\n",), |
449 |
| - |
450 | 453 | "raise_stmt0": ("%|raise\n",),
|
451 | 454 | "raise_stmt1": ("%|raise %c\n", 0),
|
452 | 455 | "raise_stmt3": ("%|raise %c, %c, %c\n", 0, 1, 2),
|
453 | 456 |
|
| 457 | + "ret_and": ("%c and %c", 0, 2), |
| 458 | + "ret_or": ("%c or %c", 0, 2), |
| 459 | + |
454 | 460 | # Note: we have a custom rule, which calls when we don't
|
455 | 461 | # have "return None"
|
456 | 462 | "return": ( "%|return %c\n", 0),
|
457 | 463 |
|
| 464 | + "set_comp_body": ("%c", 0), |
| 465 | + |
458 | 466 | "set_iter": ( "%c", 0 ),
|
459 | 467 |
|
460 | 468 | "return_if_stmt": ("return %c\n", 0),
|
|
491 | 499 | (0, "expr")
|
492 | 500 | ),
|
493 | 501 |
|
| 502 | + # This is only generated by transform |
| 503 | + # it is a string at the beginning of a function that is *not* a docstring |
| 504 | + # 3.7 test_fstring.py tests for this kind of crap. |
| 505 | + # For compatibility with older Python, we'll use "%" instead of |
| 506 | + # a format string. |
| 507 | + "string_at_beginning": ('%|"%%s" %% %c\n', 0), |
| 508 | + |
494 | 509 | "subscript": (
|
495 | 510 | "%p[%p]",
|
496 | 511 | (0, "expr", PRECEDENCE["subscript"]),
|
|
503 | 518 | (1, "expr", NO_PARENTHESIS_EVER)
|
504 | 519 | ),
|
505 | 520 |
|
| 521 | + "testtrue": ("not %p", (0, PRECEDENCE["unary_not"])), |
| 522 | + |
| 523 | + # Note: this is generated generated by grammar rules but in this phase. |
| 524 | + "tf_try_except": ("%c%-%c%+", 1, 3), |
| 525 | + "tf_tryelsestmt": ("%c%-%c%|else:\n%+%c", 1, 3, 4), |
| 526 | + |
506 | 527 | "try_except": ("%|try:\n%+%c%-%c\n\n", 1, 3),
|
507 | 528 | "tryelsestmt": ("%|try:\n%+%c%-%c%|else:\n%+%c%-\n\n", 1, 3, 4),
|
508 | 529 | "tryelsestmtc": ("%|try:\n%+%c%-%c%|else:\n%+%c%-", 1, 3, 4),
|
509 | 530 | "tryelsestmtl": ("%|try:\n%+%c%-%c%|else:\n%+%c%-", 1, 3, 4),
|
510 |
| - # Note: this is generated generated by grammar rules but in this phase. |
511 |
| - "tf_try_except": ("%c%-%c%+", 1, 3), |
512 |
| - "tf_tryelsestmt": ("%c%-%c%|else:\n%+%c", 1, 3, 4), |
513 | 531 | "tryfinallystmt": ("%|try:\n%+%c%-%|finally:\n%+%c%-\n\n", 1, 5),
|
514 | 532 |
|
515 | 533 | # unary_op (formerly "unary_expr") is the Python AST UnaryOp
|
|
0 commit comments