Skip to content

Commit 404c46c

Browse files
committed
Better const key sorting
1 parent 0b9a3c6 commit 404c46c

File tree

1 file changed

+70
-52
lines changed

1 file changed

+70
-52
lines changed

uncompyle6/semantics/consts.py

Lines changed: 70 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
# children. For example, "call" has precedence 2 so we don't get
4545
# additional the additional parenthesis of: ".. op (call())". However
4646
# 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
4848
# call((.. op ..)).
4949

5050
NO_PARENTHESIS_EVER = 100
@@ -130,7 +130,7 @@
130130
# Some parse trees created below are used for comparing code
131131
# fragments (like "return None" at the end of functions).
132132

133-
ASSIGN_DOC_STRING = lambda doc_string, doc_load: SyntaxTree(
133+
ASSIGN_DOC_STRING = lambda doc_string, doc_load: SyntaxTree( # noqa
134134
"assign",
135135
[
136136
SyntaxTree(
@@ -247,12 +247,17 @@
247247
"assert_expr_or": ("%c or %c", 0, 2),
248248
"assert_expr_and": ("%c and %c", 0, 2),
249249

250+
"assign": (
251+
"%|%c = %p\n",
252+
-1,
253+
(0, ("expr", "branch_op"), PRECEDENCE["tuple_list_starred"] + 1)
254+
),
255+
250256
"attribute": ("%c.%[1]{pattr}", (0, "expr")),
251257

252258
# This nonterminal we create on the fly in semantic routines
253259
"attribute_w_parens": ("(%c).%[1]{pattr}", (0, "expr")),
254260

255-
"assign": ("%|%c = %p\n", -1, (0, 200)),
256261
# The 2nd parameter should have a = suffix.
257262
# There is a rule with a 4th parameter "store"
258263
# which we don't use here.
@@ -268,6 +273,15 @@
268273
(0, -1, ", ", NO_PARENTHESIS_EVER)
269274
),
270275

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+
271285
"classdefdeco": ("\n\n%c", 0),
272286
"classdefdeco1": ("%|@%c\n%c", 0, 1),
273287

@@ -283,20 +297,30 @@
283297

284298
"continue": ("%|continue\n",),
285299

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-
290300
"delete_subscript": (
291301
"%|del %p[%c]\n",
292302
(0, "expr", PRECEDENCE["subscript"]),
293303
(1, "expr"),
294304
),
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),
295318

296319
"except": ("%|except:\n%+%c%-", 3),
297320
"except_cond1": ("%|except %c:\n", 1),
298321
"except_cond2": ("%|except %c as %c:\n", (1, "expr"), (5, "store")),
299322
"except_suite": ("%+%c%-%C", 0, (1, maxint, "")),
323+
300324
# In Python 3.6+, this is more complicated in the presence of "returns"
301325
"except_suite_finalize": ("%+%c%-%C", 1, (3, maxint, "")),
302326

@@ -332,15 +356,12 @@
332356
-2,
333357
),
334358

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),
336361

337-
"set_comp_body": ("%c", 0),
338362
"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+
344365
"if_exp": ("%p if %c else %c", (2, "expr", 27), 0, 4),
345366
"if_exp_lambda": ("%p if %c else %c", (2, "expr", 27), (0, "expr"), 4),
346367
"if_exp_true": ("%p if 1 else %c", (0, "expr", 27), 2),
@@ -353,28 +374,6 @@
353374
),
354375
"if_exp_not_lambda": ("%p if not %c else %c", (2, "expr", 27), 0, 4),
355376

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"])),
378377
# Generally the args here are 0: (some sort of) "testexpr",
379378
# 1: (some sort of) "cstmts_opt",
380379
# 2 or 3: "else_suite"
@@ -384,20 +383,21 @@
384383
"ifelsestmt": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3),
385384
"ifelsestmtc": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3),
386385
"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
388388
"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+
392390
"ifelsestmtr": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 2),
393391
"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+
),
401401

402402
"import": ("%|import %c\n", 2),
403403
"importlist": ("%C", (0, maxint, ", ")),
@@ -415,6 +415,7 @@
415415

416416
"kv": ("%c: %c", 3, 1),
417417
"kv2": ("%c: %c", 1, 2),
418+
418419
"kwarg": ("%[0]{pattr}=%c", 1), # Change when Python 2 does LOAD_STR
419420
"kwargs": ("%D", (0, maxint, ", ")),
420421
"kwargs1": ("%D", (0, maxint, ", ")),
@@ -436,6 +437,10 @@
436437
# and this is added, as a transformation rule.
437438
"newline": ("\n"),
438439

440+
"or": ("%p or %p", (0, PRECEDENCE["or"]), (1, PRECEDENCE["or"])),
441+
442+
"pass": ("%|pass\n",),
443+
439444
"print_item": (", %c", 0),
440445
"print_items_nl_stmt": ("%|print %c%c\n", 0, 2),
441446
"print_items_stmt": ("%|print %c%c,\n", 0, 2), # Python 2 only
@@ -445,16 +450,19 @@
445450
"print_to_items": ("%C", (0, 2, ", ")),
446451
"print_to_nl": ("%|print >> %c, %c\n", 0, 1),
447452

448-
"pass": ("%|pass\n",),
449-
450453
"raise_stmt0": ("%|raise\n",),
451454
"raise_stmt1": ("%|raise %c\n", 0),
452455
"raise_stmt3": ("%|raise %c, %c, %c\n", 0, 1, 2),
453456

457+
"ret_and": ("%c and %c", 0, 2),
458+
"ret_or": ("%c or %c", 0, 2),
459+
454460
# Note: we have a custom rule, which calls when we don't
455461
# have "return None"
456462
"return": ( "%|return %c\n", 0),
457463

464+
"set_comp_body": ("%c", 0),
465+
458466
"set_iter": ( "%c", 0 ),
459467

460468
"return_if_stmt": ("return %c\n", 0),
@@ -491,6 +499,13 @@
491499
(0, "expr")
492500
),
493501

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+
494509
"subscript": (
495510
"%p[%p]",
496511
(0, "expr", PRECEDENCE["subscript"]),
@@ -503,13 +518,16 @@
503518
(1, "expr", NO_PARENTHESIS_EVER)
504519
),
505520

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+
506527
"try_except": ("%|try:\n%+%c%-%c\n\n", 1, 3),
507528
"tryelsestmt": ("%|try:\n%+%c%-%c%|else:\n%+%c%-\n\n", 1, 3, 4),
508529
"tryelsestmtc": ("%|try:\n%+%c%-%c%|else:\n%+%c%-", 1, 3, 4),
509530
"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),
513531
"tryfinallystmt": ("%|try:\n%+%c%-%|finally:\n%+%c%-\n\n", 1, 5),
514532

515533
# unary_op (formerly "unary_expr") is the Python AST UnaryOp

0 commit comments

Comments
 (0)