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
# so this doesn't run into exponential parsing time.
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.
# It just so happens the most common case is not to mix
# dictionary comphensions with dictionary, elements
@@ -786,8 +786,8 @@ class Python3Parser(PythonParser):
)
self.addRule(rule, nop_func)
rule = """
expr ::= dict_unmap
dict_unmap ::= %s%s
expr ::= dict_unpack
dict_unpack ::= %s%s
""" % (
"expr " * token.attr,
opname,

View File

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

View File

@@ -336,8 +336,8 @@ class Python37BaseParser(PythonParser):
if opname == "BUILD_MAP_UNPACK":
self.addRule(
"""
expr ::= dict_unmap
dict_unmap ::= dict_comp BUILD_MAP_UNPACK
expr ::= dict_unpack
dict_unpack ::= dict_comp BUILD_MAP_UNPACK
""",
nop_func,
)
@@ -367,7 +367,7 @@ class Python37BaseParser(PythonParser):
# FIXME: Use the attr
# so this doesn't run into exponential parsing time.
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.
# It just so happens the most common case is not to mix
# dictionary comphensions with dictionary, elements
@@ -375,8 +375,8 @@ class Python37BaseParser(PythonParser):
rule = "dict ::= %s%s" % ("dict_comp " * token.attr, opname)
self.addRule(rule, nop_func)
rule = """
expr ::= dict_unmap
dict_unmap ::= %s%s
expr ::= dict_unpack
dict_unpack ::= %s%s
""" % (
"expr " * token.attr,
opname,

View File

@@ -51,7 +51,7 @@ def customize_for_version35(self, version):
(2, "store"),
3,
),
"dict_unmap": ("{**%C}", (0, -1, ", **")),
"dict_unpack": ("{**%C}", (0, -1, ", **")),
# "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
# it under the terms of the GNU General Public License as published by
@@ -37,6 +37,8 @@ def escape_format(s):
def customize_for_version36(self, version):
# fmt: off
PRECEDENCE["call_kw"] = 0
PRECEDENCE["call_kw36"] = 1
PRECEDENCE["call_ex"] = 1
@@ -44,7 +46,7 @@ def customize_for_version36(self, version):
PRECEDENCE["call_ex_kw2"] = 1
PRECEDENCE["call_ex_kw3"] = 1
PRECEDENCE["call_ex_kw4"] = 1
PRECEDENCE["dict_unmap"] = 0 # **{ ... }
PRECEDENCE["dict_pack"] = 0 # **{ ... }
PRECEDENCE["formatted_value1"] = 100
TABLE_DIRECT.update(
@@ -64,10 +66,13 @@ def customize_for_version36(self, version):
"call_ex": ("%c(%p)", (0, "expr"), (1, 100)),
"except_return": ("%|except:\n%+%c%-", 3),
"func_args36": ("%c(**", 0),
# This comes from 3.7. Eventually we will rebase from 3.7
# and then this can go away
"if_exp37": ("%p if %c else %c", (1, "expr", 27), 0, 3),
"ifstmtl": ("%|if %c:\n%+%c%-", (0, "testexpr"), (1, "_ifstmts_jumpl")),
"ifstmtl": ("%|if %c:\n%+%c%-",
(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),
@@ -83,6 +88,7 @@ def customize_for_version36(self, version):
"CALL_FUNCTION_EX_KW": ("%c(**%C)", 0, (2, 3, ",")),
}
)
# fmt: on
def build_unpack_tuple_with_call(node):
n = node[0]

View File

@@ -22,11 +22,13 @@ from uncompyle6.semantics.consts import (
maxint,
)
def customize_for_version37(self, version):
########################
# Python 3.7+ changes
#######################
# fmt: off
PRECEDENCE["attribute37"] = 2
PRECEDENCE["call_ex"] = 1
PRECEDENCE["call_ex_kw"] = 1
@@ -160,11 +162,12 @@ def customize_for_version37(self, version):
"testfalsel": ("not %c", (0, "expr")),
"try_except36": ("%|try:\n%+%c%-%c\n\n", 1, -2),
"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")),
"yield_from": ("yield from %c", (0, "expr")),
}
)
# fmt: on
def gen_function_parens_adjust(mapping_key, node):
"""If we can avoid the outer parenthesis
@@ -279,10 +282,7 @@ def customize_for_version37(self, version):
else:
template = "%c(%p)"
self.template_engine(
(template,
(0, "expr"),
(1, PRECEDENCE["yield"]-1)),
node
(template, (0, "expr"), (1, PRECEDENCE["yield"] - 1)), node
)
self.prec = p
self.prune()
@@ -299,6 +299,7 @@ def customize_for_version37(self, version):
self.default(node[0])
else:
self.default(node)
self.n_compare_chained = n_compare_chained
def n_importlist37(node):
@@ -309,7 +310,7 @@ def customize_for_version37(self, version):
for i in range(n, -1, -1):
if node[i] != "ROT_TWO":
break
self.template_engine(("%C", (0, i + 1, ', ')), node)
self.template_engine(("%C", (0, i + 1, ", ")), node)
self.prune()
return
@@ -323,4 +324,5 @@ def customize_for_version37(self, version):
self.comprehension_walk_newer(node, iter_index=3, code_index=0)
self.write("]")
self.prune()
self.n_list_comp_async = n_list_comp_async