function clone(l) {
	return doClone(l);
}
function doClone(l) {
	return Array.isArray(l) ? cloneArray(l) : l instanceof RegExp ? l : typeof l == "object" ? cloneObj(l) : l;
}
function cloneArray(l) {
	let z = [];
	for (let B = 0, V = l.length; B < V; B++) z[B] = doClone(l[B]);
	return z;
}
function cloneObj(l) {
	let z = {};
	for (let B in l) z[B] = doClone(l[B]);
	return z;
}
function mergeObjects(l, ...R) {
	return R.forEach((R) => {
		for (let z in R) l[z] = R[z];
	}), l;
}
function basename(l) {
	let R = ~l.lastIndexOf("/") || ~l.lastIndexOf("\\");
	return R === 0 ? l : ~R === l.length - 1 ? basename(l.substring(0, l.length - 1)) : l.substr(~R + 1);
}
var CAPTURING_REGEX_SOURCE = /\$(\d+)|\${(\d+):\/(downcase|upcase)}/g, RegexSource = class {
	static hasCaptures(l) {
		return l === null ? !1 : (CAPTURING_REGEX_SOURCE.lastIndex = 0, CAPTURING_REGEX_SOURCE.test(l));
	}
	static replaceCaptures(l, R, z) {
		return l.replace(CAPTURING_REGEX_SOURCE, (l, B, V, H) => {
			let U = z[parseInt(B || V, 10)];
			if (U) {
				let l = R.substring(U.start, U.end);
				for (; l[0] === ".";) l = l.substring(1);
				switch (H) {
					case "downcase": return l.toLowerCase();
					case "upcase": return l.toUpperCase();
					default: return l;
				}
			} else return l;
		});
	}
};
function strcmp(l, R) {
	return l < R ? -1 : l > R ? 1 : 0;
}
function strArrCmp(l, R) {
	if (l === null && R === null) return 0;
	if (!l) return -1;
	if (!R) return 1;
	let z = l.length, B = R.length;
	if (z === B) {
		for (let B = 0; B < z; B++) {
			let z = strcmp(l[B], R[B]);
			if (z !== 0) return z;
		}
		return 0;
	}
	return z - B;
}
function isValidHexColor(l) {
	return !!(/^#[0-9a-f]{6}$/i.test(l) || /^#[0-9a-f]{8}$/i.test(l) || /^#[0-9a-f]{3}$/i.test(l) || /^#[0-9a-f]{4}$/i.test(l));
}
function escapeRegExpCharacters(l) {
	return l.replace(/[\-\\\{\}\*\+\?\|\^\$\.\,\[\]\(\)\#\s]/g, "\\$&");
}
var CachedFn = class {
	constructor(l) {
		this.fn = l;
	}
	cache = /* @__PURE__ */ new Map();
	get(l) {
		if (this.cache.has(l)) return this.cache.get(l);
		let R = this.fn(l);
		return this.cache.set(l, R), R;
	}
}, Theme = class {
	constructor(l, R, z) {
		this._colorMap = l, this._defaults = R, this._root = z;
	}
	static createFromRawTheme(l, R) {
		return this.createFromParsedTheme(parseTheme(l), R);
	}
	static createFromParsedTheme(l, R) {
		return resolveParsedThemeRules(l, R);
	}
	_cachedMatchRoot = new CachedFn((l) => this._root.match(l));
	getColorMap() {
		return this._colorMap.getColorMap();
	}
	getDefaults() {
		return this._defaults;
	}
	match(l) {
		if (l === null) return this._defaults;
		let R = l.scopeName, z = this._cachedMatchRoot.get(R).find((R) => _scopePathMatchesParentScopes(l.parent, R.parentScopes));
		return z ? new StyleAttributes(z.fontStyle, z.foreground, z.background) : null;
	}
}, ScopeStack = class l {
	constructor(l, R) {
		this.parent = l, this.scopeName = R;
	}
	static push(R, z) {
		for (let B of z) R = new l(R, B);
		return R;
	}
	static from(...R) {
		let z = null;
		for (let B = 0; B < R.length; B++) z = new l(z, R[B]);
		return z;
	}
	push(R) {
		return new l(this, R);
	}
	getSegments() {
		let l = this, R = [];
		for (; l;) R.push(l.scopeName), l = l.parent;
		return R.reverse(), R;
	}
	toString() {
		return this.getSegments().join(" ");
	}
	extends(l) {
		return this === l ? !0 : this.parent === null ? !1 : this.parent.extends(l);
	}
	getExtensionIfDefined(l) {
		let R = [], z = this;
		for (; z && z !== l;) R.push(z.scopeName), z = z.parent;
		return z === l ? R.reverse() : void 0;
	}
};
function _scopePathMatchesParentScopes(l, R) {
	if (R.length === 0) return !0;
	for (let z = 0; z < R.length; z++) {
		let B = R[z], V = !1;
		if (B === ">") {
			if (z === R.length - 1) return !1;
			B = R[++z], V = !0;
		}
		for (; l && !_matchesScope(l.scopeName, B);) {
			if (V) return !1;
			l = l.parent;
		}
		if (!l) return !1;
		l = l.parent;
	}
	return !0;
}
function _matchesScope(l, R) {
	return R === l || l.startsWith(R) && l[R.length] === ".";
}
var StyleAttributes = class {
	constructor(l, R, z) {
		this.fontStyle = l, this.foregroundId = R, this.backgroundId = z;
	}
};
function parseTheme(l) {
	if (!l || !l.settings || !Array.isArray(l.settings)) return [];
	let R = l.settings, z = [], B = 0;
	for (let l = 0, V = R.length; l < V; l++) {
		let V = R[l];
		if (!V.settings) continue;
		let H;
		if (typeof V.scope == "string") {
			let l = V.scope;
			l = l.replace(/^[,]+/, ""), l = l.replace(/[,]+$/, ""), H = l.split(",");
		} else H = Array.isArray(V.scope) ? V.scope : [""];
		let U = -1;
		if (typeof V.settings.fontStyle == "string") {
			U = 0;
			let l = V.settings.fontStyle.split(" ");
			for (let R = 0, z = l.length; R < z; R++) switch (l[R]) {
				case "italic":
					U |= 1;
					break;
				case "bold":
					U |= 2;
					break;
				case "underline":
					U |= 4;
					break;
				case "strikethrough":
					U |= 8;
					break;
			}
		}
		let W = null;
		typeof V.settings.foreground == "string" && isValidHexColor(V.settings.foreground) && (W = V.settings.foreground);
		let G = null;
		typeof V.settings.background == "string" && isValidHexColor(V.settings.background) && (G = V.settings.background);
		for (let R = 0, V = H.length; R < V; R++) {
			let V = H[R].trim().split(" "), K = V[V.length - 1], q = null;
			V.length > 1 && (q = V.slice(0, V.length - 1), q.reverse()), z[B++] = new ParsedThemeRule(K, q, l, U, W, G);
		}
	}
	return z;
}
var ParsedThemeRule = class {
	constructor(l, R, z, B, V, H) {
		this.scope = l, this.parentScopes = R, this.index = z, this.fontStyle = B, this.foreground = V, this.background = H;
	}
}, FontStyle = /* @__PURE__ */ ((l) => (l[l.NotSet = -1] = "NotSet", l[l.None = 0] = "None", l[l.Italic = 1] = "Italic", l[l.Bold = 2] = "Bold", l[l.Underline = 4] = "Underline", l[l.Strikethrough = 8] = "Strikethrough", l))(FontStyle || {});
function resolveParsedThemeRules(l, R) {
	l.sort((l, R) => {
		let z = strcmp(l.scope, R.scope);
		return z !== 0 || (z = strArrCmp(l.parentScopes, R.parentScopes), z !== 0) ? z : l.index - R.index;
	});
	let z = 0, B = "#000000", V = "#ffffff";
	for (; l.length >= 1 && l[0].scope === "";) {
		let R = l.shift();
		R.fontStyle !== -1 && (z = R.fontStyle), R.foreground !== null && (B = R.foreground), R.background !== null && (V = R.background);
	}
	let H = new ColorMap(R), U = new StyleAttributes(z, H.getId(B), H.getId(V)), W = new ThemeTrieElement(new ThemeTrieElementRule(0, null, -1, 0, 0), []);
	for (let R = 0, z = l.length; R < z; R++) {
		let z = l[R];
		W.insert(0, z.scope, z.parentScopes, z.fontStyle, H.getId(z.foreground), H.getId(z.background));
	}
	return new Theme(H, U, W);
}
var ColorMap = class {
	_isFrozen;
	_lastColorId;
	_id2color;
	_color2id;
	constructor(l) {
		if (this._lastColorId = 0, this._id2color = [], this._color2id = /* @__PURE__ */ Object.create(null), Array.isArray(l)) {
			this._isFrozen = !0;
			for (let R = 0, z = l.length; R < z; R++) this._color2id[l[R]] = R, this._id2color[R] = l[R];
		} else this._isFrozen = !1;
	}
	getId(l) {
		if (l === null) return 0;
		l = l.toUpperCase();
		let R = this._color2id[l];
		if (R) return R;
		if (this._isFrozen) throw Error(`Missing color in color map - ${l}`);
		return R = ++this._lastColorId, this._color2id[l] = R, this._id2color[R] = l, R;
	}
	getColorMap() {
		return this._id2color.slice(0);
	}
}, emptyParentScopes = Object.freeze([]), ThemeTrieElementRule = class l {
	scopeDepth;
	parentScopes;
	fontStyle;
	foreground;
	background;
	constructor(l, R, z, B, V) {
		this.scopeDepth = l, this.parentScopes = R || emptyParentScopes, this.fontStyle = z, this.foreground = B, this.background = V;
	}
	clone() {
		return new l(this.scopeDepth, this.parentScopes, this.fontStyle, this.foreground, this.background);
	}
	static cloneArr(l) {
		let R = [];
		for (let z = 0, B = l.length; z < B; z++) R[z] = l[z].clone();
		return R;
	}
	acceptOverwrite(l, R, z, B) {
		this.scopeDepth > l ? console.log("how did this happen?") : this.scopeDepth = l, R !== -1 && (this.fontStyle = R), z !== 0 && (this.foreground = z), B !== 0 && (this.background = B);
	}
}, ThemeTrieElement = class l {
	constructor(l, R = [], z = {}) {
		this._mainRule = l, this._children = z, this._rulesWithParentScopes = R;
	}
	_rulesWithParentScopes;
	static _cmpBySpecificity(l, R) {
		if (l.scopeDepth !== R.scopeDepth) return R.scopeDepth - l.scopeDepth;
		let z = 0, B = 0;
		for (; l.parentScopes[z] === ">" && z++, R.parentScopes[B] === ">" && B++, !(z >= l.parentScopes.length || B >= R.parentScopes.length);) {
			let V = R.parentScopes[B].length - l.parentScopes[z].length;
			if (V !== 0) return V;
			z++, B++;
		}
		return R.parentScopes.length - l.parentScopes.length;
	}
	match(R) {
		if (R !== "") {
			let l = R.indexOf("."), z, B;
			if (l === -1 ? (z = R, B = "") : (z = R.substring(0, l), B = R.substring(l + 1)), this._children.hasOwnProperty(z)) return this._children[z].match(B);
		}
		let z = this._rulesWithParentScopes.concat(this._mainRule);
		return z.sort(l._cmpBySpecificity), z;
	}
	insert(R, z, B, V, H, U) {
		if (z === "") {
			this._doInsertHere(R, B, V, H, U);
			return;
		}
		let W = z.indexOf("."), G, K;
		W === -1 ? (G = z, K = "") : (G = z.substring(0, W), K = z.substring(W + 1));
		let q;
		this._children.hasOwnProperty(G) ? q = this._children[G] : (q = new l(this._mainRule.clone(), ThemeTrieElementRule.cloneArr(this._rulesWithParentScopes)), this._children[G] = q), q.insert(R + 1, K, B, V, H, U);
	}
	_doInsertHere(l, R, z, B, V) {
		if (R === null) {
			this._mainRule.acceptOverwrite(l, z, B, V);
			return;
		}
		for (let H = 0, U = this._rulesWithParentScopes.length; H < U; H++) {
			let U = this._rulesWithParentScopes[H];
			if (strArrCmp(U.parentScopes, R) === 0) {
				U.acceptOverwrite(l, z, B, V);
				return;
			}
		}
		z === -1 && (z = this._mainRule.fontStyle), B === 0 && (B = this._mainRule.foreground), V === 0 && (V = this._mainRule.background), this._rulesWithParentScopes.push(new ThemeTrieElementRule(l, R, z, B, V));
	}
}, EncodedTokenMetadata = class l {
	static toBinaryStr(l) {
		return l.toString(2).padStart(32, "0");
	}
	static print(R) {
		let z = l.getLanguageId(R), B = l.getTokenType(R), V = l.getFontStyle(R), H = l.getForeground(R), U = l.getBackground(R);
		console.log({
			languageId: z,
			tokenType: B,
			fontStyle: V,
			foreground: H,
			background: U
		});
	}
	static getLanguageId(l) {
		return (l & 255) >>> 0;
	}
	static getTokenType(l) {
		return (l & 768) >>> 8;
	}
	static containsBalancedBrackets(l) {
		return (l & 1024) != 0;
	}
	static getFontStyle(l) {
		return (l & 30720) >>> 11;
	}
	static getForeground(l) {
		return (l & 16744448) >>> 15;
	}
	static getBackground(l) {
		return (l & 4278190080) >>> 24;
	}
	static set(R, z, B, V, H, U, W) {
		let G = l.getLanguageId(R), K = l.getTokenType(R), q = l.containsBalancedBrackets(R) ? 1 : 0, J = l.getFontStyle(R), Y = l.getForeground(R), X = l.getBackground(R);
		return z !== 0 && (G = z), B !== 8 && (K = fromOptionalTokenType(B)), V !== null && (q = V ? 1 : 0), H !== -1 && (J = H), U !== 0 && (Y = U), W !== 0 && (X = W), (G << 0 | K << 8 | q << 10 | J << 11 | Y << 15 | X << 24) >>> 0;
	}
};
function toOptionalTokenType(l) {
	return l;
}
function fromOptionalTokenType(l) {
	return l;
}
function createMatchers(l, R) {
	let z = [], B = newTokenizer(l), V = B.next();
	for (; V !== null;) {
		let l = 0;
		if (V.length === 2 && V.charAt(1) === ":") {
			switch (V.charAt(0)) {
				case "R":
					l = 1;
					break;
				case "L":
					l = -1;
					break;
				default: console.log(`Unknown priority ${V} in scope selector`);
			}
			V = B.next();
		}
		let R = U();
		if (z.push({
			matcher: R,
			priority: l
		}), V !== ",") break;
		V = B.next();
	}
	return z;
	function H() {
		if (V === "-") {
			V = B.next();
			let l = H();
			return (R) => !!l && !l(R);
		}
		if (V === "(") {
			V = B.next();
			let l = W();
			return V === ")" && (V = B.next()), l;
		}
		if (isIdentifier(V)) {
			let l = [];
			do
				l.push(V), V = B.next();
			while (isIdentifier(V));
			return (z) => R(l, z);
		}
		return null;
	}
	function U() {
		let l = [], R = H();
		for (; R;) l.push(R), R = H();
		return (R) => l.every((l) => l(R));
	}
	function W() {
		let l = [], R = U();
		for (; R && (l.push(R), V === "|" || V === ",");) {
			do
				V = B.next();
			while (V === "|" || V === ",");
			R = U();
		}
		return (R) => l.some((l) => l(R));
	}
}
function isIdentifier(l) {
	return !!l && !!l.match(/[\w\.:]+/);
}
function newTokenizer(l) {
	let R = /([LR]:|[\w\.:][\w\.:\-]*|[\,\|\-\(\)])/g, z = R.exec(l);
	return { next: () => {
		if (!z) return null;
		let B = z[0];
		return z = R.exec(l), B;
	} };
}
function disposeOnigString(l) {
	typeof l.dispose == "function" && l.dispose();
}
var TopLevelRuleReference = class {
	constructor(l) {
		this.scopeName = l;
	}
	toKey() {
		return this.scopeName;
	}
}, TopLevelRepositoryRuleReference = class {
	constructor(l, R) {
		this.scopeName = l, this.ruleName = R;
	}
	toKey() {
		return `${this.scopeName}#${this.ruleName}`;
	}
}, ExternalReferenceCollector = class {
	_references = [];
	_seenReferenceKeys = /* @__PURE__ */ new Set();
	get references() {
		return this._references;
	}
	visitedRule = /* @__PURE__ */ new Set();
	add(l) {
		let R = l.toKey();
		this._seenReferenceKeys.has(R) || (this._seenReferenceKeys.add(R), this._references.push(l));
	}
}, ScopeDependencyProcessor = class {
	constructor(l, R) {
		this.repo = l, this.initialScopeName = R, this.seenFullScopeRequests.add(this.initialScopeName), this.Q = [new TopLevelRuleReference(this.initialScopeName)];
	}
	seenFullScopeRequests = /* @__PURE__ */ new Set();
	seenPartialScopeRequests = /* @__PURE__ */ new Set();
	Q;
	processQueue() {
		let l = this.Q;
		this.Q = [];
		let R = new ExternalReferenceCollector();
		for (let z of l) collectReferencesOfReference(z, this.initialScopeName, this.repo, R);
		for (let l of R.references) if (l instanceof TopLevelRuleReference) {
			if (this.seenFullScopeRequests.has(l.scopeName)) continue;
			this.seenFullScopeRequests.add(l.scopeName), this.Q.push(l);
		} else {
			if (this.seenFullScopeRequests.has(l.scopeName) || this.seenPartialScopeRequests.has(l.toKey())) continue;
			this.seenPartialScopeRequests.add(l.toKey()), this.Q.push(l);
		}
	}
};
function collectReferencesOfReference(l, R, z, B) {
	let V = z.lookup(l.scopeName);
	if (!V) {
		if (l.scopeName === R) throw Error(`No grammar provided for <${R}>`);
		return;
	}
	let H = z.lookup(R);
	l instanceof TopLevelRuleReference ? collectExternalReferencesInTopLevelRule({
		baseGrammar: H,
		selfGrammar: V
	}, B) : collectExternalReferencesInTopLevelRepositoryRule(l.ruleName, {
		baseGrammar: H,
		selfGrammar: V,
		repository: V.repository
	}, B);
	let U = z.injections(l.scopeName);
	if (U) for (let l of U) B.add(new TopLevelRuleReference(l));
}
function collectExternalReferencesInTopLevelRepositoryRule(l, R, z) {
	if (R.repository && R.repository[l]) {
		let B = R.repository[l];
		collectExternalReferencesInRules([B], R, z);
	}
}
function collectExternalReferencesInTopLevelRule(l, R) {
	l.selfGrammar.patterns && Array.isArray(l.selfGrammar.patterns) && collectExternalReferencesInRules(l.selfGrammar.patterns, {
		...l,
		repository: l.selfGrammar.repository
	}, R), l.selfGrammar.injections && collectExternalReferencesInRules(Object.values(l.selfGrammar.injections), {
		...l,
		repository: l.selfGrammar.repository
	}, R);
}
function collectExternalReferencesInRules(l, R, z) {
	for (let B of l) {
		if (z.visitedRule.has(B)) continue;
		z.visitedRule.add(B);
		let l = B.repository ? mergeObjects({}, R.repository, B.repository) : R.repository;
		Array.isArray(B.patterns) && collectExternalReferencesInRules(B.patterns, {
			...R,
			repository: l
		}, z);
		let H = B.include;
		if (!H) continue;
		let U = parseInclude(H);
		switch (U.kind) {
			case 0:
				collectExternalReferencesInTopLevelRule({
					...R,
					selfGrammar: R.baseGrammar
				}, z);
				break;
			case 1:
				collectExternalReferencesInTopLevelRule(R, z);
				break;
			case 2:
				collectExternalReferencesInTopLevelRepositoryRule(U.ruleName, {
					...R,
					repository: l
				}, z);
				break;
			case 3:
			case 4:
				let B = U.scopeName === R.selfGrammar.scopeName ? R.selfGrammar : U.scopeName === R.baseGrammar.scopeName ? R.baseGrammar : void 0;
				if (B) {
					let V = {
						baseGrammar: R.baseGrammar,
						selfGrammar: B,
						repository: l
					};
					U.kind === 4 ? collectExternalReferencesInTopLevelRepositoryRule(U.ruleName, V, z) : collectExternalReferencesInTopLevelRule(V, z);
				} else U.kind === 4 ? z.add(new TopLevelRepositoryRuleReference(U.scopeName, U.ruleName)) : z.add(new TopLevelRuleReference(U.scopeName));
				break;
		}
	}
}
var BaseReference = class {
	kind = 0;
}, SelfReference = class {
	kind = 1;
}, RelativeReference = class {
	constructor(l) {
		this.ruleName = l;
	}
	kind = 2;
}, TopLevelReference = class {
	constructor(l) {
		this.scopeName = l;
	}
	kind = 3;
}, TopLevelRepositoryReference = class {
	constructor(l, R) {
		this.scopeName = l, this.ruleName = R;
	}
	kind = 4;
};
function parseInclude(l) {
	if (l === "$base") return new BaseReference();
	if (l === "$self") return new SelfReference();
	let R = l.indexOf("#");
	return R === -1 ? new TopLevelReference(l) : R === 0 ? new RelativeReference(l.substring(1)) : new TopLevelRepositoryReference(l.substring(0, R), l.substring(R + 1));
}
var HAS_BACK_REFERENCES = /\\(\d+)/, BACK_REFERENCING_END = /\\(\d+)/g, endRuleId = -1, whileRuleId = -2;
function ruleIdFromNumber(l) {
	return l;
}
function ruleIdToNumber(l) {
	return l;
}
var Rule = class {
	$location;
	id;
	_nameIsCapturing;
	_name;
	_contentNameIsCapturing;
	_contentName;
	constructor(l, R, z, B) {
		this.$location = l, this.id = R, this._name = z || null, this._nameIsCapturing = RegexSource.hasCaptures(this._name), this._contentName = B || null, this._contentNameIsCapturing = RegexSource.hasCaptures(this._contentName);
	}
	get debugName() {
		let l = this.$location ? `${basename(this.$location.filename)}:${this.$location.line}` : "unknown";
		return `${this.constructor.name}#${this.id} @ ${l}`;
	}
	getName(l, R) {
		return !this._nameIsCapturing || this._name === null || l === null || R === null ? this._name : RegexSource.replaceCaptures(this._name, l, R);
	}
	getContentName(l, R) {
		return !this._contentNameIsCapturing || this._contentName === null ? this._contentName : RegexSource.replaceCaptures(this._contentName, l, R);
	}
}, CaptureRule = class extends Rule {
	retokenizeCapturedWithRuleId;
	constructor(l, R, z, B, V) {
		super(l, R, z, B), this.retokenizeCapturedWithRuleId = V;
	}
	dispose() {}
	collectPatterns(l, R) {
		throw Error("Not supported!");
	}
	compile(l, R) {
		throw Error("Not supported!");
	}
	compileAG(l, R, z, B) {
		throw Error("Not supported!");
	}
}, MatchRule = class extends Rule {
	_match;
	captures;
	_cachedCompiledPatterns;
	constructor(l, R, z, B, V) {
		super(l, R, z, null), this._match = new RegExpSource(B, this.id), this.captures = V, this._cachedCompiledPatterns = null;
	}
	dispose() {
		this._cachedCompiledPatterns &&= (this._cachedCompiledPatterns.dispose(), null);
	}
	get debugMatchRegExp() {
		return `${this._match.source}`;
	}
	collectPatterns(l, R) {
		R.push(this._match);
	}
	compile(l, R) {
		return this._getCachedCompiledPatterns(l).compile(l);
	}
	compileAG(l, R, z, B) {
		return this._getCachedCompiledPatterns(l).compileAG(l, z, B);
	}
	_getCachedCompiledPatterns(l) {
		return this._cachedCompiledPatterns || (this._cachedCompiledPatterns = new RegExpSourceList(), this.collectPatterns(l, this._cachedCompiledPatterns)), this._cachedCompiledPatterns;
	}
}, IncludeOnlyRule = class extends Rule {
	hasMissingPatterns;
	patterns;
	_cachedCompiledPatterns;
	constructor(l, R, z, B, V) {
		super(l, R, z, B), this.patterns = V.patterns, this.hasMissingPatterns = V.hasMissingPatterns, this._cachedCompiledPatterns = null;
	}
	dispose() {
		this._cachedCompiledPatterns &&= (this._cachedCompiledPatterns.dispose(), null);
	}
	collectPatterns(l, R) {
		for (let z of this.patterns) l.getRule(z).collectPatterns(l, R);
	}
	compile(l, R) {
		return this._getCachedCompiledPatterns(l).compile(l);
	}
	compileAG(l, R, z, B) {
		return this._getCachedCompiledPatterns(l).compileAG(l, z, B);
	}
	_getCachedCompiledPatterns(l) {
		return this._cachedCompiledPatterns || (this._cachedCompiledPatterns = new RegExpSourceList(), this.collectPatterns(l, this._cachedCompiledPatterns)), this._cachedCompiledPatterns;
	}
}, BeginEndRule = class extends Rule {
	_begin;
	beginCaptures;
	_end;
	endHasBackReferences;
	endCaptures;
	applyEndPatternLast;
	hasMissingPatterns;
	patterns;
	_cachedCompiledPatterns;
	constructor(l, R, z, B, V, H, U, W, G, K) {
		super(l, R, z, B), this._begin = new RegExpSource(V, this.id), this.beginCaptures = H, this._end = new RegExpSource(U || "", -1), this.endHasBackReferences = this._end.hasBackReferences, this.endCaptures = W, this.applyEndPatternLast = G || !1, this.patterns = K.patterns, this.hasMissingPatterns = K.hasMissingPatterns, this._cachedCompiledPatterns = null;
	}
	dispose() {
		this._cachedCompiledPatterns &&= (this._cachedCompiledPatterns.dispose(), null);
	}
	get debugBeginRegExp() {
		return `${this._begin.source}`;
	}
	get debugEndRegExp() {
		return `${this._end.source}`;
	}
	getEndWithResolvedBackReferences(l, R) {
		return this._end.resolveBackReferences(l, R);
	}
	collectPatterns(l, R) {
		R.push(this._begin);
	}
	compile(l, R) {
		return this._getCachedCompiledPatterns(l, R).compile(l);
	}
	compileAG(l, R, z, B) {
		return this._getCachedCompiledPatterns(l, R).compileAG(l, z, B);
	}
	_getCachedCompiledPatterns(l, R) {
		if (!this._cachedCompiledPatterns) {
			this._cachedCompiledPatterns = new RegExpSourceList();
			for (let R of this.patterns) l.getRule(R).collectPatterns(l, this._cachedCompiledPatterns);
			this.applyEndPatternLast ? this._cachedCompiledPatterns.push(this._end.hasBackReferences ? this._end.clone() : this._end) : this._cachedCompiledPatterns.unshift(this._end.hasBackReferences ? this._end.clone() : this._end);
		}
		return this._end.hasBackReferences && (this.applyEndPatternLast ? this._cachedCompiledPatterns.setSource(this._cachedCompiledPatterns.length() - 1, R) : this._cachedCompiledPatterns.setSource(0, R)), this._cachedCompiledPatterns;
	}
}, BeginWhileRule = class extends Rule {
	_begin;
	beginCaptures;
	whileCaptures;
	_while;
	whileHasBackReferences;
	hasMissingPatterns;
	patterns;
	_cachedCompiledPatterns;
	_cachedCompiledWhilePatterns;
	constructor(l, R, z, B, V, H, U, W, G) {
		super(l, R, z, B), this._begin = new RegExpSource(V, this.id), this.beginCaptures = H, this.whileCaptures = W, this._while = new RegExpSource(U, whileRuleId), this.whileHasBackReferences = this._while.hasBackReferences, this.patterns = G.patterns, this.hasMissingPatterns = G.hasMissingPatterns, this._cachedCompiledPatterns = null, this._cachedCompiledWhilePatterns = null;
	}
	dispose() {
		this._cachedCompiledPatterns &&= (this._cachedCompiledPatterns.dispose(), null), this._cachedCompiledWhilePatterns &&= (this._cachedCompiledWhilePatterns.dispose(), null);
	}
	get debugBeginRegExp() {
		return `${this._begin.source}`;
	}
	get debugWhileRegExp() {
		return `${this._while.source}`;
	}
	getWhileWithResolvedBackReferences(l, R) {
		return this._while.resolveBackReferences(l, R);
	}
	collectPatterns(l, R) {
		R.push(this._begin);
	}
	compile(l, R) {
		return this._getCachedCompiledPatterns(l).compile(l);
	}
	compileAG(l, R, z, B) {
		return this._getCachedCompiledPatterns(l).compileAG(l, z, B);
	}
	_getCachedCompiledPatterns(l) {
		if (!this._cachedCompiledPatterns) {
			this._cachedCompiledPatterns = new RegExpSourceList();
			for (let R of this.patterns) l.getRule(R).collectPatterns(l, this._cachedCompiledPatterns);
		}
		return this._cachedCompiledPatterns;
	}
	compileWhile(l, R) {
		return this._getCachedCompiledWhilePatterns(l, R).compile(l);
	}
	compileWhileAG(l, R, z, B) {
		return this._getCachedCompiledWhilePatterns(l, R).compileAG(l, z, B);
	}
	_getCachedCompiledWhilePatterns(l, R) {
		return this._cachedCompiledWhilePatterns || (this._cachedCompiledWhilePatterns = new RegExpSourceList(), this._cachedCompiledWhilePatterns.push(this._while.hasBackReferences ? this._while.clone() : this._while)), this._while.hasBackReferences && this._cachedCompiledWhilePatterns.setSource(0, R || ""), this._cachedCompiledWhilePatterns;
	}
}, RuleFactory = class l {
	static createCaptureRule(l, R, z, B, V) {
		return l.registerRule((l) => new CaptureRule(R, l, z, B, V));
	}
	static getCompiledRuleId(R, z, B) {
		return R.id || z.registerRule((H) => {
			if (R.id = H, R.match) return new MatchRule(R.$vscodeTextmateLocation, R.id, R.name, R.match, l._compileCaptures(R.captures, z, B));
			if (R.begin === void 0) {
				R.repository && (B = mergeObjects({}, B, R.repository));
				let H = R.patterns;
				return H === void 0 && R.include && (H = [{ include: R.include }]), new IncludeOnlyRule(R.$vscodeTextmateLocation, R.id, R.name, R.contentName, l._compilePatterns(H, z, B));
			}
			return R.while ? new BeginWhileRule(R.$vscodeTextmateLocation, R.id, R.name, R.contentName, R.begin, l._compileCaptures(R.beginCaptures || R.captures, z, B), R.while, l._compileCaptures(R.whileCaptures || R.captures, z, B), l._compilePatterns(R.patterns, z, B)) : new BeginEndRule(R.$vscodeTextmateLocation, R.id, R.name, R.contentName, R.begin, l._compileCaptures(R.beginCaptures || R.captures, z, B), R.end, l._compileCaptures(R.endCaptures || R.captures, z, B), R.applyEndPatternLast, l._compilePatterns(R.patterns, z, B));
		}), R.id;
	}
	static _compileCaptures(R, z, B) {
		let V = [];
		if (R) {
			let H = 0;
			for (let l in R) {
				if (l === "$vscodeTextmateLocation") continue;
				let R = parseInt(l, 10);
				R > H && (H = R);
			}
			for (let l = 0; l <= H; l++) V[l] = null;
			for (let H in R) {
				if (H === "$vscodeTextmateLocation") continue;
				let U = parseInt(H, 10), W = 0;
				R[H].patterns && (W = l.getCompiledRuleId(R[H], z, B)), V[U] = l.createCaptureRule(z, R[H].$vscodeTextmateLocation, R[H].name, R[H].contentName, W);
			}
		}
		return V;
	}
	static _compilePatterns(R, z, B) {
		let V = [];
		if (R) for (let H = 0, U = R.length; H < U; H++) {
			let U = R[H], W = -1;
			if (U.include) {
				let R = parseInclude(U.include);
				switch (R.kind) {
					case 0:
					case 1:
						W = l.getCompiledRuleId(B[U.include], z, B);
						break;
					case 2:
						let V = B[R.ruleName];
						V && (W = l.getCompiledRuleId(V, z, B));
						break;
					case 3:
					case 4:
						let H = R.scopeName, G = R.kind === 4 ? R.ruleName : null, K = z.getExternalGrammar(H, B);
						if (K) if (G) {
							let R = K.repository[G];
							R && (W = l.getCompiledRuleId(R, z, K.repository));
						} else W = l.getCompiledRuleId(K.repository.$self, z, K.repository);
						break;
				}
			} else W = l.getCompiledRuleId(U, z, B);
			if (W !== -1) {
				let l = z.getRule(W), R = !1;
				if ((l instanceof IncludeOnlyRule || l instanceof BeginEndRule || l instanceof BeginWhileRule) && l.hasMissingPatterns && l.patterns.length === 0 && (R = !0), R) continue;
				V.push(W);
			}
		}
		return {
			patterns: V,
			hasMissingPatterns: (R ? R.length : 0) !== V.length
		};
	}
}, RegExpSource = class l {
	source;
	ruleId;
	hasAnchor;
	hasBackReferences;
	_anchorCache;
	constructor(l, R) {
		if (l && typeof l == "string") {
			let R = l.length, z = 0, B = [], V = !1;
			for (let H = 0; H < R; H++) if (l.charAt(H) === "\\" && H + 1 < R) {
				let R = l.charAt(H + 1);
				R === "z" ? (B.push(l.substring(z, H)), B.push("$(?!\\n)(?<!\\n)"), z = H + 2) : (R === "A" || R === "G") && (V = !0), H++;
			}
			this.hasAnchor = V, z === 0 ? this.source = l : (B.push(l.substring(z, R)), this.source = B.join(""));
		} else this.hasAnchor = !1, this.source = l;
		this.hasAnchor ? this._anchorCache = this._buildAnchorCache() : this._anchorCache = null, this.ruleId = R, typeof this.source == "string" ? this.hasBackReferences = HAS_BACK_REFERENCES.test(this.source) : this.hasBackReferences = !1;
	}
	clone() {
		return new l(this.source, this.ruleId);
	}
	setSource(l) {
		this.source !== l && (this.source = l, this.hasAnchor && (this._anchorCache = this._buildAnchorCache()));
	}
	resolveBackReferences(l, R) {
		if (typeof this.source != "string") throw Error("This method should only be called if the source is a string");
		let z = R.map((R) => l.substring(R.start, R.end));
		return BACK_REFERENCING_END.lastIndex = 0, this.source.replace(BACK_REFERENCING_END, (l, R) => escapeRegExpCharacters(z[parseInt(R, 10)] || ""));
	}
	_buildAnchorCache() {
		if (typeof this.source != "string") throw Error("This method should only be called if the source is a string");
		let l = [], R = [], z = [], B = [], V, H, U, W;
		for (V = 0, H = this.source.length; V < H; V++) U = this.source.charAt(V), l[V] = U, R[V] = U, z[V] = U, B[V] = U, U === "\\" && V + 1 < H && (W = this.source.charAt(V + 1), W === "A" ? (l[V + 1] = "", R[V + 1] = "", z[V + 1] = "A", B[V + 1] = "A") : W === "G" ? (l[V + 1] = "", R[V + 1] = "G", z[V + 1] = "", B[V + 1] = "G") : (l[V + 1] = W, R[V + 1] = W, z[V + 1] = W, B[V + 1] = W), V++);
		return {
			A0_G0: l.join(""),
			A0_G1: R.join(""),
			A1_G0: z.join(""),
			A1_G1: B.join("")
		};
	}
	resolveAnchors(l, R) {
		return !this.hasAnchor || !this._anchorCache || typeof this.source != "string" ? this.source : l ? R ? this._anchorCache.A1_G1 : this._anchorCache.A1_G0 : R ? this._anchorCache.A0_G1 : this._anchorCache.A0_G0;
	}
}, RegExpSourceList = class {
	_items;
	_hasAnchors;
	_cached;
	_anchorCache;
	constructor() {
		this._items = [], this._hasAnchors = !1, this._cached = null, this._anchorCache = {
			A0_G0: null,
			A0_G1: null,
			A1_G0: null,
			A1_G1: null
		};
	}
	dispose() {
		this._disposeCaches();
	}
	_disposeCaches() {
		this._cached &&= (this._cached.dispose(), null), this._anchorCache.A0_G0 && (this._anchorCache.A0_G0.dispose(), this._anchorCache.A0_G0 = null), this._anchorCache.A0_G1 && (this._anchorCache.A0_G1.dispose(), this._anchorCache.A0_G1 = null), this._anchorCache.A1_G0 && (this._anchorCache.A1_G0.dispose(), this._anchorCache.A1_G0 = null), this._anchorCache.A1_G1 && (this._anchorCache.A1_G1.dispose(), this._anchorCache.A1_G1 = null);
	}
	push(l) {
		this._items.push(l), this._hasAnchors = this._hasAnchors || l.hasAnchor;
	}
	unshift(l) {
		this._items.unshift(l), this._hasAnchors = this._hasAnchors || l.hasAnchor;
	}
	length() {
		return this._items.length;
	}
	setSource(l, R) {
		this._items[l].source !== R && (this._disposeCaches(), this._items[l].setSource(R));
	}
	compile(l) {
		return this._cached ||= new CompiledRule(l, this._items.map((l) => l.source), this._items.map((l) => l.ruleId)), this._cached;
	}
	compileAG(l, R, z) {
		return this._hasAnchors ? R ? z ? (this._anchorCache.A1_G1 || (this._anchorCache.A1_G1 = this._resolveAnchors(l, R, z)), this._anchorCache.A1_G1) : (this._anchorCache.A1_G0 || (this._anchorCache.A1_G0 = this._resolveAnchors(l, R, z)), this._anchorCache.A1_G0) : z ? (this._anchorCache.A0_G1 || (this._anchorCache.A0_G1 = this._resolveAnchors(l, R, z)), this._anchorCache.A0_G1) : (this._anchorCache.A0_G0 || (this._anchorCache.A0_G0 = this._resolveAnchors(l, R, z)), this._anchorCache.A0_G0) : this.compile(l);
	}
	_resolveAnchors(l, R, z) {
		return new CompiledRule(l, this._items.map((l) => l.resolveAnchors(R, z)), this._items.map((l) => l.ruleId));
	}
}, CompiledRule = class {
	constructor(l, R, z) {
		this.regExps = R, this.rules = z, this.scanner = l.createOnigScanner(R);
	}
	scanner;
	dispose() {
		typeof this.scanner.dispose == "function" && this.scanner.dispose();
	}
	toString() {
		let l = [];
		for (let R = 0, z = this.rules.length; R < z; R++) l.push("   - " + this.rules[R] + ": " + this.regExps[R]);
		return l.join("\n");
	}
	findNextMatchSync(l, R, z) {
		let B = this.scanner.findNextMatchSync(l, R, z);
		return B ? {
			ruleId: this.rules[B.index],
			captureIndices: B.captureIndices
		} : null;
	}
}, BasicScopeAttributes = class {
	constructor(l, R) {
		this.languageId = l, this.tokenType = R;
	}
}, BasicScopeAttributesProvider = class l {
	_defaultAttributes;
	_embeddedLanguagesMatcher;
	constructor(l, R) {
		this._defaultAttributes = new BasicScopeAttributes(l, 8), this._embeddedLanguagesMatcher = new ScopeMatcher(Object.entries(R || {}));
	}
	getDefaultAttributes() {
		return this._defaultAttributes;
	}
	getBasicScopeAttributes(R) {
		return R === null ? l._NULL_SCOPE_METADATA : this._getBasicScopeAttributes.get(R);
	}
	static _NULL_SCOPE_METADATA = new BasicScopeAttributes(0, 0);
	_getBasicScopeAttributes = new CachedFn((l) => new BasicScopeAttributes(this._scopeToLanguage(l), this._toStandardTokenType(l)));
	_scopeToLanguage(l) {
		return this._embeddedLanguagesMatcher.match(l) || 0;
	}
	_toStandardTokenType(R) {
		let z = R.match(l.STANDARD_TOKEN_TYPE_REGEXP);
		if (!z) return 8;
		switch (z[1]) {
			case "comment": return 1;
			case "string": return 2;
			case "regex": return 3;
			case "meta.embedded": return 0;
		}
		throw Error("Unexpected match for standard token type!");
	}
	static STANDARD_TOKEN_TYPE_REGEXP = /\b(comment|string|regex|meta\.embedded)\b/;
}, ScopeMatcher = class {
	values;
	scopesRegExp;
	constructor(l) {
		if (l.length === 0) this.values = null, this.scopesRegExp = null;
		else {
			this.values = new Map(l);
			let R = l.map(([l, R]) => escapeRegExpCharacters(l));
			R.sort(), R.reverse(), this.scopesRegExp = RegExp(`^((${R.join(")|(")}))($|\\.)`, "");
		}
	}
	match(l) {
		if (!this.scopesRegExp) return;
		let R = l.match(this.scopesRegExp);
		if (R) return this.values.get(R[1]);
	}
};
typeof process < "u" && process.env.VSCODE_TEXTMATE_DEBUG;
var UseOnigurumaFindOptions = !1, TokenizeStringResult = class {
	constructor(l, R) {
		this.stack = l, this.stoppedEarly = R;
	}
};
function _tokenizeString(l, R, z, B, V, H, U, W) {
	let G = R.content.length, K = !1, q = -1;
	if (U) {
		let U = _checkWhileConditions(l, R, z, B, V, H);
		V = U.stack, B = U.linePos, z = U.isFirstLine, q = U.anchorPosition;
	}
	let J = Date.now();
	for (; !K;) {
		if (W !== 0 && Date.now() - J > W) return new TokenizeStringResult(V, !0);
		Y();
	}
	return new TokenizeStringResult(V, !1);
	function Y() {
		let U = matchRuleOrInjections(l, R, z, B, V, q);
		if (!U) {
			H.produce(V, G), K = !0;
			return;
		}
		let W = U.captureIndices, J = U.matchedRuleId, Y = W && W.length > 0 ? W[0].end > B : !1;
		if (J === endRuleId) {
			let U = V.getRule(l);
			H.produce(V, W[0].start), V = V.withContentNameScopesList(V.nameScopesList), handleCaptures(l, R, z, V, H, U.endCaptures, W), H.produce(V, W[0].end);
			let J = V;
			if (V = V.parent, q = J.getAnchorPos(), !Y && J.getEnterPos() === B) {
				V = J, H.produce(V, G), K = !0;
				return;
			}
		} else {
			let U = l.getRule(J);
			H.produce(V, W[0].start);
			let X = V, Z = U.getName(R.content, W), Q = V.contentNameScopesList.pushAttributed(Z, l);
			if (V = V.push(J, B, q, W[0].end === G, null, Q, Q), U instanceof BeginEndRule) {
				let B = U;
				handleCaptures(l, R, z, V, H, B.beginCaptures, W), H.produce(V, W[0].end), q = W[0].end;
				let J = B.getContentName(R.content, W), Z = Q.pushAttributed(J, l);
				if (V = V.withContentNameScopesList(Z), B.endHasBackReferences && (V = V.withEndRule(B.getEndWithResolvedBackReferences(R.content, W))), !Y && X.hasSameRuleAs(V)) {
					V = V.pop(), H.produce(V, G), K = !0;
					return;
				}
			} else if (U instanceof BeginWhileRule) {
				let B = U;
				handleCaptures(l, R, z, V, H, B.beginCaptures, W), H.produce(V, W[0].end), q = W[0].end;
				let J = B.getContentName(R.content, W), Z = Q.pushAttributed(J, l);
				if (V = V.withContentNameScopesList(Z), B.whileHasBackReferences && (V = V.withEndRule(B.getWhileWithResolvedBackReferences(R.content, W))), !Y && X.hasSameRuleAs(V)) {
					V = V.pop(), H.produce(V, G), K = !0;
					return;
				}
			} else if (handleCaptures(l, R, z, V, H, U.captures, W), H.produce(V, W[0].end), V = V.pop(), !Y) {
				V = V.safePop(), H.produce(V, G), K = !0;
				return;
			}
		}
		W[0].end > B && (B = W[0].end, z = !1);
	}
}
function _checkWhileConditions(l, R, z, B, V, H) {
	let U = V.beginRuleCapturedEOL ? 0 : -1, W = [];
	for (let R = V; R; R = R.pop()) {
		let z = R.getRule(l);
		z instanceof BeginWhileRule && W.push({
			rule: z,
			stack: R
		});
	}
	for (let G = W.pop(); G; G = W.pop()) {
		let { ruleScanner: W, findOptions: K } = prepareRuleWhileSearch(G.rule, l, G.stack.endRule, z, B === U), q = W.findNextMatchSync(R, B, K);
		if (q) {
			if (q.ruleId !== whileRuleId) {
				V = G.stack.pop();
				break;
			}
			q.captureIndices && q.captureIndices.length && (H.produce(G.stack, q.captureIndices[0].start), handleCaptures(l, R, z, G.stack, H, G.rule.whileCaptures, q.captureIndices), H.produce(G.stack, q.captureIndices[0].end), U = q.captureIndices[0].end, q.captureIndices[0].end > B && (B = q.captureIndices[0].end, z = !1));
		} else {
			V = G.stack.pop();
			break;
		}
	}
	return {
		stack: V,
		linePos: B,
		anchorPosition: U,
		isFirstLine: z
	};
}
function matchRuleOrInjections(l, R, z, B, V, H) {
	let U = matchRule(l, R, z, B, V, H), W = l.getInjections();
	if (W.length === 0) return U;
	let G = matchInjections(W, l, R, z, B, V, H);
	if (!G) return U;
	if (!U) return G;
	let K = U.captureIndices[0].start, q = G.captureIndices[0].start;
	return q < K || G.priorityMatch && q === K ? G : U;
}
function matchRule(l, R, z, B, V, H) {
	let { ruleScanner: U, findOptions: W } = prepareRuleSearch(V.getRule(l), l, V.endRule, z, B === H), G = U.findNextMatchSync(R, B, W);
	return G ? {
		captureIndices: G.captureIndices,
		matchedRuleId: G.ruleId
	} : null;
}
function matchInjections(l, R, z, B, V, H, U) {
	let W = Number.MAX_VALUE, G = null, K, q = 0, J = H.contentNameScopesList.getScopeNames();
	for (let H = 0, Y = l.length; H < Y; H++) {
		let Y = l[H];
		if (!Y.matcher(J)) continue;
		let { ruleScanner: X, findOptions: Z } = prepareRuleSearch(R.getRule(Y.ruleId), R, null, B, V === U), Q = X.findNextMatchSync(z, V, Z);
		if (!Q) continue;
		let $ = Q.captureIndices[0].start;
		if (!($ >= W) && (W = $, G = Q.captureIndices, K = Q.ruleId, q = Y.priority, W === V)) break;
	}
	return G ? {
		priorityMatch: q === -1,
		captureIndices: G,
		matchedRuleId: K
	} : null;
}
function prepareRuleSearch(l, R, z, B, V) {
	return UseOnigurumaFindOptions ? {
		ruleScanner: l.compile(R, z),
		findOptions: getFindOptions(B, V)
	} : {
		ruleScanner: l.compileAG(R, z, B, V),
		findOptions: 0
	};
}
function prepareRuleWhileSearch(l, R, z, B, V) {
	return UseOnigurumaFindOptions ? {
		ruleScanner: l.compileWhile(R, z),
		findOptions: getFindOptions(B, V)
	} : {
		ruleScanner: l.compileWhileAG(R, z, B, V),
		findOptions: 0
	};
}
function getFindOptions(l, R) {
	let z = 0;
	return l || (z |= 1), R || (z |= 4), z;
}
function handleCaptures(l, R, z, B, V, H, U) {
	if (H.length === 0) return;
	let W = R.content, G = Math.min(H.length, U.length), K = [], q = U[0].end;
	for (let R = 0; R < G; R++) {
		let G = H[R];
		if (G === null) continue;
		let J = U[R];
		if (J.length === 0) continue;
		if (J.start > q) break;
		for (; K.length > 0 && K[K.length - 1].endPos <= J.start;) V.produceFromScopes(K[K.length - 1].scopes, K[K.length - 1].endPos), K.pop();
		if (K.length > 0 ? V.produceFromScopes(K[K.length - 1].scopes, J.start) : V.produce(B, J.start), G.retokenizeCapturedWithRuleId) {
			let R = G.getName(W, U), H = B.contentNameScopesList.pushAttributed(R, l), K = G.getContentName(W, U), q = H.pushAttributed(K, l), Y = B.push(G.retokenizeCapturedWithRuleId, J.start, -1, !1, null, H, q), X = l.createOnigString(W.substring(0, J.end));
			_tokenizeString(l, X, z && J.start === 0, J.start, Y, V, !1, 0), disposeOnigString(X);
			continue;
		}
		let Y = G.getName(W, U);
		if (Y !== null) {
			let R = (K.length > 0 ? K[K.length - 1].scopes : B.contentNameScopesList).pushAttributed(Y, l);
			K.push(new LocalStackElement(R, J.end));
		}
	}
	for (; K.length > 0;) V.produceFromScopes(K[K.length - 1].scopes, K[K.length - 1].endPos), K.pop();
}
var LocalStackElement = class {
	scopes;
	endPos;
	constructor(l, R) {
		this.scopes = l, this.endPos = R;
	}
};
function createGrammar(l, R, z, B, V, H, U, W) {
	return new Grammar(l, R, z, B, V, H, U, W);
}
function collectInjections(l, R, z, B, V) {
	let H = createMatchers(R, nameMatcher), U = RuleFactory.getCompiledRuleId(z, B, V.repository);
	for (let z of H) l.push({
		debugSelector: R,
		matcher: z.matcher,
		ruleId: U,
		grammar: V,
		priority: z.priority
	});
}
function nameMatcher(l, R) {
	if (R.length < l.length) return !1;
	let z = 0;
	return l.every((l) => {
		for (let B = z; B < R.length; B++) if (scopesAreMatching(R[B], l)) return z = B + 1, !0;
		return !1;
	});
}
function scopesAreMatching(l, R) {
	if (!l) return !1;
	if (l === R) return !0;
	let z = R.length;
	return l.length > z && l.substr(0, z) === R && l[z] === ".";
}
var Grammar = class {
	constructor(l, R, z, B, V, H, U, W) {
		if (this._rootScopeName = l, this.balancedBracketSelectors = H, this._onigLib = W, this._basicScopeAttributesProvider = new BasicScopeAttributesProvider(z, B), this._rootId = -1, this._lastRuleId = 0, this._ruleId2desc = [null], this._includedGrammars = {}, this._grammarRepository = U, this._grammar = initGrammar(R, null), this._injections = null, this._tokenTypeMatchers = [], V) for (let l of Object.keys(V)) {
			let R = createMatchers(l, nameMatcher);
			for (let z of R) this._tokenTypeMatchers.push({
				matcher: z.matcher,
				type: V[l]
			});
		}
	}
	_rootId;
	_lastRuleId;
	_ruleId2desc;
	_includedGrammars;
	_grammarRepository;
	_grammar;
	_injections;
	_basicScopeAttributesProvider;
	_tokenTypeMatchers;
	get themeProvider() {
		return this._grammarRepository;
	}
	dispose() {
		for (let l of this._ruleId2desc) l && l.dispose();
	}
	createOnigScanner(l) {
		return this._onigLib.createOnigScanner(l);
	}
	createOnigString(l) {
		return this._onigLib.createOnigString(l);
	}
	getMetadataForScope(l) {
		return this._basicScopeAttributesProvider.getBasicScopeAttributes(l);
	}
	_collectInjections() {
		let l = {
			lookup: (l) => l === this._rootScopeName ? this._grammar : this.getExternalGrammar(l),
			injections: (l) => this._grammarRepository.injections(l)
		}, R = [], z = this._rootScopeName, B = l.lookup(z);
		if (B) {
			let l = B.injections;
			if (l) for (let z in l) collectInjections(R, z, l[z], this, B);
			let V = this._grammarRepository.injections(z);
			V && V.forEach((l) => {
				let z = this.getExternalGrammar(l);
				if (z) {
					let l = z.injectionSelector;
					l && collectInjections(R, l, z, this, z);
				}
			});
		}
		return R.sort((l, R) => l.priority - R.priority), R;
	}
	getInjections() {
		return this._injections === null && (this._injections = this._collectInjections()), this._injections;
	}
	registerRule(l) {
		let R = ++this._lastRuleId, z = l(ruleIdFromNumber(R));
		return this._ruleId2desc[R] = z, z;
	}
	getRule(l) {
		return this._ruleId2desc[ruleIdToNumber(l)];
	}
	getExternalGrammar(l, R) {
		if (this._includedGrammars[l]) return this._includedGrammars[l];
		if (this._grammarRepository) {
			let z = this._grammarRepository.lookup(l);
			if (z) return this._includedGrammars[l] = initGrammar(z, R && R.$base), this._includedGrammars[l];
		}
	}
	tokenizeLine(l, R, z = 0) {
		let B = this._tokenize(l, R, !1, z);
		return {
			tokens: B.lineTokens.getResult(B.ruleStack, B.lineLength),
			ruleStack: B.ruleStack,
			stoppedEarly: B.stoppedEarly
		};
	}
	tokenizeLine2(l, R, z = 0) {
		let B = this._tokenize(l, R, !0, z);
		return {
			tokens: B.lineTokens.getBinaryResult(B.ruleStack, B.lineLength),
			ruleStack: B.ruleStack,
			stoppedEarly: B.stoppedEarly
		};
	}
	_tokenize(l, R, z, B) {
		this._rootId === -1 && (this._rootId = RuleFactory.getCompiledRuleId(this._grammar.repository.$self, this, this._grammar.repository), this.getInjections());
		let V;
		if (!R || R === StateStackImpl.NULL) {
			V = !0;
			let l = this._basicScopeAttributesProvider.getDefaultAttributes(), z = this.themeProvider.getDefaults(), B = EncodedTokenMetadata.set(0, l.languageId, l.tokenType, null, z.fontStyle, z.foregroundId, z.backgroundId), H = this.getRule(this._rootId).getName(null, null), U;
			U = H ? AttributedScopeStack.createRootAndLookUpScopeName(H, B, this) : AttributedScopeStack.createRoot("unknown", B), R = new StateStackImpl(null, this._rootId, -1, -1, !1, null, U, U);
		} else V = !1, R.reset();
		l += "\n";
		let H = this.createOnigString(l), U = H.content.length, W = new LineTokens(z, l, this._tokenTypeMatchers, this.balancedBracketSelectors), G = _tokenizeString(this, H, V, 0, R, W, !0, B);
		return disposeOnigString(H), {
			lineLength: U,
			lineTokens: W,
			ruleStack: G.stack,
			stoppedEarly: G.stoppedEarly
		};
	}
};
function initGrammar(R, z) {
	return R = clone(R), R.repository = R.repository || {}, R.repository.$self = {
		$vscodeTextmateLocation: R.$vscodeTextmateLocation,
		patterns: R.patterns,
		name: R.scopeName
	}, R.repository.$base = z || R.repository.$self, R;
}
var AttributedScopeStack = class l {
	constructor(l, R, z) {
		this.parent = l, this.scopePath = R, this.tokenAttributes = z;
	}
	static fromExtension(R, z) {
		let B = R, V = R?.scopePath ?? null;
		for (let R of z) V = ScopeStack.push(V, R.scopeNames), B = new l(B, V, R.encodedTokenAttributes);
		return B;
	}
	static createRoot(R, z) {
		return new l(null, new ScopeStack(null, R), z);
	}
	static createRootAndLookUpScopeName(R, z, B) {
		let V = B.getMetadataForScope(R), H = new ScopeStack(null, R), U = B.themeProvider.themeMatch(H);
		return new l(null, H, l.mergeAttributes(z, V, U));
	}
	get scopeName() {
		return this.scopePath.scopeName;
	}
	toString() {
		return this.getScopeNames().join(" ");
	}
	equals(R) {
		return l.equals(this, R);
	}
	static equals(l, R) {
		do {
			if (l === R || !l && !R) return !0;
			if (!l || !R || l.scopeName !== R.scopeName || l.tokenAttributes !== R.tokenAttributes) return !1;
			l = l.parent, R = R.parent;
		} while (!0);
	}
	static mergeAttributes(l, R, z) {
		let B = -1, V = 0, H = 0;
		return z !== null && (B = z.fontStyle, V = z.foregroundId, H = z.backgroundId), EncodedTokenMetadata.set(l, R.languageId, R.tokenType, null, B, V, H);
	}
	pushAttributed(R, z) {
		if (R === null) return this;
		if (R.indexOf(" ") === -1) return l._pushAttributed(this, R, z);
		let B = R.split(/ /g), V = this;
		for (let R of B) V = l._pushAttributed(V, R, z);
		return V;
	}
	static _pushAttributed(R, z, B) {
		let V = B.getMetadataForScope(z), H = R.scopePath.push(z), U = B.themeProvider.themeMatch(H);
		return new l(R, H, l.mergeAttributes(R.tokenAttributes, V, U));
	}
	getScopeNames() {
		return this.scopePath.getSegments();
	}
	getExtensionIfDefined(l) {
		let R = [], z = this;
		for (; z && z !== l;) R.push({
			encodedTokenAttributes: z.tokenAttributes,
			scopeNames: z.scopePath.getExtensionIfDefined(z.parent?.scopePath ?? null)
		}), z = z.parent;
		return z === l ? R.reverse() : void 0;
	}
}, StateStackImpl = class l {
	constructor(l, R, z, B, V, H, U, W) {
		this.parent = l, this.ruleId = R, this.beginRuleCapturedEOL = V, this.endRule = H, this.nameScopesList = U, this.contentNameScopesList = W, this.depth = this.parent ? this.parent.depth + 1 : 1, this._enterPos = z, this._anchorPos = B;
	}
	_stackElementBrand = void 0;
	static NULL = new l(null, 0, 0, 0, !1, null, null, null);
	_enterPos;
	_anchorPos;
	depth;
	equals(R) {
		return R === null ? !1 : l._equals(this, R);
	}
	static _equals(l, R) {
		return l === R ? !0 : this._structuralEquals(l, R) ? AttributedScopeStack.equals(l.contentNameScopesList, R.contentNameScopesList) : !1;
	}
	static _structuralEquals(l, R) {
		do {
			if (l === R || !l && !R) return !0;
			if (!l || !R || l.depth !== R.depth || l.ruleId !== R.ruleId || l.endRule !== R.endRule) return !1;
			l = l.parent, R = R.parent;
		} while (!0);
	}
	clone() {
		return this;
	}
	static _reset(l) {
		for (; l;) l._enterPos = -1, l._anchorPos = -1, l = l.parent;
	}
	reset() {
		l._reset(this);
	}
	pop() {
		return this.parent;
	}
	safePop() {
		return this.parent ? this.parent : this;
	}
	push(R, z, B, V, H, U, W) {
		return new l(this, R, z, B, V, H, U, W);
	}
	getEnterPos() {
		return this._enterPos;
	}
	getAnchorPos() {
		return this._anchorPos;
	}
	getRule(l) {
		return l.getRule(this.ruleId);
	}
	toString() {
		let l = [];
		return this._writeString(l, 0), "[" + l.join(",") + "]";
	}
	_writeString(l, R) {
		return this.parent && (R = this.parent._writeString(l, R)), l[R++] = `(${this.ruleId}, ${this.nameScopesList?.toString()}, ${this.contentNameScopesList?.toString()})`, R;
	}
	withContentNameScopesList(l) {
		return this.contentNameScopesList === l ? this : this.parent.push(this.ruleId, this._enterPos, this._anchorPos, this.beginRuleCapturedEOL, this.endRule, this.nameScopesList, l);
	}
	withEndRule(R) {
		return this.endRule === R ? this : new l(this.parent, this.ruleId, this._enterPos, this._anchorPos, this.beginRuleCapturedEOL, R, this.nameScopesList, this.contentNameScopesList);
	}
	hasSameRuleAs(l) {
		let R = this;
		for (; R && R._enterPos === l._enterPos;) {
			if (R.ruleId === l.ruleId) return !0;
			R = R.parent;
		}
		return !1;
	}
	toStateStackFrame() {
		return {
			ruleId: ruleIdToNumber(this.ruleId),
			beginRuleCapturedEOL: this.beginRuleCapturedEOL,
			endRule: this.endRule,
			nameScopesList: this.nameScopesList?.getExtensionIfDefined(this.parent?.nameScopesList ?? null) ?? [],
			contentNameScopesList: this.contentNameScopesList?.getExtensionIfDefined(this.nameScopesList) ?? []
		};
	}
	static pushFrame(R, z) {
		let B = AttributedScopeStack.fromExtension(R?.nameScopesList ?? null, z.nameScopesList);
		return new l(R, ruleIdFromNumber(z.ruleId), z.enterPos ?? -1, z.anchorPos ?? -1, z.beginRuleCapturedEOL, z.endRule, B, AttributedScopeStack.fromExtension(B, z.contentNameScopesList));
	}
}, BalancedBracketSelectors = class {
	balancedBracketScopes;
	unbalancedBracketScopes;
	allowAny = !1;
	constructor(l, R) {
		this.balancedBracketScopes = l.flatMap((l) => l === "*" ? (this.allowAny = !0, []) : createMatchers(l, nameMatcher).map((l) => l.matcher)), this.unbalancedBracketScopes = R.flatMap((l) => createMatchers(l, nameMatcher).map((l) => l.matcher));
	}
	get matchesAlways() {
		return this.allowAny && this.unbalancedBracketScopes.length === 0;
	}
	get matchesNever() {
		return this.balancedBracketScopes.length === 0 && !this.allowAny;
	}
	match(l) {
		for (let R of this.unbalancedBracketScopes) if (R(l)) return !1;
		for (let R of this.balancedBracketScopes) if (R(l)) return !0;
		return this.allowAny;
	}
}, LineTokens = class {
	constructor(l, R, z, B) {
		this.balancedBracketSelectors = B, this._emitBinaryTokens = l, this._tokenTypeOverrides = z, this._lineText = null, this._tokens = [], this._binaryTokens = [], this._lastTokenEndIndex = 0;
	}
	_emitBinaryTokens;
	_lineText;
	_tokens;
	_binaryTokens;
	_lastTokenEndIndex;
	_tokenTypeOverrides;
	produce(l, R) {
		this.produceFromScopes(l.contentNameScopesList, R);
	}
	produceFromScopes(l, R) {
		if (this._lastTokenEndIndex >= R) return;
		if (this._emitBinaryTokens) {
			let z = l?.tokenAttributes ?? 0, B = !1;
			if (this.balancedBracketSelectors?.matchesAlways && (B = !0), this._tokenTypeOverrides.length > 0 || this.balancedBracketSelectors && !this.balancedBracketSelectors.matchesAlways && !this.balancedBracketSelectors.matchesNever) {
				let R = l?.getScopeNames() ?? [];
				for (let l of this._tokenTypeOverrides) l.matcher(R) && (z = EncodedTokenMetadata.set(z, 0, toOptionalTokenType(l.type), null, -1, 0, 0));
				this.balancedBracketSelectors && (B = this.balancedBracketSelectors.match(R));
			}
			if (B && (z = EncodedTokenMetadata.set(z, 0, 8, B, -1, 0, 0)), this._binaryTokens.length > 0 && this._binaryTokens[this._binaryTokens.length - 1] === z) {
				this._lastTokenEndIndex = R;
				return;
			}
			this._binaryTokens.push(this._lastTokenEndIndex), this._binaryTokens.push(z), this._lastTokenEndIndex = R;
			return;
		}
		let z = l?.getScopeNames() ?? [];
		this._tokens.push({
			startIndex: this._lastTokenEndIndex,
			endIndex: R,
			scopes: z
		}), this._lastTokenEndIndex = R;
	}
	getResult(l, R) {
		return this._tokens.length > 0 && this._tokens[this._tokens.length - 1].startIndex === R - 1 && this._tokens.pop(), this._tokens.length === 0 && (this._lastTokenEndIndex = -1, this.produce(l, R), this._tokens[this._tokens.length - 1].startIndex = 0), this._tokens;
	}
	getBinaryResult(l, R) {
		this._binaryTokens.length > 0 && this._binaryTokens[this._binaryTokens.length - 2] === R - 1 && (this._binaryTokens.pop(), this._binaryTokens.pop()), this._binaryTokens.length === 0 && (this._lastTokenEndIndex = -1, this.produce(l, R), this._binaryTokens[this._binaryTokens.length - 2] = 0);
		let z = new Uint32Array(this._binaryTokens.length);
		for (let l = 0, R = this._binaryTokens.length; l < R; l++) z[l] = this._binaryTokens[l];
		return z;
	}
}, SyncRegistry = class {
	constructor(l, R) {
		this._onigLib = R, this._theme = l;
	}
	_grammars = /* @__PURE__ */ new Map();
	_rawGrammars = /* @__PURE__ */ new Map();
	_injectionGrammars = /* @__PURE__ */ new Map();
	_theme;
	dispose() {
		for (let l of this._grammars.values()) l.dispose();
	}
	setTheme(l) {
		this._theme = l;
	}
	getColorMap() {
		return this._theme.getColorMap();
	}
	addGrammar(l, R) {
		this._rawGrammars.set(l.scopeName, l), R && this._injectionGrammars.set(l.scopeName, R);
	}
	lookup(l) {
		return this._rawGrammars.get(l);
	}
	injections(l) {
		return this._injectionGrammars.get(l);
	}
	getDefaults() {
		return this._theme.getDefaults();
	}
	themeMatch(l) {
		return this._theme.match(l);
	}
	grammarForScopeName(l, R, z, B, V) {
		if (!this._grammars.has(l)) {
			let H = this._rawGrammars.get(l);
			if (!H) return null;
			this._grammars.set(l, createGrammar(l, H, R, z, B, V, this, this._onigLib));
		}
		return this._grammars.get(l);
	}
}, Registry = class {
	_options;
	_syncRegistry;
	_ensureGrammarCache;
	constructor(l) {
		this._options = l, this._syncRegistry = new SyncRegistry(Theme.createFromRawTheme(l.theme, l.colorMap), l.onigLib), this._ensureGrammarCache = /* @__PURE__ */ new Map();
	}
	dispose() {
		this._syncRegistry.dispose();
	}
	setTheme(l, R) {
		this._syncRegistry.setTheme(Theme.createFromRawTheme(l, R));
	}
	getColorMap() {
		return this._syncRegistry.getColorMap();
	}
	loadGrammarWithEmbeddedLanguages(l, R, z) {
		return this.loadGrammarWithConfiguration(l, R, { embeddedLanguages: z });
	}
	loadGrammarWithConfiguration(l, R, z) {
		return this._loadGrammar(l, R, z.embeddedLanguages, z.tokenTypes, new BalancedBracketSelectors(z.balancedBracketSelectors || [], z.unbalancedBracketSelectors || []));
	}
	loadGrammar(l) {
		return this._loadGrammar(l, 0, null, null, null);
	}
	_loadGrammar(l, R, z, B, V) {
		let H = new ScopeDependencyProcessor(this._syncRegistry, l);
		for (; H.Q.length > 0;) H.Q.map((l) => this._loadSingleGrammar(l.scopeName)), H.processQueue();
		return this._grammarForScopeName(l, R, z, B, V);
	}
	_loadSingleGrammar(l) {
		this._ensureGrammarCache.has(l) || (this._doLoadSingleGrammar(l), this._ensureGrammarCache.set(l, !0));
	}
	_doLoadSingleGrammar(l) {
		let R = this._options.loadGrammar(l);
		if (R) {
			let z = typeof this._options.getInjections == "function" ? this._options.getInjections(l) : void 0;
			this._syncRegistry.addGrammar(R, z);
		}
	}
	addGrammar(l, R = [], z = 0, B = null) {
		return this._syncRegistry.addGrammar(l, R), this._grammarForScopeName(l.scopeName, z, B);
	}
	_grammarForScopeName(l, R = 0, z = null, B = null, V = null) {
		return this._syncRegistry.grammarForScopeName(l, R, z, B, V);
	}
}, INITIAL = StateStackImpl.NULL;
export { EncodedTokenMetadata, FontStyle, INITIAL, Registry, Theme };
