示例#1
0
        public IASTNode HandleIdentifierError(IToken token, RecognitionException ex)
        {
            if (token is HqlToken)
            {
                HqlToken hqlToken = (HqlToken)token;

                // ... and the token could be an identifer and the error is
                // a mismatched token error ...
                if (hqlToken.PossibleId && (ex is MismatchedTokenException))
                {
                    MismatchedTokenException mte = (MismatchedTokenException)ex;

                    // ... and the expected token type was an identifier, then:
                    if (mte.Expecting == IDENT)
                    {
                        // Use the token as an identifier.
                        _parseErrorHandler.ReportWarning("Keyword  '"
                                                         + token.Text
                                                         + "' is being interpreted as an identifier due to: " + mte.Message);

                        // Add the token to the AST.

                        token.Type = WEIRD_IDENT;

                        input.Consume();
                        return((IASTNode)adaptor.Create(token));
                    }
                }
            }

            // Otherwise, handle the error normally.
            ReflectHelper.PreserveStackTrace(ex);
            throw ex;
        }
示例#2
0
        public virtual void Match(string s)
        {
            int i = 0;

            while (i < s.Length)
            {
                if (input.LA(1) != s[i])
                {
                    if (state.backtracking > 0)
                    {
                        state.failed = true;
                        return;
                    }
                    MismatchedTokenException mte =
                        new MismatchedTokenException(s[i], input)
                    {
                        tokenNames = GetTokenNames()
                    };
                    Recover(mte);
                    throw mte;
                }
                i++;
                input.Consume();
                state.failed = false;
            }
        }
示例#3
0
        protected virtual object RecoverFromMismatchedToken(IIntStream input, int ttype, BitSet follow)
        {
            RecognitionException e = null;

            if (this.MismatchIsUnwantedToken(input, ttype))
            {
                e = new UnwantedTokenException(ttype, input, this.TokenNames);
                this.BeginResync();
                input.Consume();
                this.EndResync();
                this.ReportError(e);
                object currentInputSymbol = this.GetCurrentInputSymbol(input);
                input.Consume();
                return(currentInputSymbol);
            }
            if (this.MismatchIsMissingToken(input, follow))
            {
                object missingSymbol = this.GetMissingSymbol(input, e, ttype, follow);
                e = new MissingTokenException(ttype, input, missingSymbol);
                this.ReportError(e);
                return(missingSymbol);
            }
            e = new MismatchedTokenException(ttype, input, this.TokenNames);
            throw e;
        }
示例#4
0
        public virtual string GetErrorMessage(RecognitionException e, string[] tokenNames)
        {
            string result = e.Message;

            if (e is UnwantedTokenException)
            {
                UnwantedTokenException ex = (UnwantedTokenException)e;
                string text = "<unknown>";
                text   = ((ex.Expecting != -1) ? tokenNames[ex.Expecting] : "EndOfFile");
                result = "extraneous input " + this.GetTokenErrorDisplay(ex.UnexpectedToken) + " expecting " + text;
            }
            else if (e is MissingTokenException)
            {
                MissingTokenException ex2 = (MissingTokenException)e;
                string text2 = "<unknown>";
                text2  = ((ex2.Expecting != -1) ? tokenNames[ex2.Expecting] : "EndOfFile");
                result = "missing " + text2 + " at " + this.GetTokenErrorDisplay(e.Token);
            }
            else if (e is MismatchedTokenException)
            {
                MismatchedTokenException ex3 = (MismatchedTokenException)e;
                string text3 = "<unknown>";
                text3  = ((ex3.Expecting != -1) ? tokenNames[ex3.Expecting] : "EndOfFile");
                result = "mismatched input " + this.GetTokenErrorDisplay(e.Token) + " expecting " + text3;
            }
            else if (e is MismatchedTreeNodeException)
            {
                MismatchedTreeNodeException ex4 = (MismatchedTreeNodeException)e;
                string text4 = "<unknown>";
                text4 = ((ex4.Expecting != -1) ? tokenNames[ex4.Expecting] : "EndOfFile");
                string str = (ex4.Node != null) ? (ex4.Node.ToString() ?? string.Empty) : string.Empty;
                result = "mismatched tree node: " + str + " expecting " + text4;
            }
            else if (e is NoViableAltException)
            {
                result = "no viable alternative at input " + this.GetTokenErrorDisplay(e.Token);
            }
            else if (e is EarlyExitException)
            {
                result = "required (...)+ loop did not match anything at input " + this.GetTokenErrorDisplay(e.Token);
            }
            else if (e is MismatchedSetException)
            {
                MismatchedSetException ex5 = (MismatchedSetException)e;
                result = "mismatched input " + this.GetTokenErrorDisplay(e.Token) + " expecting set " + ex5.Expecting;
            }
            else if (e is MismatchedNotSetException)
            {
                MismatchedNotSetException ex6 = (MismatchedNotSetException)e;
                result = "mismatched input " + this.GetTokenErrorDisplay(e.Token) + " expecting set " + ex6.Expecting;
            }
            else if (e is FailedPredicateException)
            {
                FailedPredicateException ex7 = (FailedPredicateException)e;
                result = "rule " + ex7.RuleName + " failed predicate: {" + ex7.PredicateText + "}?";
            }
            return(result);
        }
示例#5
0
 public void Match(int c)
 {
     if (input.LA(1) != c)
     {
         if (state.backtracking > 0)
         {
             state.failed = true;
             return;
         }
         MismatchedTokenException mte = new MismatchedTokenException(c, input, TokenNames);
         Recover(mte);    // don't really recover; just consume in lexer
         throw mte;
     }
     input.Consume();
     state.failed = false;
 }
示例#6
0
        override public bool Matches(string rule, RecognitionException ex)
        {
            if (!base.Matches(rule, ex))
            {
                return(false);
            }

            MismatchedTokenException mmt = ex as MismatchedTokenException;

            if (mmt == null)
            {
                return(false);
            }

            return(Token == mmt.expecting);
        }
示例#7
0
        public override string GetErrorMessage(RecognitionException e, string[] tokenNames)
        {
            string msg = null;

            if (e is MismatchedTokenException)
            {
                MismatchedTokenException mte = (MismatchedTokenException)e;
                msg = "mismatched character " + GetCharErrorDisplay(e.c) + " expecting " + GetCharErrorDisplay(mte.expecting);
            }
            else if (e is NoViableAltException)
            {
                NoViableAltException nvae = (NoViableAltException)e;
                // for development, can add "decision=<<"+nvae.grammarDecisionDescription+">>"
                // and "(decision="+nvae.decisionNumber+") and
                // "state "+nvae.stateNumber
                msg = "no viable alternative at character " + GetCharErrorDisplay(e.c);
            }
            else if (e is EarlyExitException)
            {
                EarlyExitException eee = (EarlyExitException)e;
                // for development, can add "(decision="+eee.decisionNumber+")"
                msg = "required (...)+ loop did not match anything at character " + GetCharErrorDisplay(e.c);
            }
            else if (e is MismatchedNotSetException)
            {
                MismatchedNotSetException mse = (MismatchedNotSetException)e;
                msg = "mismatched character " + GetCharErrorDisplay(e.c) + " expecting set " + mse.expecting;
            }
            else if (e is MismatchedSetException)
            {
                MismatchedSetException mse = (MismatchedSetException)e;
                msg = "mismatched character " + GetCharErrorDisplay(e.c) + " expecting set " + mse.expecting;
            }
            else if (e is MismatchedRangeException)
            {
                MismatchedRangeException mre = (MismatchedRangeException)e;
                msg = "mismatched character " + GetCharErrorDisplay(e.c) + " expecting set " +
                      GetCharErrorDisplay(mre.a) + ".." + GetCharErrorDisplay(mre.b);
            }
            else
            {
                msg = base.GetErrorMessage(e, tokenNames);
            }
            return(msg);
        }
示例#8
0
        /// <summary>
        /// Generates a human-readable error message for the given error.
        /// </summary>
        /// <param name="e">The error.</param>
        /// <param name="tokenNames">The names of the tokens in the current language.</param>
        /// <returns>A human-readable error message for the given error.</returns>
        public override string GetErrorMessage(RecognitionException e, string[] tokenNames)
        {
            string msg = null;

            if (e is MismatchedTokenException)
            {
                MismatchedTokenException mte = (MismatchedTokenException)e;
                msg = String.Format("Unexpected character: '{0}'. Expecting '{1}'.", GetCharErrorDisplay(e.Character), GetCharErrorDisplay(mte.Expecting));
            }
            else if (e is NoViableAltException)
            {
                NoViableAltException nvae = (NoViableAltException)e;
                msg = String.Format("Unexpected character: '{0}'.", GetCharErrorDisplay(e.Character));
            }
            else if (e is EarlyExitException)
            {
                EarlyExitException eee = (EarlyExitException)e;
                msg = String.Format("Unexpected character: '{0}'.", GetCharErrorDisplay(e.Character));
                //msg = "required (...)+ loop did not match anything at character " + GetCharErrorDisplay(e.Character);
            }
            else if (e is MismatchedNotSetException)
            {
                MismatchedNotSetException mse = (MismatchedNotSetException)e;
                msg = String.Format("Unexpected character: '{0}'. Expecting one of {1}.", GetCharErrorDisplay(e.Character), mse.Expecting);
            }
            else if (e is MismatchedSetException)
            {
                MismatchedSetException mse = (MismatchedSetException)e;
                msg = String.Format("Unexpected character: '{0}'. Expecting one of {1}.", GetCharErrorDisplay(e.Character), mse.Expecting);
            }
            else if (e is MismatchedRangeException)
            {
                MismatchedRangeException mre = (MismatchedRangeException)e;
                msg = String.Format("Unexpected character: '{0}'. Expecting one of '{1}..{2}'.", GetCharErrorDisplay(e.Character), GetCharErrorDisplay(mre.A), GetCharErrorDisplay(mre.B));
            }
            else
            {
                msg = base.GetErrorMessage(e, tokenNames);
            }
            return(msg);
        }
示例#9
0
        private IToken MatchComment()
        {
            Match('!');

            while (!(c == '!' && input.LA(2) == delimiterStopChar))
            {
                if (c == EOF)
                {
                    RecognitionException re = new MismatchedTokenException((int)'!', input);
                    re.Line = input.Line;
                    re.CharPositionInLine = input.CharPositionInLine;
                    string message = string.Format("Nonterminated comment starting at {0}:{1}: '!{2}' missing", startLine, startCharPositionInLine, delimiterStopChar);
                    errMgr.LexerError(input.SourceName, message, templateToken, re);
                    break;
                }
                Consume();
            }

            Consume();
            Consume(); // grab !>
            return(NewToken(COMMENT));
        }
示例#10
0
        /** Fill a list of all NFA states visited during the parse */
        protected virtual void ParseEngine(string startRule,
                                           NFAState start,
                                           NFAState stop,
                                           IIntStream input,
                                           Stack <object> ruleInvocationStack,
                                           IDebugEventListener actions,
                                           IList <NFAState> visitedStates)
        {
            NFAState s = start;

            if (actions != null)
            {
                actions.EnterRule(s.nfa.Grammar.FileName, start.enclosingRule.Name);
            }
            int t = input.LA(1);

            while (s != stop)
            {
                if (visitedStates != null)
                {
                    visitedStates.Add(s);
                }
                //Console.Out.WriteLine( "parse state " + s.stateNumber + " input=" + s.nfa.Grammar.getTokenDisplayName( t ) );
                // CASE 1: decision state
                if (s.DecisionNumber > 0 && s.nfa.Grammar.GetNumberOfAltsForDecisionNFA(s) > 1)
                {
                    // decision point, must predict and jump to alt
                    DFA dfa = s.nfa.Grammar.GetLookaheadDFA(s.DecisionNumber);
                    //if ( s.nfa.Grammar.type != GrammarType.Lexer )
                    //{
                    //    Console.Out.WriteLine( "decision: " +
                    //                   dfa.getNFADecisionStartState().Description +
                    //                   " input=" + s.nfa.Grammar.getTokenDisplayName( t ) );
                    //}
                    int m            = input.Mark();
                    int predictedAlt = Predict(dfa);
                    if (predictedAlt == NFA.INVALID_ALT_NUMBER)
                    {
                        string description        = dfa.NFADecisionStartState.Description;
                        NoViableAltException nvae =
                            new NoViableAltException(description,
                                                     dfa.NfaStartStateDecisionNumber,
                                                     s.StateNumber,
                                                     input);
                        if (actions != null)
                        {
                            actions.RecognitionException(nvae);
                        }
                        input.Consume(); // recover
                        throw nvae;
                    }
                    input.Rewind(m);
                    int parseAlt =
                        s.TranslateDisplayAltToWalkAlt(predictedAlt);
                    //if ( s.nfa.Grammar.type != GrammarType.Lexer )
                    //{
                    //    Console.Out.WriteLine( "predicted alt " + predictedAlt + ", parseAlt " + parseAlt );
                    //}
                    NFAState alt;
                    if (parseAlt > s.nfa.Grammar.GetNumberOfAltsForDecisionNFA(s))
                    {
                        // implied branch of loop etc...
                        alt = s.nfa.Grammar.nfa.GetState(s.endOfBlockStateNumber);
                    }
                    else
                    {
                        alt = s.nfa.Grammar.GetNFAStateForAltOfDecision(s, parseAlt);
                    }
                    s = (NFAState)alt.transition[0].Target;
                    continue;
                }

                // CASE 2: finished matching a rule
                if (s.IsAcceptState)
                { // end of rule node
                    if (actions != null)
                    {
                        actions.ExitRule(s.nfa.Grammar.FileName, s.enclosingRule.Name);
                    }
                    if (ruleInvocationStack.Count == 0)
                    {
                        // done parsing.  Hit the start state.
                        //Console.Out.WriteLine( "stack empty in stop state for " + s.enclosingRule );
                        break;
                    }
                    // pop invoking state off the stack to know where to return to
                    NFAState invokingState = (NFAState)ruleInvocationStack.Pop();
                    RuleClosureTransition invokingTransition =
                        (RuleClosureTransition)invokingState.transition[0];
                    // move to node after state that invoked this rule
                    s = invokingTransition.FollowState;
                    continue;
                }

                Transition trans = s.transition[0];
                Label      label = trans.Label;
                if (label.IsSemanticPredicate)
                {
                    FailedPredicateException fpe =
                        new FailedPredicateException(input,
                                                     s.enclosingRule.Name,
                                                     "can't deal with predicates yet");
                    if (actions != null)
                    {
                        actions.RecognitionException(fpe);
                    }
                }

                // CASE 3: epsilon transition
                if (label.IsEpsilon)
                {
                    // CASE 3a: rule invocation state
                    if (trans is RuleClosureTransition)
                    {
                        ruleInvocationStack.Push(s);
                        s = (NFAState)trans.Target;
                        //Console.Out.WriteLine( "call " + s.enclosingRule.name + " from " + s.nfa.Grammar.getFileName() );
                        if (actions != null)
                        {
                            actions.EnterRule(s.nfa.Grammar.FileName, s.enclosingRule.Name);
                        }
                        // could be jumping to new grammar, make sure DFA created
                        if (!s.nfa.Grammar.AllDecisionDFAHaveBeenCreated)
                        {
                            s.nfa.Grammar.CreateLookaheadDFAs();
                        }
                    }
                    // CASE 3b: plain old epsilon transition, just move
                    else
                    {
                        s = (NFAState)trans.Target;
                    }
                }

                // CASE 4: match label on transition
                else if (label.Matches(t))
                {
                    if (actions != null)
                    {
                        if (s.nfa.Grammar.type == GrammarType.Parser ||
                            s.nfa.Grammar.type == GrammarType.Combined)
                        {
                            actions.ConsumeToken(((ITokenStream)input).LT(1));
                        }
                    }
                    s = (NFAState)s.transition[0].Target;
                    input.Consume();
                    t = input.LA(1);
                }

                // CASE 5: error condition; label is inconsistent with input
                else
                {
                    if (label.IsAtom)
                    {
                        MismatchedTokenException mte =
                            new MismatchedTokenException(label.Atom, input);
                        if (actions != null)
                        {
                            actions.RecognitionException(mte);
                        }
                        input.Consume(); // recover
                        throw mte;
                    }
                    else if (label.IsSet)
                    {
                        MismatchedSetException mse =
                            new MismatchedSetException(((IntervalSet)label.Set).ToRuntimeBitSet(),
                                                       input);
                        if (actions != null)
                        {
                            actions.RecognitionException(mse);
                        }
                        input.Consume(); // recover
                        throw mse;
                    }
                    else if (label.IsSemanticPredicate)
                    {
                        FailedPredicateException fpe =
                            new FailedPredicateException(input,
                                                         s.enclosingRule.Name,
                                                         label.SemanticContext.ToString());
                        if (actions != null)
                        {
                            actions.RecognitionException(fpe);
                        }
                        input.Consume(); // recover
                        throw fpe;
                    }
                    else
                    {
                        throw new RecognitionException(input);   // unknown error
                    }
                }
            }
            //Console.Out.WriteLine( "hit stop state for " + stop.enclosingRule );
            if (actions != null)
            {
                actions.ExitRule(s.nfa.Grammar.FileName, stop.enclosingRule.Name);
            }
        }
示例#11
0
        public override string GetErrorMessage(RecognitionException e, string[] tokenNames)
        {
            string msg = e.Message;

            if (e is UnwantedTokenException)
            {
                UnwantedTokenException ute = (UnwantedTokenException)e;
                string tokenName           = "<unknown>";
                if (ute.Expecting == TokenTypes.EndOfFile)
                {
                    tokenName = "EndOfFile";
                }
                else
                {
                    tokenName = tokenNames[ute.Expecting];
                }
                msg = "extraneous input " + GetTokenErrorDisplay(ute.UnexpectedToken) +
                      " expecting " + tokenName;
            }
            else if (e is MissingTokenException)
            {
                MissingTokenException mte = (MissingTokenException)e;
                string tokenName          = "<unknown>";
                if (mte.Expecting == TokenTypes.EndOfFile)
                {
                    tokenName = "EndOfFile";
                }
                else
                {
                    tokenName = tokenNames[mte.Expecting];
                }
                msg = "missing " + tokenName + " at " + GetTokenErrorDisplay(e.Token);
            }
            else if (e is MismatchedTokenException)
            {
                MismatchedTokenException mte = (MismatchedTokenException)e;
                string tokenName             = "<unknown>";
                if (mte.Expecting == TokenTypes.EndOfFile)
                {
                    tokenName = "EndOfFile";
                }
                else
                {
                    tokenName = tokenNames[mte.Expecting];
                }
                msg = "mismatched input " + GetTokenErrorDisplay(e.Token) +
                      " expecting " + tokenName;
            }
            else if (e is MismatchedTreeNodeException)
            {
                MismatchedTreeNodeException mtne = (MismatchedTreeNodeException)e;
                string tokenName = "<unknown>";
                if (mtne.Expecting == TokenTypes.EndOfFile)
                {
                    tokenName = "EndOfFile";
                }
                else
                {
                    tokenName = tokenNames[mtne.Expecting];
                }
                // workaround for a .NET framework bug (NullReferenceException)
                string nodeText = (mtne.Node != null) ? mtne.Node.ToString() ?? string.Empty : string.Empty;
                msg = "mismatched tree node: " + nodeText + " expecting " + tokenName;
            }
            else if (e is NoViableAltException)
            {
                //NoViableAltException nvae = (NoViableAltException)e;
                // for development, can add "decision=<<"+nvae.grammarDecisionDescription+">>"
                // and "(decision="+nvae.decisionNumber+") and
                // "state "+nvae.stateNumber
                msg = "no viable alternative at input " + GetTokenErrorDisplay(e.Token);
            }
            else if (e is EarlyExitException)
            {
                //EarlyExitException eee = (EarlyExitException)e;
                // for development, can add "(decision="+eee.decisionNumber+")"
                msg = "required (...)+ loop did not match anything at input " +
                      GetTokenErrorDisplay(e.Token);
            }
            else if (e is MismatchedSetException)
            {
                MismatchedSetException mse = (MismatchedSetException)e;
                msg = "mismatched input " + GetTokenErrorDisplay(e.Token) +
                      " expecting set " + mse.Expecting;
            }
            else if (e is MismatchedNotSetException)
            {
                MismatchedNotSetException mse = (MismatchedNotSetException)e;
                msg = "mismatched input " + GetTokenErrorDisplay(e.Token) +
                      " expecting set " + mse.Expecting;
            }
            else if (e is FailedPredicateException)
            {
                FailedPredicateException fpe = (FailedPredicateException)e;
                msg = "rule " + fpe.RuleName + " failed predicate: {" +
                      fpe.PredicateText + "}?";
            }
            return(msg);
        }
示例#12
0
        /** STRING : '"' ( '\\' '"' | '\\' ~'"' | ~('\\'|'"') )* '"' ; */
        private IToken MatchString()
        {
            //{setText(getText().substring(1, getText().length()-1));}
            bool          sawEscape = false;
            StringBuilder buf       = new StringBuilder();

            buf.Append(c);
            Consume();
            while (c != '"')
            {
                if (c == '\\')
                {
                    sawEscape = true;
                    Consume();
                    switch (c)
                    {
                    case 'n':
                        buf.Append('\n');
                        break;

                    case 'r':
                        buf.Append('\r');
                        break;

                    case 't':
                        buf.Append('\t');
                        break;

                    default:
                        buf.Append(c);
                        break;
                    }

                    Consume();
                    continue;
                }

                buf.Append(c);
                Consume();
                if (c == EOF)
                {
                    RecognitionException re = new MismatchedTokenException((int)'"', input);
                    re.Line = input.Line;
                    re.CharPositionInLine = input.CharPositionInLine;
                    errMgr.LexerError(input.SourceName, "EOF in string", templateToken, re);
                    break;
                }
            }

            buf.Append(c);
            Consume();

            if (sawEscape)
            {
                return(NewToken(STRING, buf.ToString()));
            }
            else
            {
                return(NewToken(STRING));
            }
        }
        /// <summary>
        /// Generates a human-readable error message for the given error.
        /// </summary>
        /// <param name="e">The error.</param>
        /// <param name="tokenNames">The names of the tokens in the current language.</param>
        /// <returns>A human-readable error message for the given error.</returns>
        public override string GetErrorMessage(RecognitionException e, string[] tokenNames)
        {
            string msg = e.Message;

            if (e is UnwantedTokenException)
            {
                UnwantedTokenException ute = (UnwantedTokenException)e;
                string tokenName           = "<unknown>";
                if (ute.Expecting == TokenTypes.EndOfFile)
                {
                    tokenName = null;
                }
                else
                {
                    tokenName = tokenNames[ute.Expecting];
                }
                msg = String.Format("Extraneous input: '{0}'.", GetTokenErrorDisplay(ute.UnexpectedToken));
                if (!String.IsNullOrEmpty(tokenName))
                {
                    msg += String.Format(" Expecting: '{0}'.", tokenName);
                }
            }
            else if (e is MissingTokenException)
            {
                MissingTokenException mte = (MissingTokenException)e;
                string tokenName          = "<unknown>";
                if (mte.Expecting == TokenTypes.EndOfFile)
                {
                    tokenName = null;
                }
                else
                {
                    tokenName = tokenNames[mte.Expecting];
                }
                if (!String.IsNullOrEmpty(tokenName))
                {
                    msg = String.Format("Missing '{0}' at '{1}'.", tokenName, GetTokenErrorDisplay(e.Token));
                }
                else
                {
                    msg = String.Format("Extraneous input: '{0}'.", GetTokenErrorDisplay(e.Token));
                }
            }
            else if (e is MismatchedTokenException)
            {
                MismatchedTokenException mte = (MismatchedTokenException)e;
                string tokenName             = "<unknown>";
                if (mte.Expecting == TokenTypes.EndOfFile)
                {
                    tokenName = null;
                }
                else
                {
                    tokenName = tokenNames[mte.Expecting];
                }
                msg = String.Format("Unexpected input: '{0}'.", GetTokenErrorDisplay(e.Token));
                if (!String.IsNullOrEmpty(tokenName))
                {
                    msg += String.Format(" Expecting: '{0}'.", tokenName);
                }
            }
            else if (e is MismatchedTreeNodeException)
            {
                MismatchedTreeNodeException mtne = (MismatchedTreeNodeException)e;
                string tokenName = "<unknown>";
                if (mtne.Expecting == TokenTypes.EndOfFile)
                {
                    tokenName = null;
                }
                else
                {
                    tokenName = tokenNames[mtne.Expecting];
                }
                // workaround for a .NET framework bug (NullReferenceException)
                string nodeText = (mtne.Node != null) ? mtne.Node.ToString() ?? string.Empty : string.Empty;
                msg = String.Format("Unexpected input: '{0}'.", nodeText);
                if (!String.IsNullOrEmpty(tokenName))
                {
                    msg += String.Format(" Expecting: '{0}'.", tokenName);
                }
            }
            else if (e is NoViableAltException)
            {
                //msg = "no viable alternative at input " + GetTokenErrorDisplay(e.Token);
                msg = String.Format("Unexpected input: '{0}'.", GetTokenErrorDisplay(e.Token));
            }
            else if (e is EarlyExitException)
            {
                //msg = "required (...)+ loop did not match anything at input " +	GetTokenErrorDisplay(e.Token);
                msg = String.Format("Unexpected input: '{0}'.", GetTokenErrorDisplay(e.Token));
            }
            else if (e is MismatchedSetException)
            {
                MismatchedSetException mse = (MismatchedSetException)e;
                msg = String.Format("Unexpected input: '{0}'. Expecting one of: {1}", GetTokenErrorDisplay(e.Token), mse.Expecting);
            }
            else if (e is MismatchedNotSetException)
            {
                MismatchedNotSetException mse = (MismatchedNotSetException)e;
                msg = String.Format("Unexpected input: '{0}'. Expecting one of: {1}", GetTokenErrorDisplay(e.Token), mse.Expecting);
            }
            else if (e is FailedPredicateException)
            {
                FailedPredicateException fpe = (FailedPredicateException)e;
                msg = "rule " + fpe.RuleName + " failed predicate: {" +
                      fpe.PredicateText + "}?";
            }
            return(msg);
        }
示例#14
0
        static public string ToString(RecognitionException Ex, string[] tokenNames)
        {
            //Stores a string that will be displayed after the actual error message
            //to give the user a specific hint what might be wrong.
            string TikzEdtNotice = "";
            //message that will be returned.
            string msg = "";

            //Types of RecognitionException are defined here:
            //http://www.antlr.org/api/Python/classantlr3_1_1_recognition_exception.html
            if (Ex is EarlyExitException)
            {
                EarlyExitException ex = Ex as EarlyExitException;
                msg += "EarlyExitException";
            }
            else if (Ex is FailedPredicateException)
            {
                FailedPredicateException ex = Ex as FailedPredicateException;
                msg += "FailedPredicateException";
            }
            else if (Ex is MismatchedRangeException)
            {
                MismatchedRangeException ex = Ex as MismatchedRangeException;
                msg += "MismatchedRangeException";
            }
            else if (Ex is MismatchedSetException)
            {
                MismatchedSetException ex = Ex as MismatchedSetException;
                msg += "MismatchedSetException";
                if (Ex is MismatchedNotSetException)
                {
                    MismatchedNotSetException ex2 = ex as MismatchedNotSetException;
                    msg += " -> MismatchedNotSetException";
                }
            }
            else if (Ex is MismatchedTokenException)
            {
                MismatchedTokenException ex = Ex as MismatchedTokenException;
                msg += "MismatchedTokenException";
                if (Ex is MissingTokenException)
                {
                    MissingTokenException ex2 = ex as MissingTokenException;
                    msg += " -> MissingTokenException";
                }
                else if (Ex is UnwantedTokenException)
                {
                    UnwantedTokenException ex2 = ex as UnwantedTokenException;
                    msg += " -> UnwantedTokenException";
                }
                else if (ex.Token != null)
                {
                    //this seem to be a parser exception.

                    msg += ": Expected token " + tokenNames[ex.Expecting] + ".";
                    if (ex.Token.Text != null)
                    {
                        msg += " Instead found \"" + ex.Token.Text.Replace("\n", "<NewLine>") + "\"";
                        if (!tokenNames[ex.Token.Type].Contains(ex.Token.Text))
                        {
                            msg += " which is of type " + tokenNames[ex.Token.Type];
                        }
                    }
                    else
                    {
                        msg          += " Instead found EOF";
                        TikzEdtNotice = "Does document include \\begin{tikzpicture} and \\end{tikzpicture}?";
                    }
                }
                else if (ex.Token == null)
                {
                    //this is probably a lexer exception.
                    if (ex.Node is String)
                    {
                        msg += ": Parser expected to start token \"" + ex.Node.ToString() + "\"";
                    }
                }
            }
            else if (Ex is MismatchedTreeNodeException)
            {
                MismatchedTreeNodeException ex = Ex as MismatchedTreeNodeException;
                msg += "MismatchedTreeNodeException";
            }
            else if (Ex is NoViableAltException)
            {
                NoViableAltException ex = Ex as NoViableAltException;
                msg += "NoViableAltException";
                if (ex.grammarDecisionDescription != null && ex.grammarDecisionDescription != "")
                {
                    if (ex.grammarDecisionDescription.ToLower().Contains("loopback"))
                    {
                        string loop = ex.grammarDecisionDescription.Substring(ex.grammarDecisionDescription.LastIndexOf(':') + 1);
                        msg += ": Parser is stuck in following recursion: " + loop.Trim();
                    }
                }
                TikzEdtNotice = "Is some \\end{} command missing?";
            }
            else
            {
                msg += "UnkownExcpetion (this is really bad)";
            }
            if (Ex.Line > 0)
            {
                msg += " in line " + Ex.Line.ToString() + " at position " + Ex.CharPositionInLine.ToString();
            }
            else
            {
                msg += " at end of document";
            }

            if (Ex.approximateLineInfo)
            {
                msg += " (approximately)";
            }

            msg += ".";

            if (TikzEdtNotice != "")
            {
                msg += " " + TikzEdtNotice;
            }

            return(msg);
        }
示例#15
0
文件: Lexer.cs 项目: ksmyth/antlr
 public virtual void Match( int c )
 {
     if ( input.LA( 1 ) != c )
     {
         if ( state.backtracking > 0 )
         {
             state.failed = true;
             return;
         }
         MismatchedTokenException mte =
             new MismatchedTokenException( c, input )
             {
                 tokenNames = GetTokenNames()
             };
         Recover( mte );  // don't really recover; just consume in lexer
         throw mte;
     }
     input.Consume();
     state.failed = false;
 }
示例#16
0
文件: Lexer.cs 项目: ksmyth/antlr
 public virtual void Match( string s )
 {
     int i = 0;
     while ( i < s.Length )
     {
         if ( input.LA( 1 ) != s[i] )
         {
             if ( state.backtracking > 0 )
             {
                 state.failed = true;
                 return;
             }
             MismatchedTokenException mte =
                 new MismatchedTokenException( s[i], input )
                 {
                     tokenNames = GetTokenNames()
                 };
             Recover( mte );
             throw mte;
         }
         i++;
         input.Consume();
         state.failed = false;
     }
 }
示例#17
0
        public void createErrorMessage(RecognitionException e)
        {
            if (AntlrException == null)
            {
                AntlrException = new ExceptionMessage();
            }
            //if (e is DataMisMatchedException)
            //{
            //    DataMisMatchedException dme = (DataMisMatchedException)e;
            //    ExceptionMessage.setErrorMessage(dme.ErrorMessage + " reason "
            //            + dme.ErrorCause);
            //}
            if (e is MismatchedTokenException)
            {
                MismatchedTokenException mte = (MismatchedTokenException)e;

                if (mte.Token.Text.Equals("then"))
                {
                    AntlrException.ErrorMessage = "Not a valid condition part";
                }
                else if (mte.Token.Text.Equals(AntlrConstants.EQ) || mte.Token.Text.Equals(AntlrConstants.GT) || mte.Token.Text.Equals(AntlrConstants.GE) || mte.Token.Text.Equals(AntlrConstants.LT) || mte.Token.Text.Equals(AntlrConstants.LE) || mte.Token.Text.Equals(AntlrConstants.NE))
                {
                    AntlrException.ErrorMessage = "Not a valid conditional Expression";
                }
                else if (mte.Token.Text.Equals(AntlrConstants.ADD) || mte.Token.Text.Equals(AntlrConstants.SUB) || mte.Token.Text.Equals(AntlrConstants.MUL) || mte.Token.Text.Equals(AntlrConstants.DIV) || mte.Token.Text.Equals(AntlrConstants.EQ))
                {
                    AntlrException.ErrorMessage = "Not a valid statement";
                }
                else if (mte.Token.Text.ToUpper().Equals(AntlrConstants.METHOD))
                {
                    AntlrException.ErrorMessage = "Not a valid function or method statement";
                }
                else
                {
                    AntlrException.ErrorMessage = "Not a valid expression";
                }
            }
            else if (e is MismatchedTreeNodeException)
            {
                MismatchedTreeNodeException mtne = (MismatchedTreeNodeException)e;
                if (e.Token.Text != null)
                {
                    AntlrException.ErrorMessage = "SYNTAX ERROR: FOUND "
                                                  + mtne.Node + "  EXPECTING "
                                                  + CalcETreeParser.tokenNames[mtne.expecting];
                }
            }
            else if (e is NoViableAltException)
            {
                NoViableAltException nvae = (NoViableAltException)e;
                if (nvae.Token.Text.Equals(AntlrConstants.SEMICOLON))
                {
                    AntlrException.ErrorMessage = "Not a valid statement";
                }
            }
            else if (e is EarlyExitException)
            {
                AntlrException.ErrorMessage = "PLEASE PROVIDE THE INPUT FOR THE METHOD";
            }
            else if (e is MismatchedSetException)
            {
                MismatchedSetException mse = (MismatchedSetException)e;
                if (e.Token.Text != null)
                {
                    AntlrException.ErrorMessage = "SYNTAX ERROR: FOUND '"
                                                  + e.Token + "'  EXPECTING  " + mse.expecting;
                }
            }
            else if (e is MismatchedNotSetException)
            {
                MismatchedNotSetException mse = (MismatchedNotSetException)e;
                if (e.Token.Text != null)
                {
                    AntlrException.ErrorMessage = "SYNTAX ERROR: FOUND '"
                                                  + e.Token + "'  EXPECTING " + mse.expecting;
                }
            }
            else if (e is FailedPredicateException)
            {
                FailedPredicateException fpe = (FailedPredicateException)e;
                if (e.Token.Text != null)
                {
                    AntlrException.ErrorMessage = "rule " + fpe.ruleName
                                                  + " failed predicate: {" + fpe.predicateText + "}?";
                }
            }
        }