Spaces:
No application file
No application file
File size: 1,975 Bytes
494fa67 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | //#region src/scanner.ts
const MAX = 4294967295;
var JavaScriptScanner = class {
patterns;
options;
regexps;
constructor(patterns, options = {}) {
this.patterns = patterns;
this.options = options;
const { forgiving = false, cache, regexConstructor } = options;
if (!regexConstructor) throw new Error("Option `regexConstructor` is not provided");
this.regexps = patterns.map((p) => {
if (typeof p !== "string") return p;
const cached = cache?.get(p);
if (cached) {
if (cached instanceof RegExp) return cached;
if (forgiving) return null;
throw cached;
}
try {
const regex = regexConstructor(p);
cache?.set(p, regex);
return regex;
} catch (e) {
cache?.set(p, e);
if (forgiving) return null;
throw e;
}
});
}
findNextMatchSync(string, startPosition, _options) {
const str = typeof string === "string" ? string : string.content;
const pending = [];
function toResult(index, match, offset = 0) {
return {
index,
captureIndices: match.indices.map((indice) => {
if (indice == null) return {
start: MAX,
end: MAX,
length: 0
};
return {
start: indice[0] + offset,
end: indice[1] + offset,
length: indice[1] - indice[0]
};
})
};
}
for (let i = 0; i < this.regexps.length; i++) {
const regexp = this.regexps[i];
if (!regexp) continue;
try {
regexp.lastIndex = startPosition;
const match = regexp.exec(str);
if (!match) continue;
if (match.index === startPosition) return toResult(i, match, 0);
pending.push([
i,
match,
0
]);
} catch (e) {
if (this.options.forgiving) continue;
throw e;
}
}
if (pending.length) {
const minIndex = Math.min(...pending.map((m) => m[1].index));
for (const [i, match, offset] of pending) if (match.index === minIndex) return toResult(i, match, offset);
}
return null;
}
};
//#endregion
export { JavaScriptScanner as t };
|