예제 #1
0
def test_error_recovery_complete():
    """
    In this test we start from the 'Result' rule so parglare will require
    input to end with 'EOF' for the parse to be successful.
    """
    parser = Parser(g, actions=actions, error_recovery=True)

    result = parser.parse("1 + 2 + * 3 & 89 - 5")

    # Both '*' and '& 89' should be dropped now as the parser expects EOF at
    # the end. Thus the parser should calculate '1 + 2 + 3 - 5'
    assert result == 1

    assert len(parser.errors) == 2

    e1, e2 = parser.errors

    assert e1.position == 8
    assert e1.length == 1

    # Characters of the second error should be packed as a single error
    # spanning the whole erroneous region. Whitespaces should be included too.
    assert e2.position == 12
    assert e2.length == 4
    assert 'Unexpected input at position (1, 12)' in str(e2)
예제 #2
0
def test_error_recovery_uncomplete():
    """
    Test default recovery for partial parse.
    parglare will try to parse as much as possible for the given grammar and
    input. If the current input can be reduced to the start rule the parse
    will succeed. In order to prevent partial parse first grammar rule should
    be ended with EOF like in the case of 'Result' rule.
    """
    parser = Parser(g, start_production=2, actions=actions,
                    error_recovery=True, debug=True)

    result = parser.parse("1 + 2 + * 3 & 89 - 5")

    # '*' after '+' will be droped but when the parser reach '&'
    # it has a complete expression and will terminate successfuly and
    # report only one error ('*' after '+').
    # The parser should thus calculate '1 + 2 + 3'
    assert result == 6

    assert len(parser.errors) == 1

    e = parser.errors[0]

    assert e.position == 8
    assert e.length == 1
    assert 'Unexpected input at position (1, 8). Expected' in str(e)
예제 #3
0
def test_custom_error_recovery():
    """
    Test that registered callable for error recovery is called with the
    right parameters.
    """

    called = [False]

    def my_recovery(parser, input, position, expected_symbols):
        called[0] = True
        assert isinstance(parser, Parser)
        assert input == '1 + 2 + * 3 - 5'
        assert position == 8
        assert type(expected_symbols) is set
        assert Terminal('(') in expected_symbols
        assert Terminal('number') in expected_symbols
        return None, None, position + 1

    parser = Parser(g, actions=actions, error_recovery=my_recovery, debug=True)

    result = parser.parse("1 + 2 + * 3 - 5")

    assert result == 1

    # Assert that recovery handler is called.
    assert called[0]
예제 #4
0
def test_invalid_input():

    grammar = get_grammar()
    p = Parser(grammar)

    with pytest.raises(ParseError) as e:
        p.parse("id+id*+id")

    assert e.value.position == 6
    assert "(" in str(e)
    assert "id" in str(e)
예제 #5
0
def test_file_name():
    "Test that file name is given in the error string when parsing file."
    grammar = get_grammar()
    p = Parser(grammar)

    input_file = os.path.join(os.path.dirname(__file__), 'parsing_errors.txt')

    try:
        p.parse_file(input_file)
    except ParseError as e:
        assert 'parsing_errors.txt' in str(e)
예제 #6
0
def test_premature_end():

    grammar = get_grammar()
    p = Parser(grammar)

    with pytest.raises(ParseError) as e:
        p.parse("id+id*")

    assert e.value.position == 6
    assert "(" in str(e)
    assert "id" in str(e)
예제 #7
0
def test_tree_str():

    grammar = get_grammar()
    p = Parser(grammar, build_tree=True)

    res = p.parse("""id+  id * (id
    +id  )
    """)

    ts = res.tree_str()

    assert '+[18, +]' in ts
    assert ')[23, )]' in ts
예제 #8
0
def test_tree_str():

    grammar = get_grammar()
    p = Parser(grammar, build_tree=True)

    res = p.parse("""id+  id * (id
    +id  )
    """)

    ts = res.tree_str()

    assert '+[18->19, "+"]' in ts
    assert ')[23->24, ")"]' in ts
    assert 'F[10->24]' in ts
예제 #9
0
def test_error_recovery_parse_error():
    """In this test we have error that can't be recovered from by a simple
    dropping of characters as we'll end up with invalid expression at the EOF.

    The current solution is to throw ParseError at the beggining of the last
    error that couldn't be recovered from.

    """
    parser = Parser(g, actions=actions, error_recovery=True, debug=True)

    with pytest.raises(ParseError) as einfo:
        parser.parse("1 + 2 + * 3 + & -")

    assert einfo.value.position == 14
예제 #10
0
def test_line_column():
    grammar = get_grammar()
    p = Parser(grammar)

    try:
        p.parse("""id + id * id + id + error * id""")
    except ParseError as e:
        loc = e.location
        assert loc.start_position == 20
        assert loc.line == 1
        assert loc.column == 20

    try:
        p.parse("""id + id * id + id + error * id

        """)
    except ParseError as e:
        loc = e.location
        assert loc.start_position == 20
        assert loc.line == 1
        assert loc.column == 20

    try:
        p.parse("""

id + id * id + id + error * id""")
    except ParseError as e:
        loc = e.location
        assert loc.start_position == 22
        assert loc.line == 3
        assert loc.column == 20

    try:
        p.parse("""

id + id * id + id + error * id

        """)
    except ParseError as e:
        loc = e.location
        assert loc.start_position == 22
        assert loc.line == 3
        assert loc.column == 20
예제 #11
0
def test_line_column():
    grammar = get_grammar()
    p = Parser(grammar)

    try:
        p.parse("""id + id * id + id + error * id""")
    except ParseError as e:
        assert e.position == 20
        assert e.line == 1
        assert e.column == 20

    try:
        p.parse("""id + id * id + id + error * id

        """)
    except ParseError as e:
        assert e.position == 20
        assert e.line == 1
        assert e.column == 20

    try:
        p.parse("""

id + id * id + id + error * id""")
    except ParseError as e:
        assert e.position == 22
        assert e.line == 3
        assert e.column == 20

    try:
        p.parse("""

id + id * id + id + error * id

        """)
    except ParseError as e:
        assert e.position == 22
        assert e.line == 3
        assert e.column == 20