Exemplo n.º 1
0
        public virtual void BuildLexerRuleActions(Lexer lexer, Rule r)
        {
            if (r.actions.Count == 0)
            {
                return;
            }

            CodeGenerator      gen     = @delegate.GetGenerator();
            Grammar            g       = @delegate.GetGrammar();
            string             ctxType = @delegate.GetTarget().GetRuleFunctionContextStructName(r);
            RuleActionFunction raf;

            if (!lexer.actionFuncs.TryGetValue(r, out raf) || raf == null)
            {
                raf = new RuleActionFunction(@delegate, r, ctxType);
            }

            foreach (ActionAST a in r.actions)
            {
                if (a is PredAST)
                {
                    PredAST             p = (PredAST)a;
                    RuleSempredFunction rsf;
                    if (!lexer.sempredFuncs.TryGetValue(r, out rsf) || rsf == null)
                    {
                        rsf = new RuleSempredFunction(@delegate, r, ctxType);
                        lexer.sempredFuncs[r] = rsf;
                    }
                    rsf.actions[g.sempreds[p]] = new Action(@delegate, p);
                }
                else if (a.Type == ANTLRParser.ACTION)
                {
                    raf.actions[g.lexerActions[a]] = new Action(@delegate, a);
                }
            }

            if (raf.actions.Count > 0 && !lexer.actionFuncs.ContainsKey(r))
            {
                // only add to lexer if the function actually contains actions
                lexer.actionFuncs[r] = raf;
            }
        }
Exemplo n.º 2
0
        /** Create RuleFunction per rule and update semantic predicates, actions of parser
         *  output object with stuff found in r.
         */
        public virtual void BuildRuleFunction(Parser parser, Rule r)
        {
            RuleFunction function = Rule(r);

            parser.funcs.Add(function);
            PushCurrentRule(function);
            function.FillNamedActions(@delegate, r);

            if (r is LeftRecursiveRule)
            {
                BuildLeftRecursiveRuleFunction((LeftRecursiveRule)r,
                                               (LeftRecursiveRuleFunction)function);
            }
            else
            {
                BuildNormalRuleFunction(r, function);
            }

            Grammar g = GetGrammar();

            foreach (ActionAST a in r.actions)
            {
                if (a is PredAST)
                {
                    PredAST             p = (PredAST)a;
                    RuleSempredFunction rsf;
                    if (!parser.sempredFuncs.TryGetValue(r, out rsf) || rsf == null)
                    {
                        rsf = new RuleSempredFunction(@delegate, r, function.ctxType);
                        parser.sempredFuncs[r] = rsf;
                    }
                    rsf.actions[g.sempreds[p]] = new Action(@delegate, p);
                }
            }

            PopCurrentRule();
        }