def test_fails_to_parse_non_ll1(self): e = self.assertSimpleParseFailure( combinators.choice( combinators.sequence(primitives.match(lambda c: c == "h"), primitives.match(lambda c: c == "a")), combinators.sequence(primitives.match(lambda c: c == "b"), primitives.match(lambda c: c == "e"))), "hello") self.assertEqual("e", e.value) self.assertEqual(list("llo"), list(e.it)) self.assertEqual(set(), e.expected)
def word(s): """Parse a sequence of characters as a single string. Args: s: Allowed characters in the word. Returns: The parsing function. """ @combinators.mapf(combinators.many1(one_of(s))) def _action(cs): return "".join(cs) return combinators.choice(_action, primitives.error(list(s)))
def literal(s): """Parse a predefined literal. Args: s: The literal to parse. Returns: The parsing function. """ @combinators.mapf(combinators.sequence(*[char(c) for c in s])) def _action(cs): return "".join(cs) return combinators.choice(combinators.suppress_expect(_action), primitives.error([s]))
def __or__(self, rhs): return Parser(combinators.choice(self, rhs))
def test_fails_to_parse(self): e = self.assertParseFailure( combinators.choice(primitives.error({"error1!"}), primitives.error({"error2!"})), "hello") self.assertEqual({"error1!", "error2!"}, e.expected)
def test_parses_second(self): self.assertParse( combinators.choice(primitives.match(lambda c: c == "h"), primitives.match(lambda c: c == "y")), "y", "ello", "yello")
def test_parses(self): self.assertParse( combinators.choice(primitives.match(lambda c: c == "h"), primitives.match(lambda c: c == "a")), "h", "ello", "hello")