Inheritance: AttributeResolver
Esempio n. 1
0
 public AltLabelStructDecl(OutputModelFactory factory, Rule r, string label)
     : base(factory, r)
 {
     this.name = // override name set in super to the label ctx
         factory.GetTarget().GetAltLabelContextStructName(label);
     derivedFromName = label;
 }
Esempio n. 2
0
 public StructDecl(OutputModelFactory factory, Rule r)
     : base(factory, factory.GetTarget().GetRuleFunctionContextStructName(r))
 {
     AddDispatchMethods(r);
     derivedFromName = r.name;
     provideCopyFrom = r.HasAltSpecificContexts();
 }
Esempio n. 3
0
 public RuleActionFunction(OutputModelFactory factory, Rule r, string ctxType)
     : base(factory)
 {
     name = r.name;
     ruleIndex = r.index;
     this.ctxType = ctxType;
 }
Esempio n. 4
0
 public ActionSniffer(Grammar g, Rule r, Alternative alt, ActionAST node, IToken actionToken)
 {
     this.g = g;
     this.r = r;
     this.alt = alt;
     this.node = node;
     this.actionToken = actionToken;
     this.errMgr = g.tool.errMgr;
 }
Esempio n. 5
0
 public override void DiscoverRule(RuleAST rule, GrammarAST ID,
                          IList<GrammarAST> modifiers, ActionAST arg,
                          ActionAST returns, GrammarAST thrws,
                          GrammarAST options, ActionAST locals,
                          IList<GrammarAST> actions,
                          GrammarAST block)
 {
     currentRule = g.GetRule(ID.Text);
 }
Esempio n. 6
0
        public override bool UndefineRule(Rule r)
        {
            if (!base.UndefineRule(r))
            {
                return false;
            }

            bool removed = modes[r.mode].Remove(r);
            Debug.Assert(removed);
            return true;
        }
Esempio n. 7
0
        public override bool DefineRule(Rule r)
        {
            if (!base.DefineRule(r))
            {
                return false;
            }

            if (modes == null)
                modes = new Runtime.Misc.MultiMap<string, Rule>();
            modes.Map(r.mode, r);
            return true;
        }
Esempio n. 8
0
 public override void AddDispatchMethods(Rule r)
 {
     dispatchMethods = new List<DispatchMethod>();
     if (factory.GetGrammar().tool.gen_listener)
     {
         dispatchMethods.Add(new ListenerDispatchMethod(factory, true));
         dispatchMethods.Add(new ListenerDispatchMethod(factory, false));
     }
     if (factory.GetGrammar().tool.gen_visitor)
     {
         dispatchMethods.Add(new VisitorDispatchMethod(factory));
     }
 }
Esempio n. 9
0
        public override void DiscoverRule(RuleAST rule, GrammarAST ID,
                                 IList<GrammarAST> modifiers, ActionAST arg,
                                 ActionAST returns, GrammarAST thrws,
                                 GrammarAST options, ActionAST locals,
                                 IList<GrammarAST> actions,
                                 GrammarAST block)
        {
            int numAlts = block.ChildCount;
            Rule r;
            if (LeftRecursiveRuleAnalyzer.HasImmediateRecursiveRuleRefs(rule, ID.Text))
            {
                r = new LeftRecursiveRule(g, ID.Text, rule);
            }
            else
            {
                r = new Rule(g, ID.Text, rule, numAlts);
            }
            rules[r.name] = r;

            if (arg != null)
            {
                r.args = ScopeParser.ParseTypedArgList(arg, arg.Text, g);
                r.args.type = AttributeDict.DictType.ARG;
                r.args.ast = arg;
                arg.resolver = r.alt[currentOuterAltNumber];
            }

            if (returns != null)
            {
                r.retvals = ScopeParser.ParseTypedArgList(returns, returns.Text, g);
                r.retvals.type = AttributeDict.DictType.RET;
                r.retvals.ast = returns;
            }

            if (locals != null)
            {
                r.locals = ScopeParser.ParseTypedArgList(locals, locals.Text, g);
                r.locals.type = AttributeDict.DictType.LOCAL;
                r.locals.ast = locals;
            }

            foreach (GrammarAST a in actions)
            {
                // a = ^(AT ID ACTION)
                ActionAST action = (ActionAST)a.GetChild(1);
                r.namedActions[a.GetChild(0).Text] = action;
                action.resolver = r;
            }
        }
Esempio n. 10
0
 public virtual void AddDispatchMethods(Rule r)
 {
     dispatchMethods = new List<DispatchMethod>();
     if (!r.HasAltSpecificContexts())
     {
         // no enter/exit for this ruleContext if rule has labels
         if (factory.GetGrammar().tool.gen_listener)
         {
             dispatchMethods.Add(new ListenerDispatchMethod(factory, true));
             dispatchMethods.Add(new ListenerDispatchMethod(factory, false));
         }
         if (factory.GetGrammar().tool.gen_visitor)
         {
             dispatchMethods.Add(new VisitorDispatchMethod(factory));
         }
     }
 }
        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);
        }
Esempio n. 12
0
        /** From state s, look for any transition to a rule that is currently
         *  being traced.  When tracing r, visitedPerRuleCheck has r
         *  initially.  If you reach a rule stop state, return but notify the
         *  invoking rule that the called rule is nullable. This implies that
         *  invoking rule must look at follow transition for that invoking state.
         *
         *  The visitedStates tracks visited states within a single rule so
         *  we can avoid epsilon-loop-induced infinite recursion here.  Keep
         *  filling the cycles in listOfRecursiveCycles and also, as a
         *  side-effect, set leftRecursiveRules.
         */
        public virtual bool Check(Rule enclosingRule, ATNState s, ISet<ATNState> visitedStates)
        {
            if (s is RuleStopState)
                return true;
            if (visitedStates.Contains(s))
                return false;
            visitedStates.Add(s);

            //System.out.println("visit "+s);
            int n = s.NumberOfTransitions;
            bool stateReachesStopState = false;
            for (int i = 0; i < n; i++)
            {
                Transition t = s.Transition(i);
                if (t is RuleTransition)
                {
                    RuleTransition rt = (RuleTransition)t;
                    Rule r = g.GetRule(rt.ruleIndex);
                    if (rulesVisitedPerRuleCheck.Contains((RuleStartState)t.target))
                    {
                        AddRulesToCycle(enclosingRule, r);
                    }
                    else
                    {
                        // must visit if not already visited; mark target, pop when done
                        rulesVisitedPerRuleCheck.Add((RuleStartState)t.target);
                        // send new visitedStates set per rule invocation
                        bool nullable = Check(r, t.target, new HashSet<ATNState>());
                        // we're back from visiting that rule
                        rulesVisitedPerRuleCheck.Remove((RuleStartState)t.target);
                        if (nullable)
                        {
                            stateReachesStopState |= Check(enclosingRule, rt.followState, visitedStates);
                        }
                    }
                }
                else if (t.IsEpsilon)
                {
                    stateReachesStopState |= Check(enclosingRule, t.target, visitedStates);
                }
                // else ignore non-epsilon transitions
            }
            return stateReachesStopState;
        }
Esempio n. 13
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;
     }
 }
Esempio n. 14
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];
        }
Esempio n. 15
0
        public virtual void FillNamedActions(OutputModelFactory factory, Rule r)
        {
            if (r.finallyAction != null)
            {
                finallyAction = new Action(factory, r.finallyAction);
            }

            namedActions = new Dictionary<string, Action>();
            foreach (string name in r.namedActions.Keys)
            {
                ActionAST ast;
                r.namedActions.TryGetValue(name, out ast);
                namedActions[name] = new Action(factory, ast);
            }
        }
Esempio n. 16
0
        public virtual string GetRuleFunctionContextStructName(Rule r)
        {
            if (r.g.IsLexer())
            {
                return GetTemplates().GetInstanceOf("LexerRuleContext").Render();
            }

            string baseName = r.GetBaseContext();
            return Utils.Capitalize(baseName) + GetTemplates().GetInstanceOf("RuleContextNameSuffix").Render();
        }
        protected virtual RuleVariants CreateLeftFactoredRuleVariant(Rule rule, string factoredElement)
        {
            RuleAST ast = (RuleAST)rule.ast.DupTree();
            BlockAST block = (BlockAST)ast.GetFirstChildWithType(ANTLRParser.BLOCK);

            RuleAST unfactoredAst = null;
            BlockAST unfactoredBlock = null;

            if (TranslateLeftFactoredDecision(block, factoredElement, true, DecisionFactorMode.FULL_FACTOR, false))
            {
                // all alternatives factored
            }
            else
            {
                ast = (RuleAST)rule.ast.DupTree();
                block = (BlockAST)ast.GetFirstChildWithType(ANTLRParser.BLOCK);
                if (!TranslateLeftFactoredDecision(block, factoredElement, true, DecisionFactorMode.PARTIAL_FACTORED, false))
                {
                    // no left factored alts
                    return RuleVariants.NONE;
                }

                unfactoredAst = (RuleAST)rule.ast.DupTree();
                unfactoredBlock = (BlockAST)unfactoredAst.GetFirstChildWithType(ANTLRParser.BLOCK);
                if (!TranslateLeftFactoredDecision(unfactoredBlock, factoredElement, true, DecisionFactorMode.PARTIAL_UNFACTORED, false))
                {
                    throw new InvalidOperationException("expected unfactored alts for partial factorization");
                }
            }

            /*
             * factored elements
             */
            {
                string variantName = ast.GetChild(0).Text + ATNSimulator.RuleLfVariantMarker + factoredElement;
                ((GrammarAST)ast.GetChild(0)).Token = adaptor.CreateToken(ast.GetChild(0).Type, variantName);
                GrammarAST ruleParent = (GrammarAST)rule.ast.Parent;
                ruleParent.InsertChild(rule.ast.ChildIndex + 1, ast);
                ruleParent.FreshenParentAndChildIndexes(rule.ast.ChildIndex);

                IList<GrammarAST> alts = block.GetAllChildrenWithType(ANTLRParser.ALT);
                Rule variant = new Rule(_g, ast.GetChild(0).Text, ast, alts.Count);
                _g.DefineRule(variant);
                for (int i = 0; i < alts.Count; i++)
                {
                    variant.alt[i + 1].ast = (AltAST)alts[i];
                }
            }

            /*
             * unfactored elements
             */
            if (unfactoredAst != null)
            {
                string variantName = unfactoredAst.GetChild(0).Text + ATNSimulator.RuleNolfVariantMarker + factoredElement;
                ((GrammarAST)unfactoredAst.GetChild(0)).Token = adaptor.CreateToken(unfactoredAst.GetChild(0).Type, variantName);
                GrammarAST ruleParent = (GrammarAST)rule.ast.Parent;
                ruleParent.InsertChild(rule.ast.ChildIndex + 1, unfactoredAst);
                ruleParent.FreshenParentAndChildIndexes(rule.ast.ChildIndex);

                IList<GrammarAST> alts = unfactoredBlock.GetAllChildrenWithType(ANTLRParser.ALT);
                Rule variant = new Rule(_g, unfactoredAst.GetChild(0).Text, unfactoredAst, alts.Count);
                _g.DefineRule(variant);
                for (int i = 0; i < alts.Count; i++)
                {
                    variant.alt[i + 1].ast = (AltAST)alts[i];
                }
            }

            /*
             * result
             */
            return unfactoredAst == null ? RuleVariants.FULLY_FACTORED : RuleVariants.PARTIALLY_FACTORED;
        }
 public LeftUnfactoredRuleFunction(OutputModelFactory factory, Rule r)
     : base(factory, r)
 {
 }
Esempio n. 19
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;
            }
        }
Esempio n. 20
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);
        }
Esempio n. 21
0
        public virtual void CheckForAttributeConflicts(Rule r)
        {
            CheckDeclarationRuleConflicts(r, r.args, nameToRuleMap.Keys, ErrorType.ARG_CONFLICTS_WITH_RULE);
            CheckDeclarationRuleConflicts(r, r.args, tokenIDs, ErrorType.ARG_CONFLICTS_WITH_TOKEN);

            CheckDeclarationRuleConflicts(r, r.retvals, nameToRuleMap.Keys, ErrorType.RETVAL_CONFLICTS_WITH_RULE);
            CheckDeclarationRuleConflicts(r, r.retvals, tokenIDs, ErrorType.RETVAL_CONFLICTS_WITH_TOKEN);

            CheckDeclarationRuleConflicts(r, r.locals, nameToRuleMap.Keys, ErrorType.LOCAL_CONFLICTS_WITH_RULE);
            CheckDeclarationRuleConflicts(r, r.locals, tokenIDs, ErrorType.LOCAL_CONFLICTS_WITH_TOKEN);

            CheckLocalConflictingDeclarations(r, r.retvals, r.args, ErrorType.RETVAL_CONFLICTS_WITH_ARG);
            CheckLocalConflictingDeclarations(r, r.locals, r.args, ErrorType.LOCAL_CONFLICTS_WITH_ARG);
            CheckLocalConflictingDeclarations(r, r.locals, r.retvals, ErrorType.LOCAL_CONFLICTS_WITH_RETVAL);
        }
Esempio n. 22
0
 public virtual RuleFunction Rule(Rule r)
 {
     RuleFunction rf = @delegate.Rule(r);
     foreach (CodeGeneratorExtension ext in extensions)
         rf = ext.Rule(rf);
     return rf;
 }
Esempio n. 23
0
        public virtual void CheckForLabelConflict(Rule r, GrammarAST labelID)
        {
            string name = labelID.Text;
            if (nameToRuleMap.ContainsKey(name))
            {
                ErrorType etype = ErrorType.LABEL_CONFLICTS_WITH_RULE;
                errMgr.GrammarError(etype, g.fileName, labelID.Token, name, r.name);
            }

            if (tokenIDs.Contains(name))
            {
                ErrorType etype = ErrorType.LABEL_CONFLICTS_WITH_TOKEN;
                errMgr.GrammarError(etype, g.fileName, labelID.Token, name, r.name);
            }

            if (r.args != null && r.args.Get(name) != null)
            {
                ErrorType etype = ErrorType.LABEL_CONFLICTS_WITH_ARG;
                errMgr.GrammarError(etype, g.fileName, labelID.Token, name, r.name);
            }

            if (r.retvals != null && r.retvals.Get(name) != null)
            {
                ErrorType etype = ErrorType.LABEL_CONFLICTS_WITH_RETVAL;
                errMgr.GrammarError(etype, g.fileName, labelID.Token, name, r.name);
            }

            if (r.locals != null && r.locals.Get(name) != null)
            {
                ErrorType etype = ErrorType.LABEL_CONFLICTS_WITH_LOCAL;
                errMgr.GrammarError(etype, g.fileName, labelID.Token, name, r.name);
            }
        }
Esempio n. 24
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;
        }
Esempio n. 25
0
 public virtual RuleFunction Rule(Rule r)
 {
     return null;
 }
Esempio n. 26
0
 public RuleSempredFunction(OutputModelFactory factory, Rule r, string ctxType)
     : base(factory, r, ctxType)
 {
 }
Esempio n. 27
0
 /** enclosingRule calls targetRule. Find the cycle containing
  *  the target and add the caller.  Find the cycle containing the caller
  *  and add the target.  If no cycles contain either, then create a new
  *  cycle.
  */
 protected virtual void AddRulesToCycle(Rule enclosingRule, Rule targetRule)
 {
     //System.err.println("left-recursion to "+targetRule.name+" from "+enclosingRule.name);
     bool foundCycle = false;
     foreach (ISet<Rule> rulesInCycle in listOfRecursiveCycles)
     {
         // ensure both rules are in same cycle
         if (rulesInCycle.Contains(targetRule))
         {
             rulesInCycle.Add(enclosingRule);
             foundCycle = true;
         }
         if (rulesInCycle.Contains(enclosingRule))
         {
             rulesInCycle.Add(targetRule);
             foundCycle = true;
         }
     }
     if (!foundCycle)
     {
         ISet<Rule> cycle = new OrderedHashSet<Rule>();
         cycle.Add(targetRule);
         cycle.Add(enclosingRule);
         listOfRecursiveCycles.Add(cycle);
     }
 }
Esempio n. 28
0
 public override void DiscoverLexerRule(RuleAST rule, GrammarAST ID, IList<GrammarAST> modifiers,
                               GrammarAST block)
 {
     currentRule = g.GetRule(ID.Text);
 }
Esempio n. 29
0
 protected virtual void WriteDOTFile(Grammar g, Rule r, string dot)
 {
     WriteDOTFile(g, r.g.name + "." + r.name, dot);
 }
Esempio n. 30
0
 public virtual IList<SrcOp> RulePostamble(RuleFunction function, Rule r)
 {
     return null;
 }
Esempio n. 31
0
 public Alternative(Rule r, int altNum)
 {
     this.rule   = r;
     this.altNum = altNum;
 }