Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fa974a9

Browse files
committed
change node Classdef to Class
add doc string to transformer module add two helper functions: parse(buf) -> AST parseFile(path) -> AST
1 parent ed95861 commit fa974a9

4 files changed

Lines changed: 50 additions & 48 deletions

File tree

Lib/compiler/ast.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,18 @@ def __init__(self, argnames, defaults, flags, code):
114114
def __repr__(self):
115115
return "Lambda(%s,%s,%s,%s)" % self._children[1:]
116116

117-
class Classdef(Node):
118-
nodes['classdef'] = 'Classdef'
117+
class Class(Node):
118+
nodes['class'] = 'Class'
119119

120120
def __init__(self, name, bases, doc, code):
121121
self.name = name
122122
self.bases = bases
123123
self.doc = doc
124124
self.code = code
125-
self._children = ('classdef', name, bases, doc, code)
125+
self._children = ('class', name, bases, doc, code)
126126

127127
def __repr__(self):
128-
return "Classdef(%s,%s,%s,%s)" % self._children[1:]
128+
return "Class(%s,%s,%s,%s)" % self._children[1:]
129129

130130
class Pass(EmptyNode):
131131
nodes['pass'] = 'Pass'

Lib/compiler/transformer.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1+
"""Parse tree transformation module.
2+
3+
Transforms Python source code into an abstract syntax tree (AST)
4+
defined in the ast module.
5+
6+
The simplest ways to invoke this module are via parse and parseFile.
7+
parse(buf) -> AST
8+
parseFile(path) -> AST
9+
"""
10+
111
# Copyright 1997-1998 Greg Stein and Bill Tutt
212
#
3-
# transformer.py -- transforms Python parse trees
4-
#
5-
# Takes an input parse tree and transforms it into a higher-level parse
6-
# tree that is a bit more amenable to code generation. Essentially, it
7-
# simply introduces some additional semantics.
8-
#
913
# Written by Greg Stein ([email protected])
1014
# and Bill Tutt ([email protected])
1115
# February 1997.
1216
#
13-
# Support for Node subclasses written by
17+
# Support for Node subclasses written and other revisions by
1418
# Jeremy Hylton ([email protected])
1519
#
1620
# The output tree has the following nodes:
@@ -83,12 +87,6 @@
8387
# invert: node
8488
#
8589

86-
"""Parse tree transformation module.
87-
88-
Exposes the Transformer class with a number of methods for returning a
89-
"cleansed AST" instead of the parse tree that the parser exposes.
90-
"""
91-
9290
import ast
9391
import parser
9492
import symbol
@@ -102,6 +100,15 @@
102100
from consts import CO_VARARGS, CO_VARKEYWORDS
103101
from consts import OP_ASSIGN, OP_DELETE, OP_APPLY
104102

103+
def parseFile(path):
104+
f = open(path)
105+
src = f.read()
106+
f.close()
107+
return parse(src)
108+
109+
def parse(buf):
110+
return Transformer().parsesuite(buf)
111+
105112
def asList(nodes):
106113
l = []
107114
for item in nodes:
@@ -262,7 +269,7 @@ def classdef(self, nodelist):
262269
# code for class
263270
code = self.com_node(nodelist[-1])
264271

265-
n = Node('classdef', name, bases, doc, code)
272+
n = Node('class', name, bases, doc, code)
266273
n.lineno = nodelist[1][2]
267274
return n
268275

@@ -1191,10 +1198,4 @@ def get_docstring(self, node, n=None):
11911198
symbol.factor,
11921199
]
11931200

1194-
# Local Variables:
1195-
# mode: python
1196-
# indent-tabs-mode: nil
1197-
# py-indent-offset: 2
1198-
# py-smart-indentation: nil
1199-
# End:
12001201

Tools/compiler/compiler/ast.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,18 @@ def __init__(self, argnames, defaults, flags, code):
114114
def __repr__(self):
115115
return "Lambda(%s,%s,%s,%s)" % self._children[1:]
116116

117-
class Classdef(Node):
118-
nodes['classdef'] = 'Classdef'
117+
class Class(Node):
118+
nodes['class'] = 'Class'
119119

120120
def __init__(self, name, bases, doc, code):
121121
self.name = name
122122
self.bases = bases
123123
self.doc = doc
124124
self.code = code
125-
self._children = ('classdef', name, bases, doc, code)
125+
self._children = ('class', name, bases, doc, code)
126126

127127
def __repr__(self):
128-
return "Classdef(%s,%s,%s,%s)" % self._children[1:]
128+
return "Class(%s,%s,%s,%s)" % self._children[1:]
129129

130130
class Pass(EmptyNode):
131131
nodes['pass'] = 'Pass'

Tools/compiler/compiler/transformer.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1+
"""Parse tree transformation module.
2+
3+
Transforms Python source code into an abstract syntax tree (AST)
4+
defined in the ast module.
5+
6+
The simplest ways to invoke this module are via parse and parseFile.
7+
parse(buf) -> AST
8+
parseFile(path) -> AST
9+
"""
10+
111
# Copyright 1997-1998 Greg Stein and Bill Tutt
212
#
3-
# transformer.py -- transforms Python parse trees
4-
#
5-
# Takes an input parse tree and transforms it into a higher-level parse
6-
# tree that is a bit more amenable to code generation. Essentially, it
7-
# simply introduces some additional semantics.
8-
#
913
# Written by Greg Stein ([email protected])
1014
# and Bill Tutt ([email protected])
1115
# February 1997.
1216
#
13-
# Support for Node subclasses written by
17+
# Support for Node subclasses written and other revisions by
1418
# Jeremy Hylton ([email protected])
1519
#
1620
# The output tree has the following nodes:
@@ -83,12 +87,6 @@
8387
# invert: node
8488
#
8589

86-
"""Parse tree transformation module.
87-
88-
Exposes the Transformer class with a number of methods for returning a
89-
"cleansed AST" instead of the parse tree that the parser exposes.
90-
"""
91-
9290
import ast
9391
import parser
9492
import symbol
@@ -102,6 +100,15 @@
102100
from consts import CO_VARARGS, CO_VARKEYWORDS
103101
from consts import OP_ASSIGN, OP_DELETE, OP_APPLY
104102

103+
def parseFile(path):
104+
f = open(path)
105+
src = f.read()
106+
f.close()
107+
return parse(src)
108+
109+
def parse(buf):
110+
return Transformer().parsesuite(buf)
111+
105112
def asList(nodes):
106113
l = []
107114
for item in nodes:
@@ -262,7 +269,7 @@ def classdef(self, nodelist):
262269
# code for class
263270
code = self.com_node(nodelist[-1])
264271

265-
n = Node('classdef', name, bases, doc, code)
272+
n = Node('class', name, bases, doc, code)
266273
n.lineno = nodelist[1][2]
267274
return n
268275

@@ -1191,10 +1198,4 @@ def get_docstring(self, node, n=None):
11911198
symbol.factor,
11921199
]
11931200

1194-
# Local Variables:
1195-
# mode: python
1196-
# indent-tabs-mode: nil
1197-
# py-indent-offset: 2
1198-
# py-smart-indentation: nil
1199-
# End:
12001201

0 commit comments

Comments
 (0)