/** * Grammar API Edge Case Tests * * These tests document the CURRENT behavior of the grammar API for known edge * cases. Each test carries a short issue ID (C1–C5, M1–M4, m1–m5, Q6–Q7) and * notes the DESIRED behavior after a fix. * * Tests pass against the current implementation. They are diagnostic, not * prescriptive — the assertions describe what actually happens today. */ import { describe, test, expect, vi } from "vitest"; import { compile } from "./compiler"; import { tokenize } from "./tokenizer"; import type { Grammar, TokenizeResult } from "./types"; // --------------------------------------------------------------------------- // Shared helper // --------------------------------------------------------------------------- function token_values(result: TokenizeResult, input: string): { type: string; value: string }[] { const out: { type: string; value: string }[] = []; const count = result.tokens.length / 3; for (let i = 0; i < count; i++) { const type_idx = result.tokens[i * 3]; const type = result.token_types[type_idx]; const start = result.tokens[i * 3 + 1]; const end = result.tokens[i * 3 + 2]; if (type !== undefined) { out.push({ type, value: input.slice(start, end) }); } } return out; } // --------------------------------------------------------------------------- // C3 — Dangling state reference: typo compiles silently to no-op // --------------------------------------------------------------------------- describe("C3: Dangling state reference", () => { test("unknown state name throws at compile time with a clear message", () => { const grammar: Grammar = { name: "dangling-state-test", states: { main: { rules: [ // Typo: "inner_state" does not exist (correct would be "inner") { match: "(", token: "open", state: "inner_state" as any }, { range: ["a", "z"], token: "letter" }, { match: ")", token: "close" }, ], }, inner: { rules: [ { match: ")", token: "close", exit: true }, { range: ["A", "Z"], token: "caps" }, ], }, }, }; expect(() => compile(grammar)).toThrow(/unknown state "inner_state"/); }); test("sideways transition to unknown state also throws at compile time", () => { const grammar: Grammar = { name: "dangling-sideways-test", states: { main: { rules: [{ match: "a", token: "a", state: "typo_state" as any, exit: true }], }, }, }; expect(() => compile(grammar)).toThrow(/unknown state "typo_state"/); }); test("dangling probe fallback reference throws at compile time", () => { const grammar: Grammar = { name: "dangling-fallback-test", states: { main: { rules: [{ range: ["a", "z"], state: "probing" as any }], }, probing: { mode: "probe", fallback: "not_a_real_state" as any, rules: [{ match: "!", state: "resolved" }], }, resolved: { rules: [{ range: ["a", "z"], token: "resolved" }], }, }, }; expect(() => compile(grammar)).toThrow(/unknown state "not_a_real_state"/); }); }); // --------------------------------------------------------------------------- // C5 — exit:true in root state is a silent no-op // --------------------------------------------------------------------------- describe("C5: exit:true in root state", () => { test("exit in root state is silently ignored — tokenizer stays in root", () => { const grammar: Grammar = { name: "root-exit-test", states: { main: { rules: [ { match: "x", token: "x", exit: true }, // exit on stackPtr=0 → no-op { range: ["a", "z"], token: "letter" }, ], }, }, }; const warn_spy = vi.spyOn(console, "warn").mockImplementation(() => {}); const compiled = compile(grammar); expect(warn_spy).toHaveBeenCalledWith(expect.stringContaining('rule 0 in root state "main"')); expect(warn_spy).toHaveBeenCalledWith(expect.stringContaining("exit will be a no-op")); warn_spy.mockRestore(); const result = tokenize("axb", compiled); const tokens = token_values(result, "axb"); // exit fires but stackPtr=0 so pop is skipped, stays in main expect(tokens).toEqual([ { type: "letter", value: "a" }, { type: "x", value: "x" }, { type: "letter", value: "b" }, ]); }); test("exit with sideways in root state IS valid — transitions without stack change", () => { // Sideways transition (state + exit) does not require a stack entry — // it just changes currentState. So this is valid even in root state. const grammar: Grammar = { name: "sideways-from-root", states: { main: { rules: [ { match: "a", token: "a", state: "other", exit: true }, { range: ["b", "z"], token: "letter" }, ], }, other: { rules: [ { range: ["a", "z"], token: "other-letter" }, { any: true, state: "main", exit: true }, ], }, }, }; const compiled = compile(grammar); const result = tokenize("abc", compiled); const tokens = token_values(result, "abc"); // 'a' → sideways to "other" state (valid, no stack) // 'b' in "other" → other-letter // 'c' in "other" → other-letter (coalesced) expect(tokens).toEqual([ { type: "a", value: "a" }, { type: "other-letter", value: "bc" }, ]); }); }); // --------------------------------------------------------------------------- // C4 — State count limit: 254 is safe, behavior at limit // --------------------------------------------------------------------------- describe("C4: State count limit", () => { test("grammar with 100 states compiles cleanly", () => { const states: Record = {}; for (let i = 0; i < 100; i++) { states[`s${i}`] = { rules: [{ match: `${i % 10}`, token: "num" }], }; } const grammar: Grammar = { name: "limit-safe", states }; expect(() => compile(grammar)).not.toThrow(); const compiled = compile(grammar); expect(compiled.states.size).toBe(100); }); test("grammar with 253 states compiles without error (well under the 254 safe limit)", () => { // match_within generates hidden states — keep user-defined states well under 254 const states: Record = {}; for (let i = 0; i < 253; i++) { states[`s${i}`] = { rules: [{ range: ["a", "z"], token: "letter" }], }; } const grammar: Grammar = { name: "limit-near", states }; expect(() => compile(grammar)).not.toThrow(); const compiled = compile(grammar); expect(compiled.states.size).toBe(253); }); test("match_within expands to hidden states — state count inflates", () => { // Each match_within with escape generates 2 hidden states. // 3 match_within rules with escape = 6 hidden states. const grammar: Grammar = { name: "hidden-state-count", states: { main: { rules: [ { match_within: { start: '"', end: '"', escape: "\\" }, token: "str1" }, { match_within: { start: "'", end: "'", escape: "\\" }, token: "str2" }, { match_within: { start: "`", end: "`", escape: "\\" }, token: "str3" }, { range: ["a", "z"], token: "letter" }, ], }, }, }; const compiled = compile(grammar); // 1 user state + 6 hidden states = 7 total // (each match_within with escape: 1 content state + 1 escape state) expect(compiled.states.size).toBe(7); }); }); // --------------------------------------------------------------------------- // C1 — match_within: "begin" key is silently ignored (docs say "begin", code uses "start") // --------------------------------------------------------------------------- describe("C1: match_within begin vs start", () => { test("using 'start' (correct) tokenizes strings properly", () => { const grammar: Grammar = { name: "match-within-start", states: { main: { rules: [ { match_within: { start: '"', end: '"' }, token: "string" }, { range: ["a", "z"], token: "word" }, ], }, }, }; const compiled = compile(grammar); const result = tokenize('"hello" world', compiled); const tokens = token_values(result, '"hello" world'); expect(tokens).toContainEqual({ type: "string", value: '"hello"' }); expect(tokens).toContainEqual({ type: "word", value: "world" }); }); test("using 'begin' (documented but wrong) throws a clear error at compile time", () => { const grammar = { name: "match-within-begin", states: { main: { rules: [ { match_within: { begin: '"', end: '"' }, token: "string" }, { range: ["a", "z"], token: "word" }, ], }, }, } as unknown as Grammar; expect(() => compile(grammar)).toThrow('match_within uses "start" not "begin"'); }); }); describe("match_within: non-ascii content", () => { const grammar: Grammar = { name: "match-within-non-ascii", states: { main: { rules: [ { match_within: { start: "" }, token: "comment" }, { match_within: { start: '"', end: '"', escape: "\\", multiline: false }, token: "string", }, { range: ["a", "z"], token: "word" }, ], }, }, }; const compiled = compile(grammar); test("non-ascii and astral characters stay inside the token", () => { const src = ""; const tokens = token_values(tokenize(src, compiled), src); expect(tokens.map((t) => t.value).join("")).toBe(src); expect(tokens.every((t) => t.type === "comment")).toBe(true); expect(tokens[0].value).toBe("