Skip to content

Commit

Permalink
more fixup of lib2to3 compiler
Browse files Browse the repository at this point in the history
git-svn-id: https://pyjamas.svn.sourceforge.net/svnroot/pyjamas/trunk@1892 7a2bd370-bda8-463c-979e-2900ccfb811e
  • Loading branch information
lkcl committed Sep 23, 2009
1 parent 8c8a237 commit 6d21f0a
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 44 deletions.
13 changes: 12 additions & 1 deletion pgen/astgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def flatten_nodes(seq):

nodes = {}

class Node:
class Node(object):
"""Abstract base class for ast nodes."""
def getChildren(self):
pass # implemented by subclasses
Expand All @@ -263,6 +263,16 @@ def asList(self): # for backwards compatibility
return self.getChildren()
def getChildNodes(self):
pass # implemented by subclasses
def _get_lineno(self):
return self._lineno
def _set_lineno(self, lineno):
if lineno is not None and not isinstance(lineno, int):
self._context = lineno
self._lineno = lineno[1][0]
else:
self._lineno = lineno
self._context = None
lineno = property(_get_lineno, _set_lineno)

class EmptyNode(Node):
pass
Expand All @@ -271,6 +281,7 @@ class Expression(Node):
# Expression is an artificial node class to support "eval"
nodes["expression"] = "Expression"
def __init__(self, node):
Node.__init__(self)
self.node = node

def getChildren(self):
Expand Down
20 changes: 10 additions & 10 deletions pgen/astpprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,41 @@
__author__ = 'Martin Blais <blais@furius.ca>'

import sys
from compiler.ast import Node

__all__ = ('printAst','getAststr')

from StringIO import StringIO

def getAststr(ast, indent=' ', initlevel=0):
def getAststr(astmod, ast, indent=' ', initlevel=0):
"Pretty-print an AST to the given output stream."
stream = StringIO()
rec_node(ast, initlevel, indent, stream.write)
rec_node(astmod, ast, initlevel, indent, stream.write)
stream.write('\n')
stream.seek(0)
return stream.read()

def printAst(ast, indent=' ', stream=sys.stdout, initlevel=0):
def printAst(astmod, ast, indent=' ', stream=sys.stdout, initlevel=0):
"Pretty-print an AST to the given output stream."
rec_node(ast, initlevel, indent, stream.write)
rec_node(astmod, ast, initlevel, indent, stream.write)
stream.write('\n')
stream.flush()

def rec_node(node, level, indent, write):
def rec_node(astmod, node, level, indent, write):
"Recurse through a node, pretty-printing it."
pfx = indent * level
if isinstance(node, Node):
if isinstance(node, astmod.Node):
write(pfx)
write(node.__class__.__name__)
write('(')

i = 0
for child in node.getChildren():
if not isinstance(child, Node):
if not isinstance(child, astmod.Node):
continue
if i != 0:
write(',')
write('\n')
rec_node(child, level+1, indent, write)
rec_node(astmod, child, level+1, indent, write)
i += 1
if i == 0:
# None of the children as nodes, simply join their repr on a single
Expand All @@ -68,6 +67,7 @@ def rec_node(node, level, indent, write):


def main():
from compiler import ast
import optparse
parser = optparse.OptionParser(__doc__.strip())
opts, args = parser.parse_args()
Expand All @@ -79,7 +79,7 @@ def main():
for fn in args:
print '\n\n%s:\n' % fn
try:
printAst(compiler.parseFile(fn), initlevel=1)
printAst(ast, compiler.parseFile(fn), initlevel=1)
except SyntaxError, e:
traceback.print_exc()

Expand Down
13 changes: 12 additions & 1 deletion pgen/lib2to3/compiler/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def flatten_nodes(seq):

nodes = {}

class Node:
class Node(object):
"""Abstract base class for ast nodes."""
def getChildren(self):
pass # implemented by subclasses
Expand All @@ -31,6 +31,16 @@ def asList(self): # for backwards compatibility
return self.getChildren()
def getChildNodes(self):
pass # implemented by subclasses
def _get_lineno(self):
return self._lineno
def _set_lineno(self, lineno):
if lineno is not None and not isinstance(lineno, int):
self._context = lineno
self._lineno = lineno[1][0]
else:
self._lineno = lineno
self._context = None
lineno = property(_get_lineno, _set_lineno)

class EmptyNode(Node):
pass
Expand All @@ -39,6 +49,7 @@ class Expression(Node):
# Expression is an artificial node class to support "eval"
nodes["expression"] = "Expression"
def __init__(self, node):
Node.__init__(self)
self.node = node

def getChildren(self):
Expand Down
2 changes: 1 addition & 1 deletion pgen/lib2to3/compiler/future.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""

from compiler import ast, walk
from lib2to3.compiler import ast, walk

def is_future(stmt):
"""Return true if statement is a well-formed future statement"""
Expand Down
6 changes: 3 additions & 3 deletions pgen/lib2to3/compiler/symbols.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Module symbol-table generator"""

from compiler import ast
from compiler.consts import SC_LOCAL, SC_GLOBAL, SC_FREE, SC_CELL, SC_UNKNOWN
from compiler.misc import mangle
from lib2to3.compiler import ast
from lib2to3.compiler.consts import SC_LOCAL, SC_GLOBAL, SC_FREE, SC_CELL, SC_UNKNOWN
from lib2to3.compiler.misc import mangle
import types


Expand Down
61 changes: 35 additions & 26 deletions pgen/lib2to3/compiler/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
# http://www.opensource.org/licenses/bsd-license.html
# and replace OWNER, ORGANIZATION, and YEAR as appropriate.

from compiler.ast import *
import parser
from lib2to3.compiler.ast import *
from lib2to3.compiler import parser
from lib2to3 import symbol
import token
from lib2to3.compiler import token

class WalkerError(StandardError):
pass
Expand Down Expand Up @@ -265,10 +265,12 @@ def funcdef(self, nodelist):
args = nodelist[-3].children[1]

if args.type == symbol.varargslist:
names, defaults, flags = self.com_arglist(args.children)
names, defaults, varargs, varkeywords = \
self.com_arglist(args.children)
else:
names = defaults = ()
flags = 0
varargs = False
varkeywords = False
doc = self.get_docstring(nodelist[-1])

# code for function
Expand All @@ -278,22 +280,26 @@ def funcdef(self, nodelist):
assert isinstance(code, Stmt)
assert isinstance(code.nodes[0], Discard)
del code.nodes[0]
return Function(decorators, name, names, defaults, flags, doc, code,
return Function(decorators, name, names, defaults,
varargs, varkeywords, doc, code,
lineno=lineno)

def lambdef(self, nodelist):
# lambdef: 'lambda' [varargslist] ':' test
#pprint(nodelist)
if nodelist[1].type == symbol.varargslist:
names, defaults, flags = self.com_arglist(nodelist[1].children)
names, defaults, varargs, varkeywords = \
self.com_arglist(nodelist[1].children)
else:
names = defaults = ()
flags = 0
varargs = False
varkeywords = False

# code for lambda
code = self.com_node(nodelist[-1])

return Lambda(names, defaults, flags, code, lineno=nodelist[0].context)
return Lambda(names, defaults, varargs, varkeywords, code,
lineno=nodelist[0].context)
old_lambdef = lambdef

def classdef(self, nodelist):
Expand All @@ -316,7 +322,7 @@ def classdef(self, nodelist):
assert isinstance(code.nodes[0], Discard)
del code.nodes[0]

return Class(name, bases, doc, code, lineno=nodelist[1].context)
return Class(name, bases, doc, code, None, lineno=nodelist[1].context)

def stmt(self, nodelist):
return self.com_stmt(nodelist[0])
Expand Down Expand Up @@ -399,13 +405,14 @@ def print_stmt(self, nodelist):
items.append(self.com_node(nodelist[i]))
if nodelist[-1].type == token.COMMA:
return Print(items, dest, lineno=nodelist[0].context)
return Printnl(items, dest, lineno=nodelist[0].context)
return Printnl(items, dest, True, lineno=nodelist[0].context)

def del_stmt(self, nodelist):
return self.com_assign(nodelist[1], OP_DELETE)

def pass_stmt(self, nodelist):
return Pass(lineno=nodelist[0].context)
lineno=nodelist[0].context
return Pass(lineno=lineno)

def break_stmt(self, nodelist):
return Break(lineno=nodelist[0].context)
Expand Down Expand Up @@ -675,9 +682,9 @@ def shift_expr(self, nodelist):
for i in range(2, len(nodelist), 2):
right = self.com_node(nodelist[i])
if nodelist[i-1].type == token.LEFTSHIFT:
node = LeftShift([node, right], lineno=nodelist[1].context)
node = LeftShift(node, right, lineno=nodelist[1].context)
elif nodelist[i-1].type == token.RIGHTSHIFT:
node = RightShift([node, right], lineno=nodelist[1].context)
node = RightShift(node, right, lineno=nodelist[1].context)
else:
raise ValueError, "unexpected token: %s" % nodelist[i-1].type
return node
Expand All @@ -687,9 +694,9 @@ def arith_expr(self, nodelist):
for i in range(2, len(nodelist), 2):
right = self.com_node(nodelist[i])
if nodelist[i-1].type == token.PLUS:
node = Add([node, right], lineno=nodelist[1].context)
node = Add(node, right, lineno=nodelist[1].context)
elif nodelist[i-1].type == token.MINUS:
node = Sub([node, right], lineno=nodelist[1].context)
node = Sub(node, right, lineno=nodelist[1].context)
else:
raise ValueError, "unexpected token: %s" % nodelist[i-1][0]
return node
Expand All @@ -700,13 +707,13 @@ def term(self, nodelist):
right = self.com_node(nodelist[i])
t = nodelist[i-1].type
if t == token.STAR:
node = Mul([node, right])
node = Mul(node, right)
elif t == token.SLASH:
node = Div([node, right])
node = Div(node, right)
elif t == token.PERCENT:
node = Mod([node, right])
node = Mod(node, right)
elif t == token.DOUBLESLASH:
node = FloorDiv([node, right])
node = FloorDiv(node, right)
else:
raise ValueError, "unexpected token: %s" % t
node.lineno = nodelist[1].context
Expand All @@ -732,7 +739,7 @@ def power(self, nodelist):
for i in range(1, len(nodelist)):
elt = nodelist[i]
if elt.type == token.DOUBLESTAR:
return Power([node, self.com_node(nodelist[i+1])],
return Power(node, self.com_node(nodelist[i+1]),
lineno=elt.context)

node = self.com_apply_trailer(node, elt)
Expand Down Expand Up @@ -822,7 +829,8 @@ def com_arglist(self, nodelist):
# fplist: fpdef (',' fpdef)* [',']
names = []
defaults = []
flags = 0
varargs = False
varkeywords = False

i = 0
while i < len(nodelist):
Expand All @@ -832,7 +840,7 @@ def com_arglist(self, nodelist):
node = nodelist[i+1]
if node.type == token.NAME:
names.append(node.value)
flags = flags | CO_VARARGS
varargs = True
i = i + 3

if i < len(nodelist):
Expand All @@ -843,7 +851,7 @@ def com_arglist(self, nodelist):
else:
raise ValueError, "unexpected token: %s" % t
names.append(node.value)
flags = flags | CO_VARKEYWORDS
varkeywords = True

break

Expand All @@ -862,7 +870,7 @@ def com_arglist(self, nodelist):
# skip the comma
i = i + 1

return names, defaults, flags
return names, defaults, varargs, varkeywords

def com_fpdef(self, node):
# fpdef: NAME | '(' fplist ')'
Expand Down Expand Up @@ -1219,7 +1227,8 @@ def com_select_member(self, primaryNode, nodelist):

def com_call_function(self, primaryNode, nodelist):
if nodelist.type == token.RPAR:
return CallFunc(primaryNode, [], lineno=extractLineNo(nodelist))
return CallFunc(primaryNode, [], None, None,
lineno=extractLineNo(nodelist))
args = []
kw = 0
star_node = dstar_node = None
Expand Down
4 changes: 2 additions & 2 deletions pgen/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ def compare_compilers(fname):
if test_std:
try:
x1 = compiler.parseFile(fname)
ys1 = getAststr(x1)
ys1 = getAststr(compiler.ast, x1)
except SyntaxError:
ys1 = traceback.format_exc(limit=0)

if test_pyjs:
try:
y = test_compiler.parseFile(fname)
ys = getAststr(y)
ys = getAststr(test_compiler.ast, y)

except SyntaxError:
ys = traceback.format_exc(limit=1)
Expand Down

0 comments on commit 6d21f0a

Please sign in to comment.