Start Python3 class(superclass) handling

This commit is contained in:
rocky
2015-12-23 15:42:52 -05:00
parent f630fe15fb
commit d47415a677
9 changed files with 46 additions and 11 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,4 @@
# Tests:
import sys
from os import path

View File

@@ -0,0 +1,3 @@
# Tests:
# assign ::= expr designator
pass

View File

@@ -370,9 +370,12 @@ class Python3Parser(PythonParser):
kwarg ::= LOAD_CONST expr kwarg ::= LOAD_CONST expr
classdef ::= buildclass designator
# Python3 introduced LOAD_BUILD_CLASS # Python3 introduced LOAD_BUILD_CLASS
classdef ::= LOAD_BUILD_CLASS mkfunc LOAD_CONST # FIXME: the below should be created by custom rules
CALL_FUNCTION_2 designator buildclass ::= LOAD_BUILD_CLASS mkfunc LOAD_CONST LOAD_NAME CALL_FUNCTION_3
buildclass ::= LOAD_BUILD_CLASS mkfunc LOAD_CONST CALL_FUNCTION_2
stmt ::= classdefdeco stmt ::= classdefdeco
classdefdeco ::= classdefdeco1 designator classdefdeco ::= classdefdeco1 designator

View File

@@ -1053,26 +1053,29 @@ class Walker(GenericASTTraversal, object):
# class definition ('class X(A,B,C):') # class definition ('class X(A,B,C):')
cclass = self.currentclass cclass = self.currentclass
if self.version > 3.0: if self.version > 3.0:
buildclass = node[1] buildclass = node[1]
self.currentclass = str(buildclass[1].pattr) build_list = node[0]
subclass = build_list[1][0].attr
else: else:
buildclass = node[0] buildclass = node[0]
self.currentclass = str(buildclass[0].pattr) build_list = buildclass[1][0]
subclass = buildclass[-3][0].attr
self.write('\n\n') self.write('\n\n')
self.currentclass = str(buildclass[0].pattr)
self.write(self.indent, 'class ', self.currentclass) self.write(self.indent, 'class ', self.currentclass)
self.print_super_classes(buildclass) if self.version > 3.0:
self.print_super_classes3(build_list)
else:
self.print_super_classes(build_list)
self.print_(':') self.print_(':')
# class body # class body
self.indentMore() self.indentMore()
self.build_class(subclass)
if self.version > 3.0:
self.build_class(buildclass[0].attr)
else:
self.build_class(buildclass[-3][0].attr)
self.indentLess() self.indentLess()
self.currentclass = cclass self.currentclass = cclass
@@ -1086,7 +1089,6 @@ class Walker(GenericASTTraversal, object):
n_classdefdeco2 = n_classdef n_classdefdeco2 = n_classdef
def print_super_classes(self, node): def print_super_classes(self, node):
node = node[1][0]
if not (node == 'build_list'): if not (node == 'build_list'):
return return
@@ -1100,6 +1102,29 @@ class Walker(GenericASTTraversal, object):
self.write(')') self.write(')')
def print_super_classes3(self, node):
# FIXME: put blow logic into grammar
# as a custom rule
i = 0
for i, n in enumerate(node[:-1]):
if n.type == 'LOAD_NAME':
break
pass
if i == 0:
return
self.write('(')
line_separator = ', '
sep = ''
while i <= len(node) - 2:
value = self.traverse(node[i])
i += 1
self.write(sep, value)
sep = line_separator
self.write(')')
def n_mapexpr(self, node): def n_mapexpr(self, node):
""" """
prettyprint a mapexpr prettyprint a mapexpr