dict_unmap -> dict_unpack matches Python AST better

This commit is contained in:
rocky
2021-12-26 19:03:58 -05:00
parent 3234673422
commit 165115289a
6 changed files with 56 additions and 48 deletions

View File

@@ -775,7 +775,7 @@ class Python3Parser(PythonParser):
# FIXME: Use the attr # FIXME: Use the attr
# so this doesn't run into exponential parsing time. # so this doesn't run into exponential parsing time.
if opname.startswith("BUILD_MAP_UNPACK"): if opname.startswith("BUILD_MAP_UNPACK"):
# FIXME: start here. The LHS should be dict_unmap, not dict. # FIXME: start here. The LHS should be dict_unpack, not dict.
# FIXME: really we need a combination of dict_entry-like things. # FIXME: really we need a combination of dict_entry-like things.
# It just so happens the most common case is not to mix # It just so happens the most common case is not to mix
# dictionary comphensions with dictionary, elements # dictionary comphensions with dictionary, elements
@@ -786,8 +786,8 @@ class Python3Parser(PythonParser):
) )
self.addRule(rule, nop_func) self.addRule(rule, nop_func)
rule = """ rule = """
expr ::= dict_unmap expr ::= dict_unpack
dict_unmap ::= %s%s dict_unpack ::= %s%s
""" % ( """ % (
"expr " * token.attr, "expr " * token.attr,
opname, opname,

View File

@@ -201,8 +201,8 @@ class Python35Parser(Python34Parser):
self.addRule(rules_str, nop_func) self.addRule(rules_str, nop_func)
elif opname == 'BUILD_MAP_UNPACK': elif opname == 'BUILD_MAP_UNPACK':
self.addRule(""" self.addRule("""
expr ::= dict_unmap expr ::= dict_unpack
dict_unmap ::= dict_comp BUILD_MAP_UNPACK dict_unpack ::= dict_comp BUILD_MAP_UNPACK
""", nop_func) """, nop_func)
elif opname == 'SETUP_WITH': elif opname == 'SETUP_WITH':

View File

@@ -336,8 +336,8 @@ class Python37BaseParser(PythonParser):
if opname == "BUILD_MAP_UNPACK": if opname == "BUILD_MAP_UNPACK":
self.addRule( self.addRule(
""" """
expr ::= dict_unmap expr ::= dict_unpack
dict_unmap ::= dict_comp BUILD_MAP_UNPACK dict_unpack ::= dict_comp BUILD_MAP_UNPACK
""", """,
nop_func, nop_func,
) )
@@ -367,7 +367,7 @@ class Python37BaseParser(PythonParser):
# FIXME: Use the attr # FIXME: Use the attr
# so this doesn't run into exponential parsing time. # so this doesn't run into exponential parsing time.
if opname.startswith("BUILD_MAP_UNPACK"): if opname.startswith("BUILD_MAP_UNPACK"):
# FIXME: start here. The LHS should be dict_unmap, not dict. # FIXME: start here. The LHS should be dict_unpack, not dict.
# FIXME: really we need a combination of dict_entry-like things. # FIXME: really we need a combination of dict_entry-like things.
# It just so happens the most common case is not to mix # It just so happens the most common case is not to mix
# dictionary comphensions with dictionary, elements # dictionary comphensions with dictionary, elements
@@ -375,8 +375,8 @@ class Python37BaseParser(PythonParser):
rule = "dict ::= %s%s" % ("dict_comp " * token.attr, opname) rule = "dict ::= %s%s" % ("dict_comp " * token.attr, opname)
self.addRule(rule, nop_func) self.addRule(rule, nop_func)
rule = """ rule = """
expr ::= dict_unmap expr ::= dict_unpack
dict_unmap ::= %s%s dict_unpack ::= %s%s
""" % ( """ % (
"expr " * token.attr, "expr " * token.attr,
opname, opname,

View File

@@ -51,7 +51,7 @@ def customize_for_version35(self, version):
(2, "store"), (2, "store"),
3, 3,
), ),
"dict_unmap": ("{**%C}", (0, -1, ", **")), "dict_unpack": ("{**%C}", (0, -1, ", **")),
# "unmapexpr": ( "{**%c}", 0), # done by n_unmapexpr # "unmapexpr": ( "{**%c}", 0), # done by n_unmapexpr
} }
) )

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2019-2020 by Rocky Bernstein # Copyright (c) 2019-2021 by Rocky Bernstein
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@@ -37,14 +37,16 @@ def escape_format(s):
def customize_for_version36(self, version): def customize_for_version36(self, version):
PRECEDENCE["call_kw"] = 0
PRECEDENCE["call_kw36"] = 1 # fmt: off
PRECEDENCE["call_ex"] = 1 PRECEDENCE["call_kw"] = 0
PRECEDENCE["call_ex_kw"] = 1 PRECEDENCE["call_kw36"] = 1
PRECEDENCE["call_ex_kw2"] = 1 PRECEDENCE["call_ex"] = 1
PRECEDENCE["call_ex_kw3"] = 1 PRECEDENCE["call_ex_kw"] = 1
PRECEDENCE["call_ex_kw4"] = 1 PRECEDENCE["call_ex_kw2"] = 1
PRECEDENCE["dict_unmap"] = 0 # **{ ... } PRECEDENCE["call_ex_kw3"] = 1
PRECEDENCE["call_ex_kw4"] = 1
PRECEDENCE["dict_pack"] = 0 # **{ ... }
PRECEDENCE["formatted_value1"] = 100 PRECEDENCE["formatted_value1"] = 100
TABLE_DIRECT.update( TABLE_DIRECT.update(
@@ -52,7 +54,7 @@ def customize_for_version36(self, version):
"ann_assign_init_value": ( "ann_assign_init_value": (
"%|%c = %p\n", "%|%c = %p\n",
(-1, "store_annotation"), (-1, "store_annotation"),
(0, "expr", 200), (0, "expr", 200),
), ),
"ann_assign_no_init": ("%|%c\n", (0, "store_annotation")), "ann_assign_no_init": ("%|%c\n", (0, "store_annotation")),
"async_for_stmt36": ( "async_for_stmt36": (
@@ -61,28 +63,32 @@ def customize_for_version36(self, version):
(1, "expr"), (1, "expr"),
(18, "for_block"), (18, "for_block"),
), ),
"call_ex": ("%c(%p)", (0, "expr"), (1, 100)), "call_ex": ("%c(%p)", (0, "expr"), (1, 100)),
"except_return": ("%|except:\n%+%c%-", 3), "except_return": ("%|except:\n%+%c%-", 3),
"func_args36": ("%c(**", 0), "func_args36": ("%c(**", 0),
# This comes from 3.7. Eventually we will rebase from 3.7 # This comes from 3.7. Eventually we will rebase from 3.7
# and then this can go away # and then this can go away
"if_exp37": ("%p if %c else %c", (1, "expr", 27), 0, 3), "if_exp37": ("%p if %c else %c", (1, "expr", 27), 0, 3),
"ifstmtl": ("%|if %c:\n%+%c%-", (0, "testexpr"), (1, "_ifstmts_jumpl")),
"try_except36": ("%|try:\n%+%c%-%c\n\n", 1, -2), "ifstmtl": ("%|if %c:\n%+%c%-",
"tryfinally36": ("%|try:\n%+%c%-%|finally:\n%+%c%-\n\n", (1, "returns"), 3), (0, "testexpr"), (1, "_ifstmts_jumpl")),
"try_except36": ("%|try:\n%+%c%-%c\n\n", 1, -2),
"tryfinally36": ("%|try:\n%+%c%-%|finally:\n%+%c%-\n\n", (1, "returns"), 3),
"tryfinally_return_stmt": ("%|try:\n%+%c%-%|finally:\n%+%|return%-\n\n", 1), "tryfinally_return_stmt": ("%|try:\n%+%c%-%|finally:\n%+%|return%-\n\n", 1),
"unpack_list": ("*%c", (0, "list")), "unpack_list": ("*%c", (0, "list")),
"store_annotation": ("%[1]{pattr}: %c", 0), "store_annotation": ("%[1]{pattr}: %c", 0),
} }
) )
TABLE_R.update( TABLE_R.update(
{ {
"CALL_FUNCTION_EX": ("%c(*%P)", 0, (1, 2, ", ", 100)), "CALL_FUNCTION_EX": ("%c(*%P)", 0, (1, 2, ", ", 100)),
# Not quite right # Not quite right
"CALL_FUNCTION_EX_KW": ("%c(**%C)", 0, (2, 3, ",")), "CALL_FUNCTION_EX_KW": ("%c(**%C)", 0, (2, 3, ",")),
} }
) )
# fmt: on
def build_unpack_tuple_with_call(node): def build_unpack_tuple_with_call(node):
n = node[0] n = node[0]

View File

@@ -22,22 +22,24 @@ from uncompyle6.semantics.consts import (
maxint, maxint,
) )
def customize_for_version37(self, version): def customize_for_version37(self, version):
######################## ########################
# Python 3.7+ changes # Python 3.7+ changes
####################### #######################
PRECEDENCE["attribute37"] = 2 # fmt: off
PRECEDENCE["call_ex"] = 1 PRECEDENCE["attribute37"] = 2
PRECEDENCE["call_ex_kw"] = 1 PRECEDENCE["call_ex"] = 1
PRECEDENCE["call_ex_kw2"] = 1 PRECEDENCE["call_ex_kw"] = 1
PRECEDENCE["call_ex_kw3"] = 1 PRECEDENCE["call_ex_kw2"] = 1
PRECEDENCE["call_ex_kw4"] = 1 PRECEDENCE["call_ex_kw3"] = 1
PRECEDENCE["call_kw"] = 0 PRECEDENCE["call_ex_kw4"] = 1
PRECEDENCE["call_kw36"] = 1 PRECEDENCE["call_kw"] = 0
PRECEDENCE["call_kw36"] = 1
PRECEDENCE["formatted_value1"] = 100 PRECEDENCE["formatted_value1"] = 100
PRECEDENCE["if_exp_37a"] = 28 PRECEDENCE["if_exp_37a"] = 28
PRECEDENCE["if_exp_37b"] = 28 PRECEDENCE["if_exp_37b"] = 28
TABLE_DIRECT.update( TABLE_DIRECT.update(
{ {
@@ -160,11 +162,12 @@ def customize_for_version37(self, version):
"testfalsel": ("not %c", (0, "expr")), "testfalsel": ("not %c", (0, "expr")),
"try_except36": ("%|try:\n%+%c%-%c\n\n", 1, -2), "try_except36": ("%|try:\n%+%c%-%c\n\n", 1, -2),
"tryfinally36": ("%|try:\n%+%c%-%|finally:\n%+%c%-\n\n", (1, "returns"), 3), "tryfinally36": ("%|try:\n%+%c%-%|finally:\n%+%c%-\n\n", (1, "returns"), 3),
"dict_unmap": ("{**%C}", (0, -1, ", **")), "dict_unpack": ("{**%C}", (0, -1, ", **")),
"unpack_list": ("*%c", (0, "list")), "unpack_list": ("*%c", (0, "list")),
"yield_from": ("yield from %c", (0, "expr")), "yield_from": ("yield from %c", (0, "expr")),
} }
) )
# fmt: on
def gen_function_parens_adjust(mapping_key, node): def gen_function_parens_adjust(mapping_key, node):
"""If we can avoid the outer parenthesis """If we can avoid the outer parenthesis
@@ -279,11 +282,8 @@ def customize_for_version37(self, version):
else: else:
template = "%c(%p)" template = "%c(%p)"
self.template_engine( self.template_engine(
(template, (template, (0, "expr"), (1, PRECEDENCE["yield"] - 1)), node
(0, "expr"), )
(1, PRECEDENCE["yield"]-1)),
node
)
self.prec = p self.prec = p
self.prune() self.prune()
else: else:
@@ -299,6 +299,7 @@ def customize_for_version37(self, version):
self.default(node[0]) self.default(node[0])
else: else:
self.default(node) self.default(node)
self.n_compare_chained = n_compare_chained self.n_compare_chained = n_compare_chained
def n_importlist37(node): def n_importlist37(node):
@@ -309,7 +310,7 @@ def customize_for_version37(self, version):
for i in range(n, -1, -1): for i in range(n, -1, -1):
if node[i] != "ROT_TWO": if node[i] != "ROT_TWO":
break break
self.template_engine(("%C", (0, i + 1, ', ')), node) self.template_engine(("%C", (0, i + 1, ", ")), node)
self.prune() self.prune()
return return
@@ -323,4 +324,5 @@ def customize_for_version37(self, version):
self.comprehension_walk_newer(node, iter_index=3, code_index=0) self.comprehension_walk_newer(node, iter_index=3, code_index=0)
self.write("]") self.write("]")
self.prune() self.prune()
self.n_list_comp_async = n_list_comp_async self.n_list_comp_async = n_list_comp_async