chevrotain
Version:
Chevrotain is a high performance fault tolerant javascript parsing DSL for building recursive decent parsers
73 lines • 3.36 kB
JavaScript
import { EarlyExitException, isRecognitionException, NoViableAltException, } from "../../exceptions_public.js";
import { getLookaheadPathsForOptionalProd, getLookaheadPathsForOr, } from "../../grammar/lookahead.js";
import { DEFAULT_PARSER_CONFIG } from "../parser.js";
/**
* Trait responsible for runtime parsing errors.
*/
export class ErrorHandler {
initErrorHandler(config) {
this._errors = [];
this.errorMessageProvider = Object.hasOwn(config, "errorMessageProvider")
? config.errorMessageProvider // assumes end user provides the correct config value/type
: DEFAULT_PARSER_CONFIG.errorMessageProvider;
}
SAVE_ERROR(error) {
if (isRecognitionException(error)) {
error.context = {
ruleStack: this.getHumanReadableRuleStack(),
ruleOccurrenceStack: this.RULE_OCCURRENCE_STACK.slice(0, this.RULE_OCCURRENCE_STACK_IDX + 1),
};
this._errors.push(error);
return error;
}
else {
throw Error("Trying to save an Error which is not a RecognitionException");
}
}
get errors() {
return [...this._errors];
}
set errors(newErrors) {
this._errors = newErrors;
}
// TODO: consider caching the error message computed information
raiseEarlyExitException(occurrence, prodType, userDefinedErrMsg) {
const ruleName = this.getCurrRuleFullName();
const ruleGrammar = this.getGAstProductions()[ruleName];
const lookAheadPathsPerAlternative = getLookaheadPathsForOptionalProd(occurrence, ruleGrammar, prodType, this.maxLookahead);
const insideProdPaths = lookAheadPathsPerAlternative[0];
const actualTokens = [];
for (let i = 1; i <= this.maxLookahead; i++) {
actualTokens.push(this.LA(i));
}
const msg = this.errorMessageProvider.buildEarlyExitMessage({
expectedIterationPaths: insideProdPaths,
actual: actualTokens,
previous: this.LA(0),
customUserDescription: userDefinedErrMsg,
ruleName: ruleName,
});
throw this.SAVE_ERROR(new EarlyExitException(msg, this.LA(1), this.LA(0)));
}
// TODO: consider caching the error message computed information
raiseNoAltException(occurrence, errMsgTypes) {
const ruleName = this.getCurrRuleFullName();
const ruleGrammar = this.getGAstProductions()[ruleName];
// TODO: getLookaheadPathsForOr can be slow for large enough maxLookahead and certain grammars, consider caching ?
const lookAheadPathsPerAlternative = getLookaheadPathsForOr(occurrence, ruleGrammar, this.maxLookahead);
const actualTokens = [];
for (let i = 1; i <= this.maxLookahead; i++) {
actualTokens.push(this.LA(i));
}
const previousToken = this.LA(0);
const errMsg = this.errorMessageProvider.buildNoViableAltMessage({
expectedPathsPerAlt: lookAheadPathsPerAlternative,
actual: actualTokens,
previous: previousToken,
customUserDescription: errMsgTypes,
ruleName: this.getCurrRuleFullName(),
});
throw this.SAVE_ERROR(new NoViableAltException(errMsg, this.LA(1), previousToken));
}
}
//# sourceMappingURL=error_handler.js.map