You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-04 09:22:40 +08:00
NT return_stmt -> return to match AST
This commit is contained in:
@@ -255,7 +255,7 @@ class PythonParser(GenericASTBuilder):
|
||||
c_stmts_opt ::= pass
|
||||
|
||||
l_stmts ::= _stmts
|
||||
l_stmts ::= return_stmts
|
||||
l_stmts ::= returns
|
||||
l_stmts ::= continues
|
||||
l_stmts ::= _stmts lastl_stmt
|
||||
l_stmts ::= lastl_stmt
|
||||
@@ -269,7 +269,7 @@ class PythonParser(GenericASTBuilder):
|
||||
l_stmts_opt ::= pass
|
||||
|
||||
suite_stmts ::= _stmts
|
||||
suite_stmts ::= return_stmts
|
||||
suite_stmts ::= returns
|
||||
suite_stmts ::= continues
|
||||
|
||||
suite_stmts_opt ::= suite_stmts
|
||||
@@ -280,7 +280,7 @@ class PythonParser(GenericASTBuilder):
|
||||
else_suite ::= suite_stmts
|
||||
else_suitel ::= l_stmts
|
||||
else_suitec ::= c_stmts
|
||||
else_suitec ::= return_stmts
|
||||
else_suitec ::= returns
|
||||
|
||||
stmt ::= assert
|
||||
|
||||
@@ -308,15 +308,15 @@ class PythonParser(GenericASTBuilder):
|
||||
del_stmt ::= DELETE_GLOBAL
|
||||
|
||||
|
||||
stmt ::= return_stmt
|
||||
return_stmt ::= ret_expr RETURN_VALUE
|
||||
stmt ::= return
|
||||
return ::= ret_expr RETURN_VALUE
|
||||
|
||||
# return_stmts are a sequence of statements that ends in a RETURN statement.
|
||||
# returns are a sequence of statements that ends in a RETURN statement.
|
||||
# In later Python versions with jump optimization, this can cause JUMPs
|
||||
# that would normally appear to be omitted.
|
||||
|
||||
return_stmts ::= return_stmt
|
||||
return_stmts ::= _stmts return_stmt
|
||||
returns ::= return
|
||||
returns ::= _stmts return
|
||||
|
||||
"""
|
||||
pass
|
||||
|
@@ -54,7 +54,7 @@ class Python2Parser(PythonParser):
|
||||
def p_grammar(self, args):
|
||||
'''
|
||||
sstmt ::= stmt
|
||||
sstmt ::= return_stmt RETURN_LAST
|
||||
sstmt ::= return RETURN_LAST
|
||||
|
||||
return_if_stmts ::= return_if_stmt
|
||||
return_if_stmts ::= _stmts return_if_stmt
|
||||
@@ -157,10 +157,10 @@ class Python2Parser(PythonParser):
|
||||
|
||||
except_suite ::= c_stmts_opt JUMP_FORWARD
|
||||
except_suite ::= c_stmts_opt jmp_abs
|
||||
except_suite ::= return_stmts
|
||||
except_suite ::= returns
|
||||
|
||||
except ::= POP_TOP POP_TOP POP_TOP c_stmts_opt _jump
|
||||
except ::= POP_TOP POP_TOP POP_TOP return_stmts
|
||||
except ::= POP_TOP POP_TOP POP_TOP returns
|
||||
|
||||
jmp_abs ::= JUMP_ABSOLUTE
|
||||
jmp_abs ::= JUMP_BACK
|
||||
@@ -483,7 +483,7 @@ class Python2Parser(PythonParser):
|
||||
if stmt == '_stmts':
|
||||
stmt = stmt[0]
|
||||
assert stmt == 'stmt'
|
||||
if stmt[0] == 'return_stmt':
|
||||
if stmt[0] == 'return':
|
||||
return i+1 != len(ast)
|
||||
pass
|
||||
return False
|
||||
|
@@ -15,7 +15,7 @@ class Python21Parser(Python22Parser):
|
||||
"""
|
||||
for_iter ::= LOAD_CONST FOR_LOOP
|
||||
for ::= SETUP_LOOP expr for_iter store
|
||||
return_stmts
|
||||
returns
|
||||
POP_BLOCK COME_FROM
|
||||
for ::= SETUP_LOOP expr for_iter store
|
||||
l_stmts_opt _jump_back
|
||||
|
@@ -58,8 +58,8 @@ class Python24Parser(Python25Parser):
|
||||
kvlist ::= kvlist kv3
|
||||
while1stmt ::= SETUP_LOOP l_stmts JUMP_BACK COME_FROM
|
||||
while1stmt ::= SETUP_LOOP l_stmts_opt JUMP_BACK COME_FROM
|
||||
while1stmt ::= SETUP_LOOP return_stmts COME_FROM
|
||||
whilestmt ::= SETUP_LOOP testexpr return_stmts POP_BLOCK COME_FROM
|
||||
while1stmt ::= SETUP_LOOP returns COME_FROM
|
||||
whilestmt ::= SETUP_LOOP testexpr returns POP_BLOCK COME_FROM
|
||||
with_cleanup ::= LOAD_FAST DELETE_FAST WITH_CLEANUP END_FINALLY
|
||||
with_cleanup ::= LOAD_NAME DELETE_NAME WITH_CLEANUP END_FINALLY
|
||||
withasstmt ::= expr setupwithas store suite_stmts_opt POP_BLOCK LOAD_CONST COME_FROM with_cleanup
|
||||
|
@@ -68,8 +68,8 @@ class Python25Parser(Python26Parser):
|
||||
return_if_lambda ::= RETURN_END_IF_LAMBDA POP_TOP
|
||||
return_if_stmt ::= ret_expr RETURN_END_IF POP_TOP
|
||||
return_if_stmts ::= return_if_stmt
|
||||
return_stmt ::= ret_expr RETURN_END_IF POP_TOP
|
||||
return_stmt ::= ret_expr RETURN_VALUE POP_TOP
|
||||
return ::= ret_expr RETURN_END_IF POP_TOP
|
||||
return ::= ret_expr RETURN_VALUE POP_TOP
|
||||
return_stmt_lambda ::= ret_expr RETURN_VALUE_LAMBDA
|
||||
setupwithas ::= DUP_TOP LOAD_ATTR ROT_TWO LOAD_ATTR CALL_FUNCTION_0 setup_finally
|
||||
stmt ::= classdefdeco
|
||||
|
@@ -135,14 +135,14 @@ class Python26Parser(Python2Parser):
|
||||
|
||||
whilestmt ::= SETUP_LOOP testexpr l_stmts_opt jb_pop POP_BLOCK _come_froms
|
||||
whilestmt ::= SETUP_LOOP testexpr l_stmts_opt jb_cf_pop bp_come_from
|
||||
whilestmt ::= SETUP_LOOP testexpr return_stmts POP_BLOCK COME_FROM
|
||||
whilestmt ::= SETUP_LOOP testexpr returns POP_BLOCK COME_FROM
|
||||
|
||||
whileelsestmt ::= SETUP_LOOP testexpr l_stmts_opt jb_pop POP_BLOCK
|
||||
else_suite COME_FROM
|
||||
while1elsestmt ::= SETUP_LOOP l_stmts JUMP_BACK else_suite COME_FROM
|
||||
|
||||
return_stmt ::= ret_expr RETURN_END_IF POP_TOP
|
||||
return_stmt ::= ret_expr RETURN_VALUE POP_TOP
|
||||
return ::= ret_expr RETURN_END_IF POP_TOP
|
||||
return ::= ret_expr RETURN_VALUE POP_TOP
|
||||
return_if_stmt ::= ret_expr RETURN_END_IF POP_TOP
|
||||
|
||||
iflaststmtl ::= testexpr c_stmts_opt JUMP_BACK come_from_pop
|
||||
@@ -174,8 +174,8 @@ class Python26Parser(Python2Parser):
|
||||
jmp_false_then ::= JUMP_IF_FALSE THEN POP_TOP
|
||||
jmp_true_then ::= JUMP_IF_TRUE THEN POP_TOP
|
||||
|
||||
while1stmt ::= SETUP_LOOP return_stmts COME_FROM
|
||||
for_block ::= return_stmts _come_froms
|
||||
while1stmt ::= SETUP_LOOP returns COME_FROM
|
||||
for_block ::= returns _come_froms
|
||||
"""
|
||||
|
||||
def p_comp26(self, args):
|
||||
@@ -241,7 +241,7 @@ class Python26Parser(Python2Parser):
|
||||
ret_cond ::= expr jmp_false_then expr ret_expr_or_cond
|
||||
|
||||
return_if_stmt ::= ret_expr RETURN_END_IF POP_TOP
|
||||
return_stmt ::= ret_expr RETURN_VALUE POP_TOP
|
||||
return ::= ret_expr RETURN_VALUE POP_TOP
|
||||
|
||||
# FIXME: split into Python 2.5
|
||||
ret_or ::= expr jmp_true ret_expr_or_cond come_froms
|
||||
|
@@ -106,7 +106,7 @@ class Python27Parser(Python2Parser):
|
||||
# assert condition, expr
|
||||
assert2 ::= assert_expr jmp_true LOAD_ASSERT expr CALL_FUNCTION_1 RAISE_VARARGS_1
|
||||
|
||||
for_block ::= return_stmts _come_froms
|
||||
for_block ::= returns _come_froms
|
||||
|
||||
withstmt ::= expr SETUP_WITH POP_TOP suite_stmts_opt
|
||||
POP_BLOCK LOAD_CONST COME_FROM_WITH
|
||||
@@ -116,7 +116,7 @@ class Python27Parser(Python2Parser):
|
||||
POP_BLOCK LOAD_CONST COME_FROM_WITH
|
||||
WITH_CLEANUP END_FINALLY
|
||||
|
||||
while1stmt ::= SETUP_LOOP return_stmts bp_come_from
|
||||
while1stmt ::= SETUP_LOOP returns bp_come_from
|
||||
while1stmt ::= SETUP_LOOP l_stmts_opt JUMP_BACK POP_BLOCK COME_FROM
|
||||
whilestmt ::= SETUP_LOOP testexpr l_stmts_opt JUMP_BACK POP_BLOCK _come_froms
|
||||
while1elsestmt ::= SETUP_LOOP l_stmts JUMP_BACK POP_BLOCK
|
||||
|
@@ -82,7 +82,7 @@ class Python3Parser(PythonParser):
|
||||
'''
|
||||
sstmt ::= stmt
|
||||
sstmt ::= ifelsestmtr
|
||||
sstmt ::= return_stmt RETURN_LAST
|
||||
sstmt ::= return RETURN_LAST
|
||||
|
||||
return_if_stmts ::= return_if_stmt come_from_opt
|
||||
return_if_stmts ::= _stmts return_if_stmt
|
||||
@@ -157,7 +157,7 @@ class Python3Parser(PythonParser):
|
||||
ifelsestmtc ::= testexpr c_stmts_opt JUMP_ABSOLUTE else_suitec
|
||||
ifelsestmtc ::= testexpr c_stmts_opt jump_absolute_else else_suitec
|
||||
|
||||
ifelsestmtr ::= testexpr return_if_stmts return_stmts
|
||||
ifelsestmtr ::= testexpr return_if_stmts returns
|
||||
|
||||
ifelsestmtl ::= testexpr c_stmts_opt JUMP_BACK else_suitel
|
||||
ifelsestmtl ::= testexpr c_stmts_opt cf_jump_back else_suitel
|
||||
@@ -224,7 +224,7 @@ class Python3Parser(PythonParser):
|
||||
except_var_finalize ::= POP_BLOCK POP_EXCEPT LOAD_CONST COME_FROM_FINALLY
|
||||
LOAD_CONST store del_stmt
|
||||
|
||||
except_suite ::= return_stmts
|
||||
except_suite ::= returns
|
||||
|
||||
except_cond1 ::= DUP_TOP expr COMPARE_OP
|
||||
jmp_false POP_TOP POP_TOP POP_TOP
|
||||
@@ -233,7 +233,7 @@ class Python3Parser(PythonParser):
|
||||
jmp_false POP_TOP store POP_TOP
|
||||
|
||||
except ::= POP_TOP POP_TOP POP_TOP c_stmts_opt POP_EXCEPT _jump
|
||||
except ::= POP_TOP POP_TOP POP_TOP return_stmts
|
||||
except ::= POP_TOP POP_TOP POP_TOP returns
|
||||
|
||||
jmp_abs ::= JUMP_ABSOLUTE
|
||||
jmp_abs ::= JUMP_BACK
|
||||
@@ -346,7 +346,7 @@ class Python3Parser(PythonParser):
|
||||
whilestmt ::= SETUP_LOOP testexpr l_stmts_opt JUMP_BACK POP_BLOCK
|
||||
COME_FROM_LOOP
|
||||
|
||||
whilestmt ::= SETUP_LOOP testexpr return_stmts POP_BLOCK
|
||||
whilestmt ::= SETUP_LOOP testexpr returns POP_BLOCK
|
||||
COME_FROM_LOOP
|
||||
|
||||
while1elsestmt ::= SETUP_LOOP l_stmts JUMP_BACK
|
||||
@@ -574,13 +574,13 @@ class Python3Parser(PythonParser):
|
||||
self.addRule("""
|
||||
stmt ::= assign3_pypy
|
||||
stmt ::= assign2_pypy
|
||||
assign3_pypy ::= expr expr expr store store store
|
||||
assign2_pypy ::= expr expr store store
|
||||
assign3_pypy ::= expr expr expr store store store
|
||||
assign2_pypy ::= expr expr store store
|
||||
return_if_lambda ::= RETURN_END_IF_LAMBDA
|
||||
return_stmt_lambda ::= ret_expr RETURN_VALUE_LAMBDA
|
||||
stmt ::= conditional_lambda
|
||||
return_lambda ::= ret_expr RETURN_VALUE_LAMBDA
|
||||
stmt ::= conditional_lambda
|
||||
conditional_lambda ::= expr jmp_false expr return_if_lambda
|
||||
return_stmt_lambda LAMBDA_MARKER
|
||||
return_lambda LAMBDA_MARKER
|
||||
""", nop_func)
|
||||
|
||||
has_get_iter_call_function1 = False
|
||||
|
@@ -18,7 +18,7 @@ class Python30Parser(Python31Parser):
|
||||
# FIXME: combine with parse3.2
|
||||
whileTruestmt ::= SETUP_LOOP l_stmts_opt JUMP_BACK
|
||||
COME_FROM_LOOP
|
||||
whileTruestmt ::= SETUP_LOOP return_stmts
|
||||
whileTruestmt ::= SETUP_LOOP returns
|
||||
COME_FROM_LOOP
|
||||
|
||||
# In many ways Python 3.0 code generation is more like Python 2.6 than
|
||||
|
@@ -25,7 +25,7 @@ class Python35Parser(Python34Parser):
|
||||
while1stmt ::= SETUP_LOOP l_stmts POP_BLOCK COME_FROM_LOOP
|
||||
while1elsestmt ::= SETUP_LOOP l_stmts JUMP_BACK
|
||||
POP_BLOCK else_suite COME_FROM_LOOP
|
||||
whilestmt ::= SETUP_LOOP testexpr return_stmts POP_BLOCK COME_FROM_LOOP
|
||||
whilestmt ::= SETUP_LOOP testexpr returns POP_BLOCK COME_FROM_LOOP
|
||||
|
||||
# The following rule is for Python 3.5+ where we can have stuff like
|
||||
# while ..
|
||||
|
@@ -42,14 +42,14 @@ class Python36Parser(Python35Parser):
|
||||
# In 3.6+, A sequence of statements ending in a RETURN can cause
|
||||
# JUMP_FORWARD END_FINALLY to be omitted from try middle
|
||||
|
||||
except_return ::= POP_TOP POP_TOP POP_TOP return_stmts
|
||||
except_return ::= POP_TOP POP_TOP POP_TOP returns
|
||||
except_handler ::= JUMP_FORWARD COME_FROM_EXCEPT except_return
|
||||
|
||||
# Try middle following a return_stmts
|
||||
# Try middle following a returns
|
||||
except_handler36 ::= COME_FROM_EXCEPT except_stmts END_FINALLY
|
||||
|
||||
stmt ::= try_except36
|
||||
try_except36 ::= SETUP_EXCEPT return_stmts except_handler36 opt_come_from_except
|
||||
try_except36 ::= SETUP_EXCEPT returns except_handler36 opt_come_from_except
|
||||
"""
|
||||
|
||||
def customize_grammar_rules(self, tokens, customize):
|
||||
|
@@ -19,14 +19,14 @@ LINE_LENGTH = 80
|
||||
# Some ASTs used for comparing code fragments (like 'return None' at
|
||||
# the end of functions).
|
||||
|
||||
RETURN_LOCALS = AST('return_stmt',
|
||||
RETURN_LOCALS = AST('return',
|
||||
[ AST('ret_expr', [AST('expr', [ Token('LOAD_LOCALS') ])]),
|
||||
Token('RETURN_VALUE')])
|
||||
|
||||
NONE = AST('expr', [ NoneToken ] )
|
||||
|
||||
RETURN_NONE = AST('stmt',
|
||||
[ AST('return_stmt',
|
||||
[ AST('return',
|
||||
[ NONE, Token('RETURN_VALUE')]) ])
|
||||
|
||||
PASS = AST('stmts',
|
||||
@@ -231,8 +231,8 @@ TABLE_DIRECT = {
|
||||
'raise_stmt0': ( '%|raise\n', ),
|
||||
'raise_stmt1': ( '%|raise %c\n', 0),
|
||||
'raise_stmt3': ( '%|raise %c, %c, %c\n', 0, 1, 2),
|
||||
# 'yield': ( 'yield %c', 0),
|
||||
# 'return_stmt': ( '%|return %c\n', 0),
|
||||
# 'yield': ( 'yield %c', 0),
|
||||
# 'return': ( '%|return %c\n', 0),
|
||||
'return_if_stmt': ( 'return %c\n', 0),
|
||||
|
||||
'ifstmt': ( '%|if %c:\n%+%c%-', 0, 1 ),
|
||||
|
@@ -206,7 +206,7 @@ class FragmentsWalker(pysource.SourceWalker, object):
|
||||
|
||||
n_tryelsestmt = n_tryelsestmtc = n_tryelsestmtl = n_tryfinallystmt = n_try_except
|
||||
|
||||
def n_return_stmt(self, node):
|
||||
def n_return(self, node):
|
||||
start = len(self.f.getvalue()) + len(self.indent)
|
||||
if self.params['is_lambda']:
|
||||
self.preorder(node[0])
|
||||
@@ -217,7 +217,7 @@ class FragmentsWalker(pysource.SourceWalker, object):
|
||||
else:
|
||||
start = len(self.f.getvalue()) + len(self.indent)
|
||||
self.write(self.indent, 'return')
|
||||
if self.return_none or node != AST('return_stmt', [AST('ret_expr', [NONE]), Token('RETURN_VALUE')]):
|
||||
if self.return_none or node != AST('return', [AST('ret_expr', [NONE]), Token('RETURN_VALUE')]):
|
||||
self.write(' ')
|
||||
self.last_finish = len(self.f.getvalue())
|
||||
self.preorder(node[0])
|
||||
@@ -243,7 +243,7 @@ class FragmentsWalker(pysource.SourceWalker, object):
|
||||
else:
|
||||
start = len(self.f.getvalue()) + len(self.indent)
|
||||
self.write(self.indent, 'return')
|
||||
if self.return_none or node != AST('return_stmt', [AST('ret_expr', [NONE]), Token('RETURN_END_IF')]):
|
||||
if self.return_none or node != AST('return', [AST('ret_expr', [NONE]), Token('RETURN_END_IF')]):
|
||||
self.write(' ')
|
||||
self.preorder(node[0])
|
||||
if hasattr(node[-1], 'offset'):
|
||||
|
@@ -817,7 +817,7 @@ class SourceWalker(GenericASTTraversal, object):
|
||||
# FIXME: should the AST expression be folded into
|
||||
# the global RETURN_NONE constant?
|
||||
return (ret or
|
||||
node == AST('return_stmt',
|
||||
node == AST('return',
|
||||
[AST('ret_expr', [NONE]), Token('RETURN_VALUE')]))
|
||||
|
||||
# Python 3.x can have be dead code as a result of its optimization?
|
||||
@@ -834,7 +834,7 @@ class SourceWalker(GenericASTTraversal, object):
|
||||
self.preorder(node[0])
|
||||
self.prune()
|
||||
|
||||
def n_return_stmt(self, node):
|
||||
def n_return(self, node):
|
||||
if self.params['is_lambda']:
|
||||
self.preorder(node[0])
|
||||
self.prune()
|
||||
|
Reference in New Issue
Block a user