You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-04 09:22:40 +08:00
Start to reduce singleton reductions
This commit is contained in:
@@ -45,6 +45,10 @@ class PythonParser(GenericASTBuilder):
|
|||||||
]
|
]
|
||||||
self.collect = frozenset(nt_list)
|
self.collect = frozenset(nt_list)
|
||||||
|
|
||||||
|
# Reduce singleton reductions in these nonterminals:
|
||||||
|
self.singleton = frozenset(('str', 'joined_str', 'expr', 'store',
|
||||||
|
'inplace_op'))
|
||||||
|
|
||||||
def ast_first_offset(self, ast):
|
def ast_first_offset(self, ast):
|
||||||
if hasattr(ast, 'offset'):
|
if hasattr(ast, 'offset'):
|
||||||
return ast.offset
|
return ast.offset
|
||||||
@@ -154,7 +158,13 @@ class PythonParser(GenericASTBuilder):
|
|||||||
return token.kind
|
return token.kind
|
||||||
|
|
||||||
def nonterminal(self, nt, args):
|
def nonterminal(self, nt, args):
|
||||||
if nt in self.collect and len(args) > 1:
|
n = len(args)
|
||||||
|
|
||||||
|
# Use this to find lots of singleton rule
|
||||||
|
# if n == 1 and nt not in self.singleton:
|
||||||
|
# print("XXX", nt)
|
||||||
|
|
||||||
|
if nt in self.collect and n > 1:
|
||||||
#
|
#
|
||||||
# Collect iterated thingies together. That is rather than
|
# Collect iterated thingies together. That is rather than
|
||||||
# stmts -> stmts stmt -> stmts stmt -> ...
|
# stmts -> stmts stmt -> stmts stmt -> ...
|
||||||
@@ -162,6 +172,8 @@ class PythonParser(GenericASTBuilder):
|
|||||||
#
|
#
|
||||||
rv = args[0]
|
rv = args[0]
|
||||||
rv.append(args[1])
|
rv.append(args[1])
|
||||||
|
elif n == 1 and args[0] in self.singleton:
|
||||||
|
rv = GenericASTBuilder.nonterminal(self, nt, args[0])
|
||||||
else:
|
else:
|
||||||
rv = GenericASTBuilder.nonterminal(self, nt, args)
|
rv = GenericASTBuilder.nonterminal(self, nt, args)
|
||||||
return rv
|
return rv
|
||||||
|
@@ -898,11 +898,13 @@ class SourceWalker(GenericASTTraversal, object):
|
|||||||
"""
|
"""
|
||||||
self.write(self.indent, 'exec ')
|
self.write(self.indent, 'exec ')
|
||||||
self.preorder(node[0])
|
self.preorder(node[0])
|
||||||
if not node[1][0].isNone():
|
|
||||||
|
if not node[1].isNone():
|
||||||
sep = ' in '
|
sep = ' in '
|
||||||
for subnode in node[1]:
|
for subnode in node[1]:
|
||||||
self.write(sep); sep = ", "
|
self.write(sep); sep = ", "
|
||||||
self.preorder(subnode)
|
self.preorder(subnode)
|
||||||
|
|
||||||
self.println()
|
self.println()
|
||||||
self.prune() # stop recursing
|
self.prune() # stop recursing
|
||||||
|
|
||||||
@@ -1316,8 +1318,8 @@ class SourceWalker(GenericASTTraversal, object):
|
|||||||
pass
|
pass
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
ast = ast[0][0]
|
list_comp = ast[0]
|
||||||
n = ast[iter_index]
|
n = list_comp[iter_index]
|
||||||
assert n == 'list_iter', n
|
assert n == 'list_iter', n
|
||||||
|
|
||||||
# FIXME: I'm not totally sure this is right.
|
# FIXME: I'm not totally sure this is right.
|
||||||
@@ -1406,8 +1408,9 @@ class SourceWalker(GenericASTTraversal, object):
|
|||||||
if node == 'set_comp':
|
if node == 'set_comp':
|
||||||
ast = ast[0][0][0]
|
ast = ast[0][0][0]
|
||||||
else:
|
else:
|
||||||
ast = ast[0][0][0][0][0]
|
ast = ast[0][0][0][0]
|
||||||
|
|
||||||
|
assert ast == 'list_comp'
|
||||||
n = ast[1]
|
n = ast[1]
|
||||||
collection = node[-3]
|
collection = node[-3]
|
||||||
list_if = None
|
list_if = None
|
||||||
|
Reference in New Issue
Block a user