@@ -174,6 +174,37 @@ protected override void ReportMissingToken(Parser recognizer)
174174 catch ( InputMismatchException ) { } // ignored
175175 }
176176
177+ protected bool inSevereError = false ; // 记录当前错误是不是严重错误。只有触发ReportError的才是严重错误,触发ReportMissingToken和ReportUnwantedToken的不是严重错误。
178+ // 错误报告时的规则:多个严重错误不报告后面的,但如果前面的是非严重错误、后面遇到严重错误,则还是需要报告。
179+ protected override void EndErrorCondition ( Parser recognizer )
180+ {
181+ base . EndErrorCondition ( recognizer ) ;
182+ inSevereError = false ;
183+ }
184+
185+ public override void ReportError ( Parser recognizer , RecognitionException e )
186+ {
187+ if ( inSevereError ) return ; // 如果前面已经有严重错误了,则不再报告后续的严重错误;但如果前面是非严重错误,则还是得报告
188+ this . BeginErrorCondition ( recognizer ) ;
189+ inSevereError = true ;
190+ switch ( e )
191+ {
192+ case NoViableAltException _:
193+ this . ReportNoViableAlternative ( recognizer , ( NoViableAltException ) e ) ;
194+ break ;
195+ case InputMismatchException _:
196+ this . ReportInputMismatch ( recognizer , ( InputMismatchException ) e ) ;
197+ break ;
198+ case FailedPredicateException _:
199+ this . ReportFailedPredicate ( recognizer , ( FailedPredicateException ) e ) ;
200+ break ;
201+ default :
202+ Console . Error . WriteLine ( "unknown recognition error type: " + e . GetType ( ) . FullName ) ;
203+ this . NotifyErrorListeners ( recognizer , e . Message , e ) ;
204+ break ;
205+ }
206+ }
207+
177208 private List < int > recoverySetAllowed = [
178209 L . COMMA , L . FALSE_EACH , Utils . TokenType ( "/" ) , Utils . TokenType ( "(" ) , Utils . TokenType ( "{" )
179210 ] ; // recover时,为了确保整个吞掉不合法的音符,而不是出现残缺的东西导致parser报错,只准同步到上面这些字符当中
0 commit comments