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(self): e = self.assertParseFailure( combinators.sep_by1(primitives.match(lambda c: c == ","), primitives.match(lambda c: c == "h", {"error!"})), "ello") 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 test_fails_to_parse_non_ll1(self): e = self.assertSimpleParseFailure( combinators.option( combinators.sequence(primitives.match(lambda c: c == "h"), primitives.match(lambda c: c == "e"))), "hzllo") self.assertEqual("z", e.value) self.assertEqual(list("llo"), list(e.it)) self.assertEqual(set(), e.expected)
def test_fails_to_parse_second(self): e = self.assertSimpleParseFailure( combinators.count(5, primitives.match(lambda c: c == "h", {"error!"})), "hello") self.assertEqual("e", e.value) self.assertEqual(list("llo"), list(e.it)) self.assertEqual({"error!"}, e.expected)
def one_of(s): """Parse a single character that match a list of characters. Args: s: Characters to parse. Returns: The parsing function. """ @combinators.mapf(primitives.match(lambda a: a.char in s, list(s))) def _action(a): return a.char return _action
def test_parses(self): self.assertParse(primitives.match(lambda c: c == "h"), "h", "ello", "hello")
def none_of(s): @combinators.mapf(primitives.match(lambda a: a.char not in s, list(s))) def _action(a): return a.char return _action
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.try_(primitives.match(lambda c: c == "h")), "h", "ello", "hello")
def test_parses_non_ll1(self): self.assertParse( combinators.not_followed_by(combinators.sequence( primitives.match(lambda c: c == "h"), primitives.match(lambda c: c == "e"))), None, "hllo", "hllo")
def test_fails_to_parse(self): e = self.assertSimpleParseFailure( combinators.not_followed_by(primitives.match(lambda c: c == "h")), "hello") self.assertEqual(set(), e.expected)
def test_parses_some(self): self.assertParse( combinators.many1(primitives.match(lambda c: c == "h")), ["h"], "ello", "hello")
def test_parses(self): self.assertParse( combinators.not_followed_by(primitives.match(lambda c: c == "h")), None, "ello", "ello")
def test_fails_to_parse(self): self.assertParseFailure(primitives.match(lambda c: c == "h"), "ello")
def test_parses(self): self.assertParse( combinators.sep_by1(primitives.match(lambda c: c == ","), primitives.match(lambda c: c == "h")), ["h", "h", "h"], "ello", "h,h,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.sep_by(primitives.match(lambda c: c == ","), primitives.match(lambda c: c == "h")), [], "ello", "ello")
def test_parses(self): self.assertParse( combinators.between(primitives.match(lambda c: c == "h"), primitives.match(lambda c: c == "l"), primitives.match(lambda c: c == "e")), "e", "lo", "hello")
def test_fails_to_parse_with_expect(self): e = self.assertParseFailure(primitives.match(lambda c: c == "h", {"error!"}), "ello") 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")