예제 #1
0
파일: gvm.py 프로젝트: alurin/gvm
def main():
    grammar = Grammar()

    # core grammar
    grammar.extend(create_core_grammar())

    # combinator grammars
    grammar.extend(create_combinator_grammar())

    # add macro grammar. e.g. expr
    grammar.add_parser('macro',
                       'name:Name "::=" combinator: combinator_sequence')

    # # dump grammar
    # dump_grammar(sys.stdout, grammar)

    content = """
expr ::= 1 2 3 ; |

def main(): pass
""".strip()

    #
    scanner = DefaultScanner(grammar, '<example>', content)
    parser = Parser(scanner)
    try:
        parser.parse(grammar.parselets['macro'])
    except ParserError as ex:
        ex.to_stream(create_writer(sys.stderr), content)
        exit(1)
예제 #2
0
파일: test_parser.py 프로젝트: alurin/gvm
def parse_expr(grammar: Grammar, content: str):
    scanner = DefaultScanner(grammar, '<example>', content)
    parser = Parser(scanner)
    result = parser.parse(grammar.parselets['expr'])
    return result
예제 #3
0
def parse_combinator(content: str):
    scanner = DefaultScanner(combinator_grammar, '<example>', content)
    parser = Parser(scanner)
    return parser.parse(combinator_grammar.parselets['combinator_sequence'])