Пример #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);
            }
        }
Пример #2
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);
        }
Пример #3
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);
            }
        }