public virtual string GetErrorMessage(RecognitionException e, string[] tokenNames) { string str1 = e.Message; if (e is UnwantedTokenException) { UnwantedTokenException unwantedTokenException = (UnwantedTokenException)e; string str2 = unwantedTokenException.Expecting != -1 ? tokenNames[unwantedTokenException.Expecting] : "EndOfFile"; str1 = "extraneous input " + this.GetTokenErrorDisplay(unwantedTokenException.UnexpectedToken) + " expecting " + str2; } else if (e is MissingTokenException) { MissingTokenException missingTokenException = (MissingTokenException)e; str1 = "missing " + (missingTokenException.Expecting != -1 ? tokenNames[missingTokenException.Expecting] : "EndOfFile") + " at " + this.GetTokenErrorDisplay(e.Token); } else if (e is MismatchedTokenException) { MismatchedTokenException mismatchedTokenException = (MismatchedTokenException)e; string str2 = mismatchedTokenException.Expecting != -1 ? tokenNames[mismatchedTokenException.Expecting] : "EndOfFile"; str1 = "mismatched input " + this.GetTokenErrorDisplay(e.Token) + " expecting " + str2; } else if (e is MismatchedTreeNodeException) { MismatchedTreeNodeException treeNodeException = (MismatchedTreeNodeException)e; string str2 = treeNodeException.Expecting != -1 ? tokenNames[treeNodeException.Expecting] : "EndOfFile"; str1 = "mismatched tree node: " + (treeNodeException.Node != null ? treeNodeException.Node.ToString() ?? string.Empty : string.Empty) + " expecting " + str2; } else if (e is NoViableAltException) { str1 = "no viable alternative at input " + this.GetTokenErrorDisplay(e.Token); } else if (e is EarlyExitException) { str1 = "required (...)+ loop did not match anything at input " + this.GetTokenErrorDisplay(e.Token); } else if (e is MismatchedSetException) { MismatchedSetException mismatchedSetException = (MismatchedSetException)e; str1 = "mismatched input " + this.GetTokenErrorDisplay(e.Token) + " expecting set " + (object)mismatchedSetException.Expecting; } else if (e is MismatchedNotSetException) { MismatchedNotSetException mismatchedNotSetException = (MismatchedNotSetException)e; str1 = "mismatched input " + this.GetTokenErrorDisplay(e.Token) + " expecting set " + (object)mismatchedNotSetException.Expecting; } else if (e is FailedPredicateException) { FailedPredicateException predicateException = (FailedPredicateException)e; str1 = "rule " + predicateException.RuleName + " failed predicate: {" + predicateException.PredicateText + "}?"; } return(str1); }
/** <summary>What error message should be generated for the various exception types?</summary> * * <remarks> * Not very object-oriented code, but I like having all error message * generation within one method rather than spread among all of the * exception classes. This also makes it much easier for the exception * handling because the exception classes do not have to have pointers back * to this object to access utility routines and so on. Also, changing * the message for an exception type would be difficult because you * would have to subclassing exception, but then somehow get ANTLR * to make those kinds of exception objects instead of the default. * This looks weird, but trust me--it makes the most sense in terms * of flexibility. * * For grammar debugging, you will want to override this to add * more information such as the stack frame with * getRuleInvocationStack(e, this.getClass().getName()) and, * for no viable alts, the decision description and state etc... * * Override this to change the message generated for one or more * exception types. * </remarks> */ public virtual 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); }
/// <summary> /// What error message should be generated for the various exception types? /// /// Not very object-oriented code, but I like having all error message generation /// within one method rather than spread among all of the exception classes. This /// also makes it much easier for the exception handling because the exception /// classes do not have to have pointers back to this object to access utility /// routines and so on. Also, changing the message for an exception type would be /// difficult because you would have to subclassing exception, but then somehow get /// ANTLR to make those kinds of exception objects instead of the default. /// /// This looks weird, but trust me--it makes the most sense in terms of flexibility. /// /// For grammar debugging, you will want to override this to add more information /// such as the stack frame with GetRuleInvocationStack(e, this.GetType().Fullname) /// and, for no viable alts, the decision description and state etc... /// /// Override this to change the message generated for one or more exception types. /// </summary> public virtual string GetErrorMessage(RecognitionException e, string[] tokenNames) { string msg = e.Message; if (e is UnwantedTokenException) { UnwantedTokenException ute = (UnwantedTokenException)e; string tokenName = "<unknown>"; if (ute.Expecting == Token.EOF) { tokenName = "EOF"; } 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 == Token.EOF) { tokenName = "EOF"; } 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 == Token.EOF) { tokenName = "EOF"; } 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 == Token.EOF) { tokenName = "EOF"; } else { tokenName = tokenNames[mtne.expecting]; } // The ternary operator is only necessary because of a bug in the .NET framework msg = "mismatched tree node: " + ((mtne.Node != null && mtne.Node.ToString() != null) ? mtne.Node : string.Empty) + " 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); }