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)
def parse_expr(grammar: Grammar, content: str): scanner = DefaultScanner(grammar, '<example>', content) parser = Parser(scanner) result = parser.parse(grammar.parselets['expr']) return result
def parse_combinator(content: str): scanner = DefaultScanner(combinator_grammar, '<example>', content) parser = Parser(scanner) return parser.parse(combinator_grammar.parselets['combinator_sequence'])