private void Fun(string pat, string ast_string) { var ast_stream = CharStreams.fromstring(ast_string); var ast_lexer = new AstLexer(ast_stream); var ast_tokens = new CommonTokenStream(ast_lexer); var ast_parser = new AstParserParser(ast_tokens); ast_parser.BuildParseTree = true; var listener = new ErrorListener <IToken>(); ast_parser.AddErrorListener(listener); IParseTree ast_tree = ast_parser.ast(); if (listener.had_error) { throw new Exception(); } _ast = ast_tree; var lexer = new AstLexer(null); var parser = new AstParserParser(new CommonTokenStream(lexer)); ParseTreePatternMatcher ptpm = new ParseTreePatternMatcher(lexer, parser); var re = ptpm.Compile(pat, AstParserParser.RULE_node); var c = ast_tree.GetChild(0); var b = re.Matches(c); System.Console.WriteLine(b); }
/// <summary> /// The same as /// <see cref="CompileParseTreePattern(string, int)"/> /// but specify a /// <see cref="Lexer"/> /// rather than trying to deduce it from this parser. /// </summary> public virtual ParseTreePattern CompileParseTreePattern(string pattern, int patternRuleIndex, Lexer lexer) { ParseTreePatternMatcher m = new ParseTreePatternMatcher(lexer, this); return(m.Compile(pattern, patternRuleIndex)); }