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; }
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); }
public override void ReportError(RecognitionException exception) { // Add error report here base.ReportError(exception); if (exception is NoViableAltException) { // Throw an error that we can't continue the execution/parsing of this input throw new ParseException("No Viable Alternate", false, exception); } else if (exception is UnwantedTokenException) { UnwantedTokenException ex = (UnwantedTokenException)exception; // check if we can continue bool canContinue = (ex.Token.Text == "{"); // TODO: change this a constant // Throw an error that we can't continue the execution/parsing of this input throw new ParseException("Unwanted token found:" + ex.Token.Text, canContinue, ex); } // Throw an error, maybe we can continue the execution? throw new ParseException(exception.Message, true, exception); }
/// <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 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); }