You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-04 01:09:52 +08:00
@@ -102,6 +102,8 @@ class Python26Parser(Python2Parser):
|
|||||||
|
|
||||||
def p_stmt26(self, args):
|
def p_stmt26(self, args):
|
||||||
"""
|
"""
|
||||||
|
stmt ::= ifelsestmtr
|
||||||
|
|
||||||
# We use filler as a placeholder to keep nonterminal positions
|
# We use filler as a placeholder to keep nonterminal positions
|
||||||
# the same across different grammars so that the same semantic actions
|
# the same across different grammars so that the same semantic actions
|
||||||
# can be used
|
# can be used
|
||||||
@@ -173,6 +175,8 @@ class Python26Parser(Python2Parser):
|
|||||||
iflaststmt ::= testexpr_then c_stmts_opt JUMP_ABSOLUTE come_froms POP_TOP
|
iflaststmt ::= testexpr_then c_stmts_opt JUMP_ABSOLUTE come_froms POP_TOP
|
||||||
iflaststmt ::= testexpr c_stmts_opt JUMP_ABSOLUTE come_froms POP_TOP
|
iflaststmt ::= testexpr c_stmts_opt JUMP_ABSOLUTE come_froms POP_TOP
|
||||||
|
|
||||||
|
ifelsestmtr ::= testexpr_then return_if_stmts returns
|
||||||
|
|
||||||
testexpr_then ::= testtrue_then
|
testexpr_then ::= testtrue_then
|
||||||
testexpr_then ::= testfalse_then
|
testexpr_then ::= testfalse_then
|
||||||
testtrue_then ::= expr jmp_true_then
|
testtrue_then ::= expr jmp_true_then
|
||||||
|
@@ -127,6 +127,8 @@ class Python27Parser(Python2Parser):
|
|||||||
|
|
||||||
def p_stmt27(self, args):
|
def p_stmt27(self, args):
|
||||||
"""
|
"""
|
||||||
|
stmt ::= ifelsestmtr
|
||||||
|
|
||||||
# assert condition
|
# assert condition
|
||||||
assert ::= assert_expr jmp_true LOAD_ASSERT RAISE_VARARGS_1
|
assert ::= assert_expr jmp_true LOAD_ASSERT RAISE_VARARGS_1
|
||||||
|
|
||||||
@@ -178,6 +180,7 @@ class Python27Parser(Python2Parser):
|
|||||||
ifelsestmtc ::= testexpr c_stmts_opt JUMP_ABSOLUTE else_suitec
|
ifelsestmtc ::= testexpr c_stmts_opt JUMP_ABSOLUTE else_suitec
|
||||||
ifelsestmtl ::= testexpr c_stmts_opt JUMP_BACK else_suitel
|
ifelsestmtl ::= testexpr c_stmts_opt JUMP_BACK else_suitel
|
||||||
ifelsestmtl ::= testexpr c_stmts_opt CONTINUE else_suitel
|
ifelsestmtl ::= testexpr c_stmts_opt CONTINUE else_suitel
|
||||||
|
ifelsestmtr ::= testexpr return_if_stmts COME_FROM returns
|
||||||
|
|
||||||
# Common with 2.6
|
# Common with 2.6
|
||||||
return_if_lambda ::= RETURN_END_IF_LAMBDA COME_FROM
|
return_if_lambda ::= RETURN_END_IF_LAMBDA COME_FROM
|
||||||
|
@@ -95,7 +95,7 @@ class Python3Parser(PythonParser):
|
|||||||
def p_grammar(self, args):
|
def p_grammar(self, args):
|
||||||
"""
|
"""
|
||||||
sstmt ::= stmt
|
sstmt ::= stmt
|
||||||
sstmt ::= ifelsestmtr
|
stmt ::= ifelsestmtr
|
||||||
sstmt ::= return RETURN_LAST
|
sstmt ::= return RETURN_LAST
|
||||||
|
|
||||||
return_if_stmts ::= return_if_stmt come_from_opt
|
return_if_stmts ::= return_if_stmt come_from_opt
|
||||||
|
@@ -353,12 +353,16 @@ TABLE_DIRECT = {
|
|||||||
'ifelsestmt': ( '%|if %c:\n%+%c%-%|else:\n%+%c%-', 0, 1, 3 ),
|
'ifelsestmt': ( '%|if %c:\n%+%c%-%|else:\n%+%c%-', 0, 1, 3 ),
|
||||||
'ifelsestmtc': ( '%|if %c:\n%+%c%-%|else:\n%+%c%-', 0, 1, 3 ),
|
'ifelsestmtc': ( '%|if %c:\n%+%c%-%|else:\n%+%c%-', 0, 1, 3 ),
|
||||||
'ifelsestmtl': ( '%|if %c:\n%+%c%-%|else:\n%+%c%-', 0, 1, 3 ),
|
'ifelsestmtl': ( '%|if %c:\n%+%c%-%|else:\n%+%c%-', 0, 1, 3 ),
|
||||||
|
'ifelsestmtr': ( '%|if %c:\n%+%c%-%|else:\n%+%c%-', 0, 1, 2 ),
|
||||||
|
'ifelsestmtr2': ( '%|if %c:\n%+%c%-%|else:\n%+%c%-\n\n', 0, 1, 3 ), # has COME_FROM
|
||||||
|
|
||||||
|
|
||||||
|
# "elif" forms are not generated by the parser but are created through tree
|
||||||
|
# transformations. See "n_ifelsestmt".
|
||||||
'ifelifstmt': ( '%|if %c:\n%+%c%-%c', 0, 1, 3 ),
|
'ifelifstmt': ( '%|if %c:\n%+%c%-%c', 0, 1, 3 ),
|
||||||
'elifelifstmt': ( '%|elif %c:\n%+%c%-%c', 0, 1, 3 ),
|
'elifelifstmt': ( '%|elif %c:\n%+%c%-%c', 0, 1, 3 ),
|
||||||
'elifstmt': ( '%|elif %c:\n%+%c%-', 0, 1 ),
|
'elifstmt': ( '%|elif %c:\n%+%c%-', 0, 1 ),
|
||||||
'elifelsestmt': ( '%|elif %c:\n%+%c%-%|else:\n%+%c%-', 0, 1, 3 ),
|
'elifelsestmt': ( '%|elif %c:\n%+%c%-%|else:\n%+%c%-', 0, 1, 3 ),
|
||||||
'ifelsestmtr': ( '%|if %c:\n%+%c%-%|else:\n%+%c%-', 0, 1, 2 ),
|
|
||||||
'ifelsestmtr2': ( '%|if %c:\n%+%c%-%|else:\n%+%c%-\n\n', 0, 1, 3 ), # has COME_FROM
|
|
||||||
'elifelsestmtr': ( '%|elif %c:\n%+%c%-%|else:\n%+%c%-\n\n', 0, 1, 2 ),
|
'elifelsestmtr': ( '%|elif %c:\n%+%c%-%|else:\n%+%c%-\n\n', 0, 1, 2 ),
|
||||||
'elifelsestmtr2': ( '%|elif %c:\n%+%c%-%|else:\n%+%c%-\n\n', 0, 1, 3 ), # has COME_FROM
|
'elifelsestmtr2': ( '%|elif %c:\n%+%c%-%|else:\n%+%c%-\n\n', 0, 1, 3 ), # has COME_FROM
|
||||||
|
|
||||||
|
@@ -646,15 +646,31 @@ class SourceWalker(GenericASTTraversal, object):
|
|||||||
self.println()
|
self.println()
|
||||||
self.prune() # stop recursing
|
self.prune() # stop recursing
|
||||||
|
|
||||||
|
# preprocess is used for handling chains of
|
||||||
|
# if elif elif
|
||||||
def n_ifelsestmt(self, node, preprocess=False):
|
def n_ifelsestmt(self, node, preprocess=False):
|
||||||
|
"""
|
||||||
|
Here we turn:
|
||||||
|
|
||||||
|
if ...
|
||||||
|
else
|
||||||
|
if ..
|
||||||
|
|
||||||
|
into:
|
||||||
|
|
||||||
|
if ..
|
||||||
|
elif ...
|
||||||
|
|
||||||
|
where appropriate
|
||||||
|
"""
|
||||||
else_suite = node[3]
|
else_suite = node[3]
|
||||||
|
|
||||||
n = else_suite[0]
|
n = else_suite[0]
|
||||||
|
|
||||||
if len(n) == 1 == len(n[0]) and n[0] == '_stmts':
|
if len(n) == 1 == len(n[0]) and n[0] == 'stmt':
|
||||||
n = n[0][0][0]
|
|
||||||
elif n[0].kind in ('lastc_stmt', 'lastl_stmt'):
|
|
||||||
n = n[0][0]
|
n = n[0][0]
|
||||||
|
elif n[0].kind in ('lastc_stmt', 'lastl_stmt'):
|
||||||
|
n = n[0]
|
||||||
else:
|
else:
|
||||||
if not preprocess:
|
if not preprocess:
|
||||||
self.default(node)
|
self.default(node)
|
||||||
|
Reference in New Issue
Block a user