| 1 |
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima). |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package com.eprosima.idl.parser.strategy; |
| 16 |
|
| 17 |
import com.eprosima.log.ColorMessage; |
| 18 |
import com.eprosima.idl.parser.exception.ParseException; |
| 19 |
import org.antlr.v4.runtime.Parser; |
| 20 |
import org.antlr.v4.runtime.RecognitionException; |
| 21 |
import org.antlr.v4.runtime.NoViableAltException; |
| 22 |
import org.antlr.v4.runtime.InputMismatchException; |
| 23 |
import org.antlr.v4.runtime.FailedPredicateException; |
| 24 |
import org.antlr.v4.runtime.Token; |
| 25 |
|
| 26 |
public class DefaultErrorStrategy extends org.antlr.v4.runtime.DefaultErrorStrategy |
| 27 |
{ |
| 28 |
public static final DefaultErrorStrategy INSTANCE = new DefaultErrorStrategy(); |
| 29 |
|
| 30 |
@Override |
| 31 |
public void reportError(Parser recognizer, RecognitionException e) |
| 32 |
{ |
| 33 |
if (inErrorRecoveryMode(recognizer)) { |
| 34 |
// System.err.print("[SPURIOUS] "); |
| 35 |
return; // don't report spurious errors |
| 36 |
} |
| 37 |
beginErrorCondition(recognizer); |
| 38 |
if ( e instanceof NoViableAltException ) { |
| 39 |
reportNoViableAlternative(recognizer, (NoViableAltException) e); |
| 40 |
} |
| 41 |
else if ( e instanceof InputMismatchException ) { |
| 42 |
reportInputMismatch(recognizer, (InputMismatchException)e); |
| 43 |
} |
| 44 |
else if ( e instanceof FailedPredicateException ) { |
| 45 |
reportFailedPredicate(recognizer, (FailedPredicateException)e); |
| 46 |
} |
| 47 |
else if ( e instanceof ParseException) { |
| 48 |
if(e.getOffendingToken() != null) |
| 49 |
{ |
| 50 |
String message = ColorMessage.bold(getTokenErrorDisplay(e.getOffendingToken()) + " ") + e.getMessage(); |
| 51 |
recognizer.notifyErrorListeners(e.getOffendingToken(), message, e); |
| 52 |
} |
| 53 |
else |
| 54 |
recognizer.notifyErrorListeners(e.getMessage()); |
| 55 |
} |
| 56 |
else { |
| 57 |
System.err.println("unknown recognition error type: "+e.getClass().getName()); |
| 58 |
recognizer.notifyErrorListeners(e.getOffendingToken(), e.getMessage(), e); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
protected void reportUnwantedToken(Parser recognizer) |
| 64 |
{ |
| 65 |
if (inErrorRecoveryMode(recognizer)) |
| 66 |
{ |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
beginErrorCondition(recognizer); |
| 71 |
Token t = recognizer.getCurrentToken(); |
| 72 |
String tokenName = getTokenErrorDisplay(t); |
| 73 |
String msg = "Unexpected input " + ColorMessage.bold(tokenName); |
| 74 |
recognizer.notifyErrorListeners(t, msg, null); |
| 75 |
} |
| 76 |
} |