Example #1
0
def test_indent_block(block):
    lines, indent_level = block

    lvlc = indent_block(p_space_consumer=scn,
                        p_reference=symbol(symbol_c,
                                           sc).result(IndentNone(symbol_c)))
    lvlb = indent_block(p_space_consumer=scn,
                        p_reference=symbol(symbol_b, sc).result(
                            IndentSome(None, lambda l: (symbol_b, l), lvlc)))
    lvla = indent_block(p_space_consumer=scn,
                        p_reference=symbol(symbol_a, sc).result(
                            IndentMany(indent_level, lambda l: (symbol_a, l),
                                       lvlb)))

    s = ''.join(lines)
    p = lvla << parsy.eof

    cols = [get_col(l) for l in lines]

    if cols[1] <= cols[0]:
        with pytest.raises(parsy.ParseError):
            p.parse(s)
    elif indent_level and cols[1] != indent_level:
        with pytest.raises(parsy.ParseError):
            p.parse(s)
    elif cols[2] <= cols[1]:
        with pytest.raises(parsy.ParseError):
            p.parse(s)
    elif cols[3] == cols[2]:
        with pytest.raises(parsy.ParseError):
            p.parse(s)
    elif cols[3] <= cols[0]:
        with pytest.raises(parsy.ParseError):
            p.parse(s)
    elif cols[3] < cols[1]:
        with pytest.raises(parsy.ParseError):
            p.parse(s)
    elif cols[3] > cols[1]:
        with pytest.raises(parsy.ParseError):
            p.parse(s)
    elif cols[4] <= cols[3]:
        with pytest.raises(parsy.ParseError):
            p.parse(s)
    else:
        val = p.parse(s)
        assert val == (symbol_a, [
            (symbol_b, [symbol_c]),
            (symbol_b, [symbol_c]),
        ])
Example #2
0
def test_indent_guard(lines):
    sp = (symbol(symbol_a, sc) << char.eol).result('')
    ip = partial(indent_guard, scn)

    @parsy.generate
    def p():
        x = yield ip(operator.gt, 1)
        return (
            sp >> ip(operator.eq, x) >> sp >> ip(operator.gt, x) >> sp >> scn)

    s = ''.join(lines)

    cols = [get_col(l) for l in lines]

    if cols[0] <= 1:
        with pytest.raises(parsy.ParseError):
            p.parse(s)
    elif cols[1] != cols[0]:
        with pytest.raises(parsy.ParseError):
            p.parse(s)
    elif cols[2] <= cols[0]:
        with pytest.raises(parsy.ParseError):
            p.parse(s)
    else:
        val = p.parse(s)
        assert val == ''
Example #3
0
def test_symbol():
    """
    when stream begins with the symbol it parses the symbol and
    trailing whitespace
    """
    p = symbol('foo').many()
    s = "foo foo\nfoo "
    val = p.parse(s)
    assert val == ['foo', 'foo', 'foo']
Example #4
0
def test_non_indented(s):
    """
    Returns the result of content-consuming parser if the start pos is indented
    else raises ParseError
    """
    p = non_indented(scn, symbol(symbol_a, scn))

    i = get_indent(s)

    if i == 0:
        val = p.parse(s)
        assert val == symbol_a
    else:
        with pytest.raises(parsy.ParseError):
            p.parse(s)
Example #5
0
 def parser():
     a = yield symbol(symbol_a, sc_)
     b = yield symbol(symbol_b, sc_)
     c = yield symbol(symbol_c, scn)
     return a, b, c