def foo(): tree = Nonterminal(symbol="a", children=[]) try: return tree.getsourcepos() except IndexError: return -42
def test_nonterminal_simple(self): pos = SourcePos(1,2,3) tree = Nonterminal(symbol="a", children=[ Symbol(symbol="b", additional_info="b", token=Token(name="B", source="b", source_pos=pos))]) assert tree.getsourcepos() == pos
def foo(): tree = Nonterminal( symbol="a", children=[ Nonterminal( symbol="c", children=[ Nonterminal( symbol="c", children=[ Nonterminal( symbol="c", children=[ Nonterminal( symbol="c", children=[ Nonterminal( symbol="c", children=[ Nonterminal( symbol="c", children=[]) ]) ]) ]) ]) ]) ]) try: return tree.getsourcepos() except IndexError: return -42
def test_nonterminal_simple(self): pos = SourcePos(1, 2, 3) tree = Nonterminal(symbol="a", children=[ Symbol(symbol="b", additional_info="b", token=Token(name="B", source="b", source_pos=pos)) ]) assert tree.getsourcepos() == pos
def test_nonterminal_nested_empty(self): tree = Nonterminal( symbol="a", children=[ Nonterminal( symbol="c", children=[ Nonterminal( symbol="c", children=[ Nonterminal( symbol="c", children=[ Nonterminal( symbol="c", children=[ Nonterminal( symbol="c", children=[ Nonterminal( symbol="c", children=[]) ]) ]) ]) ]) ]) ]) assert len(tree.children) != 0 # the not-so-trivial part. py.test.raises(IndexError, tree.getsourcepos)
def match_symbol(self, i, symbol): if (i, symbol) in self.matched: return self.matched[i, symbol] error = None # for the annotator if self.parser.is_nonterminal(symbol): rule = self.parser.get_rule(symbol) subsymbol = None error = None for expansion in rule.expansions: curr = i children = [] for subsymbol in expansion: node, next, error2 = self.match_symbol(curr, subsymbol) if node is None: error = combine_errors(error, error2) break children.append(node) curr = next else: assert len(expansion) == len(children) result = (Nonterminal(symbol, children), curr, error) self.matched[i, symbol] = result return result self.matched[i, symbol] = None, 0, error return None, 0, error else: try: input = self.input[i] if self.terminal_equality(symbol, input): result = (Symbol(symbol, input.source, input), i + 1, error) self.matched[i, symbol] = result return result else: # XXX hack unnice: handles the sort of token names that # ebnfparse produces if (symbol.startswith("__") and symbol.split("_")[2][0] in "0123456789"): expected = symbol.split("_")[-1] else: expected = symbol error = ErrorInformation(i, [expected]) except IndexError: error = ErrorInformation(i) return None, 0, error
def test_nonterminal_simple_empty(self): tree = Nonterminal(symbol="a", children=[]) assert len(tree.children) == 0 # trivial py.test.raises(IndexError, tree.getsourcepos)