Merge pull request #31 from rocky/ast-format

Ast format
This commit is contained in:
R. Bernstein
2016-06-19 03:05:40 -04:00
committed by GitHub
5 changed files with 54 additions and 40 deletions

1
.gitignore vendored
View File

@@ -1,6 +1,7 @@
*.pyc
*_dis
*~
*.pyc
/.cache
/.eggs
/.python-version

View File

@@ -1,12 +1,12 @@
# Python 2.7
# Embedded file name: simple_source/branching/05_if.py
6 0 LOAD_NAME 0 'True'
3 POP_JUMP_IF_FALSE 15 '15'
6 0 LOAD_NAME 0 'True'
3 POP_JUMP_IF_FALSE 15 '15'
7 6 LOAD_NAME 1 'False'
9 STORE_NAME 2 'b'
12 JUMP_FORWARD 0 '15'
15_0 COME_FROM '12'
15 LOAD_CONST 0 ''
18 RETURN_VALUE ''
7 6 LOAD_NAME 1 'False'
9 STORE_NAME 2 'b'
12 JUMP_FORWARD 0 '15'
15_0 COME_FROM '12'
15 LOAD_CONST 0 ''
18 RETURN_VALUE ''

View File

@@ -1,15 +1,15 @@
# Python 2.7
# Embedded file name: simple_source/branching/05_ifelse.py
3 0 LOAD_NAME 0 'True'
3 POP_JUMP_IF_FALSE 15 '15'
3 0 LOAD_NAME 0 'True'
3 POP_JUMP_IF_FALSE 15 '15'
4 6 LOAD_CONST 0 1
9 STORE_NAME 1 'b'
12 JUMP_FORWARD 6 '21'
4 6 LOAD_CONST 0 1
9 STORE_NAME 1 'b'
12 JUMP_FORWARD 6 '21'
6 15 LOAD_CONST 1 2
18 STORE_NAME 2 'd'
21_0 COME_FROM '12'
21 LOAD_CONST 2 ''
24 RETURN_VALUE ''
6 15 LOAD_CONST 1 2
18 STORE_NAME 2 'd'
21_0 COME_FROM '12'
21 LOAD_CONST 2 ''
24 RETURN_VALUE ''

View File

@@ -1,6 +1,7 @@
import sys
from uncompyle6 import PYTHON3
from uncompyle6.scanners.tok import NoneToken
from spark_parser.ast import AST as spark_AST
if PYTHON3:
intern = sys.intern
@@ -9,30 +10,42 @@ else:
from UserList import UserList
class AST(UserList):
def __init__(self, kind, kids=[]):
self.type = intern(kind)
UserList.__init__(self, kids)
class AST(spark_AST):
def isNone(self):
"""An AST None token. We can't use regular list comparisons
because AST token offsets might be different"""
return len(self.data) == 1 and NoneToken == self.data[0]
def __getslice__(self, low, high): return self.data[low:high]
def __repr__(self):
return self.__repr1__('', None)
def __eq__(self, o):
if isinstance(o, AST):
return self.type == o.type \
and UserList.__eq__(self, o)
else:
return self.type == o
def __hash__(self):
return hash(self.type)
def __repr__(self, indent=''):
def __repr1__(self, indent, sibNum=None):
rv = str(self.type)
for k in self:
rv = rv + '\n' + str(k).replace('\n', '\n ')
if sibNum is not None:
rv = "%2d. %s" % (sibNum, rv)
enumerate_children = False
if len(self) > 1:
rv += " (%d)" % (len(self))
enumerate_children = True
rv = indent + rv
indent += ' '
i = 0
for node in self:
if hasattr(node, '__repr1__'):
if enumerate_children:
child = node.__repr1__(indent, i)
else:
child = node.__repr1__(indent, None)
else:
inst = str(node)
if inst.startswith("\n"):
# Nuke leading \n
inst = inst[1:]
if enumerate_children:
child = indent + "%2d. %s" % (i, inst)
else:
child = indent + inst
pass
rv += "\n" + child
i += 1
return rv

View File

@@ -43,11 +43,11 @@ class Token:
pattr = self.pattr if self.pattr is not None else ''
prefix = '\n%4d ' % self.linestart if self.linestart else (' ' * 6)
return (prefix +
('%6s\t%-17s %r' % (self.offset, self.type, pattr)))
('%6s %-17s %r' % (self.offset, self.type, pattr)))
def format(self):
prefix = '\n%4d ' % self.linestart if self.linestart else (' ' * 5)
offset_opname = '%6s\t%-17s' % (self.offset, self.type)
prefix = '\n%4d ' % self.linestart if self.linestart else (' ' * 6)
offset_opname = '%6s %-17s' % (self.offset, self.type)
argstr = "%6d " % self.attr if isinstance(self.attr, int) else (' '*7)
pattr = self.pattr if self.pattr is not None else ''
return "%s%s%s %r" % (prefix, offset_opname, argstr, pattr)