-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: pass appropriate error handler for antlr adapter
Signed-off-by: Aman Prashant <aman.prashant@broadcom.com>
- Loading branch information
Showing
11 changed files
with
161 additions
and
141 deletions.
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
server/engine/src/main/java/org/eclipse/lsp/cobol/core/strategy/BasicCobolErrorHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Copyright (c) 2024 Broadcom. | ||
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Broadcom, Inc. - initial API and implementation | ||
* | ||
*/ | ||
package org.eclipse.lsp.cobol.core.strategy; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.antlr.v4.runtime.*; | ||
import org.eclipse.lsp.cobol.common.message.MessageService; | ||
import org.eclipse.lsp.cobol.common.message.MessageServiceProvider; | ||
|
||
/** | ||
* This implementation of the error strategy customizes error messages that are extracted from the | ||
* parsing exceptions | ||
*/ | ||
@Slf4j | ||
// for test | ||
@NoArgsConstructor | ||
public class BasicCobolErrorHandler extends DefaultErrorStrategy implements MessageServiceProvider { | ||
private static final String REPORT_NO_VIABLE_ALTERNATIVE = | ||
"ErrorStrategy.reportNoViableAlternative"; | ||
private static final String REPORT_MISSING_TOKEN = "ErrorStrategy.reportMissingToken"; | ||
|
||
@Getter | ||
@Setter | ||
private MessageService messageService; | ||
@Getter @Setter private ErrorMessageHelper errorMessageHelper; | ||
|
||
public BasicCobolErrorHandler(MessageService messageService) { | ||
this.messageService = messageService; | ||
this.errorMessageHelper = new ErrorMessageHelper(messageService); | ||
} | ||
|
||
@Override | ||
public void reportError(Parser recognizer, RecognitionException e) { | ||
// if we've already reported an error and have not matched a token | ||
// yet successfully, don't report any errors. | ||
if (!inErrorRecoveryMode(recognizer)) { | ||
beginErrorCondition(recognizer); | ||
reportErrorByType(recognizer, e); | ||
} | ||
} | ||
|
||
private void reportErrorByType(Parser recognizer, RecognitionException e) { | ||
if (e instanceof InputMismatchException) { | ||
reportInputMismatch(recognizer, (InputMismatchException) e); | ||
return; | ||
} | ||
if (e instanceof NoViableAltException) { | ||
reportNoViableAlternative(recognizer, (NoViableAltException) e); | ||
return; | ||
} | ||
if (e instanceof FailedPredicateException) { | ||
reportFailedPredicate(recognizer, (FailedPredicateException) e); | ||
return; | ||
} | ||
reportUnrecognizedException(recognizer, e); | ||
} | ||
|
||
private void reportUnrecognizedException(Parser recognizer, RecognitionException e) { | ||
LOG.error("unknown recognition error type: " + e.getClass().getName()); | ||
recognizer.notifyErrorListeners(e.getOffendingToken(), e.getMessage(), e); | ||
} | ||
|
||
@Override | ||
protected void reportInputMismatch(Parser recognizer, InputMismatchException e) { | ||
Token token = e.getOffendingToken(); | ||
String msg = | ||
errorMessageHelper.getInputMismatchMessage(recognizer, e, token, getOffendingToken(e)); | ||
recognizer.notifyErrorListeners(token, msg, e); | ||
} | ||
|
||
@Override | ||
protected void reportNoViableAlternative(Parser recognizer, NoViableAltException e) { | ||
String messageParams = errorMessageHelper.retrieveInputForNoViableException(recognizer, e); | ||
String msg = messageService.getMessage(REPORT_NO_VIABLE_ALTERNATIVE, messageParams); | ||
recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e); | ||
} | ||
|
||
@Override | ||
protected void reportUnwantedToken(Parser recognizer) { | ||
if (inErrorRecoveryMode(recognizer)) { | ||
return; | ||
} | ||
beginErrorCondition(recognizer); | ||
Token currentToken = recognizer.getCurrentToken(); | ||
String msg = | ||
errorMessageHelper.getUnwantedTokenMessage( | ||
recognizer, currentToken, getTokenErrorDisplay(currentToken)); | ||
recognizer.notifyErrorListeners(currentToken, msg, null); | ||
} | ||
|
||
@Override | ||
protected void reportMissingToken(Parser recognizer) { | ||
if (inErrorRecoveryMode(recognizer)) { | ||
return; | ||
} | ||
beginErrorCondition(recognizer); | ||
String msg = | ||
messageService.getMessage( | ||
REPORT_MISSING_TOKEN, | ||
errorMessageHelper.getExpectedText(recognizer), | ||
ErrorMessageHelper.getRule(recognizer)); | ||
recognizer.notifyErrorListeners(getPreviousToken(recognizer), msg, null); | ||
} | ||
|
||
private Token getPreviousToken(Parser recognizer) { | ||
if (recognizer.getCurrentToken().getText().trim().length() == 1) { | ||
return recognizer.getCurrentToken(); | ||
} | ||
int index = recognizer.getCurrentToken().getTokenIndex(); | ||
while (index > 0) { | ||
index--; | ||
Token token = recognizer.getTokenStream().get(index); | ||
if (!token.getText().trim().isEmpty()) { | ||
return token; | ||
} | ||
} | ||
return recognizer.getCurrentToken(); | ||
} | ||
|
||
private String getOffendingToken(InputMismatchException e) { | ||
return getTokenErrorDisplay(e.getOffendingToken()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.