More 3.x "if" checking. Abbreviate stmts->sstmt

This commit is contained in:
rocky
2020-01-26 02:58:33 -05:00
parent 7721fbd276
commit 33918bd9d2
5 changed files with 17 additions and 9 deletions

View File

@@ -89,7 +89,7 @@ class PythonParser(GenericASTBuilder):
# singleton reduction that we can simplify. It also happens to be optional
# in its other derivation
self.optional_nt |= frozenset(
("come_froms", "suite_stmts", "l_stmts_opt", "c_stmts_opt", "stmts_opt")
("come_froms", "suite_stmts", "l_stmts_opt", "c_stmts_opt", "stmts_opt", "stmt")
)
# Reduce singleton reductions in these nonterminals:

View File

@@ -380,7 +380,6 @@ class Python3Parser(PythonParser):
iflaststmt ::= testexpr _iflaststmts_jump
ifelsestmt ::= testexpr stmts_opt jump_absolute_else else_suite
ifelsestmt ::= testexpr stmts_opt jump_forward_else else_suite _come_froms
iflaststmt ::= testexpr _ifstmts_jump
else_suite ::= stmts

View File

@@ -20,11 +20,12 @@ def ifstmt(self, lhs, n, rule, ast, tokens, first, last):
last_offset = tokens[l].offset
for i in range(first, l):
t = tokens[i]
if t.kind == "POP_JUMP_IF_FALSE":
# instead of POP_JUMP_IF, should we use op attributes?
if t.kind in ("POP_JUMP_IF_FALSE", "POP_JUMP_IF_TRUE"):
pjif_target = t.attr
if pjif_target > last_offset:
# In come cases, where we have long bytecode, a
# "POP_JUMP_IF_FALSE" offset might be too
# "POP_JUMP_IF_TRUE/FALSE" offset might be too
# large for the instruction; so instead it
# jumps to a JUMP_FORWARD. Allow that here.
if tokens[l] == "JUMP_FORWARD":

View File

@@ -1103,7 +1103,10 @@ class SourceWalker(GenericASTTraversal, object):
code = Code(cn.attr, self.scanner, self.currentclass)
ast = self.build_ast(code._tokens, code._customize)
self.customize(code._customize)
ast = ast[0][0][0]
# Remove single reductions as in ("stmts", "sstmt"):
while len(ast) == 1:
ast = ast[0]
n = ast[iter_index]
assert n == "comp_iter", n
@@ -1368,7 +1371,11 @@ class SourceWalker(GenericASTTraversal, object):
code = Code(node[1].attr, self.scanner, self.currentclass)
ast = self.build_ast(code._tokens, code._customize)
self.customize(code._customize)
ast = ast[0][0][0]
# Remove single reductions as in ("stmts", "sstmt"):
while len(ast) == 1:
ast = ast[0]
store = ast[3]
collection = node[collection_index]
@@ -2208,6 +2215,7 @@ class SourceWalker(GenericASTTraversal, object):
pass
return
# This code is only for Python 1.x - 2.1 ish!
def get_tuple_parameter(self, ast, name):
"""
If the name of the formal parameter starts with dot,
@@ -2224,8 +2232,8 @@ class SourceWalker(GenericASTTraversal, object):
assert ast == "stmts"
for i in range(len(ast)):
# search for an assign-statement
assert ast[i][0] == "stmt"
node = ast[i][0][0]
assert ast[i] == "sstmt"
node = ast[i][0]
if node == "assign" and node[0] == ASSIGN_TUPLE_PARAM(name):
# okay, this assigns '.n' to something
del ast[i]

View File

@@ -346,7 +346,7 @@ class TreeTransform(GenericASTTraversal, object):
def n_stmts(self, node):
if node.first_child() == "SETUP_ANNOTATIONS":
prev = node[0][0][0]
prev = node[0][0]
new_stmts = [node[0]]
for i, sstmt in enumerate(node[1:]):
ann_assign = sstmt[0][0]