Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1982 - skips building errors / CST during backtracking #1983

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/chevrotain/src/parse/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
import {
CstNode,
IParserConfig,
IRecognitionException,
IRuleConfig,
IToken,
TokenType,
Expand Down Expand Up @@ -111,7 +110,6 @@ export interface IParserUnresolvedRefDefinitionError
}

export interface IParserState {
errors: IRecognitionException[];
lexerState: any;
RULE_STACK: number[];
CST_STACK: CstNode[];
Expand Down
11 changes: 11 additions & 0 deletions packages/chevrotain/src/parse/parser/traits/error_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { MixedInParser } from "./parser_traits.js";
import { DEFAULT_PARSER_CONFIG } from "../parser.js";

const BACKTRACKING_ERROR = "Error during backtracking attempt";
/**
* Trait responsible for runtime parsing errors.
*/
Expand Down Expand Up @@ -64,6 +65,9 @@ export class ErrorHandler {
prodType: PROD_TYPE,
userDefinedErrMsg: string | undefined,
): never {
if (this.isBackTracking()) {
throw new EarlyExitException(BACKTRACKING_ERROR, this.LA(1), this.LA(0));
}
const ruleName = this.getCurrRuleFullName();
const ruleGrammar = this.getGAstProductions()[ruleName];
const lookAheadPathsPerAlternative = getLookaheadPathsForOptionalProd(
Expand Down Expand Up @@ -94,6 +98,13 @@ export class ErrorHandler {
occurrence: number,
errMsgTypes: string | undefined,
): never {
if (this.isBackTracking()) {
throw new NoViableAltException(
BACKTRACKING_ERROR,
this.LA(1),
this.LA(0),
);
}
const ruleName = this.getCurrRuleFullName();
const ruleGrammar = this.getGAstProductions()[ruleName];
// TODO: getLookaheadPathsForOr can be slow for large enough maxLookahead and certain grammars, consider caching ?
Expand Down
62 changes: 42 additions & 20 deletions packages/chevrotain/src/parse/parser/traits/recognizer_engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,11 @@ export class RecognizerEngine {
): R {
try {
this.ruleInvocationStateUpdate(shortName, ruleName, this.subruleIdx);
impl.apply(this, args);
const cst = this.CST_STACK[this.CST_STACK.length - 1];
let cst = impl.apply(this, args);
if (this.isBackTracking()) {
return cst;
}
cst = this.CST_STACK[this.CST_STACK.length - 1];
this.cstPostRule(cst);
return cst as unknown as R;
} catch (e) {
Expand Down Expand Up @@ -664,6 +667,10 @@ export class RecognizerEngine {
this.RULE_STACK.pop();
this.RULE_OCCURRENCE_STACK.pop();

if (this.isBackTracking()) {
return;
}

// NOOP when cst is disabled
this.cstFinallyStateUpdate();

Expand All @@ -686,18 +693,24 @@ export class RecognizerEngine {
options?: SubruleMethodOpts<ARGS>,
): R {
let ruleResult;
const isBackTracking = this.isBackTracking();
try {
const args = options !== undefined ? options.ARGS : undefined;
this.subruleIdx = idx;
ruleResult = ruleToCall.apply(this, args);
this.cstPostNonTerminal(
ruleResult,
options !== undefined && options.LABEL !== undefined
? options.LABEL
: ruleToCall.ruleName,
);
if (!isBackTracking) {
this.cstPostNonTerminal(
ruleResult,
options !== undefined && options.LABEL !== undefined
? options.LABEL
: ruleToCall.ruleName,
);
}
return ruleResult;
} catch (e) {
if (isBackTracking) {
throw e;
}
throw this.subruleInternalError(e, options, ruleToCall.ruleName);
}
}
Expand Down Expand Up @@ -728,28 +741,39 @@ export class RecognizerEngine {
options: ConsumeMethodOpts | undefined,
): IToken {
let consumedToken!: IToken;
let backtrackingError!: Error;
try {
const nextToken = this.LA(1);
if (this.tokenMatcher(nextToken, tokType) === true) {
this.consumeToken();
consumedToken = nextToken;
} else {
if (this.isBackTracking()) {
backtrackingError = new Error();
backtrackingError.name = "MismatchedTokenException";
throw backtrackingError;
}
this.consumeInternalError(tokType, nextToken, options);
}
} catch (eFromConsumption) {
if (eFromConsumption === backtrackingError) {
throw backtrackingError;
}
consumedToken = this.consumeInternalRecovery(
tokType,
idx,
eFromConsumption,
);
}

this.cstPostTerminal(
options !== undefined && options.LABEL !== undefined
? options.LABEL
: tokType.name,
consumedToken,
);
if (!this.isBackTracking()) {
this.cstPostTerminal(
options !== undefined && options.LABEL !== undefined
? options.LABEL
: tokType.name,
consumedToken,
);
}
return consumedToken;
}

Expand Down Expand Up @@ -787,8 +811,7 @@ export class RecognizerEngine {
if (
this.recoveryEnabled &&
// TODO: more robust checking of the exception type. Perhaps Typescript extending expressions?
eFromConsumption.name === "MismatchedTokenException" &&
!this.isBackTracking()
eFromConsumption.name === "MismatchedTokenException"
) {
const follows = this.getFollowsForInRuleRecovery(<any>tokType, idx);
try {
Expand All @@ -809,18 +832,15 @@ export class RecognizerEngine {

saveRecogState(this: MixedInParser): IParserState {
// errors is a getter which will clone the errors array
const savedErrors = this.errors;
const savedRuleStack = clone(this.RULE_STACK);
return {
errors: savedErrors,
lexerState: this.exportLexerState(),
RULE_STACK: savedRuleStack,
CST_STACK: this.CST_STACK,
};
}

reloadRecogState(this: MixedInParser, newState: IParserState) {
this.errors = newState.errors;
this.importLexerState(newState.lexerState);
this.RULE_STACK = newState.RULE_STACK;
}
Expand All @@ -834,7 +854,9 @@ export class RecognizerEngine {
this.RULE_OCCURRENCE_STACK.push(idxInCallingRule);
this.RULE_STACK.push(shortName);
// NOOP when cst is disabled
this.cstInvocationStateUpdate(fullName);
if (!this.isBackTracking()) {
this.cstInvocationStateUpdate(fullName);
}
}

isBackTracking(this: MixedInParser): boolean {
Expand Down