More Python 2/3 grammar restriction

This commit is contained in:
rocky
2017-12-14 15:24:01 -05:00
parent 6c552bec07
commit 52f2b9341a
4 changed files with 49 additions and 14 deletions

2
.gitignore vendored
View File

@@ -19,4 +19,4 @@ build
/.venv* /.venv*
/.idea /.idea
/.hypothesis /.hypothesis
./ChangeLog ChangeLog

View File

@@ -475,7 +475,6 @@ class PythonParser(GenericASTBuilder):
expr ::= LOAD_CONST expr ::= LOAD_CONST
expr ::= LOAD_GLOBAL expr ::= LOAD_GLOBAL
expr ::= LOAD_DEREF expr ::= LOAD_DEREF
expr ::= attribute
expr ::= binary_expr expr ::= binary_expr
expr ::= list expr ::= list
expr ::= compare expr ::= compare
@@ -487,7 +486,6 @@ class PythonParser(GenericASTBuilder):
expr ::= unary_not expr ::= unary_not
expr ::= subscript expr ::= subscript
expr ::= subscript2 expr ::= subscript2
expr ::= get_iter
expr ::= yield expr ::= yield
binary_expr ::= expr expr binary_op binary_expr ::= expr expr binary_op

View File

@@ -255,8 +255,8 @@ class Python2Parser(PythonParser):
# include instructions that don't need customization, # include instructions that don't need customization,
# but we'll do a finer check after the rough breakout. # but we'll do a finer check after the rough breakout.
customize_instruction_basenames = frozenset( customize_instruction_basenames = frozenset(
('BUILD', 'CALL', 'CONTINUE', ('BUILD', 'CALL', 'CONTINUE', 'DELETE',
'DELETE', 'DUP', 'EXEC', 'JUMP', 'DUP', 'EXEC', 'GET', 'JUMP',
'LOAD', 'LOOKUP', 'MAKE', 'SETUP', 'LOAD', 'LOOKUP', 'MAKE', 'SETUP',
'RAISE', 'UNPACK')) 'RAISE', 'UNPACK'))
@@ -374,17 +374,24 @@ class Python2Parser(PythonParser):
""", nop_func) """, nop_func)
custom_ops_seen.add(opname) custom_ops_seen.add(opname)
continue continue
elif opname == 'GET_ITER':
self.addRule("""
expr ::= get_iter
attribute ::= expr GET_ITER
""", nop_func)
custom_ops_seen.add(opname)
continue
elif opname_base in ('DUP_TOPX', 'RAISE_VARARGS'): elif opname_base in ('DUP_TOPX', 'RAISE_VARARGS'):
# FIXME: remove these conditions if they are not needed. # FIXME: remove these conditions if they are not needed.
# no longer need to add a rule # no longer need to add a rule
continue continue
elif opname == 'EXEC_STMT': elif opname == 'EXEC_STMT':
self.addRule(""" self.addRule("""
stmt ::= exec_stmt stmt ::= exec_stmt
exec_stmt ::= expr exprlist DUP_TOP EXEC_STMT exec_stmt ::= expr exprlist DUP_TOP EXEC_STMT
exec_stmt ::= expr exprlist EXEC_STMT exec_stmt ::= expr exprlist EXEC_STMT
exprlist ::= expr+ exprlist ::= expr+
""", nop_func) """, nop_func)
continue continue
elif opname == 'JUMP_IF_NOT_DEBUG': elif opname == 'JUMP_IF_NOT_DEBUG':
v = token.attr v = token.attr
@@ -400,6 +407,13 @@ class Python2Parser(PythonParser):
RAISE_VARARGS_1 COME_FROM RAISE_VARARGS_1 COME_FROM
""", nop_func) """, nop_func)
continue continue
elif opname == 'LOAD_ATTR':
self.addRule("""
expr ::= attribute
attribute ::= expr LOAD_ATTR
""", nop_func)
custom_ops_seen.add(opname)
continue
elif opname == 'LOAD_LISTCOMP': elif opname == 'LOAD_LISTCOMP':
self.addRule("expr ::= listcomp", nop_func) self.addRule("expr ::= listcomp", nop_func)
custom_ops_seen.add(opname) custom_ops_seen.add(opname)
@@ -413,7 +427,11 @@ class Python2Parser(PythonParser):
continue continue
elif opname == 'LOOKUP_METHOD': elif opname == 'LOOKUP_METHOD':
# A PyPy speciality - DRY with parse3 # A PyPy speciality - DRY with parse3
self.addRule("attribute ::= expr LOOKUP_METHOD", nop_func) self.addRule("""
expr ::= attribute
attribute ::= expr LOOKUP_METHOD
""",
nop_func)
custom_ops_seen.add(opname) custom_ops_seen.add(opname)
continue continue
elif opname_base == 'MAKE_FUNCTION': elif opname_base == 'MAKE_FUNCTION':

View File

@@ -562,7 +562,7 @@ class Python3Parser(PythonParser):
# include instructions that don't need customization, # include instructions that don't need customization,
# but we'll do a finer check after the rough breakout. # but we'll do a finer check after the rough breakout.
customize_instruction_basenames = frozenset( customize_instruction_basenames = frozenset(
('BUILD', 'CALL', 'CONTINUE', 'DELETE', ('BUILD', 'CALL', 'CONTINUE', 'DELETE', 'GET',
'JUMP', 'LOAD', 'LOOKUP', 'MAKE', 'JUMP', 'LOAD', 'LOOKUP', 'MAKE',
'RAISE', 'UNPACK')) 'RAISE', 'UNPACK'))
@@ -733,6 +733,9 @@ class Python3Parser(PythonParser):
('kwarg ' * args_kw) + ('kwarg ' * args_kw) +
'expr ' * nak + opname) 'expr ' * nak + opname)
self.add_unique_rule(rule, opname, token.attr, customize) self.add_unique_rule(rule, opname, token.attr, customize)
elif opname == 'CONTINUE':
self.addRule('continue ::= CONTINUE', nop_func)
custom_ops_seen.add(opname)
elif opname == 'CONTINUE_LOOP': elif opname == 'CONTINUE_LOOP':
self.addRule('continue ::= CONTINUE_LOOP', nop_func) self.addRule('continue ::= CONTINUE_LOOP', nop_func)
custom_ops_seen.add(opname) custom_ops_seen.add(opname)
@@ -751,6 +754,12 @@ class Python3Parser(PythonParser):
delete_subscr ::= expr expr DELETE_SUBSCR delete_subscr ::= expr expr DELETE_SUBSCR
""", nop_func) """, nop_func)
custom_ops_seen.add(opname) custom_ops_seen.add(opname)
elif opname == 'GET_ITER':
self.addRule("""
expr ::= get_iter
attribute ::= expr GET_ITER
""", nop_func)
custom_ops_seen.add(opname)
elif opname == 'JUMP_IF_NOT_DEBUG': elif opname == 'JUMP_IF_NOT_DEBUG':
v = token.attr v = token.attr
self.addRule(""" self.addRule("""
@@ -782,6 +791,12 @@ class Python3Parser(PythonParser):
"GET_ITER CALL_FUNCTION_1") "GET_ITER CALL_FUNCTION_1")
self.add_make_function_rule(rule_pat, opname, token.attr, customize) self.add_make_function_rule(rule_pat, opname, token.attr, customize)
# listcomp is a custom Python3 rule # listcomp is a custom Python3 rule
elif opname == 'LOAD_ATTR':
self.addRule("""
expr ::= attribute
attribute ::= expr LOAD_ATTR
""", nop_func)
custom_ops_seen.add(opname)
elif opname == 'LOAD_LISTCOMP': elif opname == 'LOAD_LISTCOMP':
self.add_unique_rule("expr ::= listcomp", opname, token.attr, customize) self.add_unique_rule("expr ::= listcomp", opname, token.attr, customize)
elif opname == 'LOAD_SETCOMP': elif opname == 'LOAD_SETCOMP':
@@ -792,8 +807,12 @@ class Python3Parser(PythonParser):
"GET_ITER CALL_FUNCTION_1") "GET_ITER CALL_FUNCTION_1")
self.add_make_function_rule(rule_pat, opname, token.attr, customize) self.add_make_function_rule(rule_pat, opname, token.attr, customize)
elif opname == 'LOOKUP_METHOD': elif opname == 'LOOKUP_METHOD':
# A PyPy speciality - DRY with parse2 # A PyPy speciality - DRY with parse3
self.addRule("attribute ::= expr LOOKUP_METHOD", nop_func) self.addRule("""
expr ::= attribute
attribute ::= expr LOOKUP_METHOD
""",
nop_func)
custom_ops_seen.add(opname) custom_ops_seen.add(opname)
elif opname.startswith('MAKE_CLOSURE'): elif opname.startswith('MAKE_CLOSURE'):
# DRY with MAKE_FUNCTION # DRY with MAKE_FUNCTION