|
156 | 156 | ],
|
157 | 157 | )
|
158 | 158 |
|
| 159 | +NEWLINE = SyntaxTree("newline", []) |
159 | 160 | NONE = SyntaxTree("expr", [NoneToken])
|
160 | 161 |
|
161 | 162 | RETURN_NONE = SyntaxTree("stmt", [SyntaxTree("return", [NONE, Token("RETURN_VALUE")])])
|
|
240 | 241 | "UNARY_NOT": ( "not ", ),
|
241 | 242 | "UNARY_POSITIVE": ( "+",),
|
242 | 243 |
|
243 |
| - # bin_op (formerly "binary_expr") is the Python AST BinOp |
244 |
| - "bin_op": ("%c %c %c", 0, (-1, "binary_operator"), (1, "expr")), |
245 |
| - # unary_op (formerly "unary_expr") is the Python AST UnaryOp |
246 |
| - "unary_op": ("%c%c", (1, "unary_operator"), (0, "expr")), |
247 |
| - "unary_not": ("not %c", (0, "expr")), |
248 |
| - "unary_convert": ("`%c`", (0, "expr"),), |
249 |
| - "get_iter": ("iter(%c)", (0, "expr"),), |
250 |
| - |
251 |
| - "set_iter": ( "%c", 0 ), |
252 |
| - |
253 |
| - "slice0": ( |
254 |
| - "%c[:]", |
255 |
| - (0, "expr"), |
256 |
| - ), |
257 |
| - "slice1": ( |
258 |
| - "%c[%p:]", |
259 |
| - (0, "expr"), |
260 |
| - (1, NO_PARENTHESIS_EVER) |
261 |
| - ), |
262 |
| - |
263 |
| - "slice2": ( "%c[:%p]", |
264 |
| - (0, "expr"), |
265 |
| - (1, NO_PARENTHESIS_EVER) |
266 |
| - ), |
| 244 | + "and": ("%c and %c", 0, 2), |
| 245 | + "and2": ("%c", 3), |
267 | 246 |
|
268 |
| - "slice3": ( |
269 |
| - "%c[%p:%p]", |
270 |
| - (0, "expr"), |
271 |
| - (1, NO_PARENTHESIS_EVER), |
272 |
| - (2, NO_PARENTHESIS_EVER) |
273 |
| - ), |
| 247 | + "assert_expr_or": ("%c or %c", 0, 2), |
| 248 | + "assert_expr_and": ("%c and %c", 0, 2), |
274 | 249 |
|
275 | 250 | "attribute": ("%c.%[1]{pattr}", (0, "expr")),
|
276 |
| - "delete_subscript": ( |
277 |
| - "%|del %p[%c]\n", |
278 |
| - (0, "expr", PRECEDENCE["subscript"]), |
279 |
| - (1, "expr"), |
280 |
| - ), |
281 |
| - |
282 |
| - "subscript": ( |
283 |
| - "%p[%p]", |
284 |
| - (0, "expr", PRECEDENCE["subscript"]), |
285 |
| - (1, "expr", NO_PARENTHESIS_EVER) |
286 |
| - ), |
287 | 251 |
|
288 |
| - "subscript2": ( |
289 |
| - "%p[%p]", |
290 |
| - (0, "expr", PRECEDENCE["subscript"]), |
291 |
| - (1, "expr", NO_PARENTHESIS_EVER) |
292 |
| - ), |
293 |
| - |
294 |
| - "store_subscript": ("%p[%c]", (0, "expr", PRECEDENCE["subscript"]), (1, "expr")), |
295 |
| - "unpack": ("%C%,", (1, maxint, ", ")), |
296 |
| - # This nonterminal we create on the fly in semantic routines |
297 |
| - "unpack_w_parens": ("(%C%,)", (1, maxint, ", ")), |
298 | 252 | # This nonterminal we create on the fly in semantic routines
|
299 | 253 | "attribute_w_parens": ("(%c).%[1]{pattr}", (0, "expr")),
|
300 |
| - # This nonterminal we create on the fly in semantic routines |
301 |
| - "store_w_parens": ("(%c).%[1]{pattr}", (0, "expr")), |
302 |
| - "unpack_list": ("[%C]", (1, maxint, ", ")), |
303 |
| - "build_tuple2": ("%P", (0, -1, ", ", 100)), |
304 |
| - "list_iter": ("%c", 0), |
305 |
| - "list_for": (" for %c in %c%c", 2, 0, 3), |
306 |
| - "list_if": (" if %p%c", (0, "expr", 27), 2), |
307 |
| - "list_if_not": (" if not %p%c", (0, "expr", PRECEDENCE["unary_not"]), 2), |
308 |
| - "lc_body": ("",), # ignore when recursing |
309 |
| - "comp_iter": ("%c", 0), |
310 |
| - "comp_if": (" if %c%c", 0, 2), |
311 |
| - "comp_if_not": (" if not %p%c", (0, "expr", PRECEDENCE["unary_not"]), 2), |
312 |
| - "comp_body": ("",), # ignore when recusing |
313 | 254 |
|
314 |
| - "set_comp_body": ("%c", 0), |
315 |
| - "gen_comp_body": ("%c", 0), |
316 |
| - "dict_comp_body": ("%c: %c", 1, 0), |
317 | 255 | "assign": ("%|%c = %p\n", -1, (0, 200)),
|
318 | 256 | # The 2nd parameter should have a = suffix.
|
319 | 257 | # There is a rule with a 4th parameter "store"
|
320 | 258 | # which we don't use here.
|
321 | 259 | "aug_assign1": ("%|%c %c %c\n", 0, 2, 1),
|
322 | 260 | "aug_assign2": ("%|%c.%[2]{pattr} %c %c\n", 0, -3, -4),
|
323 |
| - "designList": ("%c = %c", 0, -1), |
324 |
| - "and": ("%c and %c", 0, 2), |
325 |
| - "ret_and": ("%c and %c", 0, 2), |
326 |
| - "and2": ("%c", 3), |
327 |
| - "or": ("%p or %p", (0, PRECEDENCE["or"]), (1, PRECEDENCE["or"])), |
328 |
| - "ret_or": ("%c or %c", 0, 2), |
329 |
| - "if_exp": ("%p if %c else %c", (2, "expr", 27), 0, 4), |
330 |
| - "if_exp_lambda": ("%p if %c else %c", (2, "expr", 27), (0, "expr"), 4), |
331 |
| - "if_exp_true": ("%p if 1 else %c", (0, "expr", 27), 2), |
332 |
| - "if_exp_ret": ("%p if %p else %p", (2, 27), (0, 27), (-1, 27)), |
333 |
| - "if_exp_not": ( |
334 |
| - "%p if not %p else %p", |
335 |
| - (2, 27), |
336 |
| - (0, "expr", PRECEDENCE["unary_not"]), |
337 |
| - (4, 27), |
| 261 | + |
| 262 | + # bin_op (formerly "binary_expr") is the Python AST BinOp |
| 263 | + "bin_op": ("%c %c %c", 0, (-1, "binary_operator"), (1, "expr")), |
| 264 | + |
| 265 | + "break": ("%|break\n",), |
| 266 | + "build_tuple2": ( |
| 267 | + "%P", |
| 268 | + (0, -1, ", ", NO_PARENTHESIS_EVER) |
338 | 269 | ),
|
339 |
| - "if_exp_not_lambda": ("%p if not %c else %c", (2, "expr", 27), 0, 4), |
| 270 | + |
| 271 | + "classdefdeco": ("\n\n%c", 0), |
| 272 | + "classdefdeco1": ("%|@%c\n%c", 0, 1), |
| 273 | + |
| 274 | + "comp_body": ("",), # ignore when recusing |
| 275 | + "comp_if": (" if %c%c", 0, 2), |
| 276 | + "comp_if_not": (" if not %p%c", (0, "expr", PRECEDENCE["unary_not"]), 2), |
| 277 | + "comp_iter": ("%c", 0), |
| 278 | + |
340 | 279 | "compare_single": ('%p %[-1]{pattr.replace("-", " ")} %p', (0, 19), (1, 19)),
|
341 | 280 | "compare_chained": ("%p %p", (0, 29), (1, 30)),
|
342 | 281 | "compared_chained_middle": ('%[3]{pattr.replace("-", " ")} %p %p', (0, 19), (-2, 19)),
|
343 | 282 | "compare_chained_right": ('%[1]{pattr.replace("-", " ")} %p', (0, 19)),
|
| 283 | + |
| 284 | + "continue": ("%|continue\n",), |
| 285 | + |
344 | 286 | # "classdef": (), # handled by n_classdef()
|
345 | 287 | # A custom rule in n_function def distinguishes whether to call this or
|
346 | 288 | # function_def_async
|
347 | 289 |
|
348 |
| - "function_def": ("\n\n%|def %c\n", -2), # -2 to handle closures |
349 |
| - "function_def_deco": ("\n\n%c", 0), |
350 |
| - "mkfuncdeco": ("%|@%c\n%c", 0, 1), |
351 |
| - # A custom rule in n_function def distinguishes whether to call this or |
352 |
| - # function_def_async |
353 |
| - "mkfuncdeco0": ("%|def %c\n", 0), |
354 |
| - "classdefdeco": ("\n\n%c", 0), |
355 |
| - "classdefdeco1": ("%|@%c\n%c", 0, 1), |
356 |
| - "kwarg": ("%[0]{pattr}=%c", 1), # Change when Python 2 does LOAD_STR |
357 |
| - "kwargs": ("%D", (0, maxint, ", ")), |
358 |
| - "kwargs1": ("%D", (0, maxint, ", ")), |
359 |
| - "assert_expr_or": ("%c or %c", 0, 2), |
360 |
| - "assert_expr_and": ("%c and %c", 0, 2), |
361 |
| - "print_items_stmt": ("%|print %c%c,\n", 0, 2), # Python 2 only |
362 |
| - "print_items_nl_stmt": ("%|print %c%c\n", 0, 2), |
363 |
| - "print_item": (", %c", 0), |
364 |
| - "print_nl": ("%|print\n",), |
365 |
| - "print_to": ("%|print >> %c, %c,\n", 0, 1), |
366 |
| - "print_to_nl": ("%|print >> %c, %c\n", 0, 1), |
367 |
| - "print_nl_to": ("%|print >> %c\n", 0), |
368 |
| - "print_to_items": ("%C", (0, 2, ", ")), |
369 |
| - # This is only generated by transform |
370 |
| - # it is a string at the beginning of a function that is *not* a docstring |
371 |
| - # 3.7 test_fstring.py tests for this kind of crap. |
372 |
| - # For compatibility with older Python, we'll use "%" instead of |
373 |
| - # a format string. |
374 |
| - "string_at_beginning": ('%|"%%s" %% %c\n', 0), |
375 |
| - "call_stmt": ( "%|%p\n", |
376 |
| - # When a call statement contains only a named_expr (:=) |
377 |
| - # the named_expr should have parenthesis around it. |
378 |
| - (0, PRECEDENCE["named_expr"]-1)), |
379 |
| - "break": ("%|break\n",), |
380 |
| - "continue": ("%|continue\n",), |
| 290 | + "delete_subscript": ( |
| 291 | + "%|del %p[%c]\n", |
| 292 | + (0, "expr", PRECEDENCE["subscript"]), |
| 293 | + (1, "expr"), |
| 294 | + ), |
381 | 295 |
|
382 | 296 | "except": ("%|except:\n%+%c%-", 3),
|
383 | 297 | "except_cond1": ("%|except %c:\n", 1),
|
|
418 | 332 | -2,
|
419 | 333 | ),
|
420 | 334 |
|
421 |
| - "raise_stmt0": ("%|raise\n",), |
422 |
| - "raise_stmt1": ("%|raise %c\n", 0), |
423 |
| - "raise_stmt3": ("%|raise %c, %c, %c\n", 0, 1, 2), |
424 |
| - # "yield": ( "yield %c", 0), |
| 335 | + "get_iter": ("iter(%c)", (0, "expr"),), |
425 | 336 |
|
426 |
| - # Note: we have a custom rule, which calls when we don't |
427 |
| - # have "return None" |
428 |
| - "return": ( "%|return %c\n", 0), |
| 337 | + "set_comp_body": ("%c", 0), |
| 338 | + "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), |
| 344 | + "if_exp": ("%p if %c else %c", (2, "expr", 27), 0, 4), |
| 345 | + "if_exp_lambda": ("%p if %c else %c", (2, "expr", 27), (0, "expr"), 4), |
| 346 | + "if_exp_true": ("%p if 1 else %c", (0, "expr", 27), 2), |
| 347 | + "if_exp_ret": ("%p if %p else %p", (2, 27), (0, 27), (-1, 27)), |
| 348 | + "if_exp_not": ( |
| 349 | + "%p if not %p else %p", |
| 350 | + (2, 27), |
| 351 | + (0, "expr", PRECEDENCE["unary_not"]), |
| 352 | + (4, 27), |
| 353 | + ), |
| 354 | + "if_exp_not_lambda": ("%p if not %c else %c", (2, "expr", 27), 0, 4), |
| 355 | + |
| 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)), |
429 | 369 |
|
430 |
| - "return_if_stmt": ("return %c\n", 0), |
431 | 370 | "ifstmt": (
|
432 | 371 | "%|if %c:\n%+%c%-",
|
433 | 372 | 0, # "testexpr" or "testexpr_then"
|
|
476 | 415 |
|
477 | 416 | "kv": ("%c: %c", 3, 1),
|
478 | 417 | "kv2": ("%c: %c", 1, 2),
|
| 418 | + "kwarg": ("%[0]{pattr}=%c", 1), # Change when Python 2 does LOAD_STR |
| 419 | + "kwargs": ("%D", (0, maxint, ", ")), |
| 420 | + "kwargs1": ("%D", (0, maxint, ", ")), |
| 421 | + |
| 422 | + "lc_body": ("",), # ignore when recursing |
| 423 | + "list_iter": ("%c", 0), |
| 424 | + "list_for": (" for %c in %c%c", 2, 0, 3), |
| 425 | + "list_if": (" if %p%c", (0, "expr", 27), 2), |
| 426 | + "list_if_not": (" if not %p%c", (0, "expr", PRECEDENCE["unary_not"]), 2), |
| 427 | + |
| 428 | + "mkfuncdeco": ("%|@%c\n%c", 0, 1), |
| 429 | + # A custom rule in n_function def distinguishes whether to call this or |
| 430 | + # function_def_async |
| 431 | + "mkfuncdeco0": ("%|def %c\n", 0), |
| 432 | + |
| 433 | + # In cases where we desire an explict new line. |
| 434 | + # After docstrings which are followed by a "def" is |
| 435 | + # one situations where Python formatting desires two newlines, |
| 436 | + # and this is added, as a transformation rule. |
| 437 | + "newline": ("\n"), |
| 438 | + |
| 439 | + "print_item": (", %c", 0), |
| 440 | + "print_items_nl_stmt": ("%|print %c%c\n", 0, 2), |
| 441 | + "print_items_stmt": ("%|print %c%c,\n", 0, 2), # Python 2 only |
| 442 | + "print_nl": ("%|print\n",), |
| 443 | + "print_nl_to": ("%|print >> %c\n", 0), |
| 444 | + "print_to": ("%|print >> %c, %c,\n", 0, 1), |
| 445 | + "print_to_items": ("%C", (0, 2, ", ")), |
| 446 | + "print_to_nl": ("%|print >> %c, %c\n", 0, 1), |
| 447 | + |
479 | 448 | "pass": ("%|pass\n",),
|
480 | 449 |
|
| 450 | + "raise_stmt0": ("%|raise\n",), |
| 451 | + "raise_stmt1": ("%|raise %c\n", 0), |
| 452 | + "raise_stmt3": ("%|raise %c, %c, %c\n", 0, 1, 2), |
| 453 | + |
| 454 | + # Note: we have a custom rule, which calls when we don't |
| 455 | + # have "return None" |
| 456 | + "return": ( "%|return %c\n", 0), |
| 457 | + |
| 458 | + "set_iter": ( "%c", 0 ), |
| 459 | + |
| 460 | + "return_if_stmt": ("return %c\n", 0), |
| 461 | + "slice0": ( |
| 462 | + "%c[:]", |
| 463 | + (0, "expr"), |
| 464 | + ), |
| 465 | + "slice1": ( |
| 466 | + "%c[%p:]", |
| 467 | + (0, "expr"), |
| 468 | + (1, NO_PARENTHESIS_EVER) |
| 469 | + ), |
| 470 | + |
| 471 | + "slice2": ( "%c[:%p]", |
| 472 | + (0, "expr"), |
| 473 | + (1, NO_PARENTHESIS_EVER) |
| 474 | + ), |
| 475 | + |
| 476 | + "slice3": ( |
| 477 | + "%c[%p:%p]", |
| 478 | + (0, "expr"), |
| 479 | + (1, NO_PARENTHESIS_EVER), |
| 480 | + (2, NO_PARENTHESIS_EVER) |
| 481 | + ), |
| 482 | + |
| 483 | + "store_subscript": ( |
| 484 | + "%p[%c]", |
| 485 | + (0, "expr", PRECEDENCE["subscript"]), (1, "expr") |
| 486 | + ), |
| 487 | + |
| 488 | + # This nonterminal we create on the fly in semantic routines |
| 489 | + "store_w_parens": ( |
| 490 | + "(%c).%[1]{pattr}", |
| 491 | + (0, "expr") |
| 492 | + ), |
| 493 | + |
| 494 | + "subscript": ( |
| 495 | + "%p[%p]", |
| 496 | + (0, "expr", PRECEDENCE["subscript"]), |
| 497 | + (1, "expr", NO_PARENTHESIS_EVER) |
| 498 | + ), |
| 499 | + |
| 500 | + "subscript2": ( |
| 501 | + "%p[%p]", |
| 502 | + (0, "expr", PRECEDENCE["subscript"]), |
| 503 | + (1, "expr", NO_PARENTHESIS_EVER) |
| 504 | + ), |
| 505 | + |
| 506 | + "try_except": ("%|try:\n%+%c%-%c\n\n", 1, 3), |
| 507 | + "tryelsestmt": ("%|try:\n%+%c%-%c%|else:\n%+%c%-\n\n", 1, 3, 4), |
| 508 | + "tryelsestmtc": ("%|try:\n%+%c%-%c%|else:\n%+%c%-", 1, 3, 4), |
| 509 | + "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 | + "tryfinallystmt": ("%|try:\n%+%c%-%|finally:\n%+%c%-\n\n", 1, 5), |
| 514 | + |
| 515 | + # unary_op (formerly "unary_expr") is the Python AST UnaryOp |
| 516 | + "unary_op": ("%c%c", (1, "unary_operator"), (0, "expr")), |
| 517 | + "unary_not": ("not %c", (0, "expr")), |
| 518 | + "unary_convert": ("`%c`", (0, "expr"),), |
| 519 | + |
| 520 | + "unpack": ("%C%,", (1, maxint, ", ")), |
| 521 | + "unpack_list": ("[%C]", (1, maxint, ", ")), |
| 522 | + # This nonterminal we create on the fly in semantic routines |
| 523 | + "unpack_w_parens": ("(%C%,)", (1, maxint, ", ")), |
| 524 | + |
481 | 525 | "whileTruestmt": ("%|while True:\n%+%c%-\n\n", 1),
|
482 | 526 | "whilestmt": ("%|while %c:\n%+%c%-\n\n", 1, 2),
|
483 | 527 | "while1stmt": ("%|while 1:\n%+%c%-\n\n", 1),
|
|
495 | 539 | (3, ("suite_stmts_opt", "suite_stmts")),
|
496 | 540 | ),
|
497 | 541 |
|
498 |
| - "try_except": ("%|try:\n%+%c%-%c\n\n", 1, 3), |
499 |
| - "tryelsestmt": ("%|try:\n%+%c%-%c%|else:\n%+%c%-\n\n", 1, 3, 4), |
500 |
| - "tryelsestmtc": ("%|try:\n%+%c%-%c%|else:\n%+%c%-", 1, 3, 4), |
501 |
| - "tryelsestmtl": ("%|try:\n%+%c%-%c%|else:\n%+%c%-", 1, 3, 4), |
502 |
| - # Note: this is generated generated by grammar rules but in this phase. |
503 |
| - "tf_try_except": ("%c%-%c%+", 1, 3), |
504 |
| - "tf_tryelsestmt": ("%c%-%c%|else:\n%+%c", 1, 3, 4), |
505 |
| - "tryfinallystmt": ("%|try:\n%+%c%-%|finally:\n%+%c%-\n\n", 1, 5), |
| 542 | + # "yield": ( "yield %c", 0), |
| 543 | + |
506 | 544 | }
|
507 | 545 |
|
508 | 546 |
|
|
0 commit comments