Exemplo n.º 1
0
def create_core_grammar() -> Grammar:
    """ This function is used for initialize default grammar """
    grammar = Grammar()
    grammar.add_pattern(grammar.add_token('Comment'), RE_COMMENT)
    grammar.add_pattern(grammar.add_token('Whitespace'), RE_WHITESPACE)
    grammar.add_pattern(grammar.add_token('Name'), RE_NAME)
    grammar.add_pattern(grammar.add_token('NewLine'), RE_NEWLINE)
    grammar.add_pattern(grammar.add_token('String'), RE_STRING_SINGLE)
    grammar.add_pattern(grammar.add_token('String'), RE_STRING_DOUBLE)
    grammar.add_pattern(grammar.add_token('Integer'), RE_NUMBER_BINARY)
    grammar.add_pattern(grammar.add_token('Integer'), RE_NUMBER_OCTAL)
    grammar.add_pattern(grammar.add_token('Integer'), RE_NUMBER_DECIMAL)
    grammar.add_pattern(grammar.add_token('Integer'), RE_NUMBER_HEXADECIMAL)
    grammar.add_pattern(grammar.add_token('Float'), RE_FLOAT_POINT)
    grammar.add_pattern(grammar.add_token('Float'), RE_FLOAT_EXPONENT)
    grammar.add_implicit('(')
    grammar.add_implicit(')')
    grammar.add_implicit('[')
    grammar.add_implicit(']')
    grammar.add_implicit('{')
    grammar.add_implicit('}')
    grammar.add_implicit('<')
    grammar.add_implicit('>')

    grammar.add_trivia(grammar.tokens['Comment'])
    grammar.add_trivia(grammar.tokens['Whitespace'])
    grammar.add_brackets(grammar.tokens['('], grammar.tokens[')'])
    grammar.add_brackets(grammar.tokens['['], grammar.tokens[']'])
    grammar.add_brackets(grammar.tokens['{'], grammar.tokens['}'])

    return grammar
Exemplo n.º 2
0
def test_add_brackets():
    grammar = Grammar()
    open_id = grammar.add_implicit('(')
    close_id = grammar.add_implicit(')')
    assert grammar.brackets == set()
    assert grammar.open_brackets == set()
    assert grammar.close_brackets == set()
    grammar.add_brackets(open_id, close_id)
    assert grammar.brackets == {(open_id, close_id)}
    assert grammar.open_brackets == {open_id}
    assert grammar.close_brackets == {close_id}
    assert grammar.bracket_pairs[open_id] == close_id
Exemplo n.º 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[')'])}