Beispiel #1
0
        public virtual void CheckRuleReference(GrammarAST scopeAST,
                                               GrammarAST refAST,
                                               GrammarAST argsAST,
                                               string currentRuleName)
        {
            Rule r = grammar.GetRule(refAST.Text);

            if (refAST.Type == ANTLRParser.RULE_REF)
            {
                if (argsAST != null)
                {
                    // rule[args]; ref has args
                    if (r != null && r.argActionAST == null)
                    {
                        // but rule def has no args
                        ErrorManager.GrammarError(
                            ErrorManager.MSG_RULE_HAS_NO_ARGS,
                            grammar,
                            argsAST.Token,
                            r.Name);
                    }
                }
                else
                {
                    // rule ref has no args
                    if (r != null && r.argActionAST != null)
                    {
                        // but rule def has args
                        ErrorManager.GrammarError(
                            ErrorManager.MSG_MISSING_RULE_ARGS,
                            grammar,
                            refAST.Token,
                            r.Name);
                    }
                }
            }
            else if (refAST.Type == ANTLRParser.TOKEN_REF)
            {
                if (grammar.type != GrammarType.Lexer)
                {
                    if (argsAST != null)
                    {
                        // args on a token ref not in a lexer rule
                        ErrorManager.GrammarError(
                            ErrorManager.MSG_ARGS_ON_TOKEN_REF,
                            grammar,
                            refAST.Token,
                            refAST.Text);
                    }
                    return; // ignore token refs in nonlexers
                }
                if (argsAST != null)
                {
                    // tokenRef[args]; ref has args
                    if (r != null && r.argActionAST == null)
                    {
                        // but token rule def has no args
                        ErrorManager.GrammarError(
                            ErrorManager.MSG_RULE_HAS_NO_ARGS,
                            grammar,
                            argsAST.Token,
                            r.Name);
                    }
                }
                else
                {
                    // token ref has no args
                    if (r != null && r.argActionAST != null)
                    {
                        // but token rule def has args
                        ErrorManager.GrammarError(
                            ErrorManager.MSG_MISSING_RULE_ARGS,
                            grammar,
                            refAST.Token,
                            r.Name);
                    }
                }
            }
        }
Beispiel #2
0
 /** If ref to undefined rule, give error at first occurrence.
  *
  *  Give error if you cannot find the scope override on a rule reference.
  *
  *  If you ref ID in a combined grammar and don't define ID as a lexer rule
  *  it is an error.
  */
 protected virtual void LookForReferencesToUndefinedSymbols()
 {
     // for each rule ref, ask if there is a rule definition
     foreach (GrammarAST refAST in grammar.ruleRefs)
     {
         IToken tok       = refAST.Token;
         string ruleName  = tok.Text;
         Rule   localRule = grammar.GetLocallyDefinedRule(ruleName);
         Rule   rule      = grammar.GetRule(ruleName);
         if (localRule == null && rule != null)
         { // imported rule?
             grammar.delegatedRuleReferences.Add(rule);
             rule.imported = true;
         }
         if (rule == null && grammar.GetTokenType(ruleName) != Label.EOF)
         {
             ErrorManager.GrammarError(ErrorManager.MSG_UNDEFINED_RULE_REF,
                                       grammar,
                                       tok,
                                       ruleName);
         }
     }
     if (grammar.type == GrammarType.Combined)
     {
         // if we're a combined grammar, we know which token IDs have no
         // associated lexer rule.
         foreach (IToken tok in grammar.tokenIDRefs)
         {
             string tokenID = tok.Text;
             if (!grammar.composite.lexerRules.Contains(tokenID) &&
                 grammar.GetTokenType(tokenID) != Label.EOF)
             {
                 ErrorManager.GrammarWarning(ErrorManager.MSG_NO_TOKEN_DEFINITION,
                                             grammar,
                                             tok,
                                             tokenID);
             }
         }
     }
     // check scopes and scoped rule refs
     foreach (GrammarAST scopeAST in grammar.scopedRuleRefs)
     {
         // ^(DOT ID atom)
         Grammar    scopeG   = grammar.composite.GetGrammar(scopeAST.Text);
         GrammarAST refAST   = (GrammarAST)scopeAST.GetChild(1);
         string     ruleName = refAST.Text;
         if (scopeG == null)
         {
             ErrorManager.GrammarError(ErrorManager.MSG_NO_SUCH_GRAMMAR_SCOPE,
                                       grammar,
                                       scopeAST.Token,
                                       scopeAST.Text,
                                       ruleName);
         }
         else
         {
             Rule rule = grammar.GetRule(scopeG.name, ruleName);
             if (rule == null)
             {
                 ErrorManager.GrammarError(ErrorManager.MSG_NO_SUCH_RULE_IN_SCOPE,
                                           grammar,
                                           scopeAST.Token,
                                           scopeAST.Text,
                                           ruleName);
             }
         }
     }
 }
Beispiel #3
0
 public void CompiletimeError(TemplateMessage msg)
 {
     ErrorManager.Error(ErrorManager.MSG_INTERNAL_ERROR, msg.ToString(), msg.Cause);
 }
Beispiel #4
0
 public void InternalError(TemplateMessage msg)
 {
     ErrorManager.Error(ErrorManager.MSG_INTERNAL_ERROR, msg.ToString(), msg.Cause);
 }
Beispiel #5
0
        /** For decls like "String foo" or "char *foo32[3]" compute the ID
         *  and type declarations.  Also handle "int x=3" and 'T t = new T("foo")'
         *  but if the separator is ',' you cannot use ',' in the initvalue.
         *  AttributeScope.addAttributes takes care of the separation so we are
         *  free here to use from '=' to end of string as the expression.
         *
         *  Set name, type, initvalue, and full decl instance vars.
         */
        protected virtual void ExtractAttribute(string decl)
        {
            if (decl == null)
            {
                return;
            }
            bool inID  = false;
            int  start = -1;
            int  rightEdgeOfDeclarator = decl.Length - 1;
            int  equalsIndex           = decl.IndexOf('=');

            if (equalsIndex > 0)
            {
                // everything after the '=' is the init value
                this.InitValue        = decl.Substring(equalsIndex + 1);
                rightEdgeOfDeclarator = equalsIndex - 1;
            }
            // walk backwards looking for start of an ID
            for (int i = rightEdgeOfDeclarator; i >= 0; i--)
            {
                // if we haven't found the end yet, keep going
                if (!inID && (char.IsLetterOrDigit(decl[i]) || decl[i] == '_' || decl[i] == '@'))
                {
                    inID = true;
                }
                else if (inID &&
                         !(char.IsLetterOrDigit(decl[i]) ||
                           decl[i] == '_' || decl[i] == '@'))
                {
                    start = i + 1;
                    break;
                }
            }
            if (start < 0 && inID)
            {
                start = 0;
            }
            if (start < 0)
            {
                ErrorManager.Error(ErrorManager.MSG_CANNOT_FIND_ATTRIBUTE_NAME_IN_DECL, decl);
            }
            // walk forwards looking for end of an ID
            int stop = -1;

            for (int i = start; i <= rightEdgeOfDeclarator; i++)
            {
                // if we haven't found the end yet, keep going
                if (!(char.IsLetterOrDigit(decl[i]) ||
                      decl[i] == '_' || decl[i] == '@'))
                {
                    stop = i;
                    break;
                }
                if (i == rightEdgeOfDeclarator)
                {
                    stop = i + 1;
                }
            }

            // the name is the last ID
            this.Name = decl.Substring(start, stop - start);

            // the type is the decl minus the ID (could be empty)
            this.Type = decl.Substring(0, start);
            if (stop <= rightEdgeOfDeclarator)
            {
                this.Type += decl.Substring(stop, rightEdgeOfDeclarator + 1 - stop);
            }
            this.Type = Type.Trim();
            if (this.Type.Length == 0)
            {
                this.Type = null;
            }

            this.Decl = decl;
        }