public override void ReportError( RecognitionException e ) { StringTemplateGroup group = self.Group; if ( group == StringTemplate.defaultGroup ) { self.Error( "action parse error; template context is " + self.GetEnclosingInstanceStackString(), e ); } else { self.Error( "action parse error in group " + self.Group.Name + " line " + self.GroupFileLine + "; template context is " + self.GetEnclosingInstanceStackString(), e ); } }
public override void ReportError( RecognitionException e ) { /** TODO: not thought about recovery in lexer yet. * // if we've already reported an error and have not matched a token // yet successfully, don't report any errors. if ( errorRecovery ) { //System.err.print("[SPURIOUS] "); return; } errorRecovery = true; */ DisplayRecognitionError( this.TokenNames, e ); }
public override string GetErrorMessage( RecognitionException e, string[] tokenNames ) { string msg = null; if ( e is MismatchedTokenException ) { MismatchedTokenException mte = (MismatchedTokenException)e; msg = "mismatched character " + GetCharErrorDisplay( e.Character ) + " 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.Character ); } 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.Character ); } else if ( e is MismatchedNotSetException ) { MismatchedNotSetException mse = (MismatchedNotSetException)e; msg = "mismatched character " + GetCharErrorDisplay( e.Character ) + " expecting set " + mse.Expecting; } else if ( e is MismatchedSetException ) { MismatchedSetException mse = (MismatchedSetException)e; msg = "mismatched character " + GetCharErrorDisplay( e.Character ) + " expecting set " + mse.Expecting; } else if ( e is MismatchedRangeException ) { MismatchedRangeException mre = (MismatchedRangeException)e; msg = "mismatched character " + GetCharErrorDisplay( e.Character ) + " expecting set " + GetCharErrorDisplay( mre.A ) + ".." + GetCharErrorDisplay( mre.B ); } else { msg = base.GetErrorMessage( e, tokenNames ); } return msg; }
/** <summary> * Lexers can normally match any char in it's vocabulary after matching * a token, so do the easy thing and just kill a character and hope * it all works out. You can instead use the rule invocation stack * to do sophisticated error recovery if you are in a fragment rule. * </summary> */ public virtual void Recover( RecognitionException re ) { //System.out.println("consuming char "+(char)input.LA(1)+" during recovery"); //re.printStackTrace(); input.Consume(); }
protected virtual void DebugRecognitionException(RecognitionException ex) { IDebugEventListener dbg = recognizer.DebugListener; if (dbg != null) dbg.RecognitionException(ex); }
protected override object GetMissingSymbol( IIntStream input, RecognitionException e, int expectedTokenType, BitSet follow) { string tokenText = null; if ( expectedTokenType == TokenTypes.EndOfFile ) tokenText = "<missing EOF>"; else tokenText = "<missing " + TokenNames[expectedTokenType] + ">"; CommonToken t = new CommonToken( expectedTokenType, tokenText ); IToken current = ( (ITokenStream)input ).LT( 1 ); if ( current.Type == TokenTypes.EndOfFile ) { current = ( (ITokenStream)input ).LT( -1 ); } t.Line = current.Line; t.CharPositionInLine = current.CharPositionInLine; t.Channel = DefaultTokenChannel; t.InputStream = current.InputStream; return t; }
/** Not currently used */ public virtual object RecoverFromMismatchedSet( IIntStream input, RecognitionException e, BitSet follow) { if ( MismatchIsMissingToken( input, follow ) ) { // System.out.println("missing token"); ReportError( e ); // we don't know how to conjure up a token for sets yet return GetMissingSymbol( input, e, TokenTypes.Invalid, follow ); } // TODO do single token deletion like above for Token mismatch throw e; }
/** <summary>Report a recognition problem.</summary> * * <remarks> * This method sets errorRecovery to indicate the parser is recovering * not parsing. Once in recovery mode, no errors are generated. * To get out of recovery mode, the parser must successfully match * a token (after a resync). So it will go: * * 1. error occurs * 2. enter recovery mode, report error * 3. consume until token found in resynch set * 4. try to resume parsing * 5. next match() will reset errorRecovery mode * * If you override, make sure to update syntaxErrors if you care about that. * </remarks> */ public virtual void ReportError( RecognitionException e ) { // if we've already reported an error and have not matched a token // yet successfully, don't report any errors. if ( state.errorRecovery ) { //System.err.print("[SPURIOUS] "); return; } state.syntaxErrors++; // don't count spurious state.errorRecovery = true; DisplayRecognitionError( this.TokenNames, e ); }
/** <summary> * Recover from an error found on the input stream. This is * for NoViableAlt and mismatched symbol exceptions. If you enable * single token insertion and deletion, this will usually not * handle mismatched symbol exceptions but there could be a mismatched * token that the match() routine could not recover from. * </summary> */ public virtual void Recover( IIntStream input, RecognitionException re ) { if ( state.lastErrorIndex == input.Index ) { // uh oh, another error at same token index; must be a case // where LT(1) is in the recovery token set so nothing is // consumed; consume a single token so at least to prevent // an infinite loop; this is a failsafe. input.Consume(); } state.lastErrorIndex = input.Index; BitSet followSet = ComputeErrorRecoverySet(); BeginResync(); ConsumeUntil( input, followSet ); EndResync(); }
/** <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 is the error header, normally line/character position information?</summary> */ public virtual string GetErrorHeader( RecognitionException e ) { string prefix = SourceName ?? string.Empty; if (prefix.Length > 0) prefix += ' '; return string.Format("{0}line {1}:{2}", prefix, e.Line, e.CharPositionInLine + 1); }
public virtual void DisplayRecognitionError( string[] tokenNames, RecognitionException e) { string hdr = GetErrorHeader( e ); string msg = GetErrorMessage( e, tokenNames ); EmitErrorMessage( hdr + " " + msg ); }
/** <summary>Conjure up a missing token during error recovery.</summary> * * <remarks> * The recognizer attempts to recover from single missing * symbols. But, actions might refer to that missing symbol. * For example, x=ID {f($x);}. The action clearly assumes * that there has been an identifier matched previously and that * $x points at that token. If that token is missing, but * the next token in the stream is what we want we assume that * this token is missing and we keep going. Because we * have to return some token to replace the missing token, * we have to conjure one up. This method gives the user control * over the tokens returned for missing tokens. Mostly, * you will want to create something special for identifier * tokens. For literals such as '{' and ',', the default * action in the parser or tree parser works. It simply creates * a CommonToken of the appropriate type. The text will be the token. * If you change what tokens must be created by the lexer, * override this method to create the appropriate tokens. * </remarks> */ protected virtual object GetMissingSymbol( IIntStream input, RecognitionException e, int expectedTokenType, BitSet follow) { return null; }