mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
change node Classdef to Class
add doc string to transformer module add two helper functions: parse(buf) -> AST parseFile(path) -> AST
This commit is contained in:
parent
ed9586174d
commit
fa974a9d06
4 changed files with 50 additions and 48 deletions
|
@ -114,18 +114,18 @@ class Lambda(Node):
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "Lambda(%s,%s,%s,%s)" % self._children[1:]
|
return "Lambda(%s,%s,%s,%s)" % self._children[1:]
|
||||||
|
|
||||||
class Classdef(Node):
|
class Class(Node):
|
||||||
nodes['classdef'] = 'Classdef'
|
nodes['class'] = 'Class'
|
||||||
|
|
||||||
def __init__(self, name, bases, doc, code):
|
def __init__(self, name, bases, doc, code):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.bases = bases
|
self.bases = bases
|
||||||
self.doc = doc
|
self.doc = doc
|
||||||
self.code = code
|
self.code = code
|
||||||
self._children = ('classdef', name, bases, doc, code)
|
self._children = ('class', name, bases, doc, code)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "Classdef(%s,%s,%s,%s)" % self._children[1:]
|
return "Class(%s,%s,%s,%s)" % self._children[1:]
|
||||||
|
|
||||||
class Pass(EmptyNode):
|
class Pass(EmptyNode):
|
||||||
nodes['pass'] = 'Pass'
|
nodes['pass'] = 'Pass'
|
||||||
|
|
|
@ -1,16 +1,20 @@
|
||||||
|
"""Parse tree transformation module.
|
||||||
|
|
||||||
|
Transforms Python source code into an abstract syntax tree (AST)
|
||||||
|
defined in the ast module.
|
||||||
|
|
||||||
|
The simplest ways to invoke this module are via parse and parseFile.
|
||||||
|
parse(buf) -> AST
|
||||||
|
parseFile(path) -> AST
|
||||||
|
"""
|
||||||
|
|
||||||
# Copyright 1997-1998 Greg Stein and Bill Tutt
|
# Copyright 1997-1998 Greg Stein and Bill Tutt
|
||||||
#
|
#
|
||||||
# transformer.py -- transforms Python parse trees
|
|
||||||
#
|
|
||||||
# Takes an input parse tree and transforms it into a higher-level parse
|
|
||||||
# tree that is a bit more amenable to code generation. Essentially, it
|
|
||||||
# simply introduces some additional semantics.
|
|
||||||
#
|
|
||||||
# Written by Greg Stein (gstein@lyra.org)
|
# Written by Greg Stein (gstein@lyra.org)
|
||||||
# and Bill Tutt (rassilon@lima.mudlib.org)
|
# and Bill Tutt (rassilon@lima.mudlib.org)
|
||||||
# February 1997.
|
# February 1997.
|
||||||
#
|
#
|
||||||
# Support for Node subclasses written by
|
# Support for Node subclasses written and other revisions by
|
||||||
# Jeremy Hylton (jeremy@cnri.reston.va.us)
|
# Jeremy Hylton (jeremy@cnri.reston.va.us)
|
||||||
#
|
#
|
||||||
# The output tree has the following nodes:
|
# The output tree has the following nodes:
|
||||||
|
@ -83,12 +87,6 @@
|
||||||
# invert: node
|
# invert: node
|
||||||
#
|
#
|
||||||
|
|
||||||
"""Parse tree transformation module.
|
|
||||||
|
|
||||||
Exposes the Transformer class with a number of methods for returning a
|
|
||||||
"cleansed AST" instead of the parse tree that the parser exposes.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import ast
|
import ast
|
||||||
import parser
|
import parser
|
||||||
import symbol
|
import symbol
|
||||||
|
@ -102,6 +100,15 @@ error = 'walker.error'
|
||||||
from consts import CO_VARARGS, CO_VARKEYWORDS
|
from consts import CO_VARARGS, CO_VARKEYWORDS
|
||||||
from consts import OP_ASSIGN, OP_DELETE, OP_APPLY
|
from consts import OP_ASSIGN, OP_DELETE, OP_APPLY
|
||||||
|
|
||||||
|
def parseFile(path):
|
||||||
|
f = open(path)
|
||||||
|
src = f.read()
|
||||||
|
f.close()
|
||||||
|
return parse(src)
|
||||||
|
|
||||||
|
def parse(buf):
|
||||||
|
return Transformer().parsesuite(buf)
|
||||||
|
|
||||||
def asList(nodes):
|
def asList(nodes):
|
||||||
l = []
|
l = []
|
||||||
for item in nodes:
|
for item in nodes:
|
||||||
|
@ -262,7 +269,7 @@ class Transformer:
|
||||||
# code for class
|
# code for class
|
||||||
code = self.com_node(nodelist[-1])
|
code = self.com_node(nodelist[-1])
|
||||||
|
|
||||||
n = Node('classdef', name, bases, doc, code)
|
n = Node('class', name, bases, doc, code)
|
||||||
n.lineno = nodelist[1][2]
|
n.lineno = nodelist[1][2]
|
||||||
return n
|
return n
|
||||||
|
|
||||||
|
@ -1191,10 +1198,4 @@ _assign_types = [
|
||||||
symbol.factor,
|
symbol.factor,
|
||||||
]
|
]
|
||||||
|
|
||||||
# Local Variables:
|
|
||||||
# mode: python
|
|
||||||
# indent-tabs-mode: nil
|
|
||||||
# py-indent-offset: 2
|
|
||||||
# py-smart-indentation: nil
|
|
||||||
# End:
|
|
||||||
|
|
||||||
|
|
|
@ -114,18 +114,18 @@ class Lambda(Node):
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "Lambda(%s,%s,%s,%s)" % self._children[1:]
|
return "Lambda(%s,%s,%s,%s)" % self._children[1:]
|
||||||
|
|
||||||
class Classdef(Node):
|
class Class(Node):
|
||||||
nodes['classdef'] = 'Classdef'
|
nodes['class'] = 'Class'
|
||||||
|
|
||||||
def __init__(self, name, bases, doc, code):
|
def __init__(self, name, bases, doc, code):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.bases = bases
|
self.bases = bases
|
||||||
self.doc = doc
|
self.doc = doc
|
||||||
self.code = code
|
self.code = code
|
||||||
self._children = ('classdef', name, bases, doc, code)
|
self._children = ('class', name, bases, doc, code)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "Classdef(%s,%s,%s,%s)" % self._children[1:]
|
return "Class(%s,%s,%s,%s)" % self._children[1:]
|
||||||
|
|
||||||
class Pass(EmptyNode):
|
class Pass(EmptyNode):
|
||||||
nodes['pass'] = 'Pass'
|
nodes['pass'] = 'Pass'
|
||||||
|
|
|
@ -1,16 +1,20 @@
|
||||||
|
"""Parse tree transformation module.
|
||||||
|
|
||||||
|
Transforms Python source code into an abstract syntax tree (AST)
|
||||||
|
defined in the ast module.
|
||||||
|
|
||||||
|
The simplest ways to invoke this module are via parse and parseFile.
|
||||||
|
parse(buf) -> AST
|
||||||
|
parseFile(path) -> AST
|
||||||
|
"""
|
||||||
|
|
||||||
# Copyright 1997-1998 Greg Stein and Bill Tutt
|
# Copyright 1997-1998 Greg Stein and Bill Tutt
|
||||||
#
|
#
|
||||||
# transformer.py -- transforms Python parse trees
|
|
||||||
#
|
|
||||||
# Takes an input parse tree and transforms it into a higher-level parse
|
|
||||||
# tree that is a bit more amenable to code generation. Essentially, it
|
|
||||||
# simply introduces some additional semantics.
|
|
||||||
#
|
|
||||||
# Written by Greg Stein (gstein@lyra.org)
|
# Written by Greg Stein (gstein@lyra.org)
|
||||||
# and Bill Tutt (rassilon@lima.mudlib.org)
|
# and Bill Tutt (rassilon@lima.mudlib.org)
|
||||||
# February 1997.
|
# February 1997.
|
||||||
#
|
#
|
||||||
# Support for Node subclasses written by
|
# Support for Node subclasses written and other revisions by
|
||||||
# Jeremy Hylton (jeremy@cnri.reston.va.us)
|
# Jeremy Hylton (jeremy@cnri.reston.va.us)
|
||||||
#
|
#
|
||||||
# The output tree has the following nodes:
|
# The output tree has the following nodes:
|
||||||
|
@ -83,12 +87,6 @@
|
||||||
# invert: node
|
# invert: node
|
||||||
#
|
#
|
||||||
|
|
||||||
"""Parse tree transformation module.
|
|
||||||
|
|
||||||
Exposes the Transformer class with a number of methods for returning a
|
|
||||||
"cleansed AST" instead of the parse tree that the parser exposes.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import ast
|
import ast
|
||||||
import parser
|
import parser
|
||||||
import symbol
|
import symbol
|
||||||
|
@ -102,6 +100,15 @@ error = 'walker.error'
|
||||||
from consts import CO_VARARGS, CO_VARKEYWORDS
|
from consts import CO_VARARGS, CO_VARKEYWORDS
|
||||||
from consts import OP_ASSIGN, OP_DELETE, OP_APPLY
|
from consts import OP_ASSIGN, OP_DELETE, OP_APPLY
|
||||||
|
|
||||||
|
def parseFile(path):
|
||||||
|
f = open(path)
|
||||||
|
src = f.read()
|
||||||
|
f.close()
|
||||||
|
return parse(src)
|
||||||
|
|
||||||
|
def parse(buf):
|
||||||
|
return Transformer().parsesuite(buf)
|
||||||
|
|
||||||
def asList(nodes):
|
def asList(nodes):
|
||||||
l = []
|
l = []
|
||||||
for item in nodes:
|
for item in nodes:
|
||||||
|
@ -262,7 +269,7 @@ class Transformer:
|
||||||
# code for class
|
# code for class
|
||||||
code = self.com_node(nodelist[-1])
|
code = self.com_node(nodelist[-1])
|
||||||
|
|
||||||
n = Node('classdef', name, bases, doc, code)
|
n = Node('class', name, bases, doc, code)
|
||||||
n.lineno = nodelist[1][2]
|
n.lineno = nodelist[1][2]
|
||||||
return n
|
return n
|
||||||
|
|
||||||
|
@ -1191,10 +1198,4 @@ _assign_types = [
|
||||||
symbol.factor,
|
symbol.factor,
|
||||||
]
|
]
|
||||||
|
|
||||||
# Local Variables:
|
|
||||||
# mode: python
|
|
||||||
# indent-tabs-mode: nil
|
|
||||||
# py-indent-offset: 2
|
|
||||||
# py-smart-indentation: nil
|
|
||||||
# End:
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue