You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-04 01:09:52 +08:00
grammar isolation and reduction
This commit is contained in:
@@ -20,8 +20,8 @@ def test_grammar():
|
|||||||
unused_rhs = set(['list', 'mkfunc',
|
unused_rhs = set(['list', 'mkfunc',
|
||||||
'mklambda',
|
'mklambda',
|
||||||
'unpack',])
|
'unpack',])
|
||||||
expect_right_recursive = frozenset([('designList',
|
expect_right_recursive = set([('designList',
|
||||||
('store', 'DUP_TOP', 'designList'))])
|
('store', 'DUP_TOP', 'designList'))])
|
||||||
if PYTHON3:
|
if PYTHON3:
|
||||||
expect_lhs.add('load_genexpr')
|
expect_lhs.add('load_genexpr')
|
||||||
expect_lhs.add('kvlist')
|
expect_lhs.add('kvlist')
|
||||||
@@ -35,14 +35,21 @@ def test_grammar():
|
|||||||
expect_lhs.add("annotate_arg")
|
expect_lhs.add("annotate_arg")
|
||||||
expect_lhs.add("annotate_tuple")
|
expect_lhs.add("annotate_tuple")
|
||||||
unused_rhs.add("mkfunc_annotate")
|
unused_rhs.add("mkfunc_annotate")
|
||||||
if PYTHON_VERSION != 3.6:
|
if PYTHON_VERSION < 3.6:
|
||||||
# 3.6 has at least one non-custom call rule
|
# 3.6 has at least one non-custom call rule
|
||||||
# the others don't
|
# the others don't
|
||||||
unused_rhs.add('call')
|
unused_rhs.add('call')
|
||||||
|
if PYTHON_VERSION == 3.5:
|
||||||
|
expect_right_recursive.add((('l_stmts',
|
||||||
|
('lastl_stmt', 'COME_FROM', 'l_stmts'))))
|
||||||
|
pass
|
||||||
|
pass
|
||||||
else:
|
else:
|
||||||
# These are custom rules set in 3.5
|
expect_right_recursive.add((('l_stmts',
|
||||||
|
('lastl_stmt', 'COME_FROM', 'l_stmts'))))
|
||||||
unused_rhs.add('build_map_unpack_with_call')
|
unused_rhs.add('build_map_unpack_with_call')
|
||||||
unused_rhs.add('unmapexpr')
|
unused_rhs.add('unmapexpr')
|
||||||
|
# expect_lhs.add('kwargs1')
|
||||||
pass
|
pass
|
||||||
pass
|
pass
|
||||||
pass
|
pass
|
||||||
|
@@ -42,6 +42,7 @@ class PythonParser(GenericASTBuilder):
|
|||||||
'kvlist_n',
|
'kvlist_n',
|
||||||
# Python 3.6+
|
# Python 3.6+
|
||||||
'joined_str',
|
'joined_str',
|
||||||
|
'come_from_loops',
|
||||||
]
|
]
|
||||||
self.collect = frozenset(nt_list)
|
self.collect = frozenset(nt_list)
|
||||||
|
|
||||||
@@ -157,9 +158,6 @@ class PythonParser(GenericASTBuilder):
|
|||||||
else:
|
else:
|
||||||
raise ParserError(None, -1)
|
raise ParserError(None, -1)
|
||||||
|
|
||||||
def typestring(self, token):
|
|
||||||
return token.kind
|
|
||||||
|
|
||||||
def nonterminal(self, nt, args):
|
def nonterminal(self, nt, args):
|
||||||
n = len(args)
|
n = len(args)
|
||||||
|
|
||||||
@@ -177,6 +175,7 @@ class PythonParser(GenericASTBuilder):
|
|||||||
rv.append(args[1])
|
rv.append(args[1])
|
||||||
elif n == 1 and args[0] in self.singleton:
|
elif n == 1 and args[0] in self.singleton:
|
||||||
rv = GenericASTBuilder.nonterminal(self, nt, args[0])
|
rv = GenericASTBuilder.nonterminal(self, nt, args[0])
|
||||||
|
del args[0] # save memory
|
||||||
else:
|
else:
|
||||||
rv = GenericASTBuilder.nonterminal(self, nt, args)
|
rv = GenericASTBuilder.nonterminal(self, nt, args)
|
||||||
return rv
|
return rv
|
||||||
@@ -225,7 +224,6 @@ class PythonParser(GenericASTBuilder):
|
|||||||
lastc_stmt ::= iflaststmt
|
lastc_stmt ::= iflaststmt
|
||||||
lastc_stmt ::= forelselaststmt
|
lastc_stmt ::= forelselaststmt
|
||||||
lastc_stmt ::= ifelsestmtc
|
lastc_stmt ::= ifelsestmtc
|
||||||
lastc_stmt ::= tryelsestmtc
|
|
||||||
|
|
||||||
c_stmts_opt ::= c_stmts
|
c_stmts_opt ::= c_stmts
|
||||||
c_stmts_opt ::= passstmt
|
c_stmts_opt ::= passstmt
|
||||||
|
@@ -133,6 +133,8 @@ class Python2Parser(PythonParser):
|
|||||||
POP_BLOCK LOAD_CONST
|
POP_BLOCK LOAD_CONST
|
||||||
COME_FROM suite_stmts_opt END_FINALLY
|
COME_FROM suite_stmts_opt END_FINALLY
|
||||||
|
|
||||||
|
lastc_stmt ::= tryelsestmtc
|
||||||
|
|
||||||
# Move to 2.7? 2.6 may use come_froms
|
# Move to 2.7? 2.6 may use come_froms
|
||||||
tryelsestmtc ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
|
tryelsestmtc ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
|
||||||
except_handler else_suitec COME_FROM
|
except_handler else_suitec COME_FROM
|
||||||
|
@@ -104,6 +104,7 @@ class Python3Parser(PythonParser):
|
|||||||
|
|
||||||
kwarg ::= LOAD_CONST expr
|
kwarg ::= LOAD_CONST expr
|
||||||
kwargs ::= kwarg*
|
kwargs ::= kwarg*
|
||||||
|
kwargs1 ::= kwarg+
|
||||||
|
|
||||||
classdef ::= build_class store
|
classdef ::= build_class store
|
||||||
|
|
||||||
@@ -268,7 +269,7 @@ class Python3Parser(PythonParser):
|
|||||||
except_handler ::= JUMP_FORWARD COME_FROM_EXCEPT except_stmts
|
except_handler ::= JUMP_FORWARD COME_FROM_EXCEPT except_stmts
|
||||||
END_FINALLY COME_FROM_EXCEPT_CLAUSE
|
END_FINALLY COME_FROM_EXCEPT_CLAUSE
|
||||||
|
|
||||||
for_block ::= l_stmts_opt come_from_loops JUMP_BACK
|
for_block ::= l_stmts_opt COME_FROM_LOOP JUMP_BACK
|
||||||
for_block ::= l_stmts
|
for_block ::= l_stmts
|
||||||
iflaststmtl ::= testexpr c_stmts_opt
|
iflaststmtl ::= testexpr c_stmts_opt
|
||||||
"""
|
"""
|
||||||
@@ -300,7 +301,6 @@ class Python3Parser(PythonParser):
|
|||||||
opt_come_from_except ::= come_from_except_clauses
|
opt_come_from_except ::= come_from_except_clauses
|
||||||
|
|
||||||
come_from_except_clauses ::= COME_FROM_EXCEPT_CLAUSE+
|
come_from_except_clauses ::= COME_FROM_EXCEPT_CLAUSE+
|
||||||
come_from_loops ::= COME_FROM_LOOP*
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def p_jump3(self, args):
|
def p_jump3(self, args):
|
||||||
@@ -335,7 +335,7 @@ class Python3Parser(PythonParser):
|
|||||||
def p_loop_stmt3(self, args):
|
def p_loop_stmt3(self, args):
|
||||||
"""
|
"""
|
||||||
forstmt ::= SETUP_LOOP expr _for store for_block POP_BLOCK
|
forstmt ::= SETUP_LOOP expr _for store for_block POP_BLOCK
|
||||||
come_from_loops
|
COME_FROM_LOOP
|
||||||
|
|
||||||
forelsestmt ::= SETUP_LOOP expr _for store for_block POP_BLOCK else_suite
|
forelsestmt ::= SETUP_LOOP expr _for store for_block POP_BLOCK else_suite
|
||||||
COME_FROM_LOOP
|
COME_FROM_LOOP
|
||||||
@@ -812,7 +812,7 @@ class Python3Parser(PythonParser):
|
|||||||
opname, token.attr, customize)
|
opname, token.attr, customize)
|
||||||
|
|
||||||
if args_kw > 0:
|
if args_kw > 0:
|
||||||
kwargs_str = 'kwargs '
|
kwargs_str = 'kwargs1 '
|
||||||
else:
|
else:
|
||||||
kwargs_str = ''
|
kwargs_str = ''
|
||||||
|
|
||||||
@@ -907,6 +907,11 @@ class Python3Parser(PythonParser):
|
|||||||
rule = ('mkfunc ::= kwargs %s%s %s' %
|
rule = ('mkfunc ::= kwargs %s%s %s' %
|
||||||
('pos_arg ' * args_pos, 'LOAD_CONST '*2,
|
('pos_arg ' * args_pos, 'LOAD_CONST '*2,
|
||||||
opname))
|
opname))
|
||||||
|
elif self.version > 3.5:
|
||||||
|
# positional args before keyword args
|
||||||
|
rule = ('mkfunc ::= %skwargs1 %s %s' %
|
||||||
|
('pos_arg ' * args_pos, 'LOAD_CONST '*2,
|
||||||
|
opname))
|
||||||
elif self.version > 3.3:
|
elif self.version > 3.3:
|
||||||
# positional args before keyword args
|
# positional args before keyword args
|
||||||
rule = ('mkfunc ::= %skwargs %s %s' %
|
rule = ('mkfunc ::= %skwargs %s %s' %
|
||||||
|
@@ -85,7 +85,7 @@ class Python35Parser(Python34Parser):
|
|||||||
POP_TOP POP_TOP POP_TOP POP_EXCEPT POP_BLOCK
|
POP_TOP POP_TOP POP_TOP POP_EXCEPT POP_BLOCK
|
||||||
JUMP_ABSOLUTE END_FINALLY COME_FROM
|
JUMP_ABSOLUTE END_FINALLY COME_FROM
|
||||||
for_block POP_BLOCK JUMP_ABSOLUTE
|
for_block POP_BLOCK JUMP_ABSOLUTE
|
||||||
come_from_loops
|
COME_FROM_LOOP
|
||||||
|
|
||||||
async_for_stmt ::= SETUP_LOOP expr
|
async_for_stmt ::= SETUP_LOOP expr
|
||||||
GET_AITER
|
GET_AITER
|
||||||
@@ -97,7 +97,7 @@ class Python35Parser(Python34Parser):
|
|||||||
POP_TOP POP_TOP POP_TOP POP_EXCEPT POP_BLOCK
|
POP_TOP POP_TOP POP_TOP POP_EXCEPT POP_BLOCK
|
||||||
JUMP_ABSOLUTE END_FINALLY JUMP_BACK
|
JUMP_ABSOLUTE END_FINALLY JUMP_BACK
|
||||||
passstmt POP_BLOCK JUMP_ABSOLUTE
|
passstmt POP_BLOCK JUMP_ABSOLUTE
|
||||||
come_from_loops
|
COME_FROM_LOOP
|
||||||
|
|
||||||
stmt ::= async_forelse_stmt
|
stmt ::= async_forelse_stmt
|
||||||
async_forelse_stmt ::= SETUP_LOOP expr
|
async_forelse_stmt ::= SETUP_LOOP expr
|
||||||
|
@@ -37,6 +37,10 @@ class Python36Parser(Python35Parser):
|
|||||||
call ::= expr expr CALL_FUNCTION_EX
|
call ::= expr expr CALL_FUNCTION_EX
|
||||||
call ::= expr expr expr CALL_FUNCTION_EX_KW_1
|
call ::= expr expr expr CALL_FUNCTION_EX_KW_1
|
||||||
|
|
||||||
|
for_block ::= l_stmts_opt come_from_loops JUMP_BACK
|
||||||
|
come_from_loops ::= COME_FROM_LOOP*
|
||||||
|
|
||||||
|
|
||||||
# This might be valid in < 3.6
|
# This might be valid in < 3.6
|
||||||
and ::= expr jmp_false expr
|
and ::= expr jmp_false expr
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user