def main(argv): # type: (List[str]) -> int arena = alloc.Arena() arena.PushSource(source.Stdin('')) loader = pyutil.GetResourceLoader() oil_grammar = meta.LoadOilGrammar(loader) parse_ctx = None e_parser = expr_parse.ExprParser(parse_ctx, oil_grammar) line_lexer = lexer.LineLexer('', arena) line_reader = reader.FileLineReader(sys.stdin, arena) lex = lexer.Lexer(line_lexer, line_reader) try: pnode, _ = e_parser.Parse(lex, grammar_nt.command_expr) except error.Parse as e: ui.PrettyPrintError(e, arena) return 2 #print(pnode) tr = expr_to_ast.Transformer(oil_grammar) node = tr.Expr(pnode) assert node is not None tree = node.AbbreviatedTree() #tree = node.PrettyTree() ast_f = fmt.DetectConsoleOutput(sys.stdout) fmt.PrintTree(tree, ast_f) ast_f.write('\n') return 0
def _MakeLexer(self, line_reader, arena=None): """Helper function. TODO: should we combine the LineLexer and Lexer? And the matcher? """ line_lexer = lexer.LineLexer(match.MATCHER, '', arena=arena or self.arena) return lexer.Lexer(line_lexer, line_reader)
def _MakeLexer(self, line_reader): # type: (_Reader) -> Lexer """Helper function. TODO: should we combine the LineLexer and Lexer? And the matcher? """ line_lexer = lexer.LineLexer(match.MATCHER, '', self.arena) return lexer.Lexer(line_lexer, line_reader)
def _MakeLexer(self, line_reader): # type: (_Reader) -> Lexer """Helper function. NOTE: I tried to combine the LineLexer and Lexer, and it didn't perform better. """ line_lexer = lexer.LineLexer(match.MATCHER, '', self.arena) return lexer.Lexer(line_lexer, line_reader)
def run_tests(): # type: () -> None arena = alloc.Arena() line_lexer = lexer.LineLexer('echo hi', arena) # Works! tok = line_lexer.Read(lex_mode_e.ShCommand) log("tok.val = '%s'", tok.val) tok = line_lexer.Read(lex_mode_e.ShCommand) log("tok.val = '%s'", tok.val) tok = line_lexer.Read(lex_mode_e.ShCommand) log("tok.val = '%s'", tok.val)
def ParseTeaModule(self, line_reader): # type: (_Reader) -> None """ An entire .tea file """ line_lexer = lexer.LineLexer('', self.arena) lx = lexer.Lexer(line_lexer, line_reader) pnode, last_token = self.e_parser.Parse(lx, grammar_nt.tea_module) if 1: self.p_printer.Print(pnode) #out = command.Use() # TODO: make a node #self.tr.TeaModule(pnode, out) return None
def ParseDemo(oil_grammar): # type: (Grammar) -> None arena = alloc.Arena() arena.PushSource(source__Stdin('')) parse_ctx = None # type: ParseContext e_parser = expr_parse.ExprParser(parse_ctx, oil_grammar) line_lexer = lexer.LineLexer('', arena) line_reader = reader.StringLineReader('1 + 2*3', arena) lex = lexer.Lexer(line_lexer, line_reader) try: pnode, _ = e_parser.Parse(lex, arith_nt.arith_expr) except error.Parse as e: #ui.PrettyPrintError(e, arena) log("Parse Error (TODO: print it)") return # TODO: Fill this in. Oil uses parse_lib.MakeGrammarNames() # # terminals: _Id_str? Doesn't work in mycpp # nonterminals: gr.number2symbol. Is this ever used at runtime? # # Dict[int,str] should really be a List[str] then? if 0: names = {} # type: Dict[int, str] printer = expr_parse.ParseTreePrinter(names) printer.Print(pnode) # NOTE: Could also transform # This only works for Oil if 0: tr = expr_to_ast.Transformer(oil_grammar) node = tr.Expr(pnode) assert node is not None tree = node.AbbreviatedTree() fmt.PrintTree(tree, mylib.Stdout())
def ParseDemo(oil_grammar): # type: (Grammar) -> None arena = alloc.Arena() arena.PushSource(source__Stdin('')) parse_ctx = None # type: ParseContext e_parser = expr_parse.ExprParser(parse_ctx, oil_grammar) line_lexer = lexer.LineLexer('', arena) line_reader = reader.StringLineReader('1 + 2*3', arena) lex = lexer.Lexer(line_lexer, line_reader) try: pnode, _ = e_parser.Parse(lex, arith_nt.arith_expr) except util.ParseError as e: #ui.PrettyPrintError(e, arena) print(e) return # TODO: Fill this in. Oil uses parse_lib.MakeGrammarNames() names = {} # type: Dict[int, str] printer = expr_parse.ParseTreePrinter(names) printer.Print(pnode)
def InitLexer(s, arena): """For tests only.""" line_lexer = lexer.LineLexer('', arena) line_reader = reader.StringLineReader(s, arena) lx = lexer.Lexer(line_lexer, line_reader) return line_reader, lx
def MakeOilLexer(code_str, arena): arena.PushSource(source.MainFile('pgen2_main')) line_reader = reader.StringLineReader(code_str, arena) line_lexer = lexer.LineLexer('', arena) lex = lexer.Lexer(line_lexer, line_reader) return lex