UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

50 lines • 2.23 kB
import { RE2JS, RE2JSException } from "re2js"; import { MatcherCompileError, } from "./matcher.js"; /** * The production {@link LineMatcher}, backed by re2js (a JavaScript port of RE2): automata * matching without backtracking, so a model-written pattern runs in time linear in the line. * Patterns compile with no flags; `RE2JS.LOOKBEHINDS` is never set, so lookaround and * backreferences stay compile errors here as well as in {@link compilePatterns}. re2js reads both * `(?<name>…)` and `(?P<name>…)`, and reports spans in UTF-16 units, as the port wants. */ export class Re2LineMatcher { compile(patterns) { const compiled = patterns.map((pattern, index) => compilePattern(pattern, index)); const groupNames = compiled.map(({ groups }) => groups.map(([name]) => name)); return { groupNames, matchLines(lines) { const matches = []; lines.forEach((line, lineIndex) => { compiled.forEach(({ expression, groups }, pattern) => { const matcher = expression.matcher(line); if (!matcher.find()) return; const spans = groups.map(([, group]) => { const start = matcher.start(group); return start < 0 ? undefined : { start, end: matcher.end(group) }; }); matches.push({ line: lineIndex, pattern, spans }); }); }); return matches; }, }; } } function compilePattern(pattern, index) { let expression; try { expression = RE2JS.compile(pattern); } catch (error) { if (error instanceof RE2JSException) throw new MatcherCompileError(index, error.message); throw error; } const groups = Object.entries(expression.namedGroups()).sort(([, a], [, b]) => a - b); return { expression, groups }; } /** The matcher job widgets run on: linear-time RE2 semantics. Stateless, so one instance is shared. */ export const defaultLineMatcher = new Re2LineMatcher(); //# sourceMappingURL=re2-matcher.js.map