Start to reduce singleton reductions

This commit is contained in:
rocky
2017-12-06 12:14:42 -05:00
parent 78e8b93125
commit a4e9410c07
2 changed files with 20 additions and 5 deletions

View File

@@ -45,6 +45,10 @@ class PythonParser(GenericASTBuilder):
]
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):
if hasattr(ast, 'offset'):
return ast.offset
@@ -154,7 +158,13 @@ class PythonParser(GenericASTBuilder):
return token.kind
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
# stmts -> stmts stmt -> stmts stmt -> ...
@@ -162,6 +172,8 @@ class PythonParser(GenericASTBuilder):
#
rv = args[0]
rv.append(args[1])
elif n == 1 and args[0] in self.singleton:
rv = GenericASTBuilder.nonterminal(self, nt, args[0])
else:
rv = GenericASTBuilder.nonterminal(self, nt, args)
return rv

View File

@@ -898,11 +898,13 @@ class SourceWalker(GenericASTTraversal, object):
"""
self.write(self.indent, 'exec ')
self.preorder(node[0])
if not node[1][0].isNone():
if not node[1].isNone():
sep = ' in '
for subnode in node[1]:
self.write(sep); sep = ", "
self.preorder(subnode)
self.println()
self.prune() # stop recursing
@@ -1316,8 +1318,8 @@ class SourceWalker(GenericASTTraversal, object):
pass
pass
else:
ast = ast[0][0]
n = ast[iter_index]
list_comp = ast[0]
n = list_comp[iter_index]
assert n == 'list_iter', n
# FIXME: I'm not totally sure this is right.
@@ -1406,8 +1408,9 @@ class SourceWalker(GenericASTTraversal, object):
if node == 'set_comp':
ast = ast[0][0][0]
else:
ast = ast[0][0][0][0][0]
ast = ast[0][0][0][0]
assert ast == 'list_comp'
n = ast[1]
collection = node[-3]
list_if = None