You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-04 09:22:40 +08:00
DRY redundant custom rule checking code
This commit is contained in:
@@ -28,6 +28,17 @@ nop_func = lambda self, args: None
|
|||||||
|
|
||||||
class PythonParser(GenericASTBuilder):
|
class PythonParser(GenericASTBuilder):
|
||||||
|
|
||||||
|
def add_unique_rule(self, rule, opname, count, customize):
|
||||||
|
"""Add rule to grammar, but only if it hasn't been added previously
|
||||||
|
"""
|
||||||
|
if rule not in self.new_rules:
|
||||||
|
# print("XXX ", rule) # debug
|
||||||
|
self.new_rules.add(rule)
|
||||||
|
self.addRule(rule, nop_func)
|
||||||
|
customize[opname] = count
|
||||||
|
pass
|
||||||
|
return
|
||||||
|
|
||||||
def cleanup(self):
|
def cleanup(self):
|
||||||
"""
|
"""
|
||||||
Remove recursive references to allow garbage
|
Remove recursive references to allow garbage
|
||||||
|
@@ -22,11 +22,7 @@ class Python2Parser(PythonParser):
|
|||||||
|
|
||||||
def __init__(self, debug_parser=PARSER_DEFAULT_DEBUG):
|
def __init__(self, debug_parser=PARSER_DEFAULT_DEBUG):
|
||||||
super(Python2Parser, self).__init__(AST, 'stmts', debug=debug_parser)
|
super(Python2Parser, self).__init__(AST, 'stmts', debug=debug_parser)
|
||||||
self.customized = {}
|
self.new_rules = set()
|
||||||
|
|
||||||
# FIXME: redo with parse3's add_unique_rule.
|
|
||||||
self.seen32 = False
|
|
||||||
self.seen1024 = False
|
|
||||||
|
|
||||||
def p_list_comprehension2(self, args):
|
def p_list_comprehension2(self, args):
|
||||||
"""
|
"""
|
||||||
@@ -328,30 +324,27 @@ class Python2Parser(PythonParser):
|
|||||||
expr ::= expr {expr}^n CALL_FUNCTION_KW_n POP_TOP
|
expr ::= expr {expr}^n CALL_FUNCTION_KW_n POP_TOP
|
||||||
'''
|
'''
|
||||||
for k, v in list(customize.items()):
|
for k, v in list(customize.items()):
|
||||||
# avoid adding the same rule twice to this parser
|
|
||||||
if k in self.customized:
|
|
||||||
continue
|
|
||||||
self.customized[k] = None
|
|
||||||
|
|
||||||
op = k[:k.rfind('_')]
|
op = k[:k.rfind('_')]
|
||||||
if op in ('BUILD_LIST', 'BUILD_TUPLE', 'BUILD_SET'):
|
if op in ('BUILD_LIST', 'BUILD_TUPLE', 'BUILD_SET'):
|
||||||
thousands = (v//1024)
|
thousands = (v//1024)
|
||||||
thirty32s = ((v//32)%32)
|
thirty32s = ((v//32)%32)
|
||||||
if thirty32s > 0 and not self.seen32:
|
if thirty32s > 0:
|
||||||
rule = "expr32 ::=%s" % (' expr' * 32)
|
rule = "expr32 ::=%s" % (' expr' * 32)
|
||||||
self.addRule(rule, nop_func)
|
self.add_unique_rule(rule, op, v, customize)
|
||||||
self.seen32 = True
|
self.seen32 = True
|
||||||
if thousands > 0 and not self.seen1025:
|
if thousands > 0:
|
||||||
self.addRule("expr1024 ::=%s" % (' expr32' * 32), nop_func)
|
self.add_unique_rule("expr1024 ::=%s" % (' expr32' * 32),
|
||||||
|
op, v, customize)
|
||||||
self.seen1024 = True
|
self.seen1024 = True
|
||||||
rule = ('build_list ::= ' + 'expr1024 '*thousands +
|
rule = ('build_list ::= ' + 'expr1024 '*thousands +
|
||||||
'expr32 '*thirty32s + 'expr '*(v%32) + k)
|
'expr32 '*thirty32s + 'expr '*(v%32) + k)
|
||||||
elif op == 'BUILD_MAP':
|
elif op == 'BUILD_MAP':
|
||||||
kvlist_n = "kvlist_%s" % v
|
kvlist_n = "kvlist_%s" % v
|
||||||
rule = kvlist_n + ' ::= ' + ' kv3' * v
|
rule = kvlist_n + ' ::= ' + ' kv3' * v
|
||||||
self.addRule(rule, nop_func)
|
self.add_unique_rule(rule, op, v, customize)
|
||||||
rule = "mapexpr ::= %s %s" % (k, kvlist_n)
|
rule = "mapexpr ::= %s %s" % (k, kvlist_n)
|
||||||
self.addRule(rule, nop_func)
|
self.add_unique_rule(rule, op, v, customize)
|
||||||
elif op in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'):
|
elif op in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'):
|
||||||
rule = 'unpack ::= ' + k + ' designator'*v
|
rule = 'unpack ::= ' + k + ' designator'*v
|
||||||
elif op == 'UNPACK_LIST':
|
elif op == 'UNPACK_LIST':
|
||||||
@@ -385,7 +378,7 @@ class Python2Parser(PythonParser):
|
|||||||
+ 'expr ' * nak + k
|
+ 'expr ' * nak + k
|
||||||
else:
|
else:
|
||||||
raise Exception('unknown customize token %s' % k)
|
raise Exception('unknown customize token %s' % k)
|
||||||
self.addRule(rule, nop_func)
|
self.add_unique_rule(rule, op, v, customize)
|
||||||
|
|
||||||
class Python2ParserSingle(Python2Parser, PythonParserSingle):
|
class Python2ParserSingle(Python2Parser, PythonParserSingle):
|
||||||
pass
|
pass
|
||||||
|
@@ -17,7 +17,7 @@ that a later phase can tern into a sequence of ASCII text.
|
|||||||
|
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
from uncompyle6.parser import PythonParser, PythonParserSingle, nop_func
|
from uncompyle6.parser import PythonParser, PythonParserSingle
|
||||||
from uncompyle6.parsers.astnode import AST
|
from uncompyle6.parsers.astnode import AST
|
||||||
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
|
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
|
||||||
from uncompyle6 import PYTHON3
|
from uncompyle6 import PYTHON3
|
||||||
@@ -32,17 +32,6 @@ class Python3Parser(PythonParser):
|
|||||||
super(Python3Parser, self).__init__(AST, 'stmts', debug=debug_parser)
|
super(Python3Parser, self).__init__(AST, 'stmts', debug=debug_parser)
|
||||||
self.new_rules = set()
|
self.new_rules = set()
|
||||||
|
|
||||||
def add_unique_rule(self, rule, opname, count, customize):
|
|
||||||
"""Add rule to grammar, but only if it hasn't been added previously
|
|
||||||
"""
|
|
||||||
if rule not in self.new_rules:
|
|
||||||
# print("XXX ", rule) # debug
|
|
||||||
self.new_rules.add(rule)
|
|
||||||
self.addRule(rule, nop_func)
|
|
||||||
customize[opname] = count
|
|
||||||
pass
|
|
||||||
return
|
|
||||||
|
|
||||||
def p_list_comprehension3(self, args):
|
def p_list_comprehension3(self, args):
|
||||||
"""
|
"""
|
||||||
# Python3 scanner adds LOAD_LISTCOMP. Python3 does list comprehension like
|
# Python3 scanner adds LOAD_LISTCOMP. Python3 does list comprehension like
|
||||||
|
Reference in New Issue
Block a user