// PEP-124 / PEP-152: the glossary of words a person sees (docs/glossary.md) — parsing of the table // «Понятие | Пишем | Нельзя | Пояснение» and the search of forbidden words in a text. Pure: used // by the check of screens (`pnpm lint`, packages/testkit/src/glossary-check.ts) and by the check // of agents' messages at the moment they are sent (core/agent-message.ts). export type GlossaryRule = { concept: string; write: string; forbidden: string[]; allowed: string[] }; const cells = (line: string) => line .trim() .replace(/^\||\|$/g, "") .split("|") .map((c) => c.trim()); const terms = (cell: string) => cell .split(",") .map((t) => t.trim()) .filter((t) => t && !/^[—–-]+$/.test(t)); /** Rows of the table «Понятие | Пишем | Нельзя | Пояснение». */ export function parseGlossary(markdown: string): GlossaryRule[] { const rules: GlossaryRule[] = []; let cols: { concept: number; write: number; forbidden: number } | null = null; for (const line of markdown.split("\n")) { if (!line.trim().startsWith("|")) { cols = null; continue; } const row = cells(line); if (!cols) { const forbidden = row.indexOf("Нельзя"); if (forbidden >= 0) cols = { concept: Math.max(row.indexOf("Понятие"), 0), write: row.indexOf("Пишем"), forbidden }; continue; } if (row.every((c) => /^:?-+:?$/.test(c))) continue; const write = cols.write >= 0 ? (row[cols.write] ?? "") : ""; rules.push({ concept: row[cols.concept] ?? "", write, forbidden: terms(row[cols.forbidden] ?? ""), allowed: terms(write) }); } if (!rules.some((r) => r.forbidden.length)) throw new Error("В словаре нет таблицы с колонкой «Нельзя»"); return rules; } const esc = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); const CONSONANT = /[бвгджзклмнпрстфхцчшщ]$/i; /** Russian case forms of a noun, by its ending: «раннер» → раннера, раннеров…; «ветка» → ветки, веток. */ export function wordForms(word: string): string[] { const w = word; const cyr = /[а-яё]$/i.test(w) && w.length >= 3; if (!cyr || /(ть|ться)$/i.test(w)) return [w]; const with_ = (stem: string, ends: string[]) => [...new Set([w, ...ends.map((e) => stem + e)])]; const hard = ["", "а", "у", "ом", "ем", "е", "ы", "и", "ов", "ей", "ам", "ами", "ах"]; if (/ия$/i.test(w)) return with_(w.slice(0, -1), ["я", "и", "ю", "ей", "й", "е", "ем", "ям", "ями", "ях"]); if (/[аеиоу]й$/i.test(w)) return with_(w.slice(0, -1), ["й", "я", "ю", "ё", "е", "ем", "и", "ев", "ям", "ями", "ях"]); if (/ь$/i.test(w)) return with_(w.slice(0, -1), ["ь", "и", "я", "ю", "ью", "ем", "ей", "ям", "ями", "ях"]); if (/а$/i.test(w)) { const stem = w.slice(0, -1); const forms = with_(stem, ["а", "ы", "и", "е", "у", "ой", "ою", "", "ам", "ами", "ах"]); // «ветка» → «веток», «выкатка» → «выкаток». if (/[бвгджзлмнпрстфхцчшщ]к$/i.test(stem)) forms.push(`${stem.slice(0, -1)}ок`, `${stem.slice(0, -1)}ек`); return forms; } // A plural in the glossary («токены», «инциденты»): the singular is forbidden as well. if (w.length >= 5 && (/[бвгджзклмнпрстфхцчшщ]ы$/i.test(w) || /[гкхжчшщ]и$/i.test(w))) return with_(w.slice(0, -1), hard); if (CONSONANT.test(w)) return with_(w, hard); return [w]; } const EDGE_BEFORE = "(? r.forbidden.map((word) => ({ word, write: r.write, re: termRegExp(word) }))); // «Peppa FACTORY» is what we write although «Peppa» alone is forbidden: such phrases are // blanked out before the search. const allowed = rules .flatMap((r) => r.allowed) .filter((phrase) => forbidden.some((f) => new RegExp(f.re.source, f.re.flags).test(phrase))) .map(termRegExp); return { find(text) { let clean = text; for (const re of allowed) clean = clean.replace(re, (m) => " ".repeat(m.length)); const hits: { word: string; found: string; index: number; write: string }[] = []; for (const f of forbidden) { for (const m of clean.matchAll(f.re)) hits.push({ word: f.word, found: m[0], index: m.index, write: f.write }); } // «pull request» also contains nothing shorter, but «merge request» ⊃ … — keep the longest hit per place. hits.sort((a, b) => a.index - b.index || b.found.length - a.found.length); const out: typeof hits = []; let end = -1; for (const h of hits) { if (h.index < end) continue; out.push(h); end = h.index + h.found.length; } return out; }, }; }