@sema-agent/core
Version:
Stateless, task-oriented AI agent core
80 lines (79 loc) • 3.96 kB
JavaScript
import { SHELL_LEX_DIALECT, argvOf, lexShell } from "../../shell/lex.js";
export const READINGS_MAX = 64;
const BASH_RELEASES = ["3.2", "4+"];
export function braceWordListsOf(words) {
const lists = BASH_RELEASES.map((release) => words.flatMap((w) => braceWordsOf(w, release)));
const key = (list) => list.map((w) => w.raw).join("\u0000");
return lists.filter((list, i) => lists.findIndex((other) => key(other) === key(list)) === i);
}
function braceWordsOf(word, release) {
if (!word.pieces.some((p) => p.expansion?.kind === "brace"))
return [word];
const expand = (text) => {
const w = argumentLexOf(text);
if (w === undefined || w.unresolved !== undefined)
return undefined;
const list = w.pieces.map((p) => ({ p, alternatives: p.expansion?.kind === "brace" ? braceAlternativesOf(p.expansion.body ?? "", release) : undefined })).find((x) => x.alternatives !== undefined);
if (list === undefined)
return [w];
const made = [];
const [from, to] = [list.p.span[0] - 2, list.p.span[1] - 2];
for (const alternative of list.alternatives) {
const next = `${text.slice(0, from)}${alternative}${text.slice(to)}`;
if (next === "")
continue;
const words = expand(next);
if (words === undefined)
return undefined;
made.push(...words);
if (made.length > READINGS_MAX)
return undefined;
}
return made;
};
const words = expand(word.raw);
return words === undefined || (words.length === 1 && words[0].raw === word.raw) ? [word] : words;
}
function argumentLexOf(text) {
const lexed = lexShell(SHELL_LEX_DIALECT, `: ${text}`);
const words = "parseFailure" in lexed || lexed.segments.length !== 1 ? undefined : argvOf(lexed.segments[0]);
return words?.length === 2 && words[1].raw === text ? words[1] : undefined;
}
function braceAlternativesOf(body, release) {
const lexed = lexShell(SHELL_LEX_DIALECT, `""${body}`);
const words = "parseFailure" in lexed || lexed.segments.length !== 1 ? undefined : argvOf(lexed.segments[0]);
if (words?.length !== 1)
return undefined;
const cuts = [];
for (const p of words[0].pieces)
if (p.quotedBy === undefined && p.literal !== undefined)
for (let i = p.literal.indexOf(","); i >= 0; i = p.literal.indexOf(",", i + 1))
cuts.push(p.span[0] - 2 + i);
if (cuts.length === 0)
return sequenceAlternativesOf(body, release);
return [...cuts, body.length].map((cut, k) => body.slice(k === 0 ? 0 : cuts[k - 1] + 1, cut));
}
function sequenceAlternativesOf(body, release) {
const m = /^([+-]?\d+|[A-Za-z])\.\.([+-]?\d+|[A-Za-z])(?:\.\.([+-]?\d+))?$/.exec(body);
if (m === null)
return undefined;
const [lhs, rhs, step] = [m[1], m[2], m[3]];
const letter = (t) => /^[A-Za-z]$/.test(t);
if (letter(lhs) !== letter(rhs))
return undefined;
if (step !== undefined && release === "3.2")
return [`\\{${body}\\}`];
const [from, to] = letter(lhs) ? [lhs.charCodeAt(0), rhs.charCodeAt(0)] : [Number(lhs), Number(rhs)];
const incr = Math.abs(step === undefined ? 1 : Number(step)) || 1;
if (![from, to, incr].every(Number.isSafeInteger) || Math.abs(to - from) / incr >= READINGS_MAX)
return undefined;
const terms = [];
for (let v = from; from <= to ? v <= to : v >= to; v += from <= to ? incr : -incr)
terms.push(v);
if (letter(lhs)) {
const chars = terms.map((c) => String.fromCharCode(c));
return chars.every(letter) ? chars : undefined;
}
const width = release === "3.2" || ![lhs, rhs].some((t) => /^-?0\d/.test(t)) ? 0 : Math.max(lhs.length, rhs.length);
return terms.map((v) => (width === 0 ? String(v) : v < 0 ? `-${String(-v).padStart(width - 1, "0")}` : String(v).padStart(width, "0")));
}