Commit 1162701b authored by Igor Dejanovic's avatar Igor Dejanovic

NonTerminal inherits list now.

parent c9fb4913
...@@ -603,7 +603,7 @@ class Terminal(ParseTreeNode): ...@@ -603,7 +603,7 @@ class Terminal(ParseTreeNode):
return str(self) == str(other) return str(self) == str(other)
class NonTerminal(ParseTreeNode): class NonTerminal(ParseTreeNode, list):
""" """
Non-leaf node of the Parse Tree. Represents language syntax construction. Non-leaf node of the Parse Tree. Represents language syntax construction.
...@@ -613,7 +613,7 @@ class NonTerminal(ParseTreeNode): ...@@ -613,7 +613,7 @@ class NonTerminal(ParseTreeNode):
""" """
def __init__(self, type, position, nodes, error=False): def __init__(self, type, position, nodes, error=False):
super(NonTerminal, self).__init__(type, position, error) super(NonTerminal, self).__init__(type, position, error)
self.nodes = flatten([nodes]) self.extend(flatten([nodes]))
# Child nodes cache. Used for lookup by rule name. # Child nodes cache. Used for lookup by rule name.
self._child_cache = {} self._child_cache = {}
...@@ -622,14 +622,14 @@ class NonTerminal(ParseTreeNode): ...@@ -622,14 +622,14 @@ class NonTerminal(ParseTreeNode):
def desc(self): def desc(self):
return self.name return self.name
def __iter__(self): # def __iter__(self):
return iter(self.nodes) # return self
def __str__(self): def __str__(self):
return "".join([str(x) for x in self.nodes]) return "".join([str(x) for x in self])
def __repr__(self): def __repr__(self):
return "[ %s ]" % ", ".join([repr(x) for x in self.nodes]) return "[ %s ]" % ", ".join([repr(x) for x in self])
def __getattr__(self, item): def __getattr__(self, item):
""" """
...@@ -644,7 +644,7 @@ class NonTerminal(ParseTreeNode): ...@@ -644,7 +644,7 @@ class NonTerminal(ParseTreeNode):
# If not found in the cache find it and store it in the # If not found in the cache find it and store it in the
# cache for later. # cache for later.
for n in self.nodes: for n in self:
if n.type == item: if n.type == item:
self._child_cache[item] = n self._child_cache[item] = n
return n return n
...@@ -743,7 +743,7 @@ class Parser(object): ...@@ -743,7 +743,7 @@ class Parser(object):
""" """
nodes = [] nodes = []
if isinstance(node, NonTerminal): if isinstance(node, NonTerminal):
for n in node.nodes: for n in node:
nodes.append(tree_walk(n)) nodes.append(tree_walk(n))
if node.type in sem_actions: if node.type in sem_actions:
......
...@@ -23,7 +23,7 @@ def expression(): return [regex,(identifier, Not(LEFT_ARROW)), ...@@ -23,7 +23,7 @@ def expression(): return [regex,(identifier, Not(LEFT_ARROW)),
(OPEN, ordered_choice, CLOSE), (OPEN, ordered_choice, CLOSE),
literal] literal]
def regex(): return "r", "'", _(r"(\\\'|[^\'])*"),"'" def regex(): return "r'", _(r"(\\\'|[^\'])*"),"'"
def identifier(): return _(r"[a-zA-Z_]([a-zA-Z_]|[0-9])*") def identifier(): return _(r"[a-zA-Z_]([a-zA-Z_]|[0-9])*")
#def literal(): return [_(r"\'(\\\'|[^\'])*\'"),_(r'"[^"]*"')] #def literal(): return [_(r"\'(\\\'|[^\'])*\'"),_(r'"[^"]*"')]
def literal(): return _(r'(\'(\\\'|[^\'])*\')|("[^"]*")') def literal(): return _(r'(\'(\\\'|[^\'])*\')|("[^"]*")')
...@@ -148,8 +148,8 @@ class SemIdentifier(SemanticAction): ...@@ -148,8 +148,8 @@ class SemIdentifier(SemanticAction):
class SemRegEx(SemanticAction): class SemRegEx(SemanticAction):
def first_pass(self, parser, node, nodes): def first_pass(self, parser, node, nodes):
if parser.debug: if parser.debug:
print "RegEx %s." % nodes[2].value print "RegEx %s." % nodes[1].value
return RegExMatch(nodes[2].value) return RegExMatch(nodes[1].value)
class SemLiteral(SemanticAction): class SemLiteral(SemanticAction):
def first_pass(self, parser, node, nodes): def first_pass(self, parser, node, nodes):
......
...@@ -38,7 +38,7 @@ class TestDecoratorCombine(TestCase): ...@@ -38,7 +38,7 @@ class TestDecoratorCombine(TestCase):
self.assertRaises(NoMatch, fail_nm) self.assertRaises(NoMatch, fail_nm)
self.assertIsInstance(ptree1, NonTerminal) self.assertIsInstance(ptree1, NonTerminal)
self.assertIsInstance(ptree1.nodes[0], Terminal) self.assertIsInstance(ptree1[0], Terminal)
self.assertEqual(ptree1.nodes[0].value, "abbb") self.assertEqual(ptree1[0].value, "abbb")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment