Exemple #1
0
        /**
         * Important enough to avoid multiple definitions that we do very early,
         * right after AST construction. Also check for undefined rules in
         * parser/lexer to avoid exceptions later. Return true if we find multiple
         * definitions of the same rule or a reference to an undefined rule or
         * parser rule ref in lexer rule.
         */
        public virtual bool CheckForRuleIssues(Grammar g)
        {
            // check for redefined rules
            GrammarAST         RULES = (GrammarAST)g.ast.GetFirstChildWithType(ANTLRParser.RULES);
            IList <GrammarAST> rules = new List <GrammarAST>(RULES.GetAllChildrenWithType(ANTLRParser.RULE));

            foreach (GrammarAST mode in g.ast.GetAllChildrenWithType(ANTLRParser.MODE))
            {
                foreach (GrammarAST child in mode.GetAllChildrenWithType(ANTLRParser.RULE))
                {
                    rules.Add(child);
                }
            }

            bool redefinition = false;
            IDictionary <string, RuleAST> ruleToAST = new Dictionary <string, RuleAST>();

            foreach (GrammarAST r in rules)
            {
                RuleAST    ruleAST  = (RuleAST)r;
                GrammarAST ID       = (GrammarAST)ruleAST.GetChild(0);
                string     ruleName = ID.Text;
                RuleAST    prev;
                if (ruleToAST.TryGetValue(ruleName, out prev) && prev != null)
                {
                    GrammarAST prevChild = (GrammarAST)prev.GetChild(0);
                    g.tool.errMgr.GrammarError(ErrorType.RULE_REDEFINITION,
                                               g.fileName,
                                               ID.Token,
                                               ruleName,
                                               prevChild.Token.Line);
                    redefinition = true;
                    continue;
                }
                ruleToAST[ruleName] = ruleAST;
            }

            // check for undefined rules
            UndefChecker chk = new UndefChecker(this, g, ruleToAST);

            chk.VisitGrammar(g.ast);

            return(redefinition || chk.badref);
        }
Exemple #2
0
        /**
         * Important enough to avoid multiple definitions that we do very early,
         * right after AST construction. Also check for undefined rules in
         * parser/lexer to avoid exceptions later. Return true if we find multiple
         * definitions of the same rule or a reference to an undefined rule or
         * parser rule ref in lexer rule.
         */
        public virtual bool CheckForRuleIssues(Grammar g)
        {
            // check for redefined rules
            GrammarAST RULES = (GrammarAST)g.ast.GetFirstChildWithType(ANTLRParser.RULES);
            IList<GrammarAST> rules = new List<GrammarAST>(RULES.GetAllChildrenWithType(ANTLRParser.RULE));
            foreach (GrammarAST mode in g.ast.GetAllChildrenWithType(ANTLRParser.MODE))
            {
                foreach (GrammarAST child in mode.GetAllChildrenWithType(ANTLRParser.RULE))
                    rules.Add(child);
            }

            bool redefinition = false;
            IDictionary<string, RuleAST> ruleToAST = new Dictionary<string, RuleAST>();
            foreach (GrammarAST r in rules)
            {
                RuleAST ruleAST = (RuleAST)r;
                GrammarAST ID = (GrammarAST)ruleAST.GetChild(0);
                string ruleName = ID.Text;
                RuleAST prev;
                if (ruleToAST.TryGetValue(ruleName, out prev) && prev != null)
                {
                    GrammarAST prevChild = (GrammarAST)prev.GetChild(0);
                    g.tool.errMgr.GrammarError(ErrorType.RULE_REDEFINITION,
                                               g.fileName,
                                               ID.Token,
                                               ruleName,
                                               prevChild.Token.Line);
                    redefinition = true;
                    continue;
                }
                ruleToAST[ruleName] = ruleAST;
            }

            // check for undefined rules
            UndefChecker chk = new UndefChecker(this, g, ruleToAST);
            chk.VisitGrammar(g.ast);

            return redefinition || chk.badref;
        }