Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
cython
Commits
17aca901
Commit
17aca901
authored
Jun 06, 2008
by
Robert Bradshaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Final cleanup of child accessor stuff
parent
d30d8fb7
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
84 deletions
+12
-84
Cython/Compiler/Nodes.py
Cython/Compiler/Nodes.py
+12
-84
No files found.
Cython/Compiler/Nodes.py
View file @
17aca901
...
...
@@ -66,32 +66,6 @@ def embed_position(pos, docstring):
doc
.
encoding
=
encoding
return
doc
class
_AttributeAccessor
(
object
):
"""Used as the result of the Node.get_children_accessors() generator"""
def
__init__
(
self
,
obj
,
attrname
):
self
.
obj
=
obj
self
.
attrname
=
attrname
def
get
(
self
):
try
:
return
getattr
(
self
.
obj
,
self
.
attrname
)
except
AttributeError
:
return
None
def
set
(
self
,
value
):
setattr
(
self
.
obj
,
self
.
attrname
,
value
)
def
name
(
self
):
return
self
.
attrname
class
_AttributeIterator
(
object
):
"""Used as the result of the Node.get_children_accessors() generator"""
def
__init__
(
self
,
obj
,
attrnames
):
self
.
obj
=
obj
self
.
attrnames
=
iter
(
attrnames
)
def
__iter__
(
self
):
return
self
def
__next__
(
self
):
return
_AttributeAccessor
(
self
.
obj
,
self
.
attrnames
.
next
())
next
=
__next__
class
Node
(
object
):
# pos (string, int, int) Source file position
# is_name boolean Is a NameNode
...
...
@@ -109,49 +83,6 @@ class Node(object):
self
.
pos
=
pos
self
.
__dict__
.
update
(
kw
)
def
get_child_accessors
(
self
):
"""Returns an iterator over the children of the Node. Each member in the
iterated list is an object with get(), set(value), and name() methods,
which can be used to fetch and replace the child and query the name
the relation this node has with the child. For instance, for an
assignment node, this code:
for child in assignment_node.get_child_accessors():
print(child.name())
child.set(i_node)
will print "lhs", "rhs", and change the assignment statement to "i = i"
(assuming that i_node is a node able to represent the variable i in the
tree).
Any kind of objects can in principle be returned, but the typical
candidates are either Node instances or lists of node instances.
The object returned in each iteration stage can only be used until the
iterator is advanced to the next child attribute. (However, the objects
returned by the get() function can be kept).
Typically, a Node instance will have other interesting and potentially
hierarchical attributes as well. These must be explicitly accessed -- this
method only provides access to attributes that are deemed to naturally
belong in the parse tree.
Descandant classes can either specify child_attrs, override get_child_attrs,
or override this method directly in order to provide access to their
children. All descendants of Node *must* declare their children -- leaf nodes
should simply declare "child_attrs = []".
"""
attrnames
=
self
.
get_child_attrs
()
if
attrnames
is
None
:
raise
InternalError
(
"Children access not implemented for %s"
%
\
self
.
__class__
.
__name__
)
return
_AttributeIterator
(
self
,
attrnames
)
def
get_child_attrs
(
self
):
"""Utility method for more easily implementing get_child_accessors.
If you override get_child_accessors then this method is not used."""
return
self
.
child_attrs
def
clone_node
(
self
):
"""Clone the node. This is defined as a shallow copy, except for member lists
amongst the child attributes (from get_child_accessors) which are also
...
...
@@ -216,23 +147,20 @@ class Node(object):
try
:
return
self
.
_end_pos
except
AttributeError
:
children
=
[
acc
.
get
()
for
acc
in
self
.
get_child_accessors
()]
if
len
(
children
)
==
0
:
self
.
_end_pos
=
self
.
pos
else
:
flat
=
[]
for
attr
in
self
.
child_attrs
:
child
=
getattr
(
parent
,
attr
)
# Sometimes lists, sometimes nodes
flat
=
[]
for
child
in
children
:
if
child
is
None
:
pass
elif
isinstance
(
child
,
list
):
flat
+=
child
else
:
flat
.
append
(
child
)
if
len
(
flat
)
==
0
:
self
.
_end_pos
=
self
.
pos
if
child
is
None
:
pass
elif
isinstance
(
child
,
list
):
flat
+=
child
else
:
self
.
_end_pos
=
max
([
child
.
end_pos
()
for
child
in
flat
])
flat
.
append
(
child
)
if
len
(
flat
)
==
0
:
self
.
_end_pos
=
self
.
pos
else
:
self
.
_end_pos
=
max
([
child
.
end_pos
()
for
child
in
flat
])
return
self
.
_end_pos
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment