Antlr4.Runtime.Tree.Pattern.ParseTreePatternMatcher.Compile C# (CSharp) Method

Compile() public method

For repeated use of a tree pattern, compile it to a ParseTreePattern using this method.
public Compile ( string pattern, int patternRuleIndex ) : ParseTreePattern
pattern string
patternRuleIndex int
return ParseTreePattern
        public virtual ParseTreePattern Compile(string pattern, int patternRuleIndex)
        {
            IList<IToken> tokenList = Tokenize(pattern);
            ListTokenSource tokenSrc = new ListTokenSource(tokenList);
            CommonTokenStream tokens = new CommonTokenStream(tokenSrc);
            ParserInterpreter parserInterp = new ParserInterpreter(parser.GrammarFileName, parser.Vocabulary, Arrays.AsList(parser.RuleNames), parser.GetATNWithBypassAlts(), tokens);
            IParseTree tree = null;
            try
            {
                parserInterp.ErrorHandler = new BailErrorStrategy();
                tree = parserInterp.Parse(patternRuleIndex);
            }
            catch (ParseCanceledException e)
            {
                //			System.out.println("pattern tree = "+tree.toStringTree(parserInterp));
                throw (RecognitionException)e.InnerException;
            }
            catch (RecognitionException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new ParseTreePatternMatcher.CannotInvokeStartRule(e);
            }
            // Make sure tree pattern compilation checks for a complete parse
            if (tokens.LA(1) != TokenConstants.EOF)
            {
                throw new ParseTreePatternMatcher.StartRuleDoesNotConsumeFullPattern();
            }
            return new ParseTreePattern(this, pattern, patternRuleIndex, tree);
        }