Esempio n. 1
0
def test_anychar_fail():
    p = lib.anychar()
    with pytest.raises(lib.ParseError):
        p._run_parser("")
Esempio n. 2
0
def test_bind_continuation_failure():
    p = lib.anychar().bind(lambda c: lib.tag("foo")
                           if c == 't' else lib.tag("bar"))
    with pytest.raises(lib.ParseError):
        p("tbar")
Esempio n. 3
0
def test_anychar():
    p = lib.anychar()
    assert (p._run_parser("foo") == ("f", "oo"))
Esempio n. 4
0
def test_bind():
    p = lib.anychar().bind(lambda c: lib.tag("foo")
                           if c == 't' else lib.tag("bar"))
    assert (p("tfoo") == "foo")
    assert (p("fbar") == "bar")
Esempio n. 5
0
 def builder():
     return lib.many(lib.anychar())
Esempio n. 6
0
def test_take_until_until_end_of_input():
    p = lib.take_until(lib.char("b"),
                       lib.anychar()).map(lambda cs: "".join(cs))
    assert (p("aaa") == "aaa")
Esempio n. 7
0
def test_take_until():
    p = lib.take_until(lib.char("b"), lib.anychar())
    assert (p._run_parser("foobar") == (['f', 'o', 'o'], "bar"))

def as_string(p):
    """
    Take a parser which parses a list of characters, and modify it to
    return the string consisting of those characters.
    """
    return p.map(lambda cs: "".join(cs))


newline = lib.char('\n')


line = as_string(
    lib.alternative(
        lib.left(lib.take_until(newline, lib.anychar()), newline),
        lib.many1(lib.anychar())))


def build_section_parser():
    """
    Build a parser for structure of markdown document.

    This works by recursion - we build a parser which captures all
    subsections of header depth at least n (markdown_depth(n)). A
    markdown parser is then just as many markdown_depth(0) as we can
    possibly collect.

    When we parse a header, we need to figure out whether we are going to
    add it inside the subsections of the currently accumulating section,
    or go up a level instead. To do this we use a lookahead parse (lib.peek)