Antlr4.Analysis.LeftRecursiveRuleAnalyzer.HasImmediateRecursiveRuleRefs C# (CSharp) Method

HasImmediateRecursiveRuleRefs() public static method

public static HasImmediateRecursiveRuleRefs ( GrammarAST t, string ruleName ) : bool
t Antlr4.Tool.Ast.GrammarAST
ruleName string
return bool
        public static bool HasImmediateRecursiveRuleRefs(GrammarAST t, string ruleName)
        {
            if (t == null)
                return false;
            GrammarAST blk = (GrammarAST)t.GetFirstChildWithType(BLOCK);
            if (blk == null)
                return false;
            int n = blk.Children.Count;
            for (int i = 0; i < n; i++)
            {
                GrammarAST alt = (GrammarAST)blk.Children[i];
                ITree first = alt.GetChild(0);
                if (first == null)
                    continue;
                if (first.Type == ELEMENT_OPTIONS)
                {
                    first = alt.GetChild(1);
                    if (first == null)
                    {
                        continue;
                    }
                }
                if (first.Type == RULE_REF && first.Text.Equals(ruleName))
                    return true;
                ITree rref = first.GetChild(1);
                if (rref != null && rref.Type == RULE_REF && rref.Text.Equals(ruleName))
                    return true;
            }
            return false;
        }

Usage Example

        public virtual void TranslateLeftRecursiveRules()
        {
            string language = g.GetOptionString("language");
            // translate all recursive rules
            IList <string> leftRecursiveRuleNames = new List <string>();

            foreach (Rule r in rules)
            {
                if (!Grammar.IsTokenName(r.name))
                {
                    if (LeftRecursiveRuleAnalyzer.HasImmediateRecursiveRuleRefs(r.ast, r.name))
                    {
                        bool fitsPattern = TranslateLeftRecursiveRule(ast, (LeftRecursiveRule)r, language);
                        if (fitsPattern)
                        {
                            leftRecursiveRuleNames.Add(r.name);
                        }
                        else
                        {
                            // Suppressed since this build has secondary support for left recursive rules that don't
                            // match the patterns for precedence rules.

                            // better given an error that non-conforming left-recursion exists
                            //tool.errMgr.grammarError(ErrorType.NONCONFORMING_LR_RULE, g.fileName, ((GrammarAST)r.ast.GetChild(0)).Token, r.name);
                        }
                    }
                }
            }

            // update all refs to recursive rules to have [0] argument
            foreach (GrammarAST r in ast.GetNodesWithType(ANTLRParser.RULE_REF))
            {
                if (r.Parent.Type == ANTLRParser.RULE)
                {
                    continue; // must be rule def
                }
                if (((GrammarASTWithOptions)r).GetOptionString(PRECEDENCE_OPTION_NAME) != null)
                {
                    continue; // already has arg; must be in rewritten rule
                }
                if (leftRecursiveRuleNames.Contains(r.Text))
                {
                    // found ref to recursive rule not already rewritten with arg
                    ((GrammarASTWithOptions)r).SetOption(PRECEDENCE_OPTION_NAME, (GrammarAST) new GrammarASTAdaptor().Create(ANTLRParser.INT, "0"));
                }
            }
        }