Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/isc/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import type { IscDocument, IscItem, IscRule, IscStageItem } from "./types.js"
import { IscLoadError } from "./types.js"
import { MapLogicError } from "../errors.js"
import { expandDocument, type DependencyDocResolver } from "./expand.js"
import type {
AliasItem,
Expand Down Expand Up @@ -220,10 +221,20 @@ function convertItem(item: IscItem): Item {
return r
}
case "function": {
// Functions in non-`to` positions are unexpected — fall back to alias
// lookup so a clear error surfaces at runtime.
const r: AliasItem = { kind: "alias", name: item.name }
return r
// `any_character` is the primitive spelled as a function atom
// (actions.ts lowers it this way; rs lowers it to the
// any_character stdlib alias) — keep the alias path.
if (item.name === "any_character") {
const r: AliasItem = { kind: "alias", name: item.name }
return r
}
// WO19 G6 item 2: a fn literal in FROM position is map-logic
// damage — ruby raises (canon), rs raises at build. The former
// alias-fallback degraded to a misleading "Alias <name> not
// found"; raise the named error at compile instead.
throw new MapLogicError(
`function literal "${item.name}" in from position (fn literals are directives or to-funcalls, not from values)`,
)
}
case "alias_ref": {
const r: AliasItem = { kind: "alias", name: item.name }
Expand Down
23 changes: 8 additions & 15 deletions src/isc/grammar/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,14 @@ export class TreeToDocument {
else if (h.u_lone !== undefined) out += "U"
else if (h.unicode !== undefined) {
const digits = (h.unicode as Slice).content
// DEVIATION (hand-parser parity): the grammar cannot distinguish
// `\uXXXX` from `\UXXXX` (both captures are named :unicode), but the
// hand parser only decodes lowercase \u and renders a capital \U as
// a literal "U" followed by the raw digits. The slice offset tells
// us which spelling the source used (the escape letter sits one
// position before the digits). Six corpus maps use `\U`; mirroring
// the hand parser keeps their documents — and therefore their
// transliteration output — byte-identical across the swap. The ruby
// artifact parser decodes both spellings; aligning ts/py/ruby on the
// grammar behavior is an owner decision, not this parser's to make.
if (this.src.charCodeAt((h.unicode as Slice).offset - 1) === 0x55) {
out += "U" + digits
} else {
out += String.fromCharCode(parseInt(digits, 16))
}
// WO19 G6 ruling: BOTH escape spellings decode. The former hand
// parser only decoded lowercase \u and rendered a capital \U as a
// literal "U" followed by the raw digits, so gki-bel's
// `sub "\U040E" "Ŭ"` carried the from-value "U040E" and the row
// never fired. The grammar captures both spellings under :unicode
// and the tree carries the decoded code point (ruby/rs always
// decoded both).
out += String.fromCodePoint(parseInt(digits, 16))
} else {
throw new Error(`unknown string part ${JSON.stringify(Object.keys(h))}`)
}
Expand Down
6 changes: 3 additions & 3 deletions src/isc/grammar/grammar-source.ts

Large diffs are not rendered by default.

38 changes: 18 additions & 20 deletions src/isc/grammar/isc-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Single-sourcing contract: the grammar TEXT embedded in grammar-source.ts
* is the semantic source of truth (synced from interscript-ruby's
* lib/interscript/isc/grammar/isc.parg via scripts/sync-isc-grammar.mjs).
* The three documented grammar-table extensions below (GRAMMAR_EXTENSIONS)
* The two documented grammar-table extensions below (GRAMMAR_EXTENSIONS)
* restore hand-parser/py behaviors the canonical .parg rejects — each is
* spelled in the grammar language, named, and corpus-proven dormant.
*/
Expand All @@ -31,8 +31,8 @@ interface Compiled {
let compiled: Compiled | undefined

/**
* Grammar-table extensions — the three places where the former hand
* parser (and the python runtime) accept syntax the canonical .parg
* Grammar-table extensions — the two places where the former hand
* parser (and the python runtime) accepted syntax the canonical .parg
* rejects, pinned by the ts unit suite and absent from the corpus. Each
* extension is spelled in the grammar language itself and merged into
* the rule table, so the .parg remains the only semantic source and
Expand All @@ -42,29 +42,27 @@ let compiled: Compiled | undefined
* fallback (concerns/items.rb any_constructor), which the canonical
* .parg dropped; the interscript-rs vendored copy keeps it.
* (`any(space)`, `any(space + line_end)` — parser.test.ts pins both.)
* 2. `block_rule` — accept the `when` guard AFTER the closing brace of
* the block form. The ruby DSL places the guard inside the braces;
* the ts and py runtimes both place it outside, and the ts suite pins
* the outside form (wf1.test.ts). The outside branch is tried first
* (it is the greedy one; its mandatory `}` keeps it from shadowing
* the inside form).
* 3. `run_dep` — allow hyphenated dependency names (`run map.some-dep.…`,
* 2. `run_dep` — allow hyphenated dependency names (`run map.some-dep.…`,
* wf1.test.ts). The grammar's identifier is [A-Za-z_][A-Za-z0-9_]*;
* the former hand parser's identifier admitted `-` in structural
* positions. Only the dependency slot is widened; the corpus is
* hyphen-free in every identifier position.
*
* Ruby rejects (1)'s absence aside, the outside-when and hyphen-dep
* forms are cross-runtime divergences (ruby parses neither); aligning
* ruby/ts/py on one placement is an owner decision. None of the three
* extensions fires on any corpus map (proven: the corpus tree-diff and
* document-diff harnesses both pass with them in place, and corpus
* trees were byte-identical before they were added).
* The former `block_rule` extension (the `when` guard accepted AFTER
* the closing brace of the block form) was RETIRED by WO19 G6 item 5:
* canonical rs binds the guard inside the braces and ruby parses the
* inside form only, so ts/py now reject the outside form too (the
* inside form is the canonical grammar's own rule; corpus-proven
* dormant). Ruby rejects (1)'s absence aside, the hyphen-dep form
* remains a cross-runtime divergence (ruby parses neither); aligning
* ruby/ts/py on it is an owner decision. None of the extensions fires
* on any corpus map (proven: the corpus tree-diff and document-diff
* harnesses both pass with them in place, and corpus trees were
* byte-identical before they were added).
*/
const GRAMMAR_EXTENSIONS = `
grammar IscExtensions version "0.0.0" {
any_arg = range_arg / set_arg / alias_arg / ((item) as any_item)
block_rule = ("sub" [ws] "{" [ws] "from" whitespace ((item) as from) [ws] "to" whitespace ((item) as to) ((*(whitespace constraint)) as constraints) [ws] "}" [(when_guard)]) / ("sub" [ws] "{" [ws] "from" whitespace ((item) as from) [ws] "to" whitespace ((item) as to) ((*(whitespace constraint)) as constraints) [(when_guard)] [ws] "}")
run_dep = "map." (((identifier *("-" identifier)) / identifier) as dep) ".stage." (identifier as stage)
}
`
Expand All @@ -73,10 +71,10 @@ function compileGrammar(): Compiled {
if (compiled !== undefined) return compiled
const grammar = parseParg(ISC_PARG_SOURCE)
const rules: RuleTable = { ...grammar.rules }
// Shape-guarded merge: if a future .parg edit renames any of the three
// rules, the merge throws instead of silently misparsing.
// Shape-guarded merge: if a future .parg edit renames any of the
// extension rules, the merge throws instead of silently misparsing.
const ext = parseParg(GRAMMAR_EXTENSIONS)
for (const name of ["any_arg", "block_rule", "run_dep"]) {
for (const name of ["any_arg", "run_dep"]) {
const replacement = ext.rules[name]
if (rules[name] === undefined || replacement === undefined) {
throw new Error(
Expand Down
9 changes: 9 additions & 0 deletions src/runtime/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import {
separate,
compose,
decompose,
reverse,
strip,
swapcase,
} from "../stdlib.js"

type RuleKind = Rule["kind"]
Expand All @@ -42,6 +45,12 @@ const BUILTIN_FUNCTIONS: Record<string, (input: string, opts?: Record<string, un
downcase,
upcase,
title_case: (i, o) => titleCase(i, o ?? {}),
// WO19 G6: the three new fn literals execute as directives and
// inline to-funcalls with the plain String semantics of ruby
// Stdlib::Functions (String#reverse / String#strip / String#swapcase).
reverse,
strip,
swapcase,
separate: (i, o) => separate(i, o ?? {}),
compose,
decompose,
Expand Down
32 changes: 32 additions & 0 deletions src/stdlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,38 @@ export function decompose(input: string): string {
return input.normalize("NFD")
}

/**
* Reverse a string by code point. Maps to
* `Interscript.functions.reverse` (ruby String#reverse) — WO19 G6.
*/
export function reverse(input: string): string {
return [...input].reverse().join("")
}

/**
* Trim leading/trailing whitespace. Maps to `Interscript.functions.strip`
* (ruby String#strip: the ASCII whitespace + NUL set, both ends) — WO19 G6.
*/
export function strip(input: string): string {
// ruby String#strip's character set: NUL, tab, LF, VT, FF, CR, space.
return input.replace(/^[\0\t\n\v\f\r ]+/, "").replace(/[\0\t\n\v\f\r ]+$/, "")
}

/**
* Swap the case of every character (full Unicode mapping, matching
* toUpperCase/toLowerCase semantics). Maps to
* `Interscript.functions.swapcase` (ruby String#swapcase) — WO19 G6.
*/
export function swapcase(input: string): string {
let out = ""
for (const ch of input) {
const lo = ch.toLowerCase()
const up = ch.toUpperCase()
out += lo !== ch ? lo : up !== ch ? up : ch
}
return out
}

/**
* Megaregexp parallel replace — Ruby's fallback for parallel blocks
* where any rule has `before`/`after`/`not_before`/`not_after` constraints.
Expand Down
89 changes: 89 additions & 0 deletions test/isc/fail-loud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,92 @@ describe("WO18 G4 — missing stage fails loud", () => {
expect(transliterate(code, "a")).toBe("b")
})
})

// ---------------------------------------------------------------------------
// WO19 G6 — fn-literal alignment
// ---------------------------------------------------------------------------

const FN_FROM_MAP = `# G6 item 2: a fn literal in FROM position is map-logic damage.
system "T:tst-g6-fnfrom:1" {
stage main {
parallel {
sub downcase "X"
sub "k" "q"
}
}
}`

const REVERSE_DIR_MAP = `# G6 item 3: the reverse directive executes (ruby String#reverse).
system "T:tst-g6-revdir:1" {
stage main {
sub "1" "2"
reverse
}
}`

const STRIP_DIR_MAP = `# G6 item 3: the strip directive executes (ruby String#strip).
system "T:tst-g6-stripdir:1" {
stage main {
strip
}
}`

const SWAPCASE_DIR_MAP = `# G6 item 3: the swapcase directive executes (ruby String#swapcase).
system "T:tst-g6-swapdir:1" {
stage main {
sub "a" "b"
swapcase
}
}`

const REVERSE_TO_MAP = `# G6 item 3: a reverse fn literal as a \`to\` target.
system "T:tst-g6-revto:1" {
stage main {
sub "abc" reverse
}
}`

const KEYWORD_ALIAS_MAP = `# G6 item 1: the six fn names are reserved — a bare name is never an
# alias reference, but \`downcase = any(...)\` still DECLARES an alias
# named downcase (alias_decl has no keyword guard).
system "T:tst-g6-keyword:1" {
aliases { downcase = any("a".."z") }
stage main {
sub "k" "K"
}
}`

describe("WO19 G6 — fn literals", () => {
beforeEach(() => reset())

it("a fn literal in FROM position raises MapLogicError at compile (was: alias-lookup corruption)", () => {
const code = loadMap(FN_FROM_MAP)
expect(() => transliterate(code, "kak")).toThrow(MapLogicError)
expect(() => transliterate(code, "kak")).toThrow(/function literal "downcase" in from position/)
})

it("the reverse directive executes (String#reverse)", () => {
const code = loadMap(REVERSE_DIR_MAP)
expect(transliterate(code, "abc1")).toBe("2cba")
})

it("the strip directive executes (String#strip)", () => {
const code = loadMap(STRIP_DIR_MAP)
expect(transliterate(code, " x ")).toBe("x")
})

it("the swapcase directive executes (String#swapcase)", () => {
const code = loadMap(SWAPCASE_DIR_MAP)
expect(transliterate(code, "aBc")).toBe("BbC")
})

it("a reverse fn literal as the to target executes", () => {
const code = loadMap(REVERSE_TO_MAP)
expect(transliterate(code, "xabcy")).toBe("xcbay")
})

it("downcase = any(...) parses as an alias declaration", () => {
const code = loadMap(KEYWORD_ALIAS_MAP)
expect(transliterate(code, "kak")).toBe("KaK")
})
})
11 changes: 11 additions & 0 deletions test/isc/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { describe, it, expect } from "vitest"
import { parseIsc } from "../../src/isc/parser.ts"
import { iscToCompiledMap } from "../../src/isc/converter.ts"
import { IscParseError } from "../../src/isc/types.ts"
import type { IscStageItem } from "../../src/isc/types.ts"

function minimalWrap(body: string): string {
return `system "TEST:test:Latn:Latn:2026" {\n${body}\n}`
Expand Down Expand Up @@ -438,6 +439,16 @@ describe("parseIsc — items", () => {
expect(doc.aliases[0]?.value).toEqual({ type: "string", value: "é" })
})

it("decodes the capital \\UXXXX escape spelling (WO19 G6 item 4)", () => {
// The gki-bel shape: `sub "\U040E" "Ŭ"` — the former hand parser
// carried the from as the literal text "U040E" and the row never
// fired. Both spellings decode now (ruby/rs always did).
const doc = parseIsc(minimalWrap(`stage main { sub "\\U040E" "Ŭ" }`))
const rule = doc.stages[0]!.body[0] as Extract<IscStageItem, { kind: "bare_rule" }>
expect(rule.rule.from).toEqual({ type: "string", value: "Ў" })
expect(rule.rule.to).toEqual({ type: "string", value: "Ŭ" })
})

it("parses none", () => {
const doc = parseIsc(minimalWrap(`aliases { x = none }`))
expect(doc.aliases[0]?.value).toEqual({ type: "none" })
Expand Down
13 changes: 11 additions & 2 deletions test/isc/wf1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,21 @@ describe("parseIsc — when guard suffix (W3)", () => {
expect(rule.rule.when).toEqual(["kur"])
})

it("parses the suffix on a block-form rule", () => {
const doc = parseIsc(minimalWrap(`stage main { sub { from "a" to "x" } when kur }`))
it("parses the guard inside a block-form rule (canonical placement)", () => {
// WO19 G6 item 5: canonical rs binds the when guard inside the
// braces; ruby parses the inside form only. The former ts/py
// outside-form extension (`} when kur`) is retired.
const doc = parseIsc(minimalWrap(`stage main { sub { from "a" to "x" when kur } }`))
const rule = doc.stages[0]!.body[0] as Extract<IscStageItem, { kind: "bare_rule" }>
expect(rule.rule.when).toEqual(["kur"])
})

it("rejects the outside-form when suffix on a block-form rule", () => {
expect(() => parseIsc(minimalWrap(`stage main { sub { from "a" to "x" } when kur }`))).toThrow(
IscParseError,
)
})

it("leaves unguarded rules untouched", () => {
const doc = parseIsc(minimalWrap(`stage main { parallel { sub "a" "x" } }`))
const rules = doc.stages[0]!.body[0] as Extract<IscStageItem, { kind: "parallel" }>
Expand Down
Loading