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); }
/// <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); }
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); }
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 + "}?"; } } }
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); }