Commit c9fb4913 authored by Igor Dejanovic's avatar Igor Dejanovic

refs #2 Support for referencing child node as attributes on NonTerminal

This works for rules that are referenced from the rule which created
NonTerminal.
parent 6304da60
......@@ -615,6 +615,9 @@ class NonTerminal(ParseTreeNode):
super(NonTerminal, self).__init__(type, position, error)
self.nodes = flatten([nodes])
# Child nodes cache. Used for lookup by rule name.
self._child_cache = {}
@property
def desc(self):
return self.name
......@@ -628,6 +631,26 @@ class NonTerminal(ParseTreeNode):
def __repr__(self):
return "[ %s ]" % ", ".join([repr(x) for x in self.nodes])
def __getattr__(self, item):
"""
Find a child (non)terminal by the rule name.
Args:
item(str): The name of the child node.
"""
# First check the cache
if item in self._child_cache:
return self._child_cache[item]
# If not found in the cache find it and store it in the
# cache for later.
for n in self.nodes:
if n.type == item:
self._child_cache[item] = n
return n
raise AttributeError
# ----------------------------------------------------
# Semantic Actions
......
# -*- coding: utf-8 -*-
#######################################################################
# Name: test_peg_parser
# Purpose: Test for parser constructed using PEG textual grammars.
# Author: Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2014 Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# License: MIT License
#######################################################################
from unittest import TestCase
# Grammar
from arpeggio import ParserPython, ZeroOrMore
def foo(): return "a", bar, "b", baz
def bar(): return "c"
def baz(): return "d"
class TestPEGParser(TestCase):
def test_lookup_single(self):
parser = ParserPython(foo)
result = parser.parse("a c b d")
self.assertTrue(hasattr(result, "bar"))
self.assertTrue(hasattr(result, "baz"))
self.assertTrue(not hasattr(result, "unexisting"))
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