[pypy-commit] pypy pyparser-improvements: create Nonterminal._children list lazily

cfbolz pypy.commits at gmail.com
Mon Mar 12 12:52:39 EDT 2018


Author: Carl Friedrich Bolz-Tereick <cfbolz at gmx.de>
Branch: pyparser-improvements
Changeset: r93974:d15189dd835a
Date: 2018-03-12 14:54 +0100
http://bitbucket.org/pypy/pypy/changeset/d15189dd835a/

Log:	create Nonterminal._children list lazily

diff --git a/pypy/interpreter/pyparser/parser.py b/pypy/interpreter/pyparser/parser.py
--- a/pypy/interpreter/pyparser/parser.py
+++ b/pypy/interpreter/pyparser/parser.py
@@ -127,21 +127,27 @@
 
 class Nonterminal(AbstractNonterminal):
     __slots__ = ("_children", )
-    def __init__(self, type, children):
+    def __init__(self, type):
         Node.__init__(self, type)
-        self._children = children
+        self._children = None
 
     def __repr__(self):
         return "Nonterminal(type=%s, children=%r)" % (self.type, self._children)
 
     def get_child(self, i):
+        assert self._children is not None
         return self._children[i]
 
     def num_children(self):
+        if self._children is None:
+            return 0
         return len(self._children)
 
     def append_child(self, child):
-        self._children.append(child)
+        if self._children is None:
+            self._children = [child]
+        else:
+            self._children.append(child)
 
 
 class Nonterminal1(AbstractNonterminal):
@@ -209,7 +215,7 @@
         if start == -1:
             start = self.grammar.start
         self.root = None
-        current_node = Nonterminal(start, [])
+        current_node = Nonterminal(start)
         self.stack = StackEntry(None, self.grammar.dfas[start - 256], 0, current_node)
 
     def add_token(self, token_type, value, lineno, column, line):
@@ -284,7 +290,7 @@
 
     def push(self, next_dfa, next_state, node_type, lineno, column):
         """Push a terminal and adjust the current state."""
-        new_node = Nonterminal(node_type, [])
+        new_node = Nonterminal(node_type)
 
         self.stack.state = next_state
         self.stack = self.stack.push(next_dfa, 0, new_node)
diff --git a/pypy/interpreter/pyparser/test/test_parser.py b/pypy/interpreter/pyparser/test/test_parser.py
--- a/pypy/interpreter/pyparser/test/test_parser.py
+++ b/pypy/interpreter/pyparser/test/test_parser.py
@@ -55,8 +55,7 @@
             n = parser.Terminal(tp, value, 0, 0)
         else:
             tp = gram.symbol_ids[data[0]]
-            children = []
-            n = parser.Nonterminal(tp, children)
+            n = parser.Nonterminal(tp)
         new_indent = count_indent(line)
         if new_indent >= last_indent:
             if new_indent == last_indent and node_stack:


More information about the pypy-commit mailing list