def test_fails_to_parse_front(self): e = self.assertParseFailure( combinators.between(primitives.error({"error!"}), primitives.match(lambda c: c == "l"), primitives.match(lambda c: c == "e")), "hello") self.assertEqual({"error!"}, e.expected)
def test_fails_to_parse_second(self): e = self.assertSimpleParseFailure( combinators.sequence(primitives.any_, primitives.error({"error!"})), "hello") self.assertEqual("e", e.value) self.assertEqual(list("llo"), list(e.it)) self.assertEqual({"error!"}, e.expected)
def test_fails_to_parse_ll2(self): e = self.assertParseFailure( combinators.try_(combinators.sequence( primitives.any_, primitives.any_, primitives.error({"error!"}))), "test") self.assertEqual({"error!"}, e.expected)
def test_fails_to_parse_back(self): e = self.assertSimpleParseFailure( combinators.between(primitives.match(lambda c: c == "h"), primitives.error({"error!"}), primitives.match(lambda c: c == "e")), "hello") self.assertEqual("l", e.value) self.assertEqual(list("lo"), list(e.it)) self.assertEqual({"error!"}, 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 test_fails_to_parse(self): e = self.assertParseFailure(primitives.error({"error!"}), "hello") self.assertEqual({"error!"}, e.expected)
def test_fails_to_parse(self): e = self.assertParseFailure( combinators.sequence(primitives.error({"error!"}), primitives.any_), "hello") self.assertEqual({"error!"}, e.expected)
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_fails_to_parse_2(self): e = self.assertParseFailure( combinators.sep_by1(primitives.match(lambda c: c == ","), primitives.error({"error!"})), "hello") self.assertEqual({"error!"}, e.expected)
def test_parse_some(self): self.assertParse( combinators.sep_by1(primitives.error({"error!"}), primitives.match(lambda c: c == "h")), ["h"], "ello", "hello")
def test_parses_none_2(self): self.assertParse( combinators.sep_by(primitives.match(lambda c: c == ","), primitives.error({"error!"})), [], "hello", "hello")
def test_parses_none(self): self.assertParse(combinators.many(primitives.error(set())), [], "hello", "hello")
def test_parses_none(self): self.assertParse(combinators.option(primitives.error(set())), None, "hello", "hello")
def test_fails_to_parse(self): e = self.assertParseFailure( combinators.count(1, primitives.error({"error!"})), "hello") self.assertEqual({"error!"}, e.expected)