Better const key sorting

This commit is contained in:
rocky
2024-06-03 07:18:37 -04:00
parent 0b9a3c668c
commit 404c46c6bb

View File

@@ -44,7 +44,7 @@ maxint = sys.maxsize
# children. For example, "call" has precedence 2 so we don't get # children. For example, "call" has precedence 2 so we don't get
# additional the additional parenthesis of: ".. op (call())". However # additional the additional parenthesis of: ".. op (call())". However
# for call's children, it parameters, we set the the precedence high, # for call's children, it parameters, we set the the precedence high,
# say to 100, to make sure we avoid additional prenthesis in # say to 100, to make sure we avoid additional parenthesis in
# call((.. op ..)). # call((.. op ..)).
NO_PARENTHESIS_EVER = 100 NO_PARENTHESIS_EVER = 100
@@ -130,7 +130,7 @@ LINE_LENGTH = 80
# Some parse trees created below are used for comparing code # Some parse trees created below are used for comparing code
# fragments (like "return None" at the end of functions). # fragments (like "return None" at the end of functions).
ASSIGN_DOC_STRING = lambda doc_string, doc_load: SyntaxTree( ASSIGN_DOC_STRING = lambda doc_string, doc_load: SyntaxTree( # noqa
"assign", "assign",
[ [
SyntaxTree( SyntaxTree(
@@ -247,12 +247,17 @@ TABLE_DIRECT = {
"assert_expr_or": ("%c or %c", 0, 2), "assert_expr_or": ("%c or %c", 0, 2),
"assert_expr_and": ("%c and %c", 0, 2), "assert_expr_and": ("%c and %c", 0, 2),
"assign": (
"%|%c = %p\n",
-1,
(0, ("expr", "branch_op"), PRECEDENCE["tuple_list_starred"] + 1)
),
"attribute": ("%c.%[1]{pattr}", (0, "expr")), "attribute": ("%c.%[1]{pattr}", (0, "expr")),
# This nonterminal we create on the fly in semantic routines # This nonterminal we create on the fly in semantic routines
"attribute_w_parens": ("(%c).%[1]{pattr}", (0, "expr")), "attribute_w_parens": ("(%c).%[1]{pattr}", (0, "expr")),
"assign": ("%|%c = %p\n", -1, (0, 200)),
# The 2nd parameter should have a = suffix. # The 2nd parameter should have a = suffix.
# There is a rule with a 4th parameter "store" # There is a rule with a 4th parameter "store"
# which we don't use here. # which we don't use here.
@@ -268,6 +273,15 @@ TABLE_DIRECT = {
(0, -1, ", ", NO_PARENTHESIS_EVER) (0, -1, ", ", NO_PARENTHESIS_EVER)
), ),
"call_stmt": ( "%|%p\n",
# When a call statement contains only a named_expr (:=)
# the named_expr should have parenthesis around it.
(0, PRECEDENCE["named_expr"]-1)),
# "classdef": (), # handled by n_classdef()
# A custom rule in n_function def distinguishes whether to call this or
# function_def_async
"classdefdeco": ("\n\n%c", 0), "classdefdeco": ("\n\n%c", 0),
"classdefdeco1": ("%|@%c\n%c", 0, 1), "classdefdeco1": ("%|@%c\n%c", 0, 1),
@@ -283,20 +297,30 @@ TABLE_DIRECT = {
"continue": ("%|continue\n",), "continue": ("%|continue\n",),
# "classdef": (), # handled by n_classdef()
# A custom rule in n_function def distinguishes whether to call this or
# function_def_async
"delete_subscript": ( "delete_subscript": (
"%|del %p[%c]\n", "%|del %p[%c]\n",
(0, "expr", PRECEDENCE["subscript"]), (0, "expr", PRECEDENCE["subscript"]),
(1, "expr"), (1, "expr"),
), ),
"designList": ("%c = %c", 0, -1),
"dict_comp_body": ("%c: %c", 1, 0),
"elifelifstmt": ("%|elif %c:\n%+%c%-%c", 0, 1, 3),
"elifelsestmt": ("%|elif %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3),
"elifelsestmtr": ("%|elif %c:\n%+%c%-%|else:\n%+%c%-\n\n", 0, 1, 2),
"elifelsestmtr2": (
"%|elif %c:\n%+%c%-%|else:\n%+%c%-\n\n",
0,
1,
3,
), # has COME_FROM
"elifstmt": ("%|elif %c:\n%+%c%-", 0, 1),
"except": ("%|except:\n%+%c%-", 3), "except": ("%|except:\n%+%c%-", 3),
"except_cond1": ("%|except %c:\n", 1), "except_cond1": ("%|except %c:\n", 1),
"except_cond2": ("%|except %c as %c:\n", (1, "expr"), (5, "store")), "except_cond2": ("%|except %c as %c:\n", (1, "expr"), (5, "store")),
"except_suite": ("%+%c%-%C", 0, (1, maxint, "")), "except_suite": ("%+%c%-%C", 0, (1, maxint, "")),
# In Python 3.6+, this is more complicated in the presence of "returns" # In Python 3.6+, this is more complicated in the presence of "returns"
"except_suite_finalize": ("%+%c%-%C", 1, (3, maxint, "")), "except_suite_finalize": ("%+%c%-%C", 1, (3, maxint, "")),
@@ -332,15 +356,12 @@ TABLE_DIRECT = {
-2, -2,
), ),
"function_def": ("\n\n%|def %c\n", -2), # -2 to handle closures
"function_def_deco": ("\n\n%c", 0),
"gen_comp_body": ("%c", 0),
"get_iter": ("iter(%c)", (0, "expr"),), "get_iter": ("iter(%c)", (0, "expr"),),
"set_comp_body": ("%c", 0),
"gen_comp_body": ("%c", 0),
"dict_comp_body": ("%c: %c", 1, 0),
"designList": ("%c = %c", 0, -1),
"ret_and": ("%c and %c", 0, 2),
"or": ("%p or %p", (0, PRECEDENCE["or"]), (1, PRECEDENCE["or"])),
"ret_or": ("%c or %c", 0, 2),
"if_exp": ("%p if %c else %c", (2, "expr", 27), 0, 4), "if_exp": ("%p if %c else %c", (2, "expr", 27), 0, 4),
"if_exp_lambda": ("%p if %c else %c", (2, "expr", 27), (0, "expr"), 4), "if_exp_lambda": ("%p if %c else %c", (2, "expr", 27), (0, "expr"), 4),
"if_exp_true": ("%p if 1 else %c", (0, "expr", 27), 2), "if_exp_true": ("%p if 1 else %c", (0, "expr", 27), 2),
@@ -353,28 +374,6 @@ TABLE_DIRECT = {
), ),
"if_exp_not_lambda": ("%p if not %c else %c", (2, "expr", 27), 0, 4), "if_exp_not_lambda": ("%p if not %c else %c", (2, "expr", 27), 0, 4),
"function_def": ("\n\n%|def %c\n", -2), # -2 to handle closures
"function_def_deco": ("\n\n%c", 0),
# This is only generated by transform
# it is a string at the beginning of a function that is *not* a docstring
# 3.7 test_fstring.py tests for this kind of crap.
# For compatibility with older Python, we'll use "%" instead of
# a format string.
"string_at_beginning": ('%|"%%s" %% %c\n', 0),
"call_stmt": ( "%|%p\n",
# When a call statement contains only a named_expr (:=)
# the named_expr should have parenthesis around it.
(0, PRECEDENCE["named_expr"]-1)),
"ifstmt": (
"%|if %c:\n%+%c%-",
0, # "testexpr" or "testexpr_then"
1, # "_ifstmts_jump" or "return_stmts"
),
"iflaststmt": ("%|if %c:\n%+%c%-", 0, 1),
"iflaststmtl": ("%|if %c:\n%+%c%-", 0, 1),
"testtrue": ("not %p", (0, PRECEDENCE["unary_not"])),
# Generally the args here are 0: (some sort of) "testexpr", # Generally the args here are 0: (some sort of) "testexpr",
# 1: (some sort of) "cstmts_opt", # 1: (some sort of) "cstmts_opt",
# 2 or 3: "else_suite" # 2 or 3: "else_suite"
@@ -384,20 +383,21 @@ TABLE_DIRECT = {
"ifelsestmt": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3), "ifelsestmt": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3),
"ifelsestmtc": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3), "ifelsestmtc": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3),
"ifelsestmtl": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3), "ifelsestmtl": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3),
# These are created only via transformation
# This is created only via transformation
"ifelifstmt": ("%|if %c:\n%+%c%-%c", 0, 1, 3), # "testexpr" or "testexpr_then" "ifelifstmt": ("%|if %c:\n%+%c%-%c", 0, 1, 3), # "testexpr" or "testexpr_then"
"elifelifstmt": ("%|elif %c:\n%+%c%-%c", 0, 1, 3),
"elifstmt": ("%|elif %c:\n%+%c%-", 0, 1),
"elifelsestmt": ("%|elif %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 3),
"ifelsestmtr": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 2), "ifelsestmtr": ("%|if %c:\n%+%c%-%|else:\n%+%c%-", 0, 1, 2),
"ifelsestmtr2": ("%|if %c:\n%+%c%-%|else:\n%+%c%-\n\n", 0, 1, 3), # has COME_FROM "ifelsestmtr2": ("%|if %c:\n%+%c%-%|else:\n%+%c%-\n\n", 0, 1, 3), # has COME_FROM
"elifelsestmtr": ("%|elif %c:\n%+%c%-%|else:\n%+%c%-\n\n", 0, 1, 2), "iflaststmt": ("%|if %c:\n%+%c%-", 0, 1),
"elifelsestmtr2": (
"%|elif %c:\n%+%c%-%|else:\n%+%c%-\n\n", "iflaststmtl": ("%|if %c:\n%+%c%-", 0, 1),
0,
1, "ifstmt": (
3, "%|if %c:\n%+%c%-",
), # has COME_FROM 0, # "testexpr" or "testexpr_then"
1, # "_ifstmts_jump" or "return_stmts"
),
"import": ("%|import %c\n", 2), "import": ("%|import %c\n", 2),
"importlist": ("%C", (0, maxint, ", ")), "importlist": ("%C", (0, maxint, ", ")),
@@ -415,6 +415,7 @@ TABLE_DIRECT = {
"kv": ("%c: %c", 3, 1), "kv": ("%c: %c", 3, 1),
"kv2": ("%c: %c", 1, 2), "kv2": ("%c: %c", 1, 2),
"kwarg": ("%[0]{pattr}=%c", 1), # Change when Python 2 does LOAD_STR "kwarg": ("%[0]{pattr}=%c", 1), # Change when Python 2 does LOAD_STR
"kwargs": ("%D", (0, maxint, ", ")), "kwargs": ("%D", (0, maxint, ", ")),
"kwargs1": ("%D", (0, maxint, ", ")), "kwargs1": ("%D", (0, maxint, ", ")),
@@ -436,6 +437,10 @@ TABLE_DIRECT = {
# and this is added, as a transformation rule. # and this is added, as a transformation rule.
"newline": ("\n"), "newline": ("\n"),
"or": ("%p or %p", (0, PRECEDENCE["or"]), (1, PRECEDENCE["or"])),
"pass": ("%|pass\n",),
"print_item": (", %c", 0), "print_item": (", %c", 0),
"print_items_nl_stmt": ("%|print %c%c\n", 0, 2), "print_items_nl_stmt": ("%|print %c%c\n", 0, 2),
"print_items_stmt": ("%|print %c%c,\n", 0, 2), # Python 2 only "print_items_stmt": ("%|print %c%c,\n", 0, 2), # Python 2 only
@@ -445,16 +450,19 @@ TABLE_DIRECT = {
"print_to_items": ("%C", (0, 2, ", ")), "print_to_items": ("%C", (0, 2, ", ")),
"print_to_nl": ("%|print >> %c, %c\n", 0, 1), "print_to_nl": ("%|print >> %c, %c\n", 0, 1),
"pass": ("%|pass\n",),
"raise_stmt0": ("%|raise\n",), "raise_stmt0": ("%|raise\n",),
"raise_stmt1": ("%|raise %c\n", 0), "raise_stmt1": ("%|raise %c\n", 0),
"raise_stmt3": ("%|raise %c, %c, %c\n", 0, 1, 2), "raise_stmt3": ("%|raise %c, %c, %c\n", 0, 1, 2),
"ret_and": ("%c and %c", 0, 2),
"ret_or": ("%c or %c", 0, 2),
# Note: we have a custom rule, which calls when we don't # Note: we have a custom rule, which calls when we don't
# have "return None" # have "return None"
"return": ( "%|return %c\n", 0), "return": ( "%|return %c\n", 0),
"set_comp_body": ("%c", 0),
"set_iter": ( "%c", 0 ), "set_iter": ( "%c", 0 ),
"return_if_stmt": ("return %c\n", 0), "return_if_stmt": ("return %c\n", 0),
@@ -491,6 +499,13 @@ TABLE_DIRECT = {
(0, "expr") (0, "expr")
), ),
# This is only generated by transform
# it is a string at the beginning of a function that is *not* a docstring
# 3.7 test_fstring.py tests for this kind of crap.
# For compatibility with older Python, we'll use "%" instead of
# a format string.
"string_at_beginning": ('%|"%%s" %% %c\n', 0),
"subscript": ( "subscript": (
"%p[%p]", "%p[%p]",
(0, "expr", PRECEDENCE["subscript"]), (0, "expr", PRECEDENCE["subscript"]),
@@ -503,13 +518,16 @@ TABLE_DIRECT = {
(1, "expr", NO_PARENTHESIS_EVER) (1, "expr", NO_PARENTHESIS_EVER)
), ),
"testtrue": ("not %p", (0, PRECEDENCE["unary_not"])),
# Note: this is generated generated by grammar rules but in this phase.
"tf_try_except": ("%c%-%c%+", 1, 3),
"tf_tryelsestmt": ("%c%-%c%|else:\n%+%c", 1, 3, 4),
"try_except": ("%|try:\n%+%c%-%c\n\n", 1, 3), "try_except": ("%|try:\n%+%c%-%c\n\n", 1, 3),
"tryelsestmt": ("%|try:\n%+%c%-%c%|else:\n%+%c%-\n\n", 1, 3, 4), "tryelsestmt": ("%|try:\n%+%c%-%c%|else:\n%+%c%-\n\n", 1, 3, 4),
"tryelsestmtc": ("%|try:\n%+%c%-%c%|else:\n%+%c%-", 1, 3, 4), "tryelsestmtc": ("%|try:\n%+%c%-%c%|else:\n%+%c%-", 1, 3, 4),
"tryelsestmtl": ("%|try:\n%+%c%-%c%|else:\n%+%c%-", 1, 3, 4), "tryelsestmtl": ("%|try:\n%+%c%-%c%|else:\n%+%c%-", 1, 3, 4),
# Note: this is generated generated by grammar rules but in this phase.
"tf_try_except": ("%c%-%c%+", 1, 3),
"tf_tryelsestmt": ("%c%-%c%|else:\n%+%c", 1, 3, 4),
"tryfinallystmt": ("%|try:\n%+%c%-%|finally:\n%+%c%-\n\n", 1, 5), "tryfinallystmt": ("%|try:\n%+%c%-%|finally:\n%+%c%-\n\n", 1, 5),
# unary_op (formerly "unary_expr") is the Python AST UnaryOp # unary_op (formerly "unary_expr") is the Python AST UnaryOp