Exemple #1
0
def test_extend_fail_grammar():
    grammar1 = Grammar()
    grammar1.add_parselet('expr', kind=ParseletKind.Pratt)

    grammar2 = Grammar()
    grammar2.add_parselet('expr', kind=ParseletKind.Packrat)

    with pytest.raises(GrammarError):
        Grammar.merge(grammar1, grammar2)
Exemple #2
0
def test_extend_trivia_grammar():
    grammar1 = Grammar()
    grammar1.add_trivia(grammar1.add_token('A'))
    grammar2 = Grammar()
    grammar2.add_trivia(grammar2.add_token('A'))
    grammar2.add_trivia(grammar2.add_token('B'))
    result = Grammar.merge(grammar1, grammar2)
    assert result.trivia == {
        result.tokens['A'],
        result.tokens['B'],
    }
Exemple #3
0
def test_extend_brackets_grammar():
    grammar1 = Grammar()
    grammar1.add_brackets(grammar1.add_implicit('('),
                          grammar1.add_implicit(')'))
    grammar2 = Grammar()
    grammar2.add_brackets(grammar2.add_implicit('('),
                          grammar2.add_implicit(')'))
    grammar2.add_brackets(grammar2.add_implicit('['),
                          grammar2.add_implicit(']'))
    result = Grammar.merge(grammar1, grammar2)
    assert result.brackets == {(result.tokens['['], result.tokens[']']),
                               (result.tokens['('], result.tokens[')'])}
Exemple #4
0
def test_extend_packrat_grammar():
    grammar1 = Grammar()
    grammar1.add_token('Number')
    grammar1.add_token('String')
    expr_id = grammar1.add_parselet('expr', result_type=object)
    grammar1.add_parser(expr_id, 'Number')
    grammar1.add_parser(expr_id, 'String')

    grammar2 = Grammar()
    grammar2.add_token('Number')
    grammar2.add_token('String')
    expr_id = grammar2.add_parselet('expr', result_type=object)
    grammar2.add_parser(expr_id, 'Number')
    grammar2.add_parser(expr_id, 'String')

    result = Grammar.merge(grammar1, grammar2)
    expr_id = result.parselets['expr']
    assert expr_id in result.tables
    assert len(cast(PackratTable, result.tables[expr_id]).parselets) == 4