import { stream as e } from "../utils/stream.js";
import { streamAllContents as t } from "../utils/ast-utils.js";
import { isAbstractParserRule as n, isKeyword as r, isTerminalRule as i } from "../languages/generated/ast.js";
import { escapeRegExp as a, isWhitespace as o, partialMatches as s } from "../utils/regexp-utils.js";
import { getAllReachableRules as c, terminalRegex as l } from "../utils/grammar-utils.js";
import { Lexer as u } from "../../../chevrotain/lib/src/scan/lexer_public.js";
import "../../../chevrotain/lib/src/api.js";
//#region ../../node_modules/langium/lib/parser/token-builder.js
var d = class {
	constructor() {
		this.diagnostics = [];
	}
	buildTokens(t, n) {
		let r = e(c(t, !1)), i = this.buildTerminalTokens(r), a = this.buildKeywordTokens(r, i, n);
		return a.push(...i), a;
	}
	flushLexingReport(e) {
		return { diagnostics: this.popDiagnostics() };
	}
	popDiagnostics() {
		let e = [...this.diagnostics];
		return this.diagnostics = [], e;
	}
	buildTerminalTokens(e) {
		return e.filter(i).filter((e) => !e.fragment).map((e) => this.buildTerminalToken(e)).toArray();
	}
	buildTerminalToken(e) {
		let t = l(e), n = this.requiresCustomPattern(t) ? this.regexPatternFunction(t) : t, r = {
			name: e.name,
			PATTERN: n
		};
		return typeof n == "function" && (r.LINE_BREAKS = !0), e.hidden && (r.GROUP = o(t) ? u.SKIPPED : "hidden"), r;
	}
	requiresCustomPattern(e) {
		return !!(e.flags.includes("u") || e.flags.includes("s"));
	}
	regexPatternFunction(e) {
		let t = new RegExp(e, e.flags + "y");
		return (e, n) => (t.lastIndex = n, t.exec(e));
	}
	buildKeywordTokens(e, i, a) {
		return e.filter(n).flatMap((e) => t(e).filter(r)).distinct((e) => e.value).toArray().sort((e, t) => t.value.length - e.value.length).map((e) => this.buildKeywordToken(e, i, !!a?.caseInsensitive));
	}
	buildKeywordToken(e, t, n) {
		let r = this.buildKeywordPattern(e, n), i = {
			name: e.value,
			PATTERN: r,
			LONGER_ALT: this.findLongerAlt(e, t)
		};
		return typeof r == "function" && (i.LINE_BREAKS = !0), i;
	}
	buildKeywordPattern(e, t) {
		return t ? new RegExp(a(e.value), "i") : e.value;
	}
	findLongerAlt(e, t) {
		return t.reduce((t, n) => {
			let r = n?.PATTERN;
			return r?.source && s("^" + r.source + "$", e.value) && t.push(n), t;
		}, []);
	}
};
//#endregion
export { d as DefaultTokenBuilder };
