Example #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)
Example #2
0
File: string.py Project: rfw/kessel
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)))
Example #3
0
File: string.py Project: rfw/kessel
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]))
Example #4
0
File: parser.py Project: rfw/kessel
 def __or__(self, rhs):
     return Parser(combinators.choice(self, rhs))
Example #5
0
 def test_fails_to_parse(self):
     e = self.assertParseFailure(
         combinators.choice(primitives.error({"error1!"}),
                            primitives.error({"error2!"})),
         "hello")
     self.assertEqual({"error1!", "error2!"}, e.expected)
Example #6
0
 def test_parses_second(self):
     self.assertParse(
         combinators.choice(primitives.match(lambda c: c == "h"),
                            primitives.match(lambda c: c == "y")),
         "y", "ello", "yello")
Example #7
0
 def test_parses(self):
     self.assertParse(
         combinators.choice(primitives.match(lambda c: c == "h"),
                            primitives.match(lambda c: c == "a")),
         "h", "ello", "hello")