@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
129 lines • 5.45 kB
JavaScript
/**
* The line-matching port. Widget patterns are model-written, so the production engine must run in
* linear time (RE2 semantics); a JS `RegExp` can backtrack catastrophically and is not shipped as
* an engine. {@link compilePatterns} rejects what RE2 cannot run (lookaround, backreferences,
* recursion and subroutine calls, conditionals, backtracking verbs) in every spelling before an
* engine sees a pattern, and hands it one group spelling, `(?<name>…)`.
*/
export class MatcherCompileError extends Error {
constructor(pattern, reason) {
super(`pattern ${pattern}: ${reason}`);
this.name = "MatcherCompileError";
this.pattern = pattern;
this.reason = reason;
}
}
const NAMED_GROUP_AT = /^\(\?P?<([A-Za-z_][A-Za-z0-9_]*)>/;
/**
* Why the escape whose letter is at `at` (outside a class) needs a backtracking engine: `\1`-`\9`,
* `\k<n>`, `\k'n'`, `\k{n}`, `\g1`, `\g-1`, `\g{n}` refer back, `\g<n>` and `\g'n'` call a group.
*/
function escapeReference(pattern, at) {
const letter = pattern.charAt(at);
const after = pattern.charAt(at + 1);
if (letter >= "1" && letter <= "9")
return `backreference \\${letter} is not supported`;
if (letter === "k" && (after === "<" || after === "'" || after === "{")) {
return `named backreference \\k${after}… is not supported`;
}
if (letter === "g") {
if (after === "<" || after === "'")
return `subroutine call (recursion) \\g${after}… is not supported`;
if (after === "{" || after === "-" || after === "+" || (after >= "0" && after <= "9")) {
return `backreference \\g${after}… is not supported`;
}
}
return undefined;
}
/** Why the `(?` construct starting at `at` (the `(`) needs a backtracking engine. */
function groupConstruct(pattern, at) {
const rest = pattern.slice(at + 2, at + 4);
if (/^(?:[=!*]|<[=!*])/.test(rest))
return "lookaround (?=, (?!, (?<=, (?<! is not supported";
if (rest.startsWith("P="))
return "named backreference (?P=…) is not supported";
if (rest.startsWith("P>") || rest.startsWith("&") || /^(?:R|[+-]?\d)/.test(rest)) {
return "recursion (?R), (?1), (?&…), (?P>…) is not supported";
}
if (rest.startsWith("("))
return "conditional (?(…) is not supported";
return undefined;
}
/**
* Reads a pattern's named groups and constructs outside escapes, character classes and `\Q\E`
* quoted text (literal in RE2, up to `\E` or the end of the pattern), so the groups it reports are
* the ones RE2's `namedGroups()` reports.
*/
export function scanPattern(pattern) {
const groups = [];
let normalized = "";
let inClass = false;
const fail = (error) => ({ groups, normalized: pattern, error });
for (let index = 0; index < pattern.length; index++) {
const char = pattern.charAt(index);
if (char === "\\") {
const next = pattern.charAt(index + 1);
if (!inClass && next === "Q") {
const close = pattern.indexOf("\\E", index + 2);
const end = close < 0 ? pattern.length : close + 2;
normalized += pattern.slice(index, end);
index = end - 1;
continue;
}
const reference = inClass ? undefined : escapeReference(pattern, index + 1);
if (reference !== undefined)
return fail(reference);
normalized += char + next;
index += 1;
continue;
}
if (inClass) {
if (char === "]")
inClass = false;
normalized += char;
continue;
}
if (char === "[") {
inClass = true;
normalized += char;
// A `]` right after `[` or `[^` is a literal member, not the class end.
if (pattern.charAt(index + 1) === "^")
normalized += pattern.charAt(++index);
if (pattern.charAt(index + 1) === "]")
normalized += pattern.charAt(++index);
continue;
}
if (char === "(" && pattern.charAt(index + 1) === "*") {
return fail("backtracking verb or alpha assertion (*…) is not supported");
}
if (char === "(" && pattern.charAt(index + 1) === "?") {
const construct = groupConstruct(pattern, index);
if (construct !== undefined)
return fail(construct);
const named = NAMED_GROUP_AT.exec(pattern.slice(index));
if (named) {
groups.push(named[1]);
normalized += `(?<${named[1]}>`;
index += named[0].length - 1;
continue;
}
}
normalized += char;
}
return { groups, normalized };
}
/** Named groups a pattern declares, in pattern order. */
export function patternGroupNames(pattern) {
return scanPattern(pattern).groups;
}
/** Checks every pattern's syntax, then compiles the normalized patterns on `matcher`. */
export function compilePatterns(matcher, patterns) {
const normalized = patterns.map((pattern, index) => {
const scan = scanPattern(pattern);
if (scan.error !== undefined)
throw new MatcherCompileError(index, scan.error);
return scan.normalized;
});
return matcher.compile(normalized);
}
//# sourceMappingURL=matcher.js.map