[Python-checkins] r52889 - sandbox/trunk/2to3/fix_apply.py sandbox/trunk/2to3/fix_has_key.py sandbox/trunk/2to3/pytree.py

guido.van.rossum python-checkins at python.org
Fri Dec 1 16:57:07 CET 2006


Author: guido.van.rossum
Date: Fri Dec 1 16:57:06 2006
New Revision: 52889
Modified:
 sandbox/trunk/2to3/fix_apply.py
 sandbox/trunk/2to3/fix_has_key.py
 sandbox/trunk/2to3/pytree.py
Log:
Reduce parse tree complexity by leaving out interior nodes with one child.
Modified: sandbox/trunk/2to3/fix_apply.py
==============================================================================
--- sandbox/trunk/2to3/fix_apply.py	(original)
+++ sandbox/trunk/2to3/fix_apply.py	Fri Dec 1 16:57:06 2006
@@ -57,7 +57,7 @@
 
 
 # Constant nodes
-n_apply = pytree.Node(syms.atom, [pytree.Leaf(token.NAME, "apply")])
+n_apply = pytree.Leaf(token.NAME, "apply")
 n_lpar = pytree.Leaf(token.LPAR, "(")
 n_rpar = pytree.Leaf(token.RPAR, ")")
 n_comma = pytree.Leaf(token.COMMA, ",")
@@ -75,6 +75,8 @@
 n_arglist = node.children[1].children[1]
 if n_arglist == n_rpar:
 return # apply() with no arguments?!
+ if n_arglist.type != syms.arglist:
+ return # apply() with only one argument?!
 l_args = []
 for arg in n_arglist.children:
 if arg == n_comma:
Modified: sandbox/trunk/2to3/fix_has_key.py
==============================================================================
--- sandbox/trunk/2to3/fix_has_key.py	(original)
+++ sandbox/trunk/2to3/fix_has_key.py	Fri Dec 1 16:57:06 2006
@@ -85,7 +85,7 @@
 return # ".has_key" followed by "()"
 argsnode = next_children[1]
 arg = argsnode
- if argsnode.type != syms.arglist:
+ if argsnode.type == syms.arglist:
 args = argsnode.children
 if len(args) > 2:
 return # Too many arguments
Modified: sandbox/trunk/2to3/pytree.py
==============================================================================
--- sandbox/trunk/2to3/pytree.py	(original)
+++ sandbox/trunk/2to3/pytree.py	Fri Dec 1 16:57:06 2006
@@ -73,6 +73,30 @@
 """
 raise NotImplementedError
 
+ def replace(self, new):
+ """Replaces this node with a new one in the parent.
+
+ This can also be used to remove this node from the parent by
+ passing None.
+ """
+ assert self.parent is not None, str(self)
+ assert new is None or new.parent is None, str(new)
+ l_children = []
+ found = False
+ for ch in self.parent.children:
+ if ch is self:
+ assert not found, (self.parent.children, self, new)
+ if new is not None:
+ l_children.append(new)
+ found = True
+ else:
+ l_children.append(ch)
+ assert found, (self.children, self, new)
+ self.parent.children = tuple(l_children)
+ if new is not None:
+ new.parent = self.parent
+ self.parent = None
+
 
 class Node(Base):
 
@@ -126,30 +150,6 @@
 return ""
 return self.children[0].get_prefix()
 
- def replace(self, new):
- """Replaces this node with a new one in the parent.
-
- This can also be used to remove this node from the parent by
- passing None.
- """
- assert self.parent is not None, str(self)
- assert new is None or new.parent is None, str(new)
- l_children = []
- found = False
- for ch in self.parent.children:
- if ch is self:
- assert not found, (self.parent.children, self, new)
- if new is not None:
- l_children.append(new)
- found = True
- else:
- l_children.append(ch)
- assert found, (self.children, self, new)
- self.parent.children = tuple(l_children)
- if new is not None:
- new.parent = self.parent
- self.parent = None
-
 
 class Leaf(Base):
 
@@ -207,8 +207,10 @@
 """
 type, value, context, children = raw_node
 if children or type in gr.number2symbol:
- # XXX If there's exactly one child, should return that child
- # instead of synthesizing a new node.
+ # If there's exactly one child, return that child instead of
+ # creating a new node.
+ if len(children) == 1:
+ return children[0]
 return Node(type, children, context=context)
 else:
 return Leaf(type, value, context=context)


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /