Пример #1
0
 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)
Пример #2
0
 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)
Пример #3
0
 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)
Пример #4
0
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]))
Пример #5
0
 def __add__(self, rhs):
     return Parser(combinators.sequence(self, rhs))
Пример #6
0
 def test_fails_to_parse(self):
     e = self.assertParseFailure(
         combinators.sequence(primitives.error({"error!"}), primitives.any_),
         "hello")
     self.assertEqual({"error!"}, e.expected)
Пример #7
0
 def test_parses(self):
     self.assertParse(
         combinators.sequence(primitives.any_, primitives.any_),
         ["h", "e"], "llo", "hello")
Пример #8
0
 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")