Antlr4.Tool.Ast.GrammarAST.GetAltLabel C# (CSharp) Method

GetAltLabel() public method

public GetAltLabel ( ) : string
return string
        public virtual string GetAltLabel()
        {
            IList<ITree> ancestors = this.GetAncestors();
            if (ancestors == null)
                return null;
            for (int i = ancestors.Count - 1; i >= 0; i--)
            {
                GrammarAST p = (GrammarAST)ancestors[i];
                if (p.Type == ANTLRParser.ALT)
                {
                    AltAST a = (AltAST)p;
                    if (a.altLabel != null)
                        return a.altLabel.Text;
                    if (a.leftRecursiveAltInfo != null)
                    {
                        return a.leftRecursiveAltInfo.altLabel;
                    }
                }
            }
            return null;
        }

Usage Example

Example #1
0
        public InvokeRule(ParserFactory factory, GrammarAST ast, GrammarAST labelAST)
            : base(factory, ast)
        {
            if (ast.atnState != null)
            {
                RuleTransition ruleTrans = (RuleTransition)ast.atnState.Transition(0);
                stateNumber = ast.atnState.stateNumber;
            }

            this.name = ast.Text;
            Rule r = factory.GetGrammar().GetRule(name);
            ctxName = factory.GetTarget().GetRuleFunctionContextStructName(r);

            // TODO: move to factory
            RuleFunction rf = factory.GetCurrentRuleFunction();
            if (labelAST != null)
            {
                // for x=r, define <rule-context-type> x and list_x
                string label = labelAST.Text;
                if (labelAST.Parent.Type == ANTLRParser.PLUS_ASSIGN)
                {
                    factory.DefineImplicitLabel(ast, this);
                    string listLabel = factory.GetTarget().GetListLabel(label);
                    RuleContextListDecl l = new RuleContextListDecl(factory, listLabel, ctxName);
                    rf.AddContextDecl(ast.GetAltLabel(), l);
                }
                else
                {
                    RuleContextDecl d = new RuleContextDecl(factory, label, ctxName);
                    labels.Add(d);
                    rf.AddContextDecl(ast.GetAltLabel(), d);
                }
            }

            ActionAST arg = (ActionAST)ast.GetFirstChildWithType(ANTLRParser.ARG_ACTION);
            if (arg != null)
            {
                argExprsChunks = ActionTranslator.TranslateAction(factory, rf, arg.Token, arg);
            }

            // If action refs rule as rulename not label, we need to define implicit label
            if (factory.GetCurrentOuterMostAlt().ruleRefsInActions.ContainsKey(ast.Text))
            {
                string label = factory.GetTarget().GetImplicitRuleLabel(ast.Text);
                RuleContextDecl d = new RuleContextDecl(factory, label, ctxName);
                labels.Add(d);
                rf.AddContextDecl(ast.GetAltLabel(), d);
            }
        }
All Usage Examples Of Antlr4.Tool.Ast.GrammarAST::GetAltLabel