import { augmentTokenTypes as e, isTokenType as t, tokenStructuredMatcher as n, tokenStructuredMatcherNoCategories as r } from "../../../scan/tokens.js";
import { EOF as i } from "../../../scan/tokens_public.js";
import { NextTerminalAfterAtLeastOneSepWalker as a, NextTerminalAfterAtLeastOneWalker as o, NextTerminalAfterManySepWalker as s, NextTerminalAfterManyWalker as c } from "../../grammar/interpreter.js";
import { PROD_TYPE as l } from "../../grammar/lookahead.js";
import { MismatchedTokenException as u, NotAllInputParsedException as d, isRecognitionException as f } from "../../exceptions_public.js";
import "./recoverable.js";
import { AT_LEAST_ONE_IDX as p, AT_LEAST_ONE_SEP_IDX as m, MANY_SEP_IDX as h } from "../../grammar/keys.js";
import { DEFAULT_RULE_CONFIG as g, END_OF_FILE as _ } from "../parser.js";
//#region ../../node_modules/chevrotain/lib/src/parse/parser/traits/recognizer_engine.js
var v = class {
	initRecognizerEngine(a, o) {
		if (this.className = this.constructor.name, this.shortRuleNameToFull = {}, this.fullRuleNameToShort = {}, this.ruleShortNameIdx = 256, this.tokenMatcher = r, this.subruleIdx = 0, this.currRuleShortName = 0, this.definedRulesNames = [], this.tokensMap = {}, this.isBackTrackingStack = [], this.RULE_STACK = [], this.RULE_STACK_IDX = -1, this.RULE_OCCURRENCE_STACK = [], this.RULE_OCCURRENCE_STACK_IDX = -1, this.gastProductionsCache = {}, Object.hasOwn(o, "serializedGrammar")) throw Error("The Parser's configuration can no longer contain a <serializedGrammar> property.\n	See: https://chevrotain.io/docs/changes/BREAKING_CHANGES.html#_6-0-0\n	For Further details.");
		if (Array.isArray(a)) {
			if (a.length === 0) throw Error("A Token Vocabulary cannot be empty.\n	Note that the first argument for the parser constructor\n	is no longer a Token vector (since v4.0).");
			if (typeof a[0].startOffset == "number") throw Error("The Parser constructor no longer accepts a token vector as the first argument.\n	See: https://chevrotain.io/docs/changes/BREAKING_CHANGES.html#_4-0-0\n	For Further details.");
		}
		if (Array.isArray(a)) this.tokensMap = a.reduce((e, t) => (e[t.name] = t, e), {});
		else if (Object.hasOwn(a, "modes") && Object.values(a.modes).flat().every(t)) {
			let e = Object.values(a.modes).flat();
			this.tokensMap = [...new Set(e)].reduce((e, t) => (e[t.name] = t, e), {});
		} else if (typeof a == "object" && a) this.tokensMap = Object.assign({}, a);
		else throw Error("<tokensDictionary> argument must be An Array of Token constructors, A dictionary of Token constructors or an IMultiModeLexerDefinition");
		this.tokensMap.EOF = i, this.tokenMatcher = (Object.hasOwn(a, "modes") ? Object.values(a.modes).flat() : Object.values(a)).every((e) => e.categoryMatches?.length == 0) ? r : n, e(Object.values(this.tokensMap));
	}
	defineRule(e, t, n) {
		if (this.selfAnalysisDone) throw Error(`Grammar rule <${e}> may not be defined after the 'performSelfAnalysis' method has been called'\nMake sure that all grammar rule definitions are done before 'performSelfAnalysis' is called.`);
		let r = Object.hasOwn(n, "resyncEnabled") ? n.resyncEnabled : g.resyncEnabled, i = Object.hasOwn(n, "recoveryValueFunc") ? n.recoveryValueFunc : g.recoveryValueFunc, a = this.ruleShortNameIdx << 12;
		this.ruleShortNameIdx++, this.shortRuleNameToFull[a] = e, this.fullRuleNameToShort[e] = a;
		let o;
		return o = this.outputCst === !0 ? function(...n) {
			try {
				this.ruleInvocationStateUpdate(a, e, this.subruleIdx), t.apply(this, n);
				let r = this.CST_STACK[this.CST_STACK.length - 1];
				return this.cstPostRule(r), r;
			} catch (e) {
				return this.invokeRuleCatch(e, r, i);
			} finally {
				this.ruleFinallyStateUpdate();
			}
		} : function(...n) {
			try {
				return this.ruleInvocationStateUpdate(a, e, this.subruleIdx), t.apply(this, n);
			} catch (e) {
				return this.invokeRuleCatch(e, r, i);
			} finally {
				this.ruleFinallyStateUpdate();
			}
		}, Object.assign(function(...t) {
			this.onBeforeParse(e);
			try {
				return o.apply(this, t);
			} finally {
				this.onAfterParse(e);
			}
		}, {
			ruleName: e,
			originalGrammarAction: t,
			coreRule: o
		});
	}
	invokeRuleCatch(e, t, n) {
		let r = this.RULE_STACK_IDX === 0, i = t && !this.isBackTracking() && this.recoveryEnabled;
		if (f(e)) {
			let t = e;
			if (i) {
				let r = this.findReSyncTokenType();
				if (this.isInCurrentRuleReSyncSet(r)) if (t.resyncedTokens = this.reSyncTo(r), this.outputCst) {
					let e = this.CST_STACK[this.CST_STACK.length - 1];
					return e.recoveredNode = !0, e;
				} else return n(e);
				else {
					if (this.outputCst) {
						let e = this.CST_STACK[this.CST_STACK.length - 1];
						e.recoveredNode = !0, t.partialCstResult = e;
					}
					throw t;
				}
			} else if (r) return this.moveToTerminatedState(), n(e);
			else throw t;
		} else throw e;
	}
	optionInternal(e, t) {
		let n = this.getKeyForAutomaticLookahead(512, t);
		return this.optionInternalLogic(e, t, n);
	}
	optionInternalLogic(e, t, n) {
		let r = this.getLaFuncFromCache(n), i;
		if (typeof e != "function") {
			i = e.DEF;
			let t = e.GATE;
			if (t !== void 0) {
				let e = r;
				r = () => t.call(this) && e.call(this);
			}
		} else i = e;
		if (r.call(this) === !0) return i.call(this);
	}
	atLeastOneInternal(e, t) {
		let n = this.getKeyForAutomaticLookahead(p, e);
		return this.atLeastOneInternalLogic(e, t, n);
	}
	atLeastOneInternalLogic(e, t, n) {
		let r = this.getLaFuncFromCache(n), i;
		if (typeof t != "function") {
			i = t.DEF;
			let e = t.GATE;
			if (e !== void 0) {
				let t = r;
				r = () => e.call(this) && t.call(this);
			}
		} else i = t;
		if (r.call(this) === !0) {
			let e = this.doSingleRepetition(i);
			for (; r.call(this) === !0 && e === !0;) e = this.doSingleRepetition(i);
		} else throw this.raiseEarlyExitException(e, l.REPETITION_MANDATORY, t.ERR_MSG);
		this.attemptInRepetitionRecovery(this.atLeastOneInternal, [e, t], r, p, e, o);
	}
	atLeastOneSepFirstInternal(e, t) {
		let n = this.getKeyForAutomaticLookahead(m, e);
		this.atLeastOneSepFirstInternalLogic(e, t, n);
	}
	atLeastOneSepFirstInternalLogic(e, t, n) {
		let r = t.DEF, i = t.SEP;
		if (this.getLaFuncFromCache(n).call(this) === !0) {
			r.call(this);
			let t = () => this.tokenMatcher(this.LA_FAST(1), i);
			for (; this.tokenMatcher(this.LA_FAST(1), i) === !0;) this.CONSUME(i), r.call(this);
			this.attemptInRepetitionRecovery(this.repetitionSepSecondInternal, [
				e,
				i,
				t,
				r,
				a
			], t, m, e, a);
		} else throw this.raiseEarlyExitException(e, l.REPETITION_MANDATORY_WITH_SEPARATOR, t.ERR_MSG);
	}
	manyInternal(e, t) {
		let n = this.getKeyForAutomaticLookahead(768, e);
		return this.manyInternalLogic(e, t, n);
	}
	manyInternalLogic(e, t, n) {
		let r = this.getLaFuncFromCache(n), i;
		if (typeof t != "function") {
			i = t.DEF;
			let e = t.GATE;
			if (e !== void 0) {
				let t = r;
				r = () => e.call(this) && t.call(this);
			}
		} else i = t;
		let a = !0;
		for (; r.call(this) === !0 && a === !0;) a = this.doSingleRepetition(i);
		this.attemptInRepetitionRecovery(this.manyInternal, [e, t], r, 768, e, c, a);
	}
	manySepFirstInternal(e, t) {
		let n = this.getKeyForAutomaticLookahead(h, e);
		this.manySepFirstInternalLogic(e, t, n);
	}
	manySepFirstInternalLogic(e, t, n) {
		let r = t.DEF, i = t.SEP;
		if (this.getLaFuncFromCache(n).call(this) === !0) {
			r.call(this);
			let t = () => this.tokenMatcher(this.LA_FAST(1), i);
			for (; this.tokenMatcher(this.LA_FAST(1), i) === !0;) this.CONSUME(i), r.call(this);
			this.attemptInRepetitionRecovery(this.repetitionSepSecondInternal, [
				e,
				i,
				t,
				r,
				s
			], t, h, e, s);
		}
	}
	repetitionSepSecondInternal(e, t, n, r, i) {
		for (; n();) this.CONSUME(t), r.call(this);
		/* istanbul ignore else */
		this.attemptInRepetitionRecovery(this.repetitionSepSecondInternal, [
			e,
			t,
			n,
			r,
			i
		], n, m, e, i);
	}
	doSingleRepetition(e) {
		let t = this.getLexerPosition();
		return e.call(this), this.getLexerPosition() > t;
	}
	orInternal(e, t) {
		let n = this.getKeyForAutomaticLookahead(256, t), r = Array.isArray(e) ? e : e.DEF, i = this.getLaFuncFromCache(n).call(this, r);
		if (i !== void 0) return r[i].ALT.call(this);
		this.raiseNoAltException(t, e.ERR_MSG);
	}
	ruleFinallyStateUpdate() {
		this.RULE_STACK_IDX--, this.RULE_OCCURRENCE_STACK_IDX--, this.RULE_STACK_IDX >= 0 && (this.currRuleShortName = this.RULE_STACK[this.RULE_STACK_IDX]), this.cstFinallyStateUpdate();
	}
	subruleInternal(e, t, n) {
		let r;
		try {
			let i = n === void 0 ? void 0 : n.ARGS;
			return this.subruleIdx = t, r = e.coreRule.apply(this, i), this.cstPostNonTerminal(r, n !== void 0 && n.LABEL !== void 0 ? n.LABEL : e.ruleName), r;
		} catch (t) {
			throw this.subruleInternalError(t, n, e.ruleName);
		}
	}
	subruleInternalError(e, t, n) {
		throw f(e) && e.partialCstResult !== void 0 && (this.cstPostNonTerminal(e.partialCstResult, t !== void 0 && t.LABEL !== void 0 ? t.LABEL : n), delete e.partialCstResult), e;
	}
	consumeInternal(e, t, n) {
		let r;
		try {
			let t = this.LA_FAST(1);
			this.tokenMatcher(t, e) === !0 ? (this.consumeToken(), r = t) : this.consumeInternalError(e, t, n);
		} catch (n) {
			r = this.consumeInternalRecovery(e, t, n);
		}
		return this.cstPostTerminal(n !== void 0 && n.LABEL !== void 0 ? n.LABEL : e.name, r), r;
	}
	consumeInternalError(e, t, n) {
		let r, i = this.LA(0);
		throw r = n !== void 0 && n.ERR_MSG ? n.ERR_MSG : this.errorMessageProvider.buildMismatchTokenMessage({
			expected: e,
			actual: t,
			previous: i,
			ruleName: this.getCurrRuleFullName()
		}), this.SAVE_ERROR(new u(r, t, i));
	}
	consumeInternalRecovery(e, t, n) {
		if (this.recoveryEnabled && n.name === "MismatchedTokenException" && !this.isBackTracking()) {
			let r = this.getFollowsForInRuleRecovery(e, t);
			try {
				return this.tryInRuleRecovery(e, r);
			} catch (e) {
				throw e.name === "InRuleRecoveryException" ? n : e;
			}
		} else throw n;
	}
	saveRecogState() {
		let e = this.errors, t = this.RULE_STACK.slice(0, this.RULE_STACK_IDX + 1);
		return {
			errors: e,
			lexerState: this.exportLexerState(),
			RULE_STACK: t,
			CST_STACK: this.CST_STACK
		};
	}
	reloadRecogState(e) {
		this.errors = e.errors, this.importLexerState(e.lexerState);
		let t = e.RULE_STACK;
		for (let e = 0; e < t.length; e++) this.RULE_STACK[e] = t[e];
		this.RULE_STACK_IDX = t.length - 1, this.RULE_STACK_IDX >= 0 && (this.currRuleShortName = this.RULE_STACK[this.RULE_STACK_IDX]);
	}
	ruleInvocationStateUpdate(e, t, n) {
		this.RULE_OCCURRENCE_STACK[++this.RULE_OCCURRENCE_STACK_IDX] = n, this.RULE_STACK[++this.RULE_STACK_IDX] = e, this.currRuleShortName = e, this.cstInvocationStateUpdate(t);
	}
	isBackTracking() {
		return this.isBackTrackingStack.length !== 0;
	}
	getCurrRuleFullName() {
		let e = this.currRuleShortName;
		return this.shortRuleNameToFull[e];
	}
	shortRuleNameToFullName(e) {
		return this.shortRuleNameToFull[e];
	}
	isAtEndOfInput() {
		return this.tokenMatcher(this.LA(1), i);
	}
	reset() {
		this.resetLexerState(), this.subruleIdx = 0, this.currRuleShortName = 0, this.isBackTrackingStack = [], this.errors = [], this.RULE_STACK_IDX = -1, this.RULE_OCCURRENCE_STACK_IDX = -1, this.CST_STACK = [];
	}
	onBeforeParse(e) {
		for (let e = 0; e < this.maxLookahead + 1; e++) this.tokVector.push(_);
	}
	onAfterParse(e) {
		if (this.isAtEndOfInput() === !1) {
			let e = this.LA(1), t = this.errorMessageProvider.buildNotAllInputParsedMessage({
				firstRedundant: e,
				ruleName: this.getCurrRuleFullName()
			});
			this.SAVE_ERROR(new d(t, e));
		}
		for (; this.tokVector.at(-1) === _;) this.tokVector.pop();
	}
};
//#endregion
export { v as RecognizerEngine };
