def test_lookahead(self): grammar = Grammar(r'''starts_with_a = &"a" ~"[a-z]+"''') self.assertRaises(ParseError, grammar.parse, 'burp') s = 'arp' self.assertEqual(grammar.parse('arp'), Node(grammar['starts_with_a'], s, 0, 3, children=[ Node(Lookahead(Literal('a')), s, 0, 0), Node(Regex(r'[a-z]+'), s, 0, 3)]))
def visit_lookahead_term(self, node, lookahead_term): ampersand, term, _ = lookahead_term return Lookahead(term)
"""Treat a parenthesized subexpression as just its contents. Its position in the tree suffices to maintain its grouping semantics. """ return expression def visit_quantifier(self, quantifier, (symbol, _)): """Turn a quantifier into just its symbol-matching node.""" return symbol def visit_quantified(self, quantified, (atom, quantifier)): return self.quantifier_classes[quantifier.text](atom) def visit_lookahead_term(self, lookahead_term, (ampersand, term, _)): return Lookahead(term) def visit_not_term(self, not_term, (exclamation, term, _)): return Not(term) def visit_rule(self, rule, (label, equals, expression)): """Assign a name to the Expression and return it.""" expression.name = label # Assign a name to the expr. return expression def visit_sequence(self, sequence, (term, other_terms)): """A parsed Sequence looks like [term node, OneOrMore node of ``another_term``s]. Flatten it out.""" return Sequence(term, *other_terms) def visit_ored(self, ored, (first_term, other_terms)):
def visit_lookahead_term(self, lookahead_term, xxx_todo_changeme3): (ampersand, term, _) = xxx_todo_changeme3 return Lookahead(term)
def visit_lookahead_term(self, lookahead_term, children): return Lookahead(children[1])