You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-04 01:09:52 +08:00
More grammar isolation
This commit is contained in:
@@ -73,7 +73,6 @@ class Python2Parser(PythonParser):
|
|||||||
|
|
||||||
stmt ::= continue_stmt
|
stmt ::= continue_stmt
|
||||||
continue_stmt ::= CONTINUE
|
continue_stmt ::= CONTINUE
|
||||||
continue_stmt ::= CONTINUE_LOOP
|
|
||||||
continue_stmts ::= _stmts lastl_stmt continue_stmt
|
continue_stmts ::= _stmts lastl_stmt continue_stmt
|
||||||
continue_stmts ::= lastl_stmt continue_stmt
|
continue_stmts ::= lastl_stmt continue_stmt
|
||||||
continue_stmts ::= continue_stmt
|
continue_stmts ::= continue_stmt
|
||||||
@@ -202,11 +201,8 @@ class Python2Parser(PythonParser):
|
|||||||
# In Python 3, DUP_TOPX_2 is DUP_TOP_TWO
|
# In Python 3, DUP_TOPX_2 is DUP_TOP_TWO
|
||||||
binary_subscr2 ::= expr expr DUP_TOPX_2 BINARY_SUBSCR
|
binary_subscr2 ::= expr expr DUP_TOPX_2 BINARY_SUBSCR
|
||||||
|
|
||||||
conditional ::= expr jmp_false expr JUMP_ABSOLUTE expr
|
|
||||||
|
|
||||||
# compare_chained2 is used in a "chained_compare": x <= y <= z
|
# compare_chained2 is used in a "chained_compare": x <= y <= z
|
||||||
compare_chained2 ::= expr COMPARE_OP RETURN_VALUE
|
compare_chained2 ::= expr COMPARE_OP RETURN_VALUE
|
||||||
compare_chained2 ::= expr COMPARE_OP RETURN_VALUE_LAMBDA
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def p_slice2(self, args):
|
def p_slice2(self, args):
|
||||||
@@ -347,6 +343,10 @@ class Python2Parser(PythonParser):
|
|||||||
nak = ( len(opname_base)-len('CALL_METHOD') ) // 3
|
nak = ( len(opname_base)-len('CALL_METHOD') ) // 3
|
||||||
rule = 'call_function ::= expr ' + 'expr '*args_pos + 'kwarg '*args_kw \
|
rule = 'call_function ::= expr ' + 'expr '*args_pos + 'kwarg '*args_kw \
|
||||||
+ 'expr ' * nak + opname
|
+ 'expr ' * nak + opname
|
||||||
|
elif opname == 'CONTINUE_LOOP':
|
||||||
|
self.add_unique_rule('continue_stmt ::= CONTINUE_LOOP',
|
||||||
|
opname, v, customize)
|
||||||
|
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
|
||||||
@@ -378,13 +378,18 @@ class Python2Parser(PythonParser):
|
|||||||
if i > 0 and tokens[i-1] == 'LOAD_LAMBDA':
|
if i > 0 and tokens[i-1] == 'LOAD_LAMBDA':
|
||||||
self.addRule('mklambda ::= %s load_closure LOAD_LAMBDA %s' %
|
self.addRule('mklambda ::= %s load_closure LOAD_LAMBDA %s' %
|
||||||
('expr '*v, opname), nop_func)
|
('expr '*v, opname), nop_func)
|
||||||
|
if i > 0:
|
||||||
|
prev_tok = tokens[i-1]
|
||||||
|
if prev_tok == 'LOAD_GENEXPR':
|
||||||
|
self.add_unique_rules([
|
||||||
|
('genexpr ::= %s load_closure LOAD_GENEXPR %s expr'
|
||||||
|
' GET_ITER CALL_FUNCTION_1' %
|
||||||
|
('expr '*v, opname))], customize)
|
||||||
|
pass
|
||||||
self.add_unique_rules([
|
self.add_unique_rules([
|
||||||
('genexpr ::= %s load_closure LOAD_GENEXPR %s expr'
|
|
||||||
' GET_ITER CALL_FUNCTION_1' %
|
|
||||||
('expr '*v, opname)),
|
|
||||||
('mkfunc ::= %s load_closure LOAD_CONST %s' %
|
('mkfunc ::= %s load_closure LOAD_CONST %s' %
|
||||||
('expr '*v, opname))],
|
('expr '*v, opname))], customize)
|
||||||
customize)
|
|
||||||
if self.version >= 2.7:
|
if self.version >= 2.7:
|
||||||
if i > 0:
|
if i > 0:
|
||||||
prev_tok = tokens[i-1]
|
prev_tok = tokens[i-1]
|
||||||
|
@@ -20,6 +20,8 @@ class Python21Parser(Python22Parser):
|
|||||||
forstmt ::= SETUP_LOOP expr _for designator
|
forstmt ::= SETUP_LOOP expr _for designator
|
||||||
l_stmts_opt _jump_back
|
l_stmts_opt _jump_back
|
||||||
POP_BLOCK COME_FROM
|
POP_BLOCK COME_FROM
|
||||||
|
|
||||||
|
conditional ::= expr jmp_false expr JUMP_ABSOLUTE expr
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def p_import21(self, args):
|
def p_import21(self, args):
|
||||||
|
@@ -53,8 +53,6 @@ class Python27Parser(Python2Parser):
|
|||||||
|
|
||||||
except_cond2 ::= DUP_TOP expr COMPARE_OP
|
except_cond2 ::= DUP_TOP expr COMPARE_OP
|
||||||
jmp_false POP_TOP designator POP_TOP
|
jmp_false POP_TOP designator POP_TOP
|
||||||
|
|
||||||
except_suite ::= c_stmts_opt CONTINUE
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def p_jump27(self, args):
|
def p_jump27(self, args):
|
||||||
@@ -120,6 +118,7 @@ class Python27Parser(Python2Parser):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def add_custom_rules(self, tokens, customize):
|
def add_custom_rules(self, tokens, customize):
|
||||||
|
# 2.7 changes COME_FROM to COME_FROM_FINALLY
|
||||||
self.remove_rules("""
|
self.remove_rules("""
|
||||||
while1stmt ::= SETUP_LOOP l_stmts JUMP_BACK COME_FROM
|
while1stmt ::= SETUP_LOOP l_stmts JUMP_BACK COME_FROM
|
||||||
while1elsestmt ::= SETUP_LOOP l_stmts JUMP_BACK else_suite COME_FROM
|
while1elsestmt ::= SETUP_LOOP l_stmts JUMP_BACK else_suite COME_FROM
|
||||||
|
@@ -258,6 +258,8 @@ class Scanner2(Scanner):
|
|||||||
# as CALL_FUNCTION. The value is not used in these cases, so we put
|
# as CALL_FUNCTION. The value is not used in these cases, so we put
|
||||||
# in arbitrary value 0.
|
# in arbitrary value 0.
|
||||||
customize[op_name] = 0
|
customize[op_name] = 0
|
||||||
|
elif op == self.opc.CONTINUE_LOOP:
|
||||||
|
customize[op_name] = 0
|
||||||
elif op == self.opc.JUMP_ABSOLUTE:
|
elif op == self.opc.JUMP_ABSOLUTE:
|
||||||
# Further classify JUMP_ABSOLUTE into backward jumps
|
# Further classify JUMP_ABSOLUTE into backward jumps
|
||||||
# which are used in loops, and "CONTINUE" jumps which
|
# which are used in loops, and "CONTINUE" jumps which
|
||||||
|
@@ -236,6 +236,8 @@ class Scanner26(scan.Scanner2):
|
|||||||
else:
|
else:
|
||||||
op_name = '%s_%d' % (op_name, oparg)
|
op_name = '%s_%d' % (op_name, oparg)
|
||||||
customize[op_name] = oparg
|
customize[op_name] = oparg
|
||||||
|
elif self.version > 2.0 and op == self.opc.CONTINUE_LOOP:
|
||||||
|
customize[op_name] = 0
|
||||||
elif op == self.opc.JUMP_ABSOLUTE:
|
elif op == self.opc.JUMP_ABSOLUTE:
|
||||||
# Further classify JUMP_ABSOLUTE into backward jumps
|
# Further classify JUMP_ABSOLUTE into backward jumps
|
||||||
# which are used in loops, and "CONTINUE" jumps which
|
# which are used in loops, and "CONTINUE" jumps which
|
||||||
|
Reference in New Issue
Block a user