Reduce unecessary grammar rules in 2.x

This commit is contained in:
rocky
2017-11-22 13:06:05 -05:00
parent 288516d8c2
commit c0f0485754
2 changed files with 31 additions and 24 deletions

View File

@@ -256,19 +256,25 @@ class Python2Parser(PythonParser):
PyPy adds custom rules here as well PyPy adds custom rules here as well
""" """
for opname, v in list(customize.items()): if 'PyPy' in customize:
opname_base = opname[:opname.rfind('_')] # PyPy-specific customizations
if opname == 'PyPy': self.addRule("""
self.addRule(""" stmt ::= assign3_pypy
stmt ::= assign3_pypy stmt ::= assign2_pypy
stmt ::= assign2_pypy assign3_pypy ::= expr expr expr designator designator designator
assign3_pypy ::= expr expr expr designator designator designator assign2_pypy ::= expr expr designator designator
assign2_pypy ::= expr expr designator designator list_compr ::= expr BUILD_LIST_FROM_ARG _for designator list_iter
list_compr ::= expr BUILD_LIST_FROM_ARG _for designator list_iter JUMP_BACK
JUMP_BACK """, nop_func)
""", nop_func) for i, token in enumerate(tokens):
opname = token.kind
# FIXME: remove the "v" thing in the code below
if opname in customize:
v = customize[opname]
else:
continue continue
elif opname_base in ('BUILD_LIST', 'BUILD_TUPLE', 'BUILD_SET'): opname_base = opname[:opname.rfind('_')]
if opname_base in ('BUILD_LIST', 'BUILD_TUPLE', 'BUILD_SET'):
thousands = (v//1024) thousands = (v//1024)
thirty32s = ((v//32) % 32) thirty32s = ((v//32) % 32)
if thirty32s > 0: if thirty32s > 0:
@@ -348,8 +354,9 @@ class Python2Parser(PythonParser):
# no longer need to add a rule # no longer need to add a rule
continue continue
elif opname_base == 'MAKE_FUNCTION': elif opname_base == 'MAKE_FUNCTION':
self.addRule('mklambda ::= %s LOAD_LAMBDA %s' % if i > 0 and tokens[i-1] == 'LOAD_LAMBDA':
('pos_arg '*v, opname), nop_func) self.addRule('mklambda ::= %s LOAD_LAMBDA %s' %
('pos_arg '*v, opname), nop_func)
rule = 'mkfunc ::= %s LOAD_CONST %s' % ('expr '*v, opname) rule = 'mkfunc ::= %s LOAD_CONST %s' % ('expr '*v, opname)
elif opname_base == 'MAKE_CLOSURE': elif opname_base == 'MAKE_CLOSURE':
# FIXME: use add_unique_rules to tidy this up. # FIXME: use add_unique_rules to tidy this up.

View File

@@ -625,20 +625,20 @@ class Python3Parser(PythonParser):
# Loop over instructions adding custom grammar rules based on # Loop over instructions adding custom grammar rules based on
# a specific instruction seen. # a specific instruction seen.
if 'PyPy' in customize:
self.addRule("""
stmt ::= assign3_pypy
stmt ::= assign2_pypy
assign3_pypy ::= expr expr expr designator designator designator
assign2_pypy ::= expr expr designator designator
""", nop_func)
for i, token in enumerate(tokens): for i, token in enumerate(tokens):
opname = token.kind opname = token.kind
opname_base = opname[:opname.rfind('_')] opname_base = opname[:opname.rfind('_')]
# The order listed is roughly alphabetic by instruction opcode. # The order of opname listed is roughly sorted below
if opname == 'PyPy': if opname_base == 'BUILD_CONST_KEY_MAP':
self.addRule("""
stmt ::= assign3_pypy
stmt ::= assign2_pypy
assign3_pypy ::= expr expr expr designator designator designator
assign2_pypy ::= expr expr designator designator
""", nop_func)
continue
elif opname_base == 'BUILD_CONST_KEY_MAP':
# This is in 3.6+ # This is in 3.6+
kvlist_n = 'expr ' * (token.attr) kvlist_n = 'expr ' * (token.attr)
rule = "mapexpr ::= %sLOAD_CONST %s" % (kvlist_n, opname) rule = "mapexpr ::= %sLOAD_CONST %s" % (kvlist_n, opname)