Example #1
0
        public RuleFunction(OutputModelFactory factory, Rule r)
            : base(factory)
        {
            this.name = r.name;
            this.rule = r;
            if (r.modifiers != null && r.modifiers.Count > 0)
            {
                this.modifiers = new List <string>();
                foreach (GrammarAST t in r.modifiers)
                {
                    modifiers.Add(t.Text);
                }
            }
            modifiers = Utils.NodesToStrings(r.modifiers);

            index = r.index;
            int lfIndex = name.IndexOf(ATNSimulator.RuleVariantDelimiter);

            if (lfIndex >= 0)
            {
                variantOf = name.Substring(0, lfIndex);
            }

            if (r.name.Equals(r.GetBaseContext()))
            {
                ruleCtx = new StructDecl(factory, r);
                AddContextGetters(factory, r.g.contextASTs[r.name]);

                if (r.args != null)
                {
                    ICollection <Attribute> decls = r.args.attributes.Values;
                    if (decls.Count > 0)
                    {
                        args = new List <AttributeDecl>();
                        ruleCtx.AddDecls(decls);
                        foreach (Attribute a in decls)
                        {
                            args.Add(new AttributeDecl(factory, a));
                        }
                        ruleCtx.ctorAttrs = args;
                    }
                }
                if (r.retvals != null)
                {
                    ruleCtx.AddDecls(r.retvals.attributes.Values);
                }
                if (r.locals != null)
                {
                    ruleCtx.AddDecls(r.locals.attributes.Values);
                }
            }
            else
            {
                if (r.args != null || r.retvals != null || r.locals != null)
                {
                    throw new System.NotSupportedException("customized fields are not yet supported for customized context objects");
                }
            }

            ruleLabels  = r.GetElementLabelNames();
            tokenLabels = r.GetTokenRefs();
            if (r.exceptions != null)
            {
                exceptions = new List <ExceptionClause>();
                foreach (GrammarAST e in r.exceptions)
                {
                    ActionAST catchArg    = (ActionAST)e.GetChild(0);
                    ActionAST catchAction = (ActionAST)e.GetChild(1);
                    exceptions.Add(new ExceptionClause(factory, catchArg, catchAction));
                }
            }

            startState = factory.GetGrammar().atn.ruleToStartState[r.index];
        }