GrammarError() public static method

public static GrammarError ( int msgID, Grammar g, Antlr.Runtime.IToken token ) : void
msgID int
g Grammar
token Antlr.Runtime.IToken
return void
Beispiel #1
0
 public virtual string SetOption(IDictionary <string, object> options, HashSet <string> legalOptions, Grammar grammar, string key, object value)
 {
     if (!legalOptions.Contains(key))
     {
         ErrorManager.GrammarError(ErrorManager.MSG_ILLEGAL_OPTION,
                                   grammar,
                                   Token,
                                   key);
         return(null);
     }
     if (value is string)
     {
         string vs = (string)value;
         if (vs[0] == '"')
         {
             value = vs.Substring(1, vs.Length - 2);   // strip quotes
         }
     }
     if (key.Equals("k"))
     {
         grammar.numberOfManualLookaheadOptions++;
     }
     if (key == "backtrack" && value.ToString() == "true")
     {
         grammar.composite.RootGrammar.atLeastOneBacktrackOption = true;
     }
     options[key] = value;
     return(key);
 }
Beispiel #2
0
 /** Save the option key/value pair and process it; return the key
  *  or null if invalid option.
  */
 public virtual string SetOption(string key, object value, IToken optionsStartToken)
 {
     if (!legalOptions.Contains(key))
     {
         ErrorManager.GrammarError(ErrorManager.MSG_ILLEGAL_OPTION,
                                   Grammar,
                                   optionsStartToken,
                                   key);
         return(null);
     }
     if (Options == null)
     {
         Options = new Dictionary <object, object>();
     }
     if (key.Equals("memoize") && value.ToString().Equals("true"))
     {
         Grammar.composite.RootGrammar.atLeastOneRuleMemoizes = true;
     }
     if (key == "backtrack" && value.ToString() == "true")
     {
         Grammar.composite.RootGrammar.atLeastOneBacktrackOption = true;
     }
     if (key.Equals("k"))
     {
         Grammar.numberOfManualLookaheadOptions++;
     }
     Options[key] = value;
     return(key);
 }
Beispiel #3
0
        /** Make sure a label doesn't conflict with another symbol.
         *  Labels must not conflict with: rules, tokens, scope names,
         *  return values, parameters, and rule-scope dynamic attributes
         *  defined in surrounding rule.
         */
        protected virtual void CheckForLabelConflict(Rule r, IToken label)
        {
            int    msgID = 0;
            object arg2  = null;

            if (grammar.GetGlobalScope(label.Text) != null)
            {
                msgID = ErrorManager.MSG_SYMBOL_CONFLICTS_WITH_GLOBAL_SCOPE;
            }
            else if (grammar.GetRule(label.Text) != null)
            {
                msgID = ErrorManager.MSG_LABEL_CONFLICTS_WITH_RULE;
            }
            else if (grammar.GetTokenType(label.Text) != Label.INVALID)
            {
                msgID = ErrorManager.MSG_LABEL_CONFLICTS_WITH_TOKEN;
            }
            else if (r.RuleScope != null && r.RuleScope.GetAttribute(label.Text) != null)
            {
                msgID = ErrorManager.MSG_LABEL_CONFLICTS_WITH_RULE_SCOPE_ATTRIBUTE;
                arg2  = r.Name;
            }
            else if ((r.ReturnScope != null && r.ReturnScope.GetAttribute(label.Text) != null) ||
                     (r.ParameterScope != null && r.ParameterScope.GetAttribute(label.Text) != null))
            {
                msgID = ErrorManager.MSG_LABEL_CONFLICTS_WITH_RULE_ARG_RETVAL;
                arg2  = r.Name;
            }
            if (msgID != 0)
            {
                ErrorManager.GrammarError(msgID, grammar, label, label.Text, arg2);
            }
        }
Beispiel #4
0
        protected virtual void CheckForRuleDefinitionProblems(Rule r)
        {
            string ruleName  = r.Name;
            IToken ruleToken = r.Tree.Token;
            int    msgID     = 0;

            if ((grammar.type == GrammarType.Parser || grammar.type == GrammarType.TreeParser) &&
                Rule.GetRuleType(ruleName) == RuleType.Lexer)
            {
                msgID = ErrorManager.MSG_LEXER_RULES_NOT_ALLOWED;
            }
            else if (grammar.type == GrammarType.Lexer &&
                     Rule.GetRuleType(ruleName) == RuleType.Parser &&
                     !r.IsSynPred)
            {
                msgID = ErrorManager.MSG_PARSER_RULES_NOT_ALLOWED;
            }
            else if (grammar.GetGlobalScope(ruleName) != null)
            {
                msgID = ErrorManager.MSG_SYMBOL_CONFLICTS_WITH_GLOBAL_SCOPE;
            }
            if (msgID != 0)
            {
                ErrorManager.GrammarError(msgID, grammar, ruleToken, ruleName);
            }
        }
Beispiel #5
0
 protected virtual void CheckForGlobalScopeTokenConflict(AttributeScope scope)
 {
     if (grammar.GetTokenType(scope.Name) != Label.INVALID)
     {
         ErrorManager.GrammarError(ErrorManager.MSG_SYMBOL_CONFLICTS_WITH_GLOBAL_SCOPE,
                                   grammar, null, scope.Name);
     }
 }
Beispiel #6
0
        protected override void Alias(GrammarAST t, GrammarAST s)
        {
            string tokenID = t.Text;
            string literal = s.Text;
            string prevAliasLiteralID;

            _aliasesReverseIndex.TryGetValue(literal, out prevAliasLiteralID);
            if (prevAliasLiteralID != null)
            { // we've seen this literal before
                if (tokenID.Equals(prevAliasLiteralID))
                {
                    // duplicate but identical alias; might be tokens {A='a'} and
                    // lexer rule A : 'a' ;  Is ok, just return
                    return;
                }

                // give error unless both are rules (ok if one is in tokens section)
                if (!(tokenRuleDefs.Contains(tokenID) && tokenRuleDefs.Contains(prevAliasLiteralID)))
                {
                    // don't allow alias if A='a' in tokens section and B : 'a'; is rule.
                    // Allow if both are rules.  Will get DFA nondeterminism error later.
                    ErrorManager.GrammarError(ErrorManager.MSG_TOKEN_ALIAS_CONFLICT,
                                              grammar,
                                              t.Token,
                                              tokenID + "=" + literal,
                                              prevAliasLiteralID);
                }
                return; // don't do the alias
            }
            int existingLiteralType = grammar.GetTokenType(literal);

            if (existingLiteralType != Label.INVALID)
            {
                // we've seen this before from a tokenVocab most likely
                // don't assign a new token type; use existingLiteralType.
                _tokens[tokenID] = existingLiteralType;
            }
            string prevAliasTokenID;

            _aliases.TryGetValue(tokenID, out prevAliasTokenID);
            if (prevAliasTokenID != null)
            {
                ErrorManager.GrammarError(ErrorManager.MSG_TOKEN_ALIAS_REASSIGNMENT,
                                          grammar,
                                          t.Token,
                                          tokenID + "=" + literal,
                                          prevAliasTokenID);
                return; // don't do the alias
            }
            _aliases[tokenID]             = literal;
            _aliasesReverseIndex[literal] = tokenID;
        }
Beispiel #7
0
        /** Given @scope::name {action} define it for this attribute scope. Later,
         *  the code generator will ask for the actions table.
         */
        public void DefineNamedAction(GrammarAST nameAST, GrammarAST actionAST)
        {
            string     actionName = nameAST.Text;
            GrammarAST a;

            if (Actions.TryGetValue(actionName, out a) && a != null)
            {
                ErrorManager.GrammarError(
                    ErrorManager.MSG_ACTION_REDEFINITION, grammar,
                    nameAST.Token, nameAST.Text);
            }
            else
            {
                Actions[actionName] = actionAST;
            }
        }
Beispiel #8
0
        /** Given @scope::name {action} define it for this grammar.  Later,
         *  the code generator will ask for the actions table.
         */
        public virtual void DefineNamedAction(GrammarAST ampersandAST,
                                              GrammarAST nameAST,
                                              GrammarAST actionAST)
        {
            //[email protected]("rule @"+nameAST.getText()+"{"+actionAST.getText()+"}");
            string     actionName = nameAST.Text;
            GrammarAST a          = (GrammarAST)actions.get(actionName);

            if (a != null)
            {
                ErrorManager.GrammarError(
                    ErrorManager.MSG_ACTION_REDEFINITION, grammar,
                    nameAST.Token, nameAST.Text);
            }
            else
            {
                actions[actionName] = actionAST;
            }
        }
Beispiel #9
0
 protected virtual void CheckForRuleArgumentAndReturnValueConflicts(Rule r)
 {
     if (r.ReturnScope != null)
     {
         HashSet <object> conflictingKeys = r.ReturnScope.Intersection(r.ParameterScope);
         if (conflictingKeys != null)
         {
             foreach (string key in conflictingKeys)
             {
                 ErrorManager.GrammarError(
                     ErrorManager.MSG_ARG_RETVAL_CONFLICT,
                     grammar,
                     r.Tree.Token,
                     key,
                     r.Name);
             }
         }
     }
 }
Beispiel #10
0
        /** Check for collision of a rule-scope dynamic attribute with:
         *  arg, return value, rule name itself.  Labels are checked elsewhere.
         */
        public virtual void CheckForRuleScopeAttributeConflict(Rule r, Attribute attribute)
        {
            int    msgID    = 0;
            object arg2     = null;
            string attrName = attribute.Name;

            if (r.Name.Equals(attrName))
            {
                msgID = ErrorManager.MSG_ATTRIBUTE_CONFLICTS_WITH_RULE;
                arg2  = r.Name;
            }
            else if ((r.ReturnScope != null && r.ReturnScope.GetAttribute(attrName) != null) ||
                     (r.ParameterScope != null && r.ParameterScope.GetAttribute(attrName) != null))
            {
                msgID = ErrorManager.MSG_ATTRIBUTE_CONFLICTS_WITH_RULE_ARG_RETVAL;
                arg2  = r.Name;
            }
            if (msgID != 0)
            {
                ErrorManager.GrammarError(msgID, grammar, r.Tree.Token, attrName, arg2);
            }
        }
Beispiel #11
0
 /** If type of previous label differs from new label's type, that's an error.
  */
 public virtual bool CheckForLabelTypeMismatch(Rule r, IToken label, LabelType type)
 {
     Grammar.LabelElementPair prevLabelPair;
     r.LabelNameSpace.TryGetValue(label.Text, out prevLabelPair);
     if (prevLabelPair != null)
     {
         // label already defined; if same type, no problem
         if (prevLabelPair.type != type)
         {
             string typeMismatchExpr =
                 Grammar.LabelTypeToString[(int)type] + "!=" +
                 Grammar.LabelTypeToString[(int)prevLabelPair.type];
             ErrorManager.GrammarError(
                 ErrorManager.MSG_LABEL_TYPE_CONFLICT,
                 grammar,
                 label,
                 label.Text,
                 typeMismatchExpr);
             return(true);
         }
     }
     return(false);
 }
Beispiel #12
0
 /** Track string literals (could be in tokens{} section) */
 protected override void TrackString(GrammarAST t)
 {
     // if lexer, don't allow aliasing in tokens section
     if (currentRuleName == null && grammar.type == GrammarType.Lexer)
     {
         ErrorManager.GrammarError(ErrorManager.MSG_CANNOT_ALIAS_TOKENS_IN_LEXER,
                                   grammar,
                                   t.token,
                                   t.Text);
         return;
     }
     // in a plain parser grammar rule, cannot reference literals
     // (unless defined previously via tokenVocab option)
     // don't warn until we hit root grammar as may be defined there.
     if (grammar.IsRoot &&
         grammar.type == GrammarType.Parser &&
         grammar.GetTokenType(t.Text) == Label.INVALID)
     {
         ErrorManager.GrammarError(ErrorManager.MSG_LITERAL_NOT_ASSOCIATED_WITH_LEXER_RULE,
                                   grammar,
                                   t.token,
                                   t.Text);
     }
     // Don't record literals for lexers, they are things to match not tokens
     if (grammar.type == GrammarType.Lexer)
     {
         return;
     }
     // otherwise add literal to token types if referenced from parser rule
     // or in the tokens{} section
     if ((currentRuleName == null ||
          Rule.GetRuleType(currentRuleName) == RuleType.Parser) &&
         grammar.GetTokenType(t.Text) == Label.INVALID)
     {
         stringLiterals[t.Text] = UnassignedInParserRule;
     }
 }
Beispiel #13
0
        /** From a chunk of text holding the definitions of the attributes,
         *  pull them apart and create an Attribute for each one.  Add to
         *  the list of attributes for this scope.  Pass in the character
         *  that terminates a definition such as ',' or ';'.  For example,
         *
         *  scope symbols {
         *      int n;
         *      List names;
         *  }
         *
         *  would pass in definitions equal to the text in between {...} and
         *  separator=';'.  It results in two Attribute objects.
         */
        public virtual void AddAttributes(string definitions, int separator)
        {
            IList <string> attrs = new List <string>();

            CodeGenerator.GetListOfArgumentsFromAction(definitions, 0, -1, separator, attrs);
            foreach (string a in attrs)
            {
                Attribute attr = new Attribute(a);
                if (!isReturnScope && attr.InitValue != null)
                {
                    ErrorManager.GrammarError(ErrorManager.MSG_ARG_INIT_VALUES_ILLEGAL,
                                              grammar,
                                              derivedFromToken,
                                              attr.Name);
                    attr.InitValue = null; // wipe it out
                }
                for (int i = 0; i <= attributes.Count; i++)
                {
                    if (i < attributes.Count)
                    {
                        if (attributes[i].Name == attr.Name)
                        {
                            attributes[i] = attr;
                            break;
                        }
                    }
                    else
                    {
                        attributes.Add(attr);
                        // *must* break since the count changed
                        break;
                        //attributes.put( attr.Name, attr );
                    }
                }
            }
        }
Beispiel #14
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 #15
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);
             }
         }
     }
 }