Python 3 build class parsing

This commit is contained in:
rocky
2016-05-07 23:29:23 -04:00
parent 36ffd4c31f
commit 406df297df
3 changed files with 18 additions and 2 deletions

Binary file not shown.

View File

@@ -0,0 +1,6 @@
# Tests Python 3:
# build_class ::= LOAD_BUILD_CLASS mkfunc expr call_function CALL_FUNCTION_3
from collections import namedtuple
class Event(namedtuple('Event', 'time, priority, action, argument')):
pass

View File

@@ -393,9 +393,14 @@ class Python3Parser(PythonParser):
def custom_build_class_rule(self, opname, i, token, tokens, customize):
"""
# Should the first rule be somehow folded into the 2nd one?
build_class ::= LOAD_BUILD_CLASS mkfunc
LOAD_CLASSNAME {expr}^n CALL_FUNCTION_n+2
LOAD_CONST CALL_FUNCTION_n
build_class ::= LOAD_BUILD_CLASS mkfunc
expr
call_function
CALL_FUNCTION_3
"""
# FIXME: I bet this can be simplified
# look for next MAKE_FUNCTION
@@ -405,14 +410,14 @@ class Python3Parser(PythonParser):
pass
assert i < len(tokens), "build_class needs to find MAKE_FUNCTION"
assert tokens[i+1].type == 'LOAD_CONST', \
"build_class expecing CONST after MAKE_FUNCTION"
"build_class expecting CONST after MAKE_FUNCTION"
for i in range(i, len(tokens)):
if tokens[i].type == 'CALL_FUNCTION':
call_fn_tok = tokens[i]
break
assert call_fn_tok, "build_class custom rule needs to find CALL_FUNCTION"
# customize CALL_FUNCTION
# customize build_class rule
call_function = self.call_fn_name(call_fn_tok)
args_pos = call_fn_tok.attr & 0xff
args_kw = (call_fn_tok.attr >> 8) & 0xff
@@ -420,6 +425,11 @@ class Python3Parser(PythonParser):
"%s" % (('expr ' * (args_pos - 1) + ('kwarg ' * args_kw)),
call_function))
self.add_unique_rule(rule, opname, token.attr, customize)
# Can the above build_class rule be folded into this rule?
rule = ("build_class ::= LOAD_BUILD_CLASS mkfunc expr call_function "
"CALL_FUNCTION_" + str(args_pos+1))
self.add_unique_rule(rule, opname, token.attr, customize)
return
def custom_classfunc_rule(self, opname, token, customize):