protected override void ReportFailedPredicate(Parser recognizer, FailedPredicateException e) { string ruleName = recognizer.RuleNames[recognizer.RuleContext.RuleIndex]; string msg = "规则 " + ruleName + " " + e.Message; NotifyErrorListeners(recognizer, msg, e); }
protected virtual string ErrorMessageForFailedPredicate(FailedPredicateException e, Antlr4.Runtime.Parser recognizer) { string ruleName = recognizer.RuleNames[recognizer.RuleContext.RuleIndex]; string msg = "rule " + ruleName + " " + e.Message; return(msg); }
protected override void ReportFailedPredicate(Parser recognizer, FailedPredicateException e) { string pred = e.Predicate; if (pred[0] == '!') { pred = pred.Substring(1); } switch (e.Predicate) { case "FeatureControlFlow": NotifyErrorListeners(recognizer, "Control flow expressions not supported in this version of the language", e); break; case "FeatureConditionalExpr": NotifyErrorListeners(recognizer, "Conditional expressions not supported in this version of the language", e); break; case "FeatureMutableVars": NotifyErrorListeners(recognizer, "Mutable variables not supported in this version of the language", e); break; default: base.ReportFailedPredicate(recognizer, e); break; } }
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); }
private static string GetMessage(RecognitionException re) { return(re switch { FailedPredicateException fpe => fpe.Message, InputMismatchException ime => ime.Message, LexerNoViableAltException nve1 => $"No Viable Alternative - '{nve1.OffendingToken.Text}' not recognized.", NoViableAltException nve2 => $"No Viable Alternative - '{nve2.OffendingToken.Text}' was unexpected.", _ => throw new ArgumentOutOfRangeException(nameof(re)) });
/// <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); }
protected override void ReportFailedPredicate(Parser recognizer, FailedPredicateException e) { NotifyErrorListeners(recognizer, e.Message, (RecognitionException)e); Recover(recognizer, e); }
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); }
protected override void ReportFailedPredicate(Parser recognizer, FailedPredicateException e) { OffendingTokens.Add(e.OffendingToken); }
/** 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); } }
protected override void ReportFailedPredicate([NotNull] Parser recognizer, [NotNull] FailedPredicateException e) { base.ReportFailedPredicate(recognizer, e); }
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 + "}?"; } } }
protected override void ReportFailedPredicate(Antlr4.Runtime.Parser recognizer, FailedPredicateException e) { e.ValidateNotNull(nameof(e)); switch (e.Predicate) { case "FeatureControlFlow": NotifyErrorListeners(recognizer, "Control flow expressions not supported in this version of the language", e); break; case "FeatureConditionalExpr": NotifyErrorListeners(recognizer, "Conditional expressions not supported in this version of the language", e); break; case "FeatureMutableVars": NotifyErrorListeners(recognizer, "Mutable variables not supported in this version of the language", e); break; default: base.ReportFailedPredicate(recognizer, e); break; } }
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); }
/// <summary> /// This is called by /// <see cref="ReportError(Parser, RecognitionException)"/> /// when the exception is a /// <see cref="FailedPredicateException"/> /// </summary> protected override void ReportFailedPredicate(Antlr4.Runtime.Parser recognizer, FailedPredicateException e) { string msg = ErrorMessageForFailedPredicate(e, recognizer); NotifyErrorListeners(recognizer, msg, e); }