More complete annotate handling

Still have a bit of work to do though.
This commit is contained in:
rocky
2016-10-28 19:52:30 -04:00
parent 2e355b6245
commit ccdd37611c
6 changed files with 191 additions and 174 deletions

View File

@@ -22,10 +22,12 @@ def test_grammar():
expect_right_recursive = [['designList', ('designator', 'DUP_TOP', 'designList')]]
if PYTHON3:
expect_lhs.add('load_genexpr')
unused_rhs = unused_rhs.union(set("""
except_pop_except genexpr classdefdeco2 listcomp
""".split()))
if 3.1 <= PYTHON_VERSION <= 3.4:
if 3.0 <= PYTHON_VERSION:
expect_lhs.add("annotate_arg")
unused_rhs.add("mkfunc_annotate")
pass
else:

View File

@@ -253,9 +253,8 @@ class Python3Parser(PythonParser):
stmt ::= funcdef_annotate
funcdef_annotate ::= mkfunc_annotate designator
annotate_args ::= annotate_args annotate_arg
annotate_args ::= annotate_arg
annotate_arg ::= LOAD_CONST expr
annotate_arg ::= LOAD_CONST
annotate_arg ::= LOAD_NAME
"""
def p_come_from3(self, args):

View File

@@ -45,7 +45,7 @@ class Python31Parser(Python32Parser):
# ('pos_arg ' * (args_pos), 'kwargs ' * (annotate_args-1), opname))
rule = ('mkfunc_annotate ::= %s%sLOAD_CONST EXTENDED_ARG %s' %
(('pos_arg ' * (args_pos)),
('annotate_args ' * (annotate_args-1)), opname))
('annotate_arg ' * (annotate_args)), opname))
self.add_unique_rule(rule, opname, token.attr, customize)
class Python31ParserSingle(Python31Parser, PythonParserSingle):
pass

View File

@@ -26,7 +26,7 @@ class Python32Parser(Python3Parser):
# Check that there are 2 annotated params?
rule = ('mkfunc_annotate ::= %s%sLOAD_CONST LOAD_CONST EXTENDED_ARG %s' %
(('pos_arg ' * (args_pos)),
('annotate_args ' * (annotate_args-1)), opname))
('annotate_arg ' * (annotate_args)), opname))
self.add_unique_rule(rule, opname, token.attr, customize)

View File

@@ -1,14 +1,14 @@
# Copyright (c) 2016 Rocky Bernstein
"""
spark grammar differences over Python 3.2 for Python 3.5.
spark grammar differences over Python 3.4 for Python 3.5.
"""
from __future__ import print_function
from uncompyle6.parser import PythonParserSingle
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
from uncompyle6.parsers.parse32 import Python32Parser
from uncompyle6.parsers.parse34 import Python34Parser
class Python35Parser(Python32Parser):
class Python35Parser(Python34Parser):
def __init__(self, debug_parser=PARSER_DEFAULT_DEBUG):
super(Python35Parser, self).__init__(debug_parser)

View File

@@ -612,10 +612,6 @@ class SourceWalker(GenericASTTraversal, object):
})
if version >= 3.0:
if 3.1 <= version <= 3.4:
##########################
# Python 3.1 - 3.4
##########################
TABLE_DIRECT.update({
'funcdef_annotate': ( '\n\n%|def %c%c\n', -1, 0),
'store_locals': ( '%|# inspect.currentframe().f_locals = __locals__\n', ),
@@ -624,7 +620,7 @@ class SourceWalker(GenericASTTraversal, object):
def make_function3(node, isLambda, nested=1,
codeNode=None, annotate=None):
"""Dump function defintion, doc string, and function
body. This code is specialzed for Python 3.1"""
body. This code is specialzed for Python 3"""
def build_param(ast, name, default):
"""build parameters:
@@ -692,6 +688,12 @@ class SourceWalker(GenericASTTraversal, object):
else:
self.write("(")
last_line = self.f.getvalue().split("\n")[-1]
l = len(last_line)
indent = ' ' * l
line_number = self.line_number
if 4 & code.co_flags: # flag 2 -> variable number of args
self.write('*%s' % code.co_varnames[argc + kw_pairs])
argc += 1
@@ -705,6 +707,10 @@ class SourceWalker(GenericASTTraversal, object):
self.write(paramnames[i] + '=')
i += 1
self.preorder(n)
if (line_number != self.line_number):
suffix = ",\n" + indent
line_number = self.line_number
else:
suffix = ', '
# self.println(indent, '#flags:\t', int(code.co_flags))
@@ -742,7 +748,7 @@ class SourceWalker(GenericASTTraversal, object):
else:
self.write(')')
if annotate:
self.write(' -> %s' % annotate)
self.write(' -> "%s"' % annotate)
self.println(":")
if (len(code.co_consts) > 0 and
@@ -781,13 +787,20 @@ class SourceWalker(GenericASTTraversal, object):
code = node[-3]
self.indentMore()
annotate = None
annotate_args = node[-4] if self.version == 3.1 else node[-5];
annotate_return = None
annotate_last = -4 if self.version == 3.1 else -5
annotate_arg = node[annotate_last]
if annotate_args == 'annotate_args' and annotate_args[0][0] == 'LOAD_CONST':
annotate = annotate_args[0][0].attr
if (annotate_arg == 'annotate_arg'
and annotate_arg[0] == 'LOAD_CONST'
and isinstance(annotate_arg[0].attr, tuple)):
annotate_tup = annotate_arg[0].attr
if annotate_tup[-1] == 'return':
annotate_return = node[annotate_last-1][0].attr
pass
# FIXME: handle and pass full annotate args
self.make_function3(node, isLambda=False,
codeNode=code, annotate=annotate)
codeNode=code, annotate=annotate_return)
if len(self.param_stack) > 1:
self.write('\n\n')
@@ -2285,6 +2298,9 @@ class SourceWalker(GenericASTTraversal, object):
def make_function(self, node, isLambda, nested=1, codeNode=None):
"""Dump function defintion, doc string, and function body."""
# FIXME: call make_function3 if we are self.version >= 3.0
# and then simplify the below.
def build_param(ast, name, default):
"""build parameters:
- handle defaults