2016-06-18 02:59:17 +00:00
|
|
|
# Copyright (c) 2015-2016 Rocky Bernstein
|
2016-06-02 20:58:42 +00:00
|
|
|
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
|
|
|
|
# Copyright (c) 1999 John Aycock
|
2015-12-17 03:08:29 +00:00
|
|
|
"""
|
|
|
|
A spark grammar for Python 2.x.
|
|
|
|
|
|
|
|
However instead of terminal symbols being the usual ASCII text,
|
|
|
|
e.g. 5, myvariable, "for", etc. they are CPython Bytecode tokens,
|
|
|
|
e.g. "LOAD_CONST 5", "STORE NAME myvariable", "SETUP_LOOP", etc.
|
|
|
|
|
|
|
|
If we succeed in creating a parse tree, then we have a Python program
|
2016-11-22 23:14:31 +00:00
|
|
|
that a later phase can turn into a sequence of ASCII text.
|
2015-12-17 03:08:29 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2016-05-01 11:13:36 +00:00
|
|
|
from uncompyle6.parser import PythonParser, PythonParserSingle, nop_func
|
2015-12-18 22:07:35 +00:00
|
|
|
from uncompyle6.parsers.astnode import AST
|
2016-05-01 11:13:36 +00:00
|
|
|
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
|
2015-12-17 03:08:29 +00:00
|
|
|
|
|
|
|
class Python2Parser(PythonParser):
|
|
|
|
|
2015-12-22 10:15:00 +00:00
|
|
|
def __init__(self, debug_parser=PARSER_DEFAULT_DEBUG):
|
2016-06-02 20:58:42 +00:00
|
|
|
super(Python2Parser, self).__init__(AST, 'stmts', debug=debug_parser)
|
2016-06-20 23:06:45 +00:00
|
|
|
self.new_rules = set()
|
2016-06-19 17:41:49 +00:00
|
|
|
|
2016-07-09 09:59:02 +00:00
|
|
|
def p_print2(self, args):
|
2016-11-28 12:53:32 +00:00
|
|
|
"""
|
2015-12-17 03:08:29 +00:00
|
|
|
stmt ::= print_items_stmt
|
|
|
|
stmt ::= print_nl
|
|
|
|
stmt ::= print_items_nl_stmt
|
|
|
|
|
|
|
|
print_items_stmt ::= expr PRINT_ITEM print_items_opt
|
|
|
|
print_items_nl_stmt ::= expr PRINT_ITEM print_items_opt PRINT_NEWLINE_CONT
|
2016-11-28 12:53:32 +00:00
|
|
|
print_items_opt ::= print_items?
|
|
|
|
print_items ::= print_item+
|
|
|
|
print_item ::= expr PRINT_ITEM_CONT
|
|
|
|
print_nl ::= PRINT_NEWLINE
|
|
|
|
"""
|
2015-12-17 03:08:29 +00:00
|
|
|
|
2016-07-27 17:19:42 +00:00
|
|
|
def p_stmt2(self, args):
|
|
|
|
"""
|
2016-09-27 08:02:27 +00:00
|
|
|
while1stmt ::= SETUP_LOOP l_stmts_opt JUMP_BACK POP_BLOCK COME_FROM
|
2017-04-10 11:57:56 +00:00
|
|
|
while1stmt ::= SETUP_LOOP l_stmts JUMP_BACK COME_FROM
|
2016-09-27 08:02:27 +00:00
|
|
|
while1stmt ::= SETUP_LOOP l_stmts JUMP_BACK POP_BLOCK COME_FROM
|
|
|
|
|
2017-04-10 06:47:46 +00:00
|
|
|
while1elsestmt ::= SETUP_LOOP l_stmts JUMP_BACK POP_BLOCK else_suite COME_FROM
|
|
|
|
while1elsestmt ::= SETUP_LOOP l_stmts JUMP_BACK else_suite COME_FROM
|
2016-07-28 05:23:29 +00:00
|
|
|
|
2016-07-27 22:31:56 +00:00
|
|
|
exec_stmt ::= expr exprlist DUP_TOP EXEC_STMT
|
|
|
|
exec_stmt ::= expr exprlist EXEC_STMT
|
|
|
|
|
2016-07-27 17:19:42 +00:00
|
|
|
"""
|
|
|
|
|
2015-12-17 03:08:29 +00:00
|
|
|
def p_print_to(self, args):
|
|
|
|
'''
|
|
|
|
stmt ::= print_to
|
|
|
|
stmt ::= print_to_nl
|
|
|
|
stmt ::= print_nl_to
|
|
|
|
print_to ::= expr print_to_items POP_TOP
|
|
|
|
print_to_nl ::= expr print_to_items PRINT_NEWLINE_TO
|
|
|
|
print_nl_to ::= expr PRINT_NEWLINE_TO
|
|
|
|
print_to_items ::= print_to_items print_to_item
|
|
|
|
print_to_items ::= print_to_item
|
|
|
|
print_to_item ::= DUP_TOP expr ROT_TWO PRINT_ITEM_TO
|
|
|
|
'''
|
|
|
|
|
2016-04-18 02:37:05 +00:00
|
|
|
def p_grammar(self, args):
|
|
|
|
'''
|
2015-12-17 03:08:29 +00:00
|
|
|
sstmt ::= stmt
|
|
|
|
sstmt ::= ifelsestmtr
|
|
|
|
sstmt ::= return_stmt RETURN_LAST
|
|
|
|
|
|
|
|
return_if_stmts ::= return_if_stmt
|
|
|
|
return_if_stmts ::= _stmts return_if_stmt
|
|
|
|
return_if_stmt ::= ret_expr RETURN_END_IF
|
|
|
|
|
|
|
|
stmt ::= break_stmt
|
|
|
|
break_stmt ::= BREAK_LOOP
|
|
|
|
|
|
|
|
stmt ::= continue_stmt
|
|
|
|
continue_stmt ::= CONTINUE
|
|
|
|
continue_stmt ::= CONTINUE_LOOP
|
|
|
|
continue_stmts ::= _stmts lastl_stmt continue_stmt
|
|
|
|
continue_stmts ::= lastl_stmt continue_stmt
|
|
|
|
continue_stmts ::= continue_stmt
|
|
|
|
|
|
|
|
stmt ::= raise_stmt0
|
|
|
|
stmt ::= raise_stmt1
|
|
|
|
stmt ::= raise_stmt2
|
|
|
|
stmt ::= raise_stmt3
|
|
|
|
|
|
|
|
raise_stmt0 ::= RAISE_VARARGS_0
|
|
|
|
raise_stmt1 ::= expr RAISE_VARARGS_1
|
|
|
|
raise_stmt2 ::= expr expr RAISE_VARARGS_2
|
|
|
|
raise_stmt3 ::= expr expr expr RAISE_VARARGS_3
|
|
|
|
|
|
|
|
stmt ::= exec_stmt
|
|
|
|
|
|
|
|
del_stmt ::= expr DELETE_SLICE+0
|
|
|
|
del_stmt ::= expr expr DELETE_SLICE+1
|
|
|
|
del_stmt ::= expr expr DELETE_SLICE+2
|
|
|
|
del_stmt ::= expr expr expr DELETE_SLICE+3
|
|
|
|
del_stmt ::= delete_subscr
|
|
|
|
delete_subscr ::= expr expr DELETE_SUBSCR
|
|
|
|
del_stmt ::= expr DELETE_ATTR
|
|
|
|
|
|
|
|
kwarg ::= LOAD_CONST expr
|
|
|
|
|
2015-12-23 18:49:56 +00:00
|
|
|
classdef ::= buildclass designator
|
|
|
|
|
|
|
|
buildclass ::= LOAD_CONST expr mkfunc
|
|
|
|
CALL_FUNCTION_0 BUILD_CLASS
|
2015-12-17 03:08:29 +00:00
|
|
|
|
|
|
|
stmt ::= classdefdeco
|
|
|
|
classdefdeco ::= classdefdeco1 designator
|
|
|
|
classdefdeco1 ::= expr classdefdeco1 CALL_FUNCTION_1
|
|
|
|
classdefdeco1 ::= expr classdefdeco2 CALL_FUNCTION_1
|
|
|
|
classdefdeco2 ::= LOAD_CONST expr mkfunc CALL_FUNCTION_0 BUILD_CLASS
|
|
|
|
|
|
|
|
assert2 ::= assert_expr jmp_true LOAD_ASSERT expr CALL_FUNCTION_1 RAISE_VARARGS_1
|
|
|
|
|
|
|
|
assert_expr ::= expr
|
|
|
|
assert_expr ::= assert_expr_or
|
|
|
|
assert_expr ::= assert_expr_and
|
|
|
|
assert_expr_or ::= assert_expr jmp_true expr
|
|
|
|
assert_expr_and ::= assert_expr jmp_false expr
|
|
|
|
|
|
|
|
ifstmt ::= testexpr _ifstmts_jump
|
2017-01-24 05:53:30 +00:00
|
|
|
ifstmt ::= testexpr return_if_stmts COME_FROM
|
2015-12-17 03:08:29 +00:00
|
|
|
|
|
|
|
testexpr ::= testfalse
|
|
|
|
testexpr ::= testtrue
|
|
|
|
testfalse ::= expr jmp_false
|
|
|
|
testtrue ::= expr jmp_true
|
|
|
|
|
|
|
|
_ifstmts_jump ::= return_if_stmts
|
|
|
|
|
|
|
|
iflaststmt ::= testexpr c_stmts_opt JUMP_ABSOLUTE
|
|
|
|
|
|
|
|
iflaststmtl ::= testexpr c_stmts_opt JUMP_BACK
|
|
|
|
|
|
|
|
ifelsestmt ::= testexpr c_stmts_opt JUMP_FORWARD else_suite COME_FROM
|
|
|
|
|
|
|
|
ifelsestmtc ::= testexpr c_stmts_opt JUMP_ABSOLUTE else_suitec
|
|
|
|
|
|
|
|
ifelsestmtr ::= testexpr return_if_stmts return_stmts
|
|
|
|
|
2017-01-24 05:53:30 +00:00
|
|
|
ifelsestmtr ::= testexpr return_if_stmts COME_FROM return_stmts
|
|
|
|
|
2015-12-17 03:08:29 +00:00
|
|
|
ifelsestmtl ::= testexpr c_stmts_opt JUMP_BACK else_suitel
|
|
|
|
|
|
|
|
|
2015-12-25 22:45:13 +00:00
|
|
|
# this is nested inside a trystmt
|
2016-05-06 00:56:41 +00:00
|
|
|
tryfinallystmt ::= SETUP_FINALLY suite_stmts_opt
|
2015-12-25 22:45:13 +00:00
|
|
|
POP_BLOCK LOAD_CONST
|
|
|
|
COME_FROM suite_stmts_opt END_FINALLY
|
|
|
|
|
2016-07-03 02:53:58 +00:00
|
|
|
# Move to 2.7? 2.6 may use come_froms
|
2015-12-17 03:08:29 +00:00
|
|
|
tryelsestmtc ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
|
2015-12-25 22:45:13 +00:00
|
|
|
try_middle else_suitec COME_FROM
|
2015-12-17 03:08:29 +00:00
|
|
|
|
|
|
|
tryelsestmtl ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
|
2015-12-25 22:45:13 +00:00
|
|
|
try_middle else_suitel COME_FROM
|
2015-12-17 03:08:29 +00:00
|
|
|
|
2016-06-25 00:06:10 +00:00
|
|
|
trystmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
|
|
|
|
try_middle COME_FROM
|
|
|
|
|
2016-09-05 08:02:37 +00:00
|
|
|
try_middle ::= JUMP_FORWARD COME_FROM except_stmts
|
|
|
|
END_FINALLY COME_FROM
|
|
|
|
try_middle ::= jmp_abs COME_FROM except_stmts
|
|
|
|
END_FINALLY
|
|
|
|
|
2016-11-28 12:53:32 +00:00
|
|
|
except_stmts ::= except_stmt+
|
2015-12-17 03:08:29 +00:00
|
|
|
|
|
|
|
except_stmt ::= except_cond1 except_suite
|
|
|
|
except_stmt ::= except
|
|
|
|
|
|
|
|
except_suite ::= c_stmts_opt JUMP_FORWARD
|
|
|
|
except_suite ::= c_stmts_opt jmp_abs
|
|
|
|
except_suite ::= return_stmts
|
|
|
|
|
2016-06-19 14:19:45 +00:00
|
|
|
except ::= POP_TOP POP_TOP POP_TOP c_stmts_opt _jump
|
2015-12-17 03:08:29 +00:00
|
|
|
except ::= POP_TOP POP_TOP POP_TOP return_stmts
|
|
|
|
|
|
|
|
jmp_abs ::= JUMP_ABSOLUTE
|
|
|
|
jmp_abs ::= JUMP_BACK
|
|
|
|
'''
|
|
|
|
|
2016-05-15 23:28:22 +00:00
|
|
|
def p_genexpr2(self, args):
|
|
|
|
'''
|
|
|
|
genexpr ::= LOAD_GENEXPR MAKE_FUNCTION_0 expr GET_ITER CALL_FUNCTION_1
|
|
|
|
'''
|
|
|
|
|
2016-05-03 01:20:17 +00:00
|
|
|
def p_expr2(self, args):
|
2016-07-27 22:31:56 +00:00
|
|
|
"""
|
2015-12-17 03:08:29 +00:00
|
|
|
expr ::= LOAD_LOCALS
|
2016-07-27 22:31:56 +00:00
|
|
|
expr ::= slice0
|
|
|
|
expr ::= slice1
|
|
|
|
expr ::= slice2
|
|
|
|
expr ::= slice3
|
|
|
|
expr ::= unary_convert
|
2016-05-03 01:20:17 +00:00
|
|
|
|
2016-10-05 07:34:29 +00:00
|
|
|
and ::= expr jmp_false expr come_from_opt
|
2016-10-06 02:54:50 +00:00
|
|
|
or ::= expr jmp_true expr come_from_opt
|
2016-10-05 07:34:29 +00:00
|
|
|
|
2016-07-27 22:31:56 +00:00
|
|
|
unary_convert ::= expr UNARY_CONVERT
|
2016-05-08 22:41:39 +00:00
|
|
|
|
|
|
|
# In Python 3, DUP_TOPX_2 is DUP_TOP_TWO
|
|
|
|
binary_subscr2 ::= expr expr DUP_TOPX_2 BINARY_SUBSCR
|
2016-07-27 22:31:56 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def p_slice2(self, args):
|
|
|
|
"""
|
|
|
|
designator ::= expr STORE_SLICE+0
|
|
|
|
designator ::= expr expr STORE_SLICE+1
|
|
|
|
designator ::= expr expr STORE_SLICE+2
|
|
|
|
designator ::= expr expr expr STORE_SLICE+3
|
|
|
|
augassign1 ::= expr expr inplace_op ROT_TWO STORE_SLICE+0
|
|
|
|
augassign1 ::= expr expr inplace_op ROT_THREE STORE_SLICE+1
|
|
|
|
augassign1 ::= expr expr inplace_op ROT_THREE STORE_SLICE+2
|
|
|
|
augassign1 ::= expr expr inplace_op ROT_FOUR STORE_SLICE+3
|
|
|
|
slice0 ::= expr SLICE+0
|
|
|
|
slice0 ::= expr DUP_TOP SLICE+0
|
|
|
|
slice1 ::= expr expr SLICE+1
|
|
|
|
slice1 ::= expr expr DUP_TOPX_2 SLICE+1
|
|
|
|
slice2 ::= expr expr SLICE+2
|
|
|
|
slice2 ::= expr expr DUP_TOPX_2 SLICE+2
|
|
|
|
slice3 ::= expr expr expr SLICE+3
|
|
|
|
slice3 ::= expr expr expr DUP_TOPX_3 SLICE+3
|
|
|
|
"""
|
|
|
|
|
|
|
|
def p_op2(self, args):
|
|
|
|
"""
|
|
|
|
inplace_op ::= INPLACE_DIVIDE
|
|
|
|
binary_op ::= BINARY_DIVIDE
|
|
|
|
"""
|
2015-12-18 22:07:35 +00:00
|
|
|
|
|
|
|
def add_custom_rules(self, tokens, customize):
|
2016-11-25 17:30:42 +00:00
|
|
|
"""
|
2016-07-24 22:54:51 +00:00
|
|
|
Special handling for opcodes such as those that take a variable number
|
2015-12-18 22:07:35 +00:00
|
|
|
of arguments -- we add a new rule for each:
|
|
|
|
|
2015-12-21 11:39:01 +00:00
|
|
|
build_list ::= {expr}^n BUILD_LIST_n
|
2015-12-22 16:41:10 +00:00
|
|
|
build_list ::= {expr}^n BUILD_TUPLE_n
|
2015-12-18 22:07:35 +00:00
|
|
|
unpack_list ::= UNPACK_LIST {expr}^n
|
2015-12-21 11:39:01 +00:00
|
|
|
unpack ::= UNPACK_TUPLE {expr}^n
|
|
|
|
unpack ::= UNPACK_SEQEUENCE {expr}^n
|
|
|
|
|
2015-12-22 16:41:10 +00:00
|
|
|
mkfunc ::= {expr}^n LOAD_CONST MAKE_FUNCTION_n
|
|
|
|
mklambda ::= {expr}^n LOAD_LAMBDA MAKE_FUNCTION_n
|
|
|
|
mkfunc ::= {expr}^n load_closure LOAD_CONST MAKE_FUNCTION_n
|
2015-12-18 22:07:35 +00:00
|
|
|
expr ::= expr {expr}^n CALL_FUNCTION_n
|
|
|
|
expr ::= expr {expr}^n CALL_FUNCTION_VAR_n POP_TOP
|
|
|
|
expr ::= expr {expr}^n CALL_FUNCTION_VAR_KW_n POP_TOP
|
|
|
|
expr ::= expr {expr}^n CALL_FUNCTION_KW_n POP_TOP
|
2016-07-24 22:54:51 +00:00
|
|
|
|
2016-07-26 14:21:12 +00:00
|
|
|
PyPy adds custom rules here as well
|
2016-11-25 17:30:42 +00:00
|
|
|
"""
|
2016-07-25 13:26:32 +00:00
|
|
|
for opname, v in list(customize.items()):
|
|
|
|
opname_base = opname[:opname.rfind('_')]
|
2016-07-26 19:56:49 +00:00
|
|
|
if opname == 'PyPy':
|
2016-07-27 13:26:39 +00:00
|
|
|
self.addRule("""
|
|
|
|
stmt ::= assign3_pypy
|
|
|
|
stmt ::= assign2_pypy
|
|
|
|
assign3_pypy ::= expr expr expr designator designator designator
|
|
|
|
assign2_pypy ::= expr expr designator designator
|
2016-07-27 21:35:21 +00:00
|
|
|
list_compr ::= expr BUILD_LIST_FROM_ARG _for designator list_iter
|
|
|
|
JUMP_BACK
|
2016-07-27 13:26:39 +00:00
|
|
|
""", nop_func)
|
2016-07-26 19:56:49 +00:00
|
|
|
continue
|
|
|
|
elif opname_base in ('BUILD_LIST', 'BUILD_TUPLE', 'BUILD_SET'):
|
2016-06-19 17:41:49 +00:00
|
|
|
thousands = (v//1024)
|
2016-12-19 01:18:19 +00:00
|
|
|
thirty32s = ((v//32) % 32)
|
2016-06-20 23:06:45 +00:00
|
|
|
if thirty32s > 0:
|
2016-06-19 17:41:49 +00:00
|
|
|
rule = "expr32 ::=%s" % (' expr' * 32)
|
2016-07-25 13:26:32 +00:00
|
|
|
self.add_unique_rule(rule, opname_base, v, customize)
|
2016-06-19 17:41:49 +00:00
|
|
|
self.seen32 = True
|
2016-06-20 23:06:45 +00:00
|
|
|
if thousands > 0:
|
|
|
|
self.add_unique_rule("expr1024 ::=%s" % (' expr32' * 32),
|
2016-07-25 13:26:32 +00:00
|
|
|
opname_base, v, customize)
|
2016-06-19 17:41:49 +00:00
|
|
|
self.seen1024 = True
|
|
|
|
rule = ('build_list ::= ' + 'expr1024 '*thousands +
|
2016-12-24 12:45:02 +00:00
|
|
|
'expr32 '*thirty32s + 'expr '*(v % 32) + opname)
|
2016-07-25 17:05:54 +00:00
|
|
|
elif opname == 'LOOKUP_METHOD':
|
|
|
|
# A PyPy speciality - DRY with parse3
|
2016-07-25 20:03:56 +00:00
|
|
|
self.add_unique_rule("load_attr ::= expr LOOKUP_METHOD",
|
2016-07-25 17:05:54 +00:00
|
|
|
opname, v, customize)
|
2016-07-24 22:54:51 +00:00
|
|
|
continue
|
2016-07-25 13:26:32 +00:00
|
|
|
elif opname == 'JUMP_IF_NOT_DEBUG':
|
2016-07-26 14:21:12 +00:00
|
|
|
self.add_unique_rules([
|
|
|
|
'jmp_true_false ::= POP_JUMP_IF_TRUE',
|
|
|
|
'jmp_true_false ::= POP_JUMP_IF_FALSE',
|
|
|
|
"stmt ::= assert_pypy",
|
|
|
|
"stmt ::= assert2_pypy",
|
|
|
|
"assert_pypy ::= JUMP_IF_NOT_DEBUG assert_expr jmp_true_false "
|
|
|
|
"LOAD_ASSERT RAISE_VARARGS_1 COME_FROM",
|
|
|
|
"assert2_pypy ::= JUMP_IF_NOT_DEBUG assert_expr jmp_true_false "
|
|
|
|
"LOAD_ASSERT expr CALL_FUNCTION_1 RAISE_VARARGS_1 COME_FROM",
|
|
|
|
], customize)
|
2016-07-25 13:06:13 +00:00
|
|
|
continue
|
2016-07-25 13:26:32 +00:00
|
|
|
elif opname_base == 'BUILD_MAP':
|
2016-07-26 01:53:56 +00:00
|
|
|
if opname == 'BUILD_MAP_n':
|
2016-07-25 21:50:19 +00:00
|
|
|
# PyPy sometimes has no count. Sigh.
|
2016-07-26 14:21:12 +00:00
|
|
|
self.add_unique_rules([
|
|
|
|
'kvlist_n ::= kvlist_n kv3',
|
|
|
|
'kvlist_n ::=',
|
|
|
|
'mapexpr ::= BUILD_MAP_n kvlist_n',
|
|
|
|
], customize)
|
2017-11-15 14:38:48 +00:00
|
|
|
if self.version >= 2.7:
|
|
|
|
self.add_unique_rule(
|
|
|
|
'dictcomp_func ::= BUILD_MAP_n LOAD_FAST FOR_ITER designator '
|
|
|
|
'comp_iter JUMP_BACK RETURN_VALUE RETURN_LAST',
|
|
|
|
'dictcomp_func', 0, customize)
|
|
|
|
|
2016-07-25 21:50:19 +00:00
|
|
|
else:
|
|
|
|
kvlist_n = "kvlist_%s" % v
|
2016-07-26 14:21:12 +00:00
|
|
|
self.add_unique_rules([
|
|
|
|
(kvlist_n + " ::=" + ' kv3' * v),
|
|
|
|
"mapexpr ::= %s %s" % (opname, kvlist_n)
|
|
|
|
], customize)
|
|
|
|
continue
|
|
|
|
elif opname == 'SETUP_EXCEPT':
|
|
|
|
# FIXME: have a way here to detect PyPy. Right now we
|
|
|
|
# only have SETUP_EXCEPT customization for PyPy, but that might not
|
|
|
|
# always be the case.
|
|
|
|
self.add_unique_rules([
|
|
|
|
"stmt ::= trystmt_pypy",
|
|
|
|
"trystmt_pypy ::= SETUP_EXCEPT suite_stmts_opt try_middle_pypy",
|
|
|
|
"try_middle_pypy ::= COME_FROM except_stmts END_FINALLY COME_FROM"
|
|
|
|
], customize)
|
|
|
|
continue
|
2016-07-26 19:56:49 +00:00
|
|
|
elif opname == 'SETUP_FINALLY':
|
|
|
|
# FIXME: have a way here to detect PyPy. Right now we
|
|
|
|
# only have SETUP_EXCEPT customization for PyPy, but that might not
|
|
|
|
# always be the case.
|
|
|
|
self.add_unique_rules([
|
|
|
|
"stmt ::= tryfinallystmt_pypy",
|
2016-11-22 23:14:31 +00:00
|
|
|
"tryfinallystmt_pypy ::= SETUP_FINALLY suite_stmts_opt COME_FROM_FINALLY "
|
2016-07-26 19:56:49 +00:00
|
|
|
"suite_stmts_opt END_FINALLY"
|
|
|
|
], customize)
|
|
|
|
continue
|
2016-07-25 13:26:32 +00:00
|
|
|
elif opname_base in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'):
|
|
|
|
rule = 'unpack ::= ' + opname + ' designator'*v
|
|
|
|
elif opname_base == 'UNPACK_LIST':
|
|
|
|
rule = 'unpack_list ::= ' + opname + ' designator'*v
|
|
|
|
elif opname_base in ('DUP_TOPX', 'RAISE_VARARGS'):
|
2016-07-27 10:01:49 +00:00
|
|
|
# FIXME: remove these conditions if they are not needed.
|
|
|
|
# no longer need to add a rule
|
2015-12-18 22:07:35 +00:00
|
|
|
continue
|
2016-07-25 13:26:32 +00:00
|
|
|
elif opname_base == 'MAKE_FUNCTION':
|
2015-12-18 22:07:35 +00:00
|
|
|
self.addRule('mklambda ::= %s LOAD_LAMBDA %s' %
|
2016-07-25 13:26:32 +00:00
|
|
|
('pos_arg '*v, opname), nop_func)
|
|
|
|
rule = 'mkfunc ::= %s LOAD_CONST %s' % ('expr '*v, opname)
|
|
|
|
elif opname_base == 'MAKE_CLOSURE':
|
2016-07-26 19:56:49 +00:00
|
|
|
# FIXME: use add_unique_rules to tidy this up.
|
2016-07-27 10:01:49 +00:00
|
|
|
self.add_unique_rules([
|
|
|
|
('mklambda ::= %s load_closure LOAD_LAMBDA %s' %
|
|
|
|
('expr '*v, opname)),
|
|
|
|
('genexpr ::= %s load_closure LOAD_GENEXPR %s expr'
|
|
|
|
' GET_ITER CALL_FUNCTION_1' %
|
|
|
|
('expr '*v, opname)),
|
|
|
|
('mkfunc ::= %s load_closure LOAD_CONST %s' %
|
|
|
|
('expr '*v, opname))],
|
|
|
|
customize)
|
2017-11-15 14:38:48 +00:00
|
|
|
if self.version >= 2.7:
|
|
|
|
self.add_unique_rules([
|
|
|
|
('setcomp ::= %s load_closure LOAD_SETCOMP %s expr'
|
|
|
|
' GET_ITER CALL_FUNCTION_1' %
|
|
|
|
('expr '*v, opname)),
|
|
|
|
('dictcomp ::= %s load_closure LOAD_DICTCOMP %s expr'
|
|
|
|
' GET_ITER CALL_FUNCTION_1' %
|
|
|
|
('expr '*v, opname))],
|
|
|
|
customize)
|
2016-07-27 10:01:49 +00:00
|
|
|
continue
|
2016-07-25 13:26:32 +00:00
|
|
|
elif opname_base in ('CALL_FUNCTION', 'CALL_FUNCTION_VAR',
|
2016-07-25 17:05:54 +00:00
|
|
|
'CALL_FUNCTION_VAR_KW', 'CALL_FUNCTION_KW'):
|
|
|
|
args_pos = (v & 0xff) # positional parameters
|
|
|
|
args_kw = (v >> 8) & 0xff # keyword parameters
|
2015-12-18 22:07:35 +00:00
|
|
|
# number of apply equiv arguments:
|
2016-07-25 13:26:32 +00:00
|
|
|
nak = ( len(opname_base)-len('CALL_FUNCTION') ) // 3
|
2016-07-25 17:05:54 +00:00
|
|
|
rule = 'call_function ::= expr ' + 'expr '*args_pos + 'kwarg '*args_kw \
|
|
|
|
+ 'expr ' * nak + opname
|
|
|
|
elif opname_base == 'CALL_METHOD':
|
|
|
|
# PyPy only - DRY with parse3
|
2016-08-16 10:09:10 +00:00
|
|
|
args_pos = (v & 0xff) # positional parameters
|
2016-07-25 17:05:54 +00:00
|
|
|
args_kw = (v >> 8) & 0xff # keyword parameters
|
|
|
|
# number of apply equiv arguments:
|
|
|
|
nak = ( len(opname_base)-len('CALL_METHOD') ) // 3
|
|
|
|
rule = 'call_function ::= expr ' + 'expr '*args_pos + 'kwarg '*args_kw \
|
2016-07-25 13:26:32 +00:00
|
|
|
+ 'expr ' * nak + opname
|
2015-12-18 22:07:35 +00:00
|
|
|
else:
|
2016-07-25 13:26:32 +00:00
|
|
|
raise Exception('unknown customize token %s' % opname)
|
|
|
|
self.add_unique_rule(rule, opname_base, v, customize)
|
2016-11-25 17:30:42 +00:00
|
|
|
pass
|
|
|
|
self.check_reduce['augassign1'] = 'AST'
|
|
|
|
self.check_reduce['augassign2'] = 'AST'
|
2016-11-27 02:41:45 +00:00
|
|
|
self.check_reduce['_stmts'] = 'AST'
|
2016-11-25 17:30:42 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def reduce_is_invalid(self, rule, ast, tokens, first, last):
|
|
|
|
lhs = rule[0]
|
2017-11-14 15:58:41 +00:00
|
|
|
if lhs in ('augassign1', 'augassign2') and ast[0] and ast[0][0] == 'and':
|
2016-11-25 17:30:42 +00:00
|
|
|
return True
|
2016-11-27 02:41:45 +00:00
|
|
|
elif lhs == '_stmts':
|
|
|
|
for i, stmt in enumerate(ast):
|
|
|
|
if stmt == '_stmts':
|
|
|
|
stmt = stmt[0]
|
|
|
|
assert stmt == 'stmt'
|
|
|
|
if stmt[0] == 'return_stmt':
|
|
|
|
return i+1 != len(ast)
|
|
|
|
pass
|
|
|
|
return False
|
2016-11-25 17:30:42 +00:00
|
|
|
return False
|
2016-04-18 02:37:05 +00:00
|
|
|
|
2016-05-01 11:13:36 +00:00
|
|
|
class Python2ParserSingle(Python2Parser, PythonParserSingle):
|
|
|
|
pass
|
2016-06-18 02:59:17 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# Check grammar
|
|
|
|
p = Python2Parser()
|
2017-10-10 19:11:08 +00:00
|
|
|
p.check_grammar()
|