Inheritance: OutputModelObject
Ejemplo n.º 1
0
 protected SrcOp(OutputModelFactory factory, GrammarAST ast)
     : base(factory, ast)
 {
     if (ast != null)
         uniqueID = ast.Token.TokenIndex;
     enclosingBlock = factory.GetCurrentBlock();
     enclosingRuleRunction = factory.GetCurrentRuleFunction();
 }
Ejemplo n.º 2
0
 protected SrcOp(OutputModelFactory factory, GrammarAST ast)
     : base(factory, ast)
 {
     if (ast != null)
     {
         uniqueID = ast.Token.TokenIndex;
     }
     enclosingBlock        = factory.GetCurrentBlock();
     enclosingRuleRunction = factory.GetCurrentRuleFunction();
 }
Ejemplo n.º 3
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);
            }
        }
        public override void RuleRef(GrammarAST @ref, ActionAST arg)
        {
            if (@ref is GrammarASTWithOptions)
            {
                GrammarASTWithOptions grammarASTWithOptions = (GrammarASTWithOptions)@ref;
                if (bool.Parse(grammarASTWithOptions.GetOptionString(LeftFactoringRuleTransformer.SUPPRESS_ACCESSOR) ?? "false"))
                {
                    return;
                }
            }

            frequencies.Peek().Add(RuleFunction.GetLabelName(grammar, @ref));
            minFrequencies.Peek().Add(RuleFunction.GetLabelName(grammar, @ref));
        }
Ejemplo n.º 5
0
        public Action(OutputModelFactory factory, ActionAST ast)
            : base(factory, ast)
        {
            RuleFunction rf = factory.GetCurrentRuleFunction();

            if (ast != null)
            {
                chunks = ActionTranslator.TranslateAction(factory, rf, ast.Token, ast);
            }
            else
            {
                chunks = new List <ActionChunk>();
            }
            //System.out.println("actions="+chunks);
        }
Ejemplo n.º 6
0
        public Action(OutputModelFactory factory, StructDecl ctx, string action)
            : base(factory, null)
        {
            ActionAST    ast = new ActionAST(new CommonToken(ANTLRParser.ACTION, action));
            RuleFunction rf  = factory.GetCurrentRuleFunction();

            if (rf != null)
            { // we can translate
                ast.resolver = rf.rule;
                chunks       = ActionTranslator.TranslateActionChunk(factory, rf, action, ast);
            }
            else
            {
                chunks = new List <ActionChunk>();
                chunks.Add(new ActionText(ctx, action));
            }
        }
Ejemplo n.º 7
0
        public override IList<SrcOp> RulePostamble(RuleFunction function, Rule r)
        {
            if (r.namedActions.ContainsKey("after") || r.namedActions.ContainsKey("finally"))
            {
                // See OutputModelController.buildLeftRecursiveRuleFunction
                // and Parser.exitRule for other places which set stop.
                CodeGenerator gen = GetGenerator();
                TemplateGroup codegenTemplates = gen.GetTemplates();
                Template setStopTokenAST = codegenTemplates.GetInstanceOf("recRuleSetStopToken");
                Action setStopTokenAction = new Action(this, function.ruleCtx, setStopTokenAST);
                IList<SrcOp> ops = new List<SrcOp>(1);
                ops.Add(setStopTokenAction);
                return ops;
            }

            return base.RulePostamble(function, r);
        }
Ejemplo n.º 8
0
        public static IList<ActionChunk> TranslateAction(OutputModelFactory factory,
                                                        RuleFunction rf,
                                                        IToken tokenWithinAction,
                                                        ActionAST node)
        {
            string action = tokenWithinAction.Text;
            if (action != null && action.Length > 0 && action[0] == '{')
            {
                int firstCurly = action.IndexOf('{');
                int lastCurly = action.LastIndexOf('}');
                if (firstCurly >= 0 && lastCurly >= 0)
                {
                    action = action.Substring(firstCurly + 1, lastCurly - firstCurly - 1); // trim {...}
                }
            }

            return TranslateActionChunk(factory, rf, action, node);
        }
Ejemplo n.º 9
0
 public override RuleFunction Rule(Rule r)
 {
     if (r is LeftRecursiveRule)
     {
         return new LeftRecursiveRuleFunction(this, (LeftRecursiveRule)r);
     }
     else if (r.name.Contains(ATNSimulator.RuleLfVariantMarker))
     {
         return new LeftFactoredRuleFunction(this, r);
     }
     else if (r.name.Contains(ATNSimulator.RuleNolfVariantMarker))
     {
         return new LeftUnfactoredRuleFunction(this, r);
     }
     else
     {
         RuleFunction rf = new RuleFunction(this, r);
         return rf;
     }
 }
Ejemplo n.º 10
0
        public SemPred(OutputModelFactory factory, [NotNull] ActionAST ast)
            : base(factory, ast)
        {
            Debug.Assert(ast.atnState != null &&
                         ast.atnState.NumberOfTransitions == 1 &&
                         ast.atnState.Transition(0) is AbstractPredicateTransition);

            GrammarAST failNode = ast.GetOptionAST("fail");

            predicate = ast.Text;
            if (predicate.StartsWith("{") && predicate.EndsWith("}?"))
            {
                predicate = predicate.Substring(1, predicate.Length - 3);
            }
            predicate = factory.GetTarget().GetTargetStringLiteralFromString(predicate);

            if (failNode == null)
            {
                return;
            }

            if (failNode is ActionAST)
            {
                ActionAST    failActionNode = (ActionAST)failNode;
                RuleFunction rf             = factory.GetCurrentRuleFunction();
                failChunks = ActionTranslator.TranslateAction(factory, rf,
                                                              failActionNode.Token,
                                                              failActionNode);
            }
            else
            {
                msg = factory.GetTarget().GetTargetStringLiteralFromANTLRStringLiteral(factory.GetGenerator(),
                                                                                       failNode.Text,
                                                                                       true);
            }
        }
Ejemplo n.º 11
0
        /** If we know which actual function, we can provide the actual ctx type.
         *  This will contain implicit labels etc...  From outside, though, we
         *  see only ParserRuleContext unless there are externally visible stuff
         *  like args, locals, explicit labels, etc...
         */
        public virtual string GetRuleFunctionContextStructName(RuleFunction function)
        {
            Rule r = function.rule;
            if (r.g.IsLexer())
            {
                return GetTemplates().GetInstanceOf("LexerRuleContext").Render();
            }

            string baseName = r.GetBaseContext();
            return Utils.Capitalize(baseName) + GetTemplates().GetInstanceOf("RuleContextNameSuffix").Render();
        }
Ejemplo n.º 12
0
 public virtual void PushCurrentRule(RuleFunction r)
 {
     currentRule.Push(r);
 }
Ejemplo n.º 13
0
        public virtual IList<SrcOp> RulePostamble(RuleFunction function, Rule r)
        {
            IList<SrcOp> ops = @delegate.RulePostamble(function, r);
            foreach (CodeGeneratorExtension ext in extensions)
                ops = ext.RulePostamble(ops);

            return ops;
        }
Ejemplo n.º 14
0
        public virtual void BuildNormalRuleFunction(Rule r, RuleFunction function)
        {
            CodeGenerator gen = @delegate.GetGenerator();
            // TRIGGER factory functions for rule alts, elements
            GrammarASTAdaptor adaptor = new GrammarASTAdaptor(r.ast.Token.InputStream);
            GrammarAST blk = (GrammarAST)r.ast.GetFirstChildWithType(ANTLRParser.BLOCK);
            CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, blk);
            walker = new SourceGenTriggers(nodes, this);
            try
            {
                // walk AST of rule alts/elements
                function.code = DefaultOutputModelFactory.List(walker.block(null, null));
                function.hasLookaheadBlock = walker.hasLookaheadBlock;
            }
            catch (Antlr.Runtime.RecognitionException e)
            {
                Console.Error.WriteLine(e.Message);
                Console.Error.WriteLine(e.StackTrace);
            }

            function.ctxType = @delegate.GetTarget().GetRuleFunctionContextStructName(function);

            function.postamble = RulePostamble(function, r);
        }
Ejemplo n.º 15
0
 public virtual RuleFunction Rule(RuleFunction rf)
 {
     return rf;
 }
Ejemplo n.º 16
0
 public virtual IList<SrcOp> RulePostamble(RuleFunction function, Rule r)
 {
     return null;
 }
Ejemplo n.º 17
0
 public static IList<ActionChunk> TranslateActionChunk(OutputModelFactory factory,
                                                      RuleFunction rf,
                                                      string action,
                                                      ActionAST node)
 {
     IToken tokenWithinAction = node.Token;
     ActionTranslator translator = new ActionTranslator(factory, node);
     translator.rf = rf;
     factory.GetGrammar().tool.Log("action-translator", "translate " + action);
     string altLabel = node.GetAltLabel();
     if (rf != null)
     {
         translator.nodeContext = rf.ruleCtx;
         if (altLabel != null)
         {
             AltLabelStructDecl decl;
             rf.altLabelCtxs.TryGetValue(altLabel, out decl);
             translator.nodeContext = decl;
         }
     }
     ANTLRStringStream @in = new ANTLRStringStream(action);
     @in.Line = tokenWithinAction.Line;
     @in.CharPositionInLine = tokenWithinAction.CharPositionInLine;
     ActionSplitter trigger = new ActionSplitter(@in, translator);
     // forces eval, triggers listener methods
     trigger.GetActionTokens();
     return translator.chunks;
 }