import { IN as e } from "../../constants.js";
import { EOF as t, createTokenInstance as n, tokenMatcher as r } from "../../../scan/tokens_public.js";
import { NextAfterTokenWalker as i } from "../../grammar/interpreter.js";
import { MismatchedTokenException as a } from "../../exceptions_public.js";
import { DEFAULT_PARSER_CONFIG as o } from "../parser.js";
//#region ../../node_modules/chevrotain/lib/src/parse/parser/traits/recoverable.js
var s = {}, c = "InRuleRecoveryException", l = class extends Error {
	constructor(e) {
		super(e), this.name = c;
	}
}, u = class {
	initRecoverable(e) {
		this.firstAfterRepMap = {}, this.resyncFollows = {}, this.recoveryEnabled = Object.hasOwn(e, "recoveryEnabled") ? e.recoveryEnabled : o.recoveryEnabled, this.recoveryEnabled && (this.attemptInRepetitionRecovery = d);
	}
	getTokenToInsert(e) {
		let t = n(e, "", NaN, NaN, NaN, NaN, NaN, NaN);
		return t.isInsertedInRecovery = !0, t;
	}
	canTokenTypeBeInsertedInRecovery(e) {
		return !0;
	}
	canTokenTypeBeDeletedInRecovery(e) {
		return !0;
	}
	tryInRepetitionRecovery(e, t, n, r) {
		let i = this.findReSyncTokenType(), o = this.exportLexerState(), s = [], c = !1, l = this.LA_FAST(1), u = this.LA_FAST(1), d = () => {
			let e = this.LA(0), t = new a(this.errorMessageProvider.buildMismatchTokenMessage({
				expected: r,
				actual: l,
				previous: e,
				ruleName: this.getCurrRuleFullName()
			}), l, this.LA(0));
			t.resyncedTokens = s.slice(0, -1), this.SAVE_ERROR(t);
		};
		for (; !c;) if (this.tokenMatcher(u, r)) {
			d();
			return;
		} else if (n.call(this)) {
			d(), e.apply(this, t);
			return;
		} else this.tokenMatcher(u, i) ? c = !0 : (u = this.SKIP_TOKEN(), this.addToResyncTokens(u, s));
		this.importLexerState(o);
	}
	shouldInRepetitionRecoveryBeTried(e, t, n) {
		return !(n === !1 || this.tokenMatcher(this.LA_FAST(1), e) || this.isBackTracking() || this.canPerformInRuleRecovery(e, this.getFollowsForInRuleRecovery(e, t)));
	}
	getNextPossibleTokenTypes(e) {
		let t = e.ruleStack[0], n = this.getGAstProductions()[t];
		return new i(n, e).startWalking();
	}
	getFollowsForInRuleRecovery(e, t) {
		let n = this.getCurrentGrammarPath(e, t);
		return this.getNextPossibleTokenTypes(n);
	}
	tryInRuleRecovery(e, t) {
		if (this.canRecoverWithSingleTokenInsertion(e, t)) return this.getTokenToInsert(e);
		if (this.canRecoverWithSingleTokenDeletion(e)) {
			let e = this.SKIP_TOKEN();
			return this.consumeToken(), e;
		}
		throw new l("sad sad panda");
	}
	canPerformInRuleRecovery(e, t) {
		return this.canRecoverWithSingleTokenInsertion(e, t) || this.canRecoverWithSingleTokenDeletion(e);
	}
	canRecoverWithSingleTokenInsertion(e, t) {
		if (!this.canTokenTypeBeInsertedInRecovery(e) || t.length === 0) return !1;
		let n = this.LA_FAST(1);
		return t.find((e) => this.tokenMatcher(n, e)) !== void 0;
	}
	canRecoverWithSingleTokenDeletion(e) {
		return this.canTokenTypeBeDeletedInRecovery(e) ? this.tokenMatcher(this.LA(2), e) : !1;
	}
	isInCurrentRuleReSyncSet(e) {
		let t = this.getCurrFollowKey();
		return this.getFollowSetFromFollowKey(t).includes(e);
	}
	findReSyncTokenType() {
		let e = this.flattenFollowSet(), t = this.LA_FAST(1), n = 2;
		for (;;) {
			let i = e.find((e) => r(t, e));
			if (i !== void 0) return i;
			t = this.LA(n), n++;
		}
	}
	getCurrFollowKey() {
		if (this.RULE_STACK_IDX === 0) return s;
		let e = this.currRuleShortName, t = this.getLastExplicitRuleOccurrenceIndex(), n = this.getPreviousExplicitRuleShortName();
		return {
			ruleName: this.shortRuleNameToFullName(e),
			idxInCallingRule: t,
			inRule: this.shortRuleNameToFullName(n)
		};
	}
	buildFullFollowKeyStack() {
		let e = this.RULE_STACK, t = this.RULE_OCCURRENCE_STACK, n = this.RULE_STACK_IDX + 1, r = Array(n);
		for (let i = 0; i < n; i++) i === 0 ? r[i] = s : r[i] = {
			ruleName: this.shortRuleNameToFullName(e[i]),
			idxInCallingRule: t[i],
			inRule: this.shortRuleNameToFullName(e[i - 1])
		};
		return r;
	}
	flattenFollowSet() {
		return this.buildFullFollowKeyStack().map((e) => this.getFollowSetFromFollowKey(e)).flat();
	}
	getFollowSetFromFollowKey(n) {
		if (n === s) return [t];
		let r = n.ruleName + n.idxInCallingRule + e + n.inRule;
		return this.resyncFollows[r];
	}
	addToResyncTokens(e, n) {
		return this.tokenMatcher(e, t) || n.push(e), n;
	}
	reSyncTo(e) {
		let t = [], n = this.LA_FAST(1);
		for (; this.tokenMatcher(n, e) === !1;) n = this.SKIP_TOKEN(), this.addToResyncTokens(n, t);
		return t.slice(0, -1);
	}
	attemptInRepetitionRecovery(e, t, n, r, i, a, o) {}
	getCurrentGrammarPath(e, t) {
		return {
			ruleStack: this.getHumanReadableRuleStack(),
			occurrenceStack: this.RULE_OCCURRENCE_STACK.slice(0, this.RULE_OCCURRENCE_STACK_IDX + 1),
			lastTok: e,
			lastTokOccurrence: t
		};
	}
	getHumanReadableRuleStack() {
		let e = this.RULE_STACK_IDX + 1, t = Array(e);
		for (let n = 0; n < e; n++) t[n] = this.shortRuleNameToFullName(this.RULE_STACK[n]);
		return t;
	}
};
function d(e, n, r, i, a, o, s) {
	let c = this.getKeyForAutomaticLookahead(i, a), l = this.firstAfterRepMap[c];
	if (l === void 0) {
		let e = this.getCurrRuleFullName(), t = this.getGAstProductions()[e];
		l = new o(t, a).startWalking(), this.firstAfterRepMap[c] = l;
	}
	let u = l.token, d = l.occurrence, f = l.isEndOfRule;
	this.RULE_STACK_IDX === 0 && f && u === void 0 && (u = t, d = 1), !(u === void 0 || d === void 0) && this.shouldInRepetitionRecoveryBeTried(u, d, s) && this.tryInRepetitionRecovery(e, n, r, u);
}
//#endregion
export { c as IN_RULE_RECOVERY_EXCEPTION, u as Recoverable };
