diff --git a/.gitattributes b/.gitattributes index f6a525d..a6d3fe9 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,3 +1,6 @@ * text=auto eol=lf + +# Polydoc требует пустую строку в конце промежуточных source parts. +root-docs/**/body-*.md whitespace=-blank-at-eof *.bat text eol=crlf *.cmd text eol=crlf diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9feaff4..97a7bf5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -113,33 +113,46 @@ jobs: ini-values: ffi.enable=1, xdebug.mode=off coverage: xdebug - # Provenance (review3 #7, reworked review4): Cargo.lock is - # generated in CI, NOT committed. Generate it, then pin the - # published core with a real TOML parse (shell: bash — the matrix - # includes windows-latest): the ktav [[package]] entry must be - # exactly one, version 0.8.1, from the crates.io registry, with - # the published checksum. A local-path override, an unreleased - # branch or a swapped artifact fails the job here. - - name: Generate Cargo.lock and verify the published ktav 0.8.1 pin + # Provenance (release guards NOT weakened — the target moved): + # Cargo.lock is generated in CI, NOT committed. The ktav core is + # the IMMUTABLE GIT PIN chosen by the user for the 0.9 + # source-edit ABI (rev 447a00bd012e8d3f347c7e1eb59c3f0d9a78332f); + # the TOML parse must show exactly one ktav [[package]] whose + # source is that exact git rev — a registry swap, a local-path + # override, a moved rev or a different branch fails the job here. + - name: Generate Cargo.lock and verify the immutable ktav git-core pin (447a00b, ABI 2) shell: bash run: | set -e cargo generate-lockfile python3 - <<'PY' import sys, tomllib + REV = '447a00bd012e8d3f347c7e1eb59c3f0d9a78332f' with open('Cargo.lock', 'rb') as f: data = tomllib.load(f) pkgs = [p for p in data.get('package', []) if p.get('name') == 'ktav'] if len(pkgs) != 1: sys.exit('expected exactly one ktav [[package]] entry, found %d' % len(pkgs)) p = pkgs[0] - if p.get('version') != '0.8.1': - sys.exit('ktav version %r != 0.8.1' % p.get('version')) - if p.get('source') != 'registry+https://github.com/rust-lang/crates.io-index': - sys.exit('ktav source %r is not the crates.io registry' % p.get('source')) - if p.get('checksum') != '7140dfe2fa6c320e74768cd6a637828c2d26f81746254f4fcd805f3993bd3fd5': - sys.exit('ktav checksum %r mismatch' % p.get('checksum')) - print('ktav 0.8.1 provenance verified:', p['source']) + src = p.get('source', '') + if src != 'git+https://github.com/ktav-lang/rust?rev=' + REV + '#' + REV or p.get('version') != '0.9.0': + sys.exit('ktav source %r is not the ktav-lang/rust git pin' % src) + if REV not in src: + sys.exit('ktav git source %r does not pin the immutable rev %s' % (src, REV)) + if 'registry+' in src or 'path+' in src: + sys.exit('ktav source %r is a registry or local-path dependency' % src) + print('ktav immutable git-core pin verified:', src) + PY + # ABI gate: the pinned core must export the ABI-2 set before + # any PHP native cell runs. + python3 - <<'PY' + import sys, tomllib + with open('Cargo.toml', 'rb') as f: + ws = tomllib.load(f) + ktav = ws['workspace']['dependencies']['ktav'] + if ktav.get('git') != 'https://github.com/ktav-lang/rust' or ktav.get('rev') != '447a00bd012e8d3f347c7e1eb59c3f0d9a78332f': + sys.exit('workspace ktav dependency is not the immutable 447a00b pin: %r' % (ktav,)) + print('workspace ktav dependency pinned:', ktav['rev']) PY - name: Build native library @@ -410,8 +423,8 @@ jobs: KTAV_LIB_PATH: ${{ github.workspace }}/target/debug/libktav_cabi.so run: vendor/bin/kahlan --spec=tests/NativeDebugEmptySpec.php --reporter=verbose - - name: Assert the loaded native reports 0.8.1 + - name: Assert the loaded native serves calls through the ABI-2 gate env: KTAV_LIB_PATH: ${{ github.workspace }}/target/debug/libktav_cabi.so run: | - php -r "require 'vendor/autoload.php'; exit(\\Ktav\\Ktav::nativeVersion() === '0.8.1' ? 0 : 1);" + php -r "require 'vendor/autoload.php'; if (\Ktav\Ktav::nativeVersion() === '') { exit(1); } if (\Ktav\NativeLib::EXPECTED_ABI_VERSION !== 2) { exit(1); } exit(0);" diff --git a/CHANGELOG.md b/CHANGELOG.md index b20548d..5a7f78e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,12 @@ This changelog tracks **binding releases**, not changes to the Ktav format itself — for the latter see [`ktav-lang/spec`](https://github.com/ktav-lang/spec/blob/main/CHANGELOG.md). +## Unreleased — 0.9 preparation + +- Add native and standalone pure editing by textual path or literal segments: source splicing, preserved container forms, minimal readability transitions, comment protection, byte-identical no-op and semantic verification. +- Apply 0.9 source diagnostics and the pinned 412-case conformance corpus, including 72 diagnostic fixtures with raw UTF-8 byte locations and EOF opener spans. +- Native uses the immutable preparing-0.9 core and C ABI 2; pure remains independent of native binaries. Package versions are unchanged; this functionality is not published in 0.8.1. + ## Unreleased ### 0.8.1 diff --git a/Cargo.toml b/Cargo.toml index 77510a3..0b8992c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,8 +10,8 @@ repository = "https://github.com/ktav-lang/php" rust-version = "1.71" [workspace.dependencies] -# Minimum core version; CI verifies the resolved release artifact separately. -ktav = ">=0.8.1, <0.9" +# Подготовка 0.9: immutable core pin с source-edit и ABI 2; пакет ещё не опубликован. +ktav = { git = "https://github.com/ktav-lang/rust", rev = "447a00bd012e8d3f347c7e1eb59c3f0d9a78332f" } serde = { version = "1", features = ["derive"] } serde_json = { version = "1", features = ["preserve_order"] } indexmap = "2" diff --git a/README.md b/README.md index dfa54b1..9df596c 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,8 @@ $text = Ktav::dumps($doc); A complete runnable example lives in [`examples/basic.php`](examples/basic.php). +> **Unreleased (0.9 preparation):** These editing APIs and updated diagnostics are not included in published 0.8.1 packages. Native backends require the pinned 0.9 core (C ABI 2 for C-FFI); pure backends remain standalone. + ## API | Method | Purpose | @@ -142,6 +144,39 @@ Documented differences: - Pure mode needs enabled standard `ext-json`, but no third-party runtime Composer packages, FFI or native binary. +Source editing (spec 0.9). `editValue($source, $path, $replacement)` +and `editValueSegments($source, $segments, $replacement)` replace an +EXISTING value in the source text and return the new text; the input +is never mutated. Only the bytes of the target (and of inline wrappers +the layout rules force to expand) change: comments, key spelling and +order, dotted/inline/block forms, BOM, CR/LF/CRLF and the final-EOL +state survive; a no-op is byte-identical even for noncanonical +spelling. The textual path `key1.key2.0.key4` uses `.` as the +separator, `\.` for a literal dot and `\\` for a literal backslash; +segments are already-decoded literal names. Errors are distinct +envelope codes: `EditInvalidPath` (body = UTF-8 byte offset in the +path), `EditNotFound` (missing key, out-of-bounds index, scalar +descent, empty path) and `EditUnsupported` (noncontiguous +dotted/reopen targets, structural replacement of a comment-carrying +container, a replacement String containing CR or a line `))`). A +source that does not parse raises the ordinary parser envelope. On the +native side these methods require a `ktav_cabi` exporting the +source-edit ABI (version 2); an older binary fails with a clear +version error and is never replaced by a hidden pure fallback. A PHP +empty array as the replacement follows the nested empty-LIST +convention; an EMPTY `stdClass` is the explicit empty Object (`[]` +cannot express it), a NON-empty `stdClass` is a `Message` transport +failure; typed payloads (`['$i' => …]`, `['$f' => …]`) go through +the same strict wire ingest as the native codec — `$i` is canonicalized +(explicit `+` stripped, redundant leading zeros trimmed, signed zero +normalized to `0`, bignums digit-exact), `$f` must be a decimal lexical +form re-emitted shortest round-trip, and any invalid payload is a +`Message` transport failure, never a coerced scalar. Integer identity +is exact digit-string equality and Float identity is exact f64 equality +(the sign of zero is preserved, never a `precision`-ini string cast); +Object equality IGNORES key order (Rust IndexMap PartialEq), arrays +stay ordered. + ### Formatting `Ktav::format()` takes Ktav **source text** and returns Ktav source diff --git a/docs/ru/CHANGELOG.ru.md b/docs/ru/CHANGELOG.ru.md index 43ed650..4053ff1 100644 --- a/docs/ru/CHANGELOG.ru.md +++ b/docs/ru/CHANGELOG.ru.md @@ -11,6 +11,12 @@ MINOR — ломающий. формата Ktav — для последнего см. [`ktav-lang/spec`](https://github.com/ktav-lang/spec/blob/main/CHANGELOG.md). +## Ещё не выпущено — подготовка 0.9 + +- Добавлено редактирование по текстовому пути или буквальным сегментам в native- и самостоятельном pure-backend: source splice, сохранение форм контейнеров, минимальные переходы читаемости, защита комментариев, byte-identical no-op и семантическая проверка. +- Применена диагностика исходника 0.9 и закреплён corpus из 412 фикстур, включая 72 диагностические фикстуры с позициями сырых байтов UTF-8 и EOF spans от opener. +- Native использует immutable ядро подготовки 0.9 и C ABI 2; pure не требует native-бинарников. Версии пакетов не менялись; функционал не опубликован в 0.8.1. + ## Unreleased ### 0.8.1 diff --git a/docs/ru/README.ru.md b/docs/ru/README.ru.md index 07d5447..352f735 100644 --- a/docs/ru/README.ru.md +++ b/docs/ru/README.ru.md @@ -92,6 +92,8 @@ $text = Ktav::dumps($doc); Полный запускаемый пример — в [`examples/basic.php`](../../examples/basic.php). +> **Ещё не выпущено (подготовка 0.9):** Эти API редактирования и обновлённая диагностика не входят в опубликованные пакеты 0.8.1. Native-backend требует закреплённое ядро 0.9 (C ABI 2 для C-FFI); pure-backend остаётся самостоятельным. + ## API | Метод | Назначение | @@ -143,6 +145,40 @@ Object, последовательные массивы — Array. Для бол - Чистому режиму нужно включённое стандартное `ext-json`, но не сторонние runtime-зависимости Composer, FFI или нативный бинарник. +Правка исходника (spec 0.9). `editValue($source, $path, $replacement)` +и `editValueSegments($source, $segments, $replacement)` заменяют +СУЩЕСТВУЮЩЕЕ значение в тексте исходника и возвращают новую строку; +вход не мутируется. Меняются только байты цели (и inline-обёрток, +которые требует раскрыть правило компоновки): комментарии, написание +и порядок ключей, формы dotted/inline/block, BOM, CR/LF/CRLF и наличие +финального EOL сохраняются; no-op байт-в-байт идентичен исходнику, +даже при неканоничном написании. Текстовый путь `key1.key2.0.key4` +использует `.` как разделитель, `\.` для literal точки и `\\` для +literal обратного слеша; сегменты — уже декодированные literal имена. +Ошибки — отдельные коды конверта: `EditInvalidPath` (body = UTF-8 +смещение в пути), `EditNotFound` (нет ключа, индекс вне диапазона, +спуск через скаляр, пустой путь) и `EditUnsupported` +(неконтiguous dotted/reopen-цели, структурная замена контейнера с +комментариями, строка замены с CR или строкой `))`). Исходник с +ошибкой разбора даёт обычный конверт парсера. На нативной стороне эти +методы требуют `ktav_cabi` с ABI правки исходника (версия 2); старый +бинарник отклоняется с понятной ошибкой версии и никогда не +подменяется скрытым pure-fallback. Пустой PHP-массив в качестве +замены следует соглашению вложенного пустого LIST; ПУСТОЙ `stdClass` — +это явный пустой Object (`[]` его не выражает), НЕпустой `stdClass` — +транспортный сбой `Message`; типизированные +полезные нагрузки (`['$i' => …]`, `['$f' => …]`) проходят тот же +строгий wire-ingest, что и нативный кодек — `$i` канонизируется +(явный `+` снимается, лишние ведущие нули срезаются, знаковый нуль +нормализуется к `0`, бигнумы точны по цифрам), `$f` обязана быть +десятичной лексической формой и переиспускается в кратчайшей форме, +любой невалидный payload — транспортный сбой `Message`, а не +принудительно приведённый скаляр. Идентичность Integer — точное +сравнение строк цифр, Float — точное сравнение f64 (знак нуля +сохраняется, без строкового каста через `precision`); равенство +Object ИГНОРИРУЕТ порядок ключей (Rust IndexMap PartialEq), массивы +остаются упорядоченными. + ### Форматирование `Ktav::format()` принимает Ktav-**исходник** и возвращает Ktav-исходник. diff --git a/docs/zh/CHANGELOG.zh.md b/docs/zh/CHANGELOG.zh.md index 074f092..7da1e86 100644 --- a/docs/zh/CHANGELOG.zh.md +++ b/docs/zh/CHANGELOG.zh.md @@ -11,6 +11,12 @@ MINOR 递增即破坏性变更。 后者见 [`ktav-lang/spec`](https://github.com/ktav-lang/spec/blob/main/CHANGELOG.md)。 +## 尚未发布 — 0.9 准备版本 + +- 新增原生及独立纯实现的路径编辑,支持点分文本或字面路径段:源文本拼接、保留容器形式、最小可读性展开、注释保护、逐字节 no-op 及语义验证。 +- 应用 0.9 源诊断及固定的 412 个一致性 fixture,包括 72 个诊断 fixture,检查原始 UTF-8 字节位置及从起始标记到 EOF 的区间。 +- 原生后端使用不可变的 0.9 准备核心及 C ABI 2;纯实现不需要原生二进制。包版本不变,这些功能尚未包含在已发布的 0.8.1 中。 + ## Unreleased ### 0.8.1 diff --git a/docs/zh/README.zh.md b/docs/zh/README.zh.md index a764c79..a9bcbf6 100644 --- a/docs/zh/README.zh.md +++ b/docs/zh/README.zh.md @@ -92,6 +92,8 @@ $text = Ktav::dumps($doc); 完整可运行示例见 [`examples/basic.php`](../../examples/basic.php)。 +> **尚未发布(0.9 准备版本):** 这些编辑 API 和更新后的诊断不包含在已发布的 0.8.1 包中。原生后端需要固定的 0.9 核心(C-FFI 使用 C ABI 2);纯实现仍完全独立。 + ## API | 方法 | 用途 | @@ -139,6 +141,29 @@ FFI,选择原生模式也不会执行 PHP 解析。 - 纯模式需要启用标准 `ext-json`,但无需第三方 Composer 运行时依赖、 FFI 或原生二进制。 +源码编辑(spec 0.9)。`editValue($source, $path, $replacement)` 和 +`editValueSegments($source, $segments, $replacement)` 会替换源文本中的 +已有值并返回新文本;输入不会被修改。只有目标字节(以及布局规则要求 +展开的 inline 包装)会改变:注释、键的拼写与顺序、dotted/inline/block +形式、BOM、CR/LF/CRLF 和末尾 EOL 状态都会保留;即使源码拼写非规范, +no-op 结果也与源码逐字节相同。文本路径 `key1.key2.0.key4` 以 `.` 为 +分隔符,`\.` 表示字面点号,`\\` 表示字面反斜杠;segments 是已解码的 +字面名称。错误是不同的信封代码:`EditInvalidPath`(body = 路径中的 +UTF-8 字节偏移)、`EditNotFound`(键缺失、索引越界、穿过标量、空 +路径)和 `EditUnsupported`(dotted/reopen 的非连续目标、对含注释 +容器的结构性替换、包含 CR 或行 `))` 的替换字符串)。源码解析失败会 +抛出普通的解析器信封。原生侧这些方法需要导出源码编辑 ABI(版本 2) +的 `ktav_cabi`;旧二进制会以明确的版本错误被拒绝,绝不会静默退化为 +纯 PHP 后备。空 PHP 数组作为替换值遵循嵌套空 LIST 约定;空的 `stdClass` 表示显式的空 +Object(`[]` 无法表达),非空 `stdClass` 是 `Message` 传输失败;类型化载荷 +(`['$i' => …]`、`['$f' => …]`)经过与原生编解码器相同的严格 wire-ingest +—— `$i` 会规范化(去掉显式 `+`、裁剪多余的先导零、带符号零归一化为 +`0`、大数按位精确),`$f` 必须是十进制词法形式并按最短往返形式重新 +输出;任何无效载荷都是 `Message` 传输失败,绝不会被强转成标量。 +Integer 身份是精确的数字串比较,Float 身份是精确的 f64 比较(保留零 +的符号,不经过 `precision` 的字符串转换);Object 相等会忽略键顺序 +(Rust IndexMap PartialEq),数组保持有序。 + ### 格式化 `Ktav::format()` 接受 Ktav **源文本**,并返回 Ktav 源文本。 diff --git a/root-docs/CHANGELOG/manifest.js b/root-docs/CHANGELOG/manifest.js index 4d42f07..2a292b1 100644 --- a/root-docs/CHANGELOG/manifest.js +++ b/root-docs/CHANGELOG/manifest.js @@ -1,5 +1,6 @@ export default [ "preamble", + "source-edit09", "unreleased", "v0-8-0", "v0-6-4", diff --git a/root-docs/CHANGELOG/source-edit09/body-1.md b/root-docs/CHANGELOG/source-edit09/body-1.md new file mode 100644 index 0000000..e01b9cf --- /dev/null +++ b/root-docs/CHANGELOG/source-edit09/body-1.md @@ -0,0 +1,21 @@ +>>>>> lang=en +## Unreleased — 0.9 preparation + +- Add native and standalone pure editing by textual path or literal segments: source splicing, preserved container forms, minimal readability transitions, comment protection, byte-identical no-op and semantic verification. +- Apply 0.9 source diagnostics and the pinned 412-case conformance corpus, including 72 diagnostic fixtures with raw UTF-8 byte locations and EOF opener spans. +- Native uses the immutable preparing-0.9 core and C ABI 2; pure remains independent of native binaries. Package versions are unchanged; this functionality is not published in 0.8.1. + +>>>>> lang=ru +## Ещё не выпущено — подготовка 0.9 + +- Добавлено редактирование по текстовому пути или буквальным сегментам в native- и самостоятельном pure-backend: source splice, сохранение форм контейнеров, минимальные переходы читаемости, защита комментариев, byte-identical no-op и семантическая проверка. +- Применена диагностика исходника 0.9 и закреплён corpus из 412 фикстур, включая 72 диагностические фикстуры с позициями сырых байтов UTF-8 и EOF spans от opener. +- Native использует immutable ядро подготовки 0.9 и C ABI 2; pure не требует native-бинарников. Версии пакетов не менялись; функционал не опубликован в 0.8.1. + +>>>>> lang=zh +## 尚未发布 — 0.9 准备版本 + +- 新增原生及独立纯实现的路径编辑,支持点分文本或字面路径段:源文本拼接、保留容器形式、最小可读性展开、注释保护、逐字节 no-op 及语义验证。 +- 应用 0.9 源诊断及固定的 412 个一致性 fixture,包括 72 个诊断 fixture,检查原始 UTF-8 字节位置及从起始标记到 EOF 的区间。 +- 原生后端使用不可变的 0.9 准备核心及 C ABI 2;纯实现不需要原生二进制。包版本不变,这些功能尚未包含在已发布的 0.8.1 中。 + diff --git a/root-docs/CHANGELOG/source-edit09/meta.js b/root-docs/CHANGELOG/source-edit09/meta.js new file mode 100644 index 0000000..eb86416 --- /dev/null +++ b/root-docs/CHANGELOG/source-edit09/meta.js @@ -0,0 +1,7 @@ +export default { + "kind": "frontmatter", + "number": null, + "level": null, + "title": null, + "bodyParts": 1 +} diff --git a/root-docs/README/api/body-3.md b/root-docs/README/api/body-3.md new file mode 100644 index 0000000..216ace0 --- /dev/null +++ b/root-docs/README/api/body-3.md @@ -0,0 +1,93 @@ +>>>>> lang=en +Source editing (spec 0.9). `editValue($source, $path, $replacement)` +and `editValueSegments($source, $segments, $replacement)` replace an +EXISTING value in the source text and return the new text; the input +is never mutated. Only the bytes of the target (and of inline wrappers +the layout rules force to expand) change: comments, key spelling and +order, dotted/inline/block forms, BOM, CR/LF/CRLF and the final-EOL +state survive; a no-op is byte-identical even for noncanonical +spelling. The textual path `key1.key2.0.key4` uses `.` as the +separator, `\.` for a literal dot and `\\` for a literal backslash; +segments are already-decoded literal names. Errors are distinct +envelope codes: `EditInvalidPath` (body = UTF-8 byte offset in the +path), `EditNotFound` (missing key, out-of-bounds index, scalar +descent, empty path) and `EditUnsupported` (noncontiguous +dotted/reopen targets, structural replacement of a comment-carrying +container, a replacement String containing CR or a line `))`). A +source that does not parse raises the ordinary parser envelope. On the +native side these methods require a `ktav_cabi` exporting the +source-edit ABI (version 2); an older binary fails with a clear +version error and is never replaced by a hidden pure fallback. A PHP +empty array as the replacement follows the nested empty-LIST +convention; an EMPTY `stdClass` is the explicit empty Object (`[]` +cannot express it), a NON-empty `stdClass` is a `Message` transport +failure; typed payloads (`['$i' => …]`, `['$f' => …]`) go through +the same strict wire ingest as the native codec — `$i` is canonicalized +(explicit `+` stripped, redundant leading zeros trimmed, signed zero +normalized to `0`, bignums digit-exact), `$f` must be a decimal lexical +form re-emitted shortest round-trip, and any invalid payload is a +`Message` transport failure, never a coerced scalar. Integer identity +is exact digit-string equality and Float identity is exact f64 equality +(the sign of zero is preserved, never a `precision`-ini string cast); +Object equality IGNORES key order (Rust IndexMap PartialEq), arrays +stay ordered. + +>>>>> lang=ru +Правка исходника (spec 0.9). `editValue($source, $path, $replacement)` +и `editValueSegments($source, $segments, $replacement)` заменяют +СУЩЕСТВУЮЩЕЕ значение в тексте исходника и возвращают новую строку; +вход не мутируется. Меняются только байты цели (и inline-обёрток, +которые требует раскрыть правило компоновки): комментарии, написание +и порядок ключей, формы dotted/inline/block, BOM, CR/LF/CRLF и наличие +финального EOL сохраняются; no-op байт-в-байт идентичен исходнику, +даже при неканоничном написании. Текстовый путь `key1.key2.0.key4` +использует `.` как разделитель, `\.` для literal точки и `\\` для +literal обратного слеша; сегменты — уже декодированные literal имена. +Ошибки — отдельные коды конверта: `EditInvalidPath` (body = UTF-8 +смещение в пути), `EditNotFound` (нет ключа, индекс вне диапазона, +спуск через скаляр, пустой путь) и `EditUnsupported` +(неконтiguous dotted/reopen-цели, структурная замена контейнера с +комментариями, строка замены с CR или строкой `))`). Исходник с +ошибкой разбора даёт обычный конверт парсера. На нативной стороне эти +методы требуют `ktav_cabi` с ABI правки исходника (версия 2); старый +бинарник отклоняется с понятной ошибкой версии и никогда не +подменяется скрытым pure-fallback. Пустой PHP-массив в качестве +замены следует соглашению вложенного пустого LIST; ПУСТОЙ `stdClass` — +это явный пустой Object (`[]` его не выражает), НЕпустой `stdClass` — +транспортный сбой `Message`; типизированные +полезные нагрузки (`['$i' => …]`, `['$f' => …]`) проходят тот же +строгий wire-ingest, что и нативный кодек — `$i` канонизируется +(явный `+` снимается, лишние ведущие нули срезаются, знаковый нуль +нормализуется к `0`, бигнумы точны по цифрам), `$f` обязана быть +десятичной лексической формой и переиспускается в кратчайшей форме, +любой невалидный payload — транспортный сбой `Message`, а не +принудительно приведённый скаляр. Идентичность Integer — точное +сравнение строк цифр, Float — точное сравнение f64 (знак нуля +сохраняется, без строкового каста через `precision`); равенство +Object ИГНОРИРУЕТ порядок ключей (Rust IndexMap PartialEq), массивы +остаются упорядоченными. + +>>>>> lang=zh +源码编辑(spec 0.9)。`editValue($source, $path, $replacement)` 和 +`editValueSegments($source, $segments, $replacement)` 会替换源文本中的 +已有值并返回新文本;输入不会被修改。只有目标字节(以及布局规则要求 +展开的 inline 包装)会改变:注释、键的拼写与顺序、dotted/inline/block +形式、BOM、CR/LF/CRLF 和末尾 EOL 状态都会保留;即使源码拼写非规范, +no-op 结果也与源码逐字节相同。文本路径 `key1.key2.0.key4` 以 `.` 为 +分隔符,`\.` 表示字面点号,`\\` 表示字面反斜杠;segments 是已解码的 +字面名称。错误是不同的信封代码:`EditInvalidPath`(body = 路径中的 +UTF-8 字节偏移)、`EditNotFound`(键缺失、索引越界、穿过标量、空 +路径)和 `EditUnsupported`(dotted/reopen 的非连续目标、对含注释 +容器的结构性替换、包含 CR 或行 `))` 的替换字符串)。源码解析失败会 +抛出普通的解析器信封。原生侧这些方法需要导出源码编辑 ABI(版本 2) +的 `ktav_cabi`;旧二进制会以明确的版本错误被拒绝,绝不会静默退化为 +纯 PHP 后备。空 PHP 数组作为替换值遵循嵌套空 LIST 约定;空的 `stdClass` 表示显式的空 +Object(`[]` 无法表达),非空 `stdClass` 是 `Message` 传输失败;类型化载荷 +(`['$i' => …]`、`['$f' => …]`)经过与原生编解码器相同的严格 wire-ingest +—— `$i` 会规范化(去掉显式 `+`、裁剪多余的先导零、带符号零归一化为 +`0`、大数按位精确),`$f` 必须是十进制词法形式并按最短往返形式重新 +输出;任何无效载荷都是 `Message` 传输失败,绝不会被强转成标量。 +Integer 身份是精确的数字串比较,Float 身份是精确的 f64 比较(保留零 +的符号,不经过 `precision` 的字符串转换);Object 相等会忽略键顺序 +(Rust IndexMap PartialEq),数组保持有序。 + diff --git a/root-docs/README/api/meta.js b/root-docs/README/api/meta.js index 7e0a98b..6f4ff20 100644 --- a/root-docs/README/api/meta.js +++ b/root-docs/README/api/meta.js @@ -3,5 +3,5 @@ export default { "number": null, "level": null, "title": null, - "bodyParts": 2 + "bodyParts": 3 } diff --git a/root-docs/README/manifest.js b/root-docs/README/manifest.js index 0137363..097cfed 100644 --- a/root-docs/README/manifest.js +++ b/root-docs/README/manifest.js @@ -2,6 +2,7 @@ export default [ "title", "install", "quick-start", + "source-edit-status", "api", "formatting", "errors", diff --git a/root-docs/README/source-edit-status/body-1.md b/root-docs/README/source-edit-status/body-1.md new file mode 100644 index 0000000..00dd212 --- /dev/null +++ b/root-docs/README/source-edit-status/body-1.md @@ -0,0 +1,9 @@ +>>>>> lang=en +> **Unreleased (0.9 preparation):** These editing APIs and updated diagnostics are not included in published 0.8.1 packages. Native backends require the pinned 0.9 core (C ABI 2 for C-FFI); pure backends remain standalone. + +>>>>> lang=ru +> **Ещё не выпущено (подготовка 0.9):** Эти API редактирования и обновлённая диагностика не входят в опубликованные пакеты 0.8.1. Native-backend требует закреплённое ядро 0.9 (C ABI 2 для C-FFI); pure-backend остаётся самостоятельным. + +>>>>> lang=zh +> **尚未发布(0.9 准备版本):** 这些编辑 API 和更新后的诊断不包含在已发布的 0.8.1 包中。原生后端需要固定的 0.9 核心(C-FFI 使用 C ABI 2);纯实现仍完全独立。 + diff --git a/root-docs/README/source-edit-status/meta.js b/root-docs/README/source-edit-status/meta.js new file mode 100644 index 0000000..eb86416 --- /dev/null +++ b/root-docs/README/source-edit-status/meta.js @@ -0,0 +1,7 @@ +export default { + "kind": "frontmatter", + "number": null, + "level": null, + "title": null, + "bodyParts": 1 +} diff --git a/spec b/spec index 3889821..a452756 160000 --- a/spec +++ b/spec @@ -1 +1 @@ -Subproject commit 388982118c0d8f767bf283356ac9c9bb50fdc3f5 +Subproject commit a45275688557dc9ecc24a82afb491dd46e615d4d diff --git a/src/Ktav.php b/src/Ktav.php index dbf8953..07e6573 100644 --- a/src/Ktav.php +++ b/src/Ktav.php @@ -140,8 +140,88 @@ public static function canonicalFromSource(string $src): string } /** - * Version string reported by the loaded `ktav_cabi`. Useful for - * sanity checks against {@see ExpectedNativeVersion}. + * Replace an EXISTING value in Ktav SOURCE TEXT at a textual path + * (spec 0.9 source edit). Only the bytes of the target (and of the + * inline wrappers the layout criterion forces to expand) change — + * never parse-mutate-serialize of the whole document: comments, + * key spelling/order, dotted/inline/block forms, BOM, CR/LF/CRLF + * and EOF bytes outside the edit are preserved. Byte-identical + * no-op when the replacement equals the current value. + * + * Path: `key1.key2.0.key4`, separator '.', literal dot `\.`, + * literal backslash `\\`; other escapes, dangling escapes and + * empty segments are EditInvalidPath (body = UTF-8 byte offset). + * Numeric segments are keys in Objects and zero-based ASCII-digit + * indexes (leading zeros allowed, no signs) in Arrays; missing + * key / overflow / scalar descent / empty path is EditNotFound. + * + * $replacement is a PHP value; int/float/string/bool/null keep + * their type (arbitrary integers pass `['$i' => '']`, + * exact float lexemes `['$f' => '']`). Errors: + * EditInvalidPath / EditNotFound / EditUnsupported envelopes; the + * SOURCE itself failing to parse raises the ordinary 0.9 parser + * envelope. Requires a ktav_cabi that exports the source-edit + * symbols — an older binary raises a clear unsupported error, no + * pure fallback. + * + * @param mixed $replacement + * @throws KtavException + */ + public static function editValue(string $source, string $path, $replacement): string + { + return NativeLib::callEditBytes( + 'ktav_edit_value', + $source, + $path, + WireJson::encodeEditReplacement($replacement) + ); + } + + /** + * {@see editValue} with pre-decoded literal segments (no extra + * unescaping — a segment containing '.' is that literal key). + * + * @param string[] $segments + * @param mixed $replacement + * @throws KtavException + */ + public static function editValueSegments(string $source, array $segments, $replacement): string + { + // The segments transport is a JSON array of literal strings. + // A json_encode failure is surfaced as a typed transport error + // BEFORE any native call — the cause is never swallowed by a + // silent (string) cast to empty path bytes, and no partial + // edit can happen. + $pathJson = json_encode(array_values($segments), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); + if ($pathJson === false) { + throw KtavException::fromEnvelope([ + 'error' => 'Message', + 'reason' => null, + 'line' => null, + 'line_text' => null, + 'span' => null, + 'path' => null, + 'canonical' => null, + 'spec_section' => null, + 'body' => null, + 'message' => 'edit segments transport is not JSON-encodable: ' + . json_last_error_msg(), + ]); + } + return NativeLib::callEditBytes( + 'ktav_edit_value_segments', + $source, + $pathJson, + WireJson::encodeEditReplacement($replacement) + ); + } + + /** + * Version string reported by the loaded `ktav_cabi`. With the + * immutable git-core pin the string is informational; the + * AUTHORITATIVE compatibility gate is the C ABI version (see + * NativeLib::EXPECTED_ABI_VERSION — every native call is served + * only by an ABI-2 binary). */ public static function nativeVersion(): string { @@ -149,9 +229,10 @@ public static function nativeVersion(): string } /** - * Native library version this build was compiled against. If - * {@see nativeVersion()} differs at runtime, the loaded binary - * was not the one we expected. + * Release-tag version this build's release artifacts are cut from + * (download cache path / release URL). NOT a runtime gate: since + * the ABI-2 git-core pin, compatibility is enforced by the C ABI + * version check at load time, not by this string. */ public const ExpectedNativeVersion = NativeLib::LIB_VERSION; diff --git a/src/NativeLib.php b/src/NativeLib.php index a8e527e..fbb9340 100644 --- a/src/NativeLib.php +++ b/src/NativeLib.php @@ -58,6 +58,7 @@ final class NativeLib void ktav_free(uint8_t *ptr, size_t len); const char *ktav_version(); + int ktav_abi_version(); C; private static ?\FFI $ffi = null; @@ -82,19 +83,199 @@ private static function ffi(): \FFI $e, ); } - // Provenance check: an override (KTAV_LIB_PATH / setLibraryPath) - // or a stale cache entry may carry an OLD binary; a LIB_VERSION - // bump alone would not reject it. Verify the loaded library - // reports the expected version before it serves any call. - $actual = self::versionFromFfi(); - if ($actual !== self::LIB_VERSION) { - self::$ffi = null; + // Provenance: a stale override / cache entry may carry an OLD + // binary. Since the immutable git-core pin (447a00b) the + // version STRING is no longer statically knowable, so the + // AUTHORITATIVE load gate is the C ABI version: an ABI-1 (or + // any non-2) binary is rejected for EVERY native operation. + // The exact 447a00b rev is provenance-checked in CI against + // Cargo.lock instead of here. + self::assertAbiVersion((int) self::$ffi->ktav_abi_version(), $path); + return self::$ffi; + } + + /** + * Shared ABI gate: since the user-approved ABI 1 -> 2 bump, EVERY + * native call (original nine AND source edit) is served only by a + * binary reporting ktav_abi_version() == 2; an ABI-1 binary is + * rejected at load time with a clear version error — never a + * silent pure fallback. Static so the rejection mechanism itself + * is testable without an ABI-1 artifact on disk. + */ + public static function assertAbiVersion(int $actual, string $path = 'ktav_cabi'): void + { + if ($actual !== self::EXPECTED_ABI_VERSION) { throw new KtavException( - 'ktav native library version mismatch in ' . $path - . ': expected ' . self::LIB_VERSION . ', got ' . $actual + 'ktav_cabi ABI version mismatch in ' . $path + . ': expected ' . self::EXPECTED_ABI_VERSION . ', got ' . $actual + . ' — the loaded binary predates the user-approved ABI-2' + . ' source-edit set; pin the matching native library' ); } - return self::$ffi; + } + + /** + * Source-edit C ABI (0.9): two extra exports sharing the standard + * return-0/1 + output-buffer + ktav_free pattern. Declared in a + * SEPARATE \FFI instance ONLY so that symbol resolution of the + * edit exports happens at a known point with a precise error; the + * ABI gate itself is SHARED (see ffi/assertAbiVersion) — an ABI-1 + * binary is rejected for every native call, never a silent pure + * fallback. + */ + private const EDIT_CDEF = <<<'C' + int ktav_edit_value( + const uint8_t *src, size_t src_len, + const uint8_t *path, size_t path_len, + const uint8_t *replacement, size_t replacement_len, + uint8_t **out_buf, size_t *out_len, + char **out_err, size_t *out_err_len); + + int ktav_edit_value_segments( + const uint8_t *src, size_t src_len, + const uint8_t *path, size_t path_len, + const uint8_t *replacement, size_t replacement_len, + uint8_t **out_buf, size_t *out_len, + char **out_err, size_t *out_err_len); + + int ktav_abi_version(); + + void ktav_free(uint8_t *ptr, size_t len); + C; + + private static ?\FFI $editFfi = null; + + /** + * Expected C ABI version. Bumped 1 -> 2 by explicit user approval + * (2026-10-25): EVERY native operation rejects an ABI-1 binary at + * load time (see {@see assertAbiVersion}), not only the edit set. + */ + public const EXPECTED_ABI_VERSION = 2; + + /** + * FFI handle for the edit symbols. Symbol resolution happens at + * cdef() time, so a library without the edit exports fails HERE + * with a typed, actionable message; the ABI gate is the shared + * {@see assertAbiVersion} (same for all native calls). + */ + private static function editFfi(): \FFI + { + if (self::$editFfi !== null) { + return self::$editFfi; + } + if (!extension_loaded('ffi')) { + throw new KtavException( + 'PHP FFI extension is required (php.ini: ffi.enable=1).' + ); + } + $path = NativeLoader::resolve(); + try { + self::$editFfi = \FFI::cdef(self::EDIT_CDEF, $path); + } catch (\FFI\Exception $e) { + throw new KtavException( + 'ktav_edit_value / ktav_edit_value_segments are not available in ' . $path + . ' — the loaded ktav_cabi does not export the ABI-' + . self::EXPECTED_ABI_VERSION . ' source-edit set.' + . ' Pin the native library that exports these symbols; no pure fallback is performed.' + . ' Detail: ' . $e->getMessage(), + 0, + $e, + ); + } + self::assertAbiVersion((int) self::$editFfi->ktav_abi_version(), $path); + return self::$editFfi; + } + + /** + * Call `ktav_edit_value` / `ktav_edit_value_segments`: three UTF-8 + * byte inputs (source; textual path OR a JSON array of literal + * string segments; replacement on the tagged-numeric JSON wire), + * Ktav-source bytes out. Errors are the structured envelope with + * error = EditInvalidPath / EditNotFound / EditUnsupported. + * + * @throws KtavException carrying the structured envelope + */ + public static function callEditBytes(string $fn, string $src, string $path, string $replacement): string + { + $ffi = self::editFfi(); + $lens = [strlen($src), strlen($path), strlen($replacement)]; + $inputs = [$src, $path, $replacement]; + + // One live byte per input (see callBytes for the NULL-with-len-0 + // rationale); the pad byte is never read. + $buffers = []; + foreach ($inputs as $k => $input) { + $alloc = max(1, $lens[$k]); + $buf = $ffi->new("uint8_t[$alloc]", false); + if ($lens[$k] > 0) { + \FFI::memcpy($buf, $input, $lens[$k]); + } + $buffers[] = $buf; + } + + $outBuf = $ffi->new('uint8_t*'); + $outLen = $ffi->new('size_t'); + $outErr = $ffi->new('char*'); + $outErrLen = $ffi->new('size_t'); + + try { + $rc = $ffi->{$fn}( + $buffers[0], $lens[0], + $buffers[1], $lens[1], + $buffers[2], $lens[2], + \FFI::addr($outBuf), + \FFI::addr($outLen), + \FFI::addr($outErr), + \FFI::addr($outErrLen), + ); + if ($rc !== 0) { + // Free through the SAME FFI instance that produced the + // pointers: CData types are per-instance, and the base + // instance's ktav_free(uint8_t*) rejects a char* from + // this one (CI: "Passing incompatible argument 1 of + // C function 'ktav_free'"). char* is cast to uint8_t* + // — same pointer, FFI-visible type only. + $payload = self::copyAndFreeEdit($ffi, $outErr, (int) $outErrLen->cdata); + self::freeIfPresentEdit($ffi, $outBuf, (int) $outLen->cdata); + $fields = $payload !== '' ? json_decode($payload, true) : null; + if (is_array($fields) && isset($fields['error']) && is_string($fields['error'])) { + throw KtavException::fromEnvelope($fields); + } + throw new KtavException($payload !== '' ? $payload : 'native call failed with code ' . $rc); + } + self::freeIfPresentEdit($ffi, $outErr, (int) $outErrLen->cdata); + return self::copyAndFreeEdit($ffi, $outBuf, (int) $outLen->cdata); + } finally { + foreach ($buffers as $buf) { + if ($buf !== null) { + \FFI::free($buf); + } + } + } + } + + /** + * copyAndFree counterpart bound to the EDIT FFI instance (the + * output pointers come from its cdef). $ptr may be a char* (the + * contract's out_err) — cast to the declared uint8_t*. + */ + private static function copyAndFreeEdit(\FFI $ffi, $ptr, int $len): string + { + if ($len <= 0 || $ptr === null) { + return ''; + } + $bytes = \FFI::string($ptr, $len); + $ffi->ktav_free($ffi->cast('uint8_t*', $ptr), $len); + return $bytes !== false ? $bytes : ''; + } + + /** freeIfPresent counterpart bound to the EDIT FFI instance. */ + private static function freeIfPresentEdit(\FFI $ffi, $ptr, int $len): void + { + if ($len <= 0 || $ptr === null) { + return; + } + $ffi->ktav_free($ffi->cast('uint8_t*', $ptr), $len); } /** diff --git a/src/Pure/Edit/EditEnvelope.php b/src/Pure/Edit/EditEnvelope.php new file mode 100644 index 0000000..70d4895 --- /dev/null +++ b/src/Pure/Edit/EditEnvelope.php @@ -0,0 +1,370 @@ + $code, + 'reason' => null, + 'line' => null, + 'line_text' => null, + 'span' => null, + 'path' => null, + 'body' => $body, + 'canonical' => null, + 'spec_section' => null, + 'message' => $message, + ]); + } + + // ----------------------------------------------------------------- + // Comparable value model (cval) + // ----------------------------------------------------------------- + + /** + * Convert a public PHP value (as produced by the pure parser's + * toPublic, bigints as digit strings) into the order-preserving + * cval form: + * scalar -> ['t'=>'null'|'bool'|'int'|'float'|'string', ...] + * object -> ['t'=>'object', 'pairs'=>[[key, cval], ...]] + * array -> ['t'=>'array', 'items'=>[cval, ...]] + * + * @param mixed $v + * @return mixed + */ + public static function toCval($v) + { + if ($v instanceof PureObject) { + $pairs = []; + foreach ($v->pairs as $k => $child) { + $pairs[] = [(string) $k, self::toCval($child)]; + } + return ['t' => 'object', 'pairs' => $pairs]; + } + if ($v instanceof PureList) { + $items = []; + foreach ($v->items as $child) { + $items[] = self::toCval($child); + } + return ['t' => 'array', 'items' => $items]; + } + if ($v instanceof PureBigInt) { + return ['t' => 'int', 'text' => $v->digits]; + } + if (is_int($v)) { + return ['t' => 'int', 'text' => (string) $v]; + } + if (is_float($v)) { + return ['t' => 'float', 'v' => $v]; + } + if (is_bool($v)) { + return ['t' => 'bool', 'v' => $v]; + } + if ($v === null) { + return ['t' => 'null']; + } + return ['t' => 'string', 'v' => (string) $v]; + } + + /** Structural equality of two cvals (int and float never equal). */ + public static function cvalEqual($a, $b): bool + { + if (!is_array($a) || !is_array($b)) { + return $a === $b; + } + if (($a['t'] ?? null) !== ($b['t'] ?? null)) { + return false; + } + switch ($a['t']) { + case 'object': + // Rust IndexMap PartialEq IGNORES pair order — Object + // equality is key-map equality. Arrays stay ordered. + $pb = $b['pairs']; + if (count($a['pairs']) !== count($pb)) { + return false; + } + foreach ($a['pairs'] as $pair) { + $matched = false; + foreach ($pb as $other) { + if ($other[0] === $pair[0]) { + if (!self::cvalEqual($pair[1], $other[1])) { + return false; + } + $matched = true; + break; + } + } + if (!$matched) { + return false; + } + } + return true; + case 'array': + $ia = $a['items']; + $ib = $b['items']; + if (count($ia) !== count($ib)) { + return false; + } + foreach ($ia as $i => $item) { + if (!self::cvalEqual($item, $ib[$i])) { + return false; + } + } + return true; + case 'int': + return PureScanner::digitCmp(ltrim($a['text'], '+'), ltrim($b['text'], '+')) === 0 + && (strpos($a['text'], '-') === 0) === (strpos($b['text'], '-') === 0); + case 'float': + // Exact f64 identity via shortest round-trip digits + // (injective on f64, -0.0 keeps its sign) — never the + // lossy `precision`-governed (string) cast. Wire-only + // non-finite payloads compare by verbatim text. + $fa = (float) $a['v']; + $fb = (float) $b['v']; + if (!is_finite($fa) || !is_finite($fb)) { + return isset($a['text'], $b['text']) + && $a['text'] === $b['text']; + } + return PureFloat::shortestDigits($fa) + === PureFloat::shortestDigits($fb); + default: + return $a === $b; + } + } + + // ----------------------------------------------------------------- + // § 3.7 inline scalar encoding / decoding + // ----------------------------------------------------------------- + + /** + * Encode a String body for an inline context: every byte that + * would break the inline split/decode (`\\` `\,` `\}` `\]` `\{` + * `\[`) and LF/CR are escaped; edge § 3.3 whitespace becomes + * `\uXXXX` (physical edges are trimmed on re-parse). With + * $forceString the FIRST code point is emitted as `\uXXXX` so the + * body re-parses as a String, not as a number/keyword. + */ + public static function encodeInline(string $s, bool $forceString): string + { + $out = ''; + $n = strlen($s); + $i = 0; + $first = true; + while ($i < $n) { + list($cp, $len) = PureText::cpAt($s, $i); + $last = $i + $len >= $n; + if ($forceString && $first) { + $out .= self::uEscape($cp); + } else { + $out .= self::encodeInlineCp($cp, $s, $i, $len, $first, $last); + } + $first = false; + $i += $len; + } + return $out; + } + + /** @return string */ + private static function encodeInlineCp(int $cp, string $s, int $i, int $len, bool $first, bool $last): string + { + switch ($cp) { + case 0x5C: return '\\\\'; + case 0x2C: return '\\,'; + case 0x7D: return '\\}'; + case 0x5D: return '\\]'; + case 0x7B: return '\\{'; + case 0x5B: return '\\['; + case 0x0A: return '\\n'; + case 0x0D: return '\\r'; + } + if (($first || $last) && PureText::isKtavWsCp($cp)) { + return self::uEscape($cp); + } + return substr($s, $i, $len); + } + + /** \uXXXX, surrogate pair for > U+FFFF (§ 3.7.1, uppercase hex). */ + public static function uEscape(int $cp): string + { + if ($cp > 0xFFFF) { + $v = $cp - 0x10000; + $hi = 0xD800 | ($v >> 10); + $lo = 0xDC00 | ($v & 0x3FF); + return sprintf('\u%04X\u%04X', $hi, $lo); + } + return sprintf('\u%04X', $cp); + } + + /** + * Decode every escape in an inline scalar body (§ 3.7). Returns + * [decodedText, hadEscape] or null on a malformed escape. + * + * @return array{string,bool}|null + */ + public static function decodeInlineScalar(string $raw): ?array + { + $n = strlen($raw); + $out = ''; + $had = false; + $i = 0; + while ($i < $n) { + if (ord($raw[$i]) === 0x5C) { + $d = PureScanner::decodeEscape($raw, $i, $n); + if ($d === null) { + return null; + } + $out .= $d[0]; + $i = $d[1]; + $had = true; + continue; + } + $out .= $raw[$i]; + ++$i; + } + return [$out, $had]; + } + + /** Unicode scalar value count (byte walk, no preg / mbstring). + * A scalar is any byte that is NOT a continuation byte (10xxxxxx) + * — valid UTF-8 input makes this exact. */ + public static function countScalars(string $s): int + { + $n = strlen($s); + $count = 0; + for ($i = 0; $i < $n; ++$i) { + if ((ord($s[$i]) & 0xC0) !== 0x80) { + ++$count; + } + } + return $count; + } + + /** + * The requires_block layout criterion for a replacement: a String + * containing LF/CR, or a String whose § 3.7 inline encoding needs + * >= 4 escape sequences with overhead >= 25% of the decoded scalar + * count (per contract, per-string, not per-array sum). Compounds + * recurse; a hit anywhere forces the containing wrapper to expand. + * + * @param mixed $c replacement cval + * @throws KtavException (EditUnsupported) beyond the inline depth limit + */ + public static function requiresBlock($c, int $depth = 0): bool + { + if ($depth > 128) { + throw self::unsupported('replacement nesting exceeds inline depth limit (128)'); + } + if (!is_array($c)) { + return false; + } + switch ($c['t']) { + case 'string': + $s = $c['v']; + if (strpos($s, "\n") !== false || strpos($s, "\r") !== false) { + return true; + } + $encoded = self::encodeInline($s, false); + $escapes = 0; + $i = 0; + $n = strlen($encoded); + while ($i < $n) { + if (ord($encoded[$i]) === 0x5C) { + ++$escapes; + $i += 2; + continue; + } + ++$i; + } + $plain = self::countScalars($s); + $overhead = self::countScalars($encoded) - $plain; + $threshold = intdiv($plain, 4) + ($plain % 4 !== 0 ? 1 : 0); + return $escapes >= 4 && $overhead >= $threshold; + case 'object': + foreach ($c['pairs'] as $pair) { + if (self::requiresBlock($pair[1], $depth + 1)) { + return true; + } + } + return false; + case 'array': + foreach ($c['items'] as $item) { + if (self::requiresBlock($item, $depth + 1)) { + return true; + } + } + return false; + default: + return false; + } + } +} diff --git a/src/Pure/Edit/EditSplice.php b/src/Pure/Edit/EditSplice.php new file mode 100644 index 0000000..bbc5ad6 --- /dev/null +++ b/src/Pure/Edit/EditSplice.php @@ -0,0 +1,1666 @@ + EditNotFound; malformed textual path + * => EditInvalidPath with the UTF-8 byte offset; + * - dotted/reopened noncontiguous targets and structural replacement + * of a container holding comment lines => EditUnsupported BEFORE the + * splice; a replacement String containing CR or a line `))` also has + * no lossless form; + * - byte-identical no-op even for noncanonical source spelling; + * - every patch is re-parsed and semantically verified: exactly the + * target replaced, the rest of the tree equal — fail closed. + */ +final class EditSplice +{ + /** Inline-line growth limit (Unicode scalar values). */ + const LINE_LIMIT = 100; + + // ----------------------------------------------------------------- + // Public entry points + // ----------------------------------------------------------------- + + /** + * edit_value(source, textualPath, replacement): the textual path + * `key1.key2.0.key4` with `\.` / `\\` escapes. + * + * ORDER mirrors the C ABI: UTF-8 validate + parse the SOURCE + * first (a broken source wins with the ordinary 0.9 parser + * envelope), THEN decode the path/replacement transport, then + * edit. Invalid path bytes are a Message transport diagnostic, + * never a guessed NotFound. + * + * @param mixed $replacement public PHP value (or `['$i'=>…]` / + * `['$f'=>…]` tagged scalars) + * @throws KtavException the edit envelope, a 0.9 parser envelope, + * or a Message transport envelope + */ + public static function editValue(string $src, string $path, $replacement): string + { + return self::apply( + $src, + static function () use ($path): array { + if (PureScanner::validateUtf8($path) !== null) { + // Transport decode failure: the path argument is + // not valid UTF-8 (source-less Message envelope). + throw EditEnvelope::message('edit path is not valid UTF-8'); + } + return self::parsePath($path); + }, + $replacement + ); + } + + /** + * edit_value_segments(source, decodedSegments, replacement): the + * segments are literal, already-decoded names — no extra unescape; + * a numeric segment is a key in an Object and a zero-based + * ASCII-digits index in an Array. Source-first like the C ABI: an + * empty segments list is EditNotFound only AFTER the source + * parsed; a non-string segment is a Message transport failure. + * + * @param string[] $segments + * @param mixed $replacement + */ + public static function editValueSegments(string $src, array $segments, $replacement): string + { + return self::apply( + $src, + static function () use ($segments): array { + if ($segments === []) { + // Contract: empty segments list -> NotFound (after + // the source parse, like the Rust entry). + throw EditEnvelope::notFound(); + } + foreach ($segments as $seg) { + if (!is_string($seg)) { + throw EditEnvelope::message( + 'edit segments transport must be a list of strings' + ); + } + } + return $segments; + }, + $replacement + ); + } + + /** + * Textual path grammar: '.' separates; `\.` and `\\` are literals; + * any other escape or a trailing lone backslash, an empty path and + * empty segments are EditInvalidPath with the byte offset. + * + * @return string[] + */ + public static function parsePath(string $path): array + { + $n = strlen($path); + if ($n === 0) { + throw EditEnvelope::invalidPath(0); + } + $segments = []; + $segStart = 0; + $cur = null; + $i = 0; + while ($i < $n) { + $b = ord($path[$i]); + if ($b === 0x2E) { // '.' + if ($segStart === $i && $cur === null) { + throw EditEnvelope::invalidPath($i); + } + $segments[] = $cur !== null ? $cur : substr($path, $segStart, $i - $segStart); + $cur = null; + $segStart = $i + 1; + ++$i; + continue; + } + if ($b === 0x5C) { // '\' + if ($i + 1 < $n && (ord($path[$i + 1]) === 0x2E || ord($path[$i + 1]) === 0x5C)) { + if ($cur === null) { + $cur = substr($path, $segStart, $i - $segStart); + } + $cur .= $path[$i + 1] === '.' ? '.' : '\\'; + $i += 2; + continue; + } + throw EditEnvelope::invalidPath($i); + } + // UTF-8 lead byte length (input is valid UTF-8; continuation + // bytes are copied inside their sequence). + $len = 1; + if ($b >= 0xC2 && $b <= 0xDF) { + $len = 2; + } elseif ($b >= 0xE0 && $b <= 0xEF) { + $len = 3; + } elseif ($b >= 0xF0 && $b <= 0xF7) { + $len = 4; + } + if ($cur !== null) { + $cur .= substr($path, $i, $len); + } + $i += $len; + } + if ($segStart === $n && $cur === null) { + throw EditEnvelope::invalidPath($n); // trailing '.' + } + $segments[] = $cur !== null ? $cur : substr($path, $segStart); + return $segments; + } + + /** + * Public PHP replacement value -> cval. Int/float/string/bool/null + * keep their type; tagged payloads go through the SAME strict + * wire-ingest as the native codec (cabi/wire.rs canonical_integer / + * canonical_float): the payload must be a STRING, `$i` is + * canonicalized (explicit `+` stripped, redundant leading zeros + * trimmed, signed zero normalized to "0", bignums pass through + * digit-exactly), `$f` must be a finite-decimal lexical form + * ('.' or exponent) parsed to f64 and re-emitted shortest + * round-trip — a non-finite result (e.g. "1e400") passes through + * verbatim as a wire-only value. ANY invalid payload is a + * source-less Message transport failure, never a silently coerced + * scalar (a bogus `$f` must not collapse to 0.0 and produce a + * false no-op). An empty PHP array follows the nested EMPTY LIST + * convention (documented like the writer's); a sequential array is + * an Array, an associative one an Object. + * + * @param mixed $v + * @return mixed cval + */ + public static function publicToCval($v) + { + if (is_array($v) && count($v) === 1 + && (array_key_exists('$i', $v) || array_key_exists('$f', $v))) { + // Single-key tagged payload: the tag is recognized even for + // a null value (array_key_exists, not isset). + if (array_key_exists('$i', $v)) { + $p = $v['$i']; + if (!is_string($p)) { + throw EditEnvelope::message( + 'edit replacement transport: $i payload must be a string' + ); + } + return ['t' => 'int', 'text' => self::canonicalWireInteger($p)]; + } + $p = $v['$f']; + if (!is_string($p)) { + throw EditEnvelope::message( + 'edit replacement transport: $f payload must be a string' + ); + } + return self::canonicalWireFloat($p); + } + if ($v === null) { + return ['t' => 'null']; + } + if (is_bool($v)) { + return ['t' => 'bool', 'v' => $v]; + } + if (is_int($v)) { + return ['t' => 'int', 'text' => (string) $v]; + } + if (is_float($v)) { + // Direct PHP non-finite floats never reach the native codec + // (the wire wrapper rejects them pre-call) — mirror that as + // a transport Message instead of rendering garbage. + if (!is_finite($v)) { + throw EditEnvelope::message( + 'edit replacement transport: non-finite Float' + ); + } + return ['t' => 'float', 'v' => $v, 'text' => self::floatText($v)]; + } + if (is_string($v)) { + return ['t' => 'string', 'v' => $v]; + } + // EMPTY stdClass only: the documented escape hatch for a + // replacement empty Object (PHP [] is the empty LIST). A + // NON-empty stdClass — including a self-referencing one — is a + // typed Message transport failure BEFORE any recursion, so a + // cycle can never hang the conversion. + if ($v instanceof \stdClass) { + if ((array) $v !== []) { + throw EditEnvelope::message( + 'edit replacement transport: only an EMPTY stdClass' + . ' (an explicit empty Object) is supported' + ); + } + return ['t' => 'object', 'pairs' => []]; + } + if (is_array($v)) { + if ($v === [] || EditSplice::isList($v)) { + $items = []; + foreach ($v as $child) { + $items[] = self::publicToCval($child); + } + return ['t' => 'array', 'items' => $items]; + } + $pairs = []; + foreach ($v as $k => $child) { + $pairs[] = [(string) $k, self::publicToCval($child)]; + } + return ['t' => 'object', 'pairs' => $pairs]; + } + // Wire/transport failure: no invented source location, same + // Message category as the native JSON transport. + throw EditEnvelope::message( + 'edit replacement transport: unsupported PHP type ' . gettype($v) + ); + } + + /** @param mixed $v */ + private static function isList($v): bool + { + $i = 0; + foreach ($v as $k => $_) { + if ($k !== $i++) { + return false; + } + } + return true; + } + + /** Pretty float text (shortest round-trip, forced decimal point). */ + private static function floatText(float $f): string + { + $s = PureFloat::shortestDigits($f); + if (strpos($s, '.') === false && strpos($s, 'e') === false && strpos($s, 'E') === false) { + $s .= '.0'; + } + return $s; + } + + /** + * cabi/wire.rs canonical_integer: optional sign, ASCII digits + * only, explicit `+` stripped, redundant leading zeros trimmed, + * signed zero normalized to "0", i64-overflow bignums pass through + * digit-exactly. Anything else is a wire-contract Message. + */ + private static function canonicalWireInteger(string $s): string + { + $sign = ''; + $digits = $s; + $b0 = $s === '' ? 0 : ord($s[0]); + if ($b0 === 0x2D) { // '-' + $sign = '-'; + $digits = substr($s, 1); + } elseif ($b0 === 0x2B) { // '+' + $digits = substr($s, 1); + } + if ($digits === '' || !ctype_digit($digits)) { + throw EditEnvelope::message( + 'edit replacement transport: $i payload not an integer literal' + ); + } + $trimmed = ltrim($digits, '0'); + if ($trimmed === '') { + return '0'; // "-0", "+0", "000" -> "0" + } + return $sign . $trimmed; + } + + /** + * cabi/wire.rs canonical_float: the payload must be a decimal + * lexical form ('.' or exponent), parse to f64; finite results are + * re-emitted shortest round-trip, a non-finite parse (extreme + * exponent, e.g. "1e400") passes through VERBATIM as a wire-only + * value the writers reject at render time. Invalid payloads are a + * wire-contract Message — never a silent 0.0 coercion. + */ + private static function canonicalWireFloat(string $s): array + { + if (strpos($s, '.') === false && strpos($s, 'e') === false && strpos($s, 'E') === false) { + throw EditEnvelope::message( + 'edit replacement transport: $f payload must contain \'.\' or exponent' + ); + } + // Strict decimal parse (no leading/trailing whitespace, no PHP + // hex strings — mirroring f64::from_str). + $t = $s; + if ($t !== '' && (ord($t[0]) === 0x2B || ord($t[0]) === 0x2D)) { + $t = substr($t, 1); + } + $valid = $t !== '' && preg_match('/^\d*\.?\d+([eE][+-]?\d+)?$|^\d+\.([eE][+-]?\d+)?$/', $t) === 1; + if (!$valid) { + throw EditEnvelope::message( + 'edit replacement transport: $f payload not a finite decimal' + ); + } + $val = (float) $s; + if (!is_finite($val)) { + // Wire-only non-finite: verbatim text (never normalized). + return ['t' => 'float', 'v' => $val, 'text' => $s]; + } + return ['t' => 'float', 'v' => $val, 'text' => self::floatText($val)]; + } + + // ----------------------------------------------------------------- + // Core apply + // ----------------------------------------------------------------- + + /** + * @param string[] $segments + * @param mixed $c replacement cval + */ + private static function apply(string $src, callable $decodePath, $c): string + { + // (1) SOURCE FIRST (C ABI order): UTF-8 validate + parse; its + // envelope propagates VERBATIM (0.9 parser diagnostics, never + // an edit code). The parsed original feeds no-op detection and + // verification. + $original = EditEnvelope::toCval(PureParser::parse($src, false)); + if (is_array($c)) { + \Ktav\Internal\ArrayGraph::assertAcyclic($c); + } + try { + // (2) THEN the path/replacement transport decode. + $segments = $decodePath(); + return self::applyInner($src, $segments, self::publicToCval($c), $original); + } catch (KtavException $e) { + $code = $e->getError(); + if ($code === 'EditInvalidPath' || $code === 'EditNotFound' + || $code === 'EditUnsupported' || $code === 'Message') { + throw $e; + } + // A parser envelope surfaced mid-edit (e.g. an invalid + // standalone render during layout): typed Unsupported, + // keeping the original message text. + throw EditEnvelope::unsupported($e->getMessage()); + } catch (\RuntimeException $e) { + throw EditEnvelope::unsupported('internal: ' . $e->getMessage()); + } + } + + /** + * @param string[] $segments + * @param mixed $c + * @param mixed $original parsed original cval (target included) + */ + private static function applyInner(string $src, array $segments, $c, $original): string + { + // (2) The target must exist in the parsed original. + $target = self::lookupValue($original, $segments); + if ($target === null) { + throw EditEnvelope::notFound(); + } + // (3) Byte-identical no-op (even noncanonical source spelling). + if (self::replacementEqual($target, $c)) { + return $src; + } + + // (4) Positional tree + descent. + list($root, $rootExplicit) = EditTree::build($src); + list($node, $inlineLeaf, $rootFirst) = self::descend($root, $segments, $rootExplicit); + + $slot = $node['slot']; + if ($slot === null) { + throw EditEnvelope::unsupported( + 'target is a noncontiguous value (dotted-key/reopen intermediate);' + . ' no lossless single-range splice exists' + ); + } + $slotKind = $slot['r']; // 'pair' | 'item' | 'block' + + list($b0, $b1, $a) = self::slotRanges($slot); + if ($b0 > $b1 || $a > $b1 || !($a <= strlen($src)) || !($b1 <= strlen($src))) { + throw EditEnvelope::unsupported('internal: invalid marker/body offsets'); + } + + $inlineItem = $inlineLeaf && $slotKind === 'item'; + $existingRaw = false; + $wsRaw = ''; + $wsPlain = ''; + if ($slotKind !== 'block' && !$inlineItem) { + $existingRaw = $a + 1 < strlen($src) && ord($src[$a + 1]) === 0x3A; + if ($existingRaw) { + $wsRaw = substr($src, $a + 2, $b0 - ($a + 2)); + if ($wsRaw === false) { + throw EditEnvelope::unsupported('internal: invalid raw marker range'); + } + } + if ($slotKind === 'pair') { + $wsPlain = substr($src, $a + 1, $b0 - ($a + 1)); + if ($wsPlain === false) { + throw EditEnvelope::unsupported('internal: invalid plain marker range'); + } + } + } + + // Structural replacement of a container holding comment lines: + // typed refusal BEFORE the splice. Multiline scalar blocks are + // data and excluded from the scan. + if ($node['kind'] !== 'scalar') { + $skip = []; + self::collectMultilineScalarRanges($node, $skip); + if (self::hasCommentLine($src, $b0, $b1, $skip)) { + throw EditEnvelope::unsupported( + 'target container contains comment lines; structural' + . ' replacement cannot preserve them losslessly' + ); + } + } + + $indent = EditTree::lineIndent($src, $a); + $eol = EditTree::detectEol($src, $b1); + $isCompound = $node['kind'] !== 'scalar'; + $roleItemBlock = $slotKind === 'block' && $slot['anchor'] === null; + $wsPairBlock = ''; + if ($slotKind === 'block' && !$roleItemBlock) { + $wsPairBlock = substr($src, $a + 1, $b0 - ($a + 1)); + if ($wsPairBlock === false) { + throw EditEnvelope::unsupported('internal: invalid block pair marker range'); + } + } + + // ---- Build the splice plan ------------------------------------- + if ($inlineLeaf) { + list($planFrom, $prefix0, $prefix1, $body) = self::planInline( + $slotKind, $c, $existingRaw, $src, $a, $b0, $b1, $wsRaw + ); + } elseif ($slotKind === 'block') { + list($planFrom, $prefix0, $prefix1, $body) = self::planBlock( + $c, $roleItemBlock, $existingRaw, $rootFirst, $indent, $eol, + $isCompound, $a, $b0, $wsPairBlock + ); + } else { + list($planFrom, $prefix0, $prefix1, $body) = self::planLinear( + $c, $slotKind, $existingRaw, $rootFirst, $indent, $eol, + $isCompound, $a, $b0, $b1, $wsRaw, $wsPlain + ); + } + + $insertion = strlen($prefix0) + strlen($prefix1) + strlen($body); + $candidate = substr($src, 0, $planFrom) . $prefix0 . $prefix1 . $body . substr($src, $b1); + + // ---- Layout: minimal inline-wrapper expansion ------------------- + $candidate = self::layoutAdjust( + $src, $candidate, $root, $segments, $c, + $planFrom, $b1, $insertion, $inlineLeaf + ); + + // ---- Mandatory semantic verification (fail closed) -------------- + $patched = EditEnvelope::toCval(PureParser::parse($candidate, false)); + $actual = self::takeTarget($patched, $segments); + if ($actual === null || !self::replacementEqual($actual, $c)) { + throw EditEnvelope::unsupported('semantic mismatch after splice; no lossless patch produced'); + } + $expected = $original; + if (self::takeTarget($expected, $segments) === null) { + throw EditEnvelope::notFound(); + } + if (!EditEnvelope::cvalEqual($patched, $expected)) { + throw EditEnvelope::unsupported('semantic mismatch after splice; no lossless patch produced'); + } + return $candidate; + } + + /** @return array{int,int,int} [bodyStart, bodyEnd, markerAnchor] */ + private static function slotRanges(array $slot): array + { + switch ($slot['r']) { + case 'pair': + return [$slot['start'], $slot['end'], $slot['colon']]; + case 'item': + return [$slot['body0'], $slot['end'], $slot['start']]; + default: // block + return [$slot['start'], $slot['end'], $slot['anchor'] !== null ? $slot['anchor'] : $slot['start']]; + } + } + + /** + * Descend the positional tree by decoded segments. Returns + * [node, physicallyInline, rootFirst]. + * + * @param string[] $segments + * @return array{array,bool,bool} + */ + private static function descend(array $root, array $segments, bool $rootExplicit): array + { + $containerIsRoot = true; + $cur = $root; + $rootFirst = false; + foreach ($segments as $seg) { + if ($cur['kind'] === 'object') { + $rootFirst = false; + $cur = self::childOf($cur, $seg); + if ($cur === null) { + throw EditEnvelope::notFound(); + } + } elseif ($cur['kind'] === 'array') { + $idx = self::parseIndex($seg); + $rootFirst = $containerIsRoot && !$rootExplicit && $idx === 0; + if (!isset($cur['items'][$idx])) { + throw EditEnvelope::notFound(); + } + $cur = $cur['items'][$idx]; + } else { + throw EditEnvelope::notFound(); // scalar descent + } + $containerIsRoot = false; + } + return [$cur, $cur['inline'], $rootFirst]; + } + + /** @return array|null */ + private static function childOf(array $node, string $key) + { + foreach ($node['pairs'] as $pair) { + if ($pair[0] === $key) { + return $pair[1]; + } + } + return null; + } + + /** ASCII digits only; leading zeros allowed; signs are not indexes. */ + private static function parseIndex(string $seg): int + { + $n = strlen($seg); + if ($n === 0) { + throw EditEnvelope::notFound(); + } + $v = 0; + for ($i = 0; $i < $n; ++$i) { + $b = ord($seg[$i]); + if ($b < 0x30 || $b > 0x39) { + throw EditEnvelope::notFound(); + } + $v = $v * 10 + ($b - 0x30); + if ($v > PHP_INT_MAX / 2 - 1) { + // Overflow guard: definitely out of bounds. + $v = PHP_INT_MAX; + } + } + return (int) $v; + } + + /** + * Current value at the path in a parsed cval (null when absent). + * + * @param mixed $v + * @param string[] $segments + * @return mixed + */ + private static function lookupValue($v, array $segments) + { + $cur = $v; + foreach ($segments as $seg) { + if (!is_array($cur)) { + return null; + } + if ($cur['t'] === 'object') { + $cur = self::cvalChild($cur, $seg); + } elseif ($cur['t'] === 'array') { + $idx = self::parseIndexSoft($seg); + if ($idx === null || !isset($cur['items'][$idx])) { + return null; + } + $cur = $cur['items'][$idx]; + } else { + return null; + } + } + return $cur; + } + + /** @return mixed|null */ + private static function cvalChild(array $v, string $key) + { + foreach ($v['pairs'] as $pair) { + if ($pair[0] === $key) { + return $pair[1]; + } + } + return null; + } + + /** Like parseIndex but null instead of an envelope. */ + private static function parseIndexSoft(string $seg): ?int + { + try { + return self::parseIndex($seg); + } catch (KtavException $e) { + return null; + } + } + + /** + * Remove (and return) the target from a mutable cval tree by + * replacing it with the ['t'=>'removed'] sentinel. + * + * @param mixed $v + * @param string[] $segments + * @return mixed|null + */ + private static function takeTarget(&$v, array $segments) + { + $last = count($segments) - 1; + $cur = &$v; + foreach ($segments as $i => $seg) { + if (!is_array($cur) || ($cur['t'] !== 'object' && $cur['t'] !== 'array')) { + return null; + } + if ($i === $last) { + if ($cur['t'] === 'object') { + foreach ($cur['pairs'] as $j => $pair) { + if ($pair[0] === $seg) { + $taken = $cur['pairs'][$j][1]; + $cur['pairs'][$j][1] = ['t' => 'removed']; + return $taken; + } + } + return null; + } + $idx = self::parseIndexSoft($seg); + if ($idx === null || !isset($cur['items'][$idx])) { + return null; + } + $taken = $cur['items'][$idx]; + $cur['items'][$idx] = ['t' => 'removed']; + return $taken; + } + if ($cur['t'] === 'object') { + $next = null; + foreach ($cur['pairs'] as $j => $pair) { + if ($pair[0] === $seg) { + $next = $j; + break; + } + } + if ($next === null) { + return null; + } + $cur = &$cur['pairs'][$next][1]; + } else { + $idx = self::parseIndexSoft($seg); + if ($idx === null || !isset($cur['items'][$idx])) { + return null; + } + $cur = &$cur['items'][$idx]; + } + } + return null; + } + + /** + * No-op / verification equality: ints compare as digit strings, + * floats numerically (so a noncanonical source spelling of the + * SAME value stays byte-identical), strings exactly. + * + * @param mixed $parsed parsed-side cval + * @param mixed $c replacement cval + */ + private static function replacementEqual($parsed, $c): bool + { + if (!is_array($parsed) || !is_array($c)) { + return false; + } + $pt = $parsed['t']; + $ct = $c['t']; + if ($pt !== $ct) { + return false; + } + switch ($ct) { + case 'object': + // Rust IndexMap PartialEq IGNORES pair order (map + // equality by keys) — an equal object with a different + // key order is a byte-identical no-op. Arrays stay + // ordered; only Object is a map. + $pc = $c['pairs']; + if (count($parsed['pairs']) !== count($pc)) { + return false; + } + foreach ($parsed['pairs'] as $pair) { + $matched = false; + foreach ($pc as $other) { + if ($other[0] === $pair[0]) { + if (!self::replacementEqual($pair[1], $other[1])) { + return false; + } + $matched = true; + break; + } + } + if (!$matched) { + return false; + } + } + return true; + case 'array': + $ia = $parsed['items']; + $ic = $c['items']; + if (count($ia) !== count($ic)) { + return false; + } + foreach ($ia as $i => $item) { + if (!self::replacementEqual($item, $ic[$i])) { + return false; + } + } + return true; + case 'int': + return PureScanner::digitCmp(ltrim($parsed['text'], '+'), ltrim($c['text'], '+')) === 0 + && (strpos($parsed['text'], '-') === 0) === (strpos($c['text'], '-') === 0); + case 'float': + // Exact f64 identity via the ini-independent shortest + // round-trip digits (injective on f64, sign of zero + // kept) — never a lossy (string) cast under the + // `precision` ini, and -0.0 !== 0.0 semantically. + // Wire-only non-finite payloads (e.g. "1e400" tagged) + // compare by their verbatim text; a parsed source value + // is always finite, so they never false-match. + $pf = (float) $parsed['v']; + $cf = (float) $c['v']; + if (!is_finite($pf) || !is_finite($cf)) { + return isset($parsed['text'], $c['text']) + && $parsed['text'] === $c['text']; + } + return PureFloat::shortestDigits($pf) + === PureFloat::shortestDigits($cf); + default: + return $parsed === $c; + } + } + + // ----------------------------------------------------------------- + // Comment-line scan for structural replacement + // ----------------------------------------------------------------- + + /** @param array[] $node + * @param array[] $out */ + private static function collectMultilineScalarRanges(array $node, array &$out) + { + if ($node['kind'] === 'scalar') { + if (is_array($node['slot']) && $node['slot']['r'] === 'block') { + $out[] = [$node['slot']['start'], $node['slot']['end']]; + } + return; + } + if ($node['kind'] === 'object') { + foreach ($node['pairs'] as $pair) { + self::collectMultilineScalarRanges($pair[1], $out); + } + return; + } + foreach ($node['items'] as $item) { + self::collectMultilineScalarRanges($item, $out); + } + } + + /** + * True when [from, to) contains a line whose first non-§3.3-whitespace + * code point starts `##`, outside the (sorted) multiline-scalar data + * ranges. Monotonic cursor — O(lines + ranges). + * + * @param array[] $skip + */ + private static function hasCommentLine(string $text, int $from, int $to, array &$skip): bool + { + usort($skip, static function (array $x, array $y): int { + return $x[0] <=> $y[0]; + }); + $skipI = 0; + $n = strlen($text); + $i = $from; + while ($i < $to && $i < $n) { + $lineStart = $i; + $j = $i; + while ($j < $to && $j < $n && ord($text[$j]) !== 0x0A && ord($text[$j]) !== 0x0D) { + ++$j; + } + // First non-whitespace byte of the line. + $k = $lineStart; + while ($k < $j && ($l = PureScanner::wsLenAt($text, $k, $j, true)) > 0) { + $k += $l; + } + while ($skipI < count($skip) && $skip[$skipI][1] <= $k) { + ++$skipI; + } + $inData = $skipI < count($skip) && $skip[$skipI][0] <= $k && $k < $skip[$skipI][1]; + if (!$inData && $k + 2 <= $n && substr($text, $k, 2) === '##') { + return true; + } + $i = $j; + while ($i < $to && $i < $n && (ord($text[$i]) === 0x0A || ord($text[$i]) === 0x0D)) { + ++$i; + } + } + return false; + } + + // ----------------------------------------------------------------- + // Inline bodies (§ 3.7 encoding) + // ----------------------------------------------------------------- + + /** Scalar body text (non-string scalars only). */ + private static function scalarBodyText(array $c): string + { + switch ($c['t']) { + case 'null': return 'null'; + case 'bool': return $c['v'] ? 'true' : 'false'; + case 'int': + case 'float': + return $c['text']; + } + throw EditEnvelope::unsupported('internal: scalar body requested for a compound'); + } + + /** Body of a PHYSICALLY inline value (marker rules differ). */ + private static function inlineBody(array $c, bool $forceString): string + { + if ($c['t'] === 'string') { + return EditEnvelope::encodeInline($c['v'], $forceString && PureText::needsRawMarker($c['v'])); + } + if ($c['t'] === 'object' || $c['t'] === 'array') { + $out = ''; + self::pushInlineValue($c, 0, $out); + return $out; + } + return self::scalarBodyText($c); + } + + /** @param mixed $c */ + private static function pushInlineValue($c, int $depth, string &$out) + { + if ($depth > 128) { + throw EditEnvelope::unsupported('replacement nesting exceeds inline depth limit (128)'); + } + if ($c['t'] === 'object') { + $out .= '{'; + $first = true; + foreach ($c['pairs'] as $pair) { + if (!$first) { + $out .= ', '; + } + $first = false; + $out .= self::escapedKeySegment($pair[0]); + $out .= ': '; + $child = $pair[1]; + if ($child['t'] === 'string') { + $out .= EditEnvelope::encodeInline($child['v'], PureText::needsRawMarker($child['v'])); + } else { + self::pushInlineValue($child, $depth + 1, $out); + } + } + $out .= '}'; + return; + } + if ($c['t'] === 'array') { + $out .= '['; + $first = true; + foreach ($c['items'] as $child) { + if (!$first) { + $out .= ', '; + } + $first = false; + if ($child['t'] === 'string' && $child['v'] === '') { + $out .= '()'; + } elseif ($child['t'] === 'string') { + $out .= EditEnvelope::encodeInline($child['v'], PureText::needsRawMarker($child['v'])); + } else { + self::pushInlineValue($child, $depth + 1, $out); + } + } + $out .= ']'; + return; + } + if ($c['t'] === 'string') { + $out .= EditEnvelope::encodeInline($c['v'], PureText::needsRawMarker($c['v'])); + return; + } + $out .= self::scalarBodyText($c); + } + + /** + * § 5.9.10 key segment emitter for inline compound bodies: bare + * with re-escaping, or a quoted `"` form for structural / edge + * code points (matches the pure writer's rules). + */ + private static function escapedKeySegment(string $key): string + { + $n = strlen($key); + if ($n > 0) { + $b0 = ord($key[0]); + if (PureScanner::isQuoteByte($b0) + || substr($key, 0, 2) === '##' + || substr($key, 0, 3) === "\xEF\xBB\xBF") { + return self::quotedKey($key); + } + list($cp0,) = PureText::cpAt($key, 0); + if (PureText::isInlineWsCp($cp0)) { + return self::quotedKey($key); + } + $j = $n - 1; + while ($j > 0 && (ord($key[$j]) & 0xC0) === 0x80) { + --$j; + } + list($cpL,) = PureText::cpAt($key, $j); + if (PureText::isInlineWsCp($cpL)) { + return self::quotedKey($key); + } + } + $out = ''; + $i = 0; + while ($i < $n) { + $b = ord($key[$i]); + switch ($b) { + case 0x2E: case 0x3A: case 0x2C: + case 0x7B: case 0x7D: case 0x5B: case 0x5D: + case 0x28: case 0x29: + return self::quotedKey($key); + case 0x5C: + $out .= '\\\\'; + break; + case 0x0A: + $out .= '\n'; + break; + case 0x0D: + $out .= '\r'; + break; + case 0x09: case 0x0B: case 0x0C: + $out .= chr($b); + break; + default: + if ($b < 0x20 || $b === 0x7F) { + $out .= EditEnvelope::uEscape($b); + } else { + $out .= chr($b); + } + } + ++$i; + } + return $out; + } + + /** @return string */ + private static function quotedKey(string $key): string + { + $out = '"'; + $n = strlen($key); + $i = 0; + while ($i < $n) { + $b = ord($key[$i]); + if ($b < 0x80) { + switch ($b) { + case 0x22: $out .= '\\"'; break; + case 0x5C: $out .= '\\\\'; break; + case 0x0A: $out .= '\n'; break; + case 0x0D: $out .= '\r'; break; + default: + if (($b < 0x20 && $b !== 0x09 && $b !== 0x0B && $b !== 0x0C) || $b === 0x7F) { + $out .= EditEnvelope::uEscape($b); + } else { + $out .= chr($b); + } + } + ++$i; + continue; + } + list($cp, $len) = PureText::cpAt($key, $i); + if (($cp < 0x20 && !($cp === 0x09 || $cp === 0x0B || $cp === 0x0C)) || $cp === 0x7F) { + $out .= EditEnvelope::uEscape($cp); + } else { + $out .= substr($key, $i, $len); + } + $i += $len; + } + return $out . '"'; + } + + // ----------------------------------------------------------------- + // Plans + // ----------------------------------------------------------------- + + /** + * Physically inline target. Returns [from, prefix0, prefix1, body]. + * + * @return array{int,string,string,string} + */ + private static function planInline( + string $slotKind, + array $c, + bool $existingRaw, + string $src, + int $a, + int $b0, + int $b1, + string $wsRaw + ): array { + if ($slotKind === 'item') { + // Inline array item: `::` is literal data — no marker. + $body = ($c['t'] === 'string' && $c['v'] === '') ? '()' : self::inlineBody($c, true); + return [$b0, '', '', $body]; + } + if ($existingRaw && $c['t'] !== 'string') { + // Raw body is literal: strip only the SECOND colon, keep the + // original whitespace. + return [$a + 1, $wsRaw, '', self::inlineBody($c, false)]; + } + return [$b0, '', '', self::inlineBody($c, !$existingRaw)]; + } + + /** + * Linear (pair/item line) slots. Returns [from, prefix0, prefix1, body]. + * + * @return array{int,string,string,string} + */ + private static function planLinear( + array $c, + string $slotKind, + bool $existingRaw, + bool $rootFirst, + string $indent, + string $eol, + bool $isCompound, + int $a, + int $b0, + int $b1, + string $wsRaw, + string $wsPlain + ): array { + list($body, $desiredRaw) = self::planValue( + $c, $slotKind, $existingRaw, $rootFirst, $indent, $eol, + $isCompound, false + ); + if ($existingRaw === $desiredRaw) { + $prefix = ($b0 === $b1 && $wsPlain === '' && $wsRaw === '') ? ' ' : ''; + return [$b0, $prefix, '', $body]; + } + if ($existingRaw && !$desiredRaw) { + if ($slotKind === 'pair') { + // 'k:: x' -> 'k: x': strip only the second colon. + return [$a + 1, self::sepWs($b0, $b1, $wsRaw), '', $body]; + } + // Item: remove '::' but keep the original whitespace. + return [$a, $wsRaw, '', $body]; + } + // plain -> raw + if ($slotKind === 'pair') { + return [$a + 1, ':', self::sepWs($b0, $b1, $wsPlain), $body]; + } + return [$a, ':: ', '', $body]; + } + + /** Marker-zone whitespace helper: one space on an empty body. */ + private static function sepWs(int $b0, int $b1, string $ws): string + { + return ($b0 === $b1 && $ws === '') ? ' ' : $ws; + } + + /** + * plan_value: (body, desiredRaw) for a linear slot. + * + * @return array{string,bool} + */ + private static function planValue( + array $c, + string $slotKind, + bool $existingRaw, + bool $rootFirst, + string $indent, + string $eol, + bool $isCompoundInline, + bool $isCompoundBlock + ): array { + if ($c['t'] === 'string') { + return self::planString($c['v'], $slotKind, $existingRaw, $rootFirst, $indent, $eol); + } + if ($c['t'] !== 'object' && $c['t'] !== 'array') { + return [self::scalarBodyText($c), false]; + } + if ($isCompoundInline) { + $out = ''; + self::pushInlineValue($c, 0, $out); + return [$out, false]; + } + $isEmpty = $c['t'] === 'object' ? $c['pairs'] === [] : $c['items'] === []; + if ($isEmpty) { + if ($isCompoundBlock) { + $opener = $c['t'] === 'object' ? '{' : '['; + return [$opener . $eol . $indent . ($c['t'] === 'object' ? '}' : ']'), false]; + } + return [$c['t'] === 'object' ? '{}' : '[]', false]; + } + $rendered = self::renderCompoundBlock($c, $indent, $eol); + return [$rendered, false]; + } + + /** + * String replacement plan for linear slots (outside inline). + * + * @return array{string,bool} + */ + private static function planString( + string $s, + string $slotKind, + bool $existingRaw, + bool $rootFirst, + string $indent, + string $eol + ): array { + if (strpos($s, "\r") !== false) { + throw EditEnvelope::unsupported('replacement string contains CR (U+000D)'); + } + $edgeWs = self::edgeWs($s); + if (strpos($s, "\n") !== false || $edgeWs) { + // Plain marker is mandatory: raw does not open multiline + // blocks and trims edges. + return [self::verbatimBlockBody($s, $indent, $eol), false]; + } + $desiredRaw = $slotKind === 'item' + ? (PureText::itemNeedsRawMarker($s) || $s === '') + : PureText::needsRawMarker($s); + if ($slotKind === 'item' && $rootFirst) { + // § 5.0.1 root dispatch: pair-shaped / BOM bodies of the + // first item of an IMPLICIT root array must be raw. + $desiredRaw = $desiredRaw || PureScanner::isPairShape($s) + || strpos($s, "\xEF\xBB\xBF") === 0; + } + if ($existingRaw) { + // Raw preservation: compatible single-line bodies keep the + // marker (raw unnecessary != raw forbidden). + $desiredRaw = true; + } + return [$s, $desiredRaw]; + } + + /** @return bool first/last code point is § 3.3 whitespace */ + private static function edgeWs(string $s): bool + { + $n = strlen($s); + if ($n === 0) { + return false; + } + list($cp0,) = PureText::cpAt($s, 0); + if (PureText::isKtavWsCp($cp0)) { + return true; + } + $j = $n - 1; + while ($j > 0 && (ord($s[$j]) & 0xC0) === 0x80) { + --$j; + } + list($cpL,) = PureText::cpAt($s, $j); + return PureText::isKtavWsCp($cpL); + } + + /** + * Verbatim block `(( ... ))`: content verbatim, '\n' -> document + * EOL; a line equal to `))` has no lossless verbatim form. + */ + private static function verbatimBlockBody(string $s, string $indent, string $eol): string + { + foreach (explode("\n", $s) as $line) { + $t = trim($line, " \t"); + if ($t === '))') { + throw EditEnvelope::unsupported( + "multiline string contains a line equal to '))'; no lossless verbatim form exists" + ); + } + } + $content = $eol === "\n" ? $s : str_replace("\n", $eol, $s); + return '((' . $eol . $content . $eol . $indent . '))'; + } + + /** + * Multiline-BLOCK target. Returns [from, prefix0, prefix1, body]. + * + * @return array{int,string,string,string} + */ + private static function planBlock( + array $c, + bool $roleItemBlock, + bool $existingRaw, + bool $rootFirst, + string $indent, + string $eol, + bool $isCompound, + int $a, + int $b0, + string $wsPairBlock + ): array { + if ($c['t'] === 'string') { + $s = $c['v']; + if (strpos($s, "\r") !== false) { + throw EditEnvelope::unsupported('replacement string contains CR (U+000D)'); + } + $multiline = strpos($s, "\n") !== false || self::edgeWs($s); + if ($multiline) { + return [$b0, '', '', self::verbatimBlockBody($s, $indent, $eol)]; + } + if ($roleItemBlock && $s === '') { + // `()` parses as the empty String; a bare empty line + // would be blank. + return [$b0, '', '', '()']; + } + $itemRaw = $roleItemBlock + && (PureText::itemNeedsRawMarker($s) + || ($rootFirst && (PureScanner::isPairShape($s) || strpos($s, "\xEF\xBB\xBF") === 0))); + $pairRaw = !$roleItemBlock && PureText::needsRawMarker($s); + if ($itemRaw) { + return [$b0, ':: ', '', $s]; + } + if ($pairRaw) { + // Switch the pair marker to raw: second colon + the + // original whitespace + body. + return [$a + 1, ':', $wsPairBlock, $s]; + } + return [$b0, '', '', $s]; + } + list($body, ) = self::planValue( + $c, 'block', $existingRaw, $rootFirst, $indent, $eol, false, true + ); + return [$b0, '', '', $body]; + } + + /** + * Pretty block render of a non-empty compound at the target line + * indent (four spaces per new level, source EOL). Strings follow + * the pure writer's § 5.9.7 form selection via renderDocument. + */ + private static function renderCompoundBlock(array $c, string $indent, string $eol): string + { + $public = self::cvalToPublic($c); + $doc = PureWriter::renderDocument($public, false, false); + $doc = substr($doc, 0, -1); // trailing newline + $opener = $c['t'] === 'object' ? '{' : '['; + $closer = $c['t'] === 'object' ? '}' : ']'; + $childIndent = $indent . ' '; + return $opener . $eol . self::indentLines($doc, $childIndent, $eol) . $eol . $indent . $closer; + } + + /** @param mixed $c */ + private static function cvalToPublic($c) + { + if (!is_array($c)) { + return $c; + } + switch ($c['t']) { + case 'object': + $out = []; + foreach ($c['pairs'] as $pair) { + $out[$pair[0]] = self::cvalToPublic($pair[1]); + } + return $out; + case 'array': + $out = []; + foreach ($c['items'] as $item) { + $out[] = self::cvalToPublic($item); + } + return $out; + case 'int': + $t = ltrim($c['text'], '+'); + if (preg_match('/^-?\d+$/', $t) === 1 + && PureScanner::digitCmp(ltrim($t, '-'), '9223372036854775807') <= 0 + && ($t[0] !== '-' || PureScanner::digitCmp(substr($t, 1), '9223372036854775808') <= 0)) { + return (int) $t; + } + return ['$i' => $c['text']]; + case 'float': + return (float) $c['text']; + case 'bool': + return $c['v']; + case 'null': + return null; + default: + return $c['v']; + } + } + + /** + * Prefix every non-empty line with $childIndent, keeping verbatim + * multiline-string content verbatim (context heuristic, fail-closed + * under the semantic verification). + */ + private static function indentLines(string $doc, string $childIndent, string $eol): string + { + $lines = explode("\n", $doc); + $out = []; + $inVerbatim = false; + foreach ($lines as $line) { + if ($line !== '' && !$inVerbatim) { + $out[] = $childIndent . $line; + } else { + $out[] = $line; + } + $t = trim($line, " \t"); + if (substr($line, -2) === '((') { + $inVerbatim = true; + } elseif ($t === '))') { + $inVerbatim = false; + } + } + return implode($eol, $out); + } + + // ----------------------------------------------------------------- + // Layout: minimal inline-wrapper expansion (port of layout.rs) + // ----------------------------------------------------------------- + + /** + * @param string[] $segments + * @param mixed $c + */ + private static function layoutAdjust( + string $source, + string $candidate, + array $root, + array $segments, + array $c, + int $from, + int $to, + int $inserted, + bool $inlineLeaf + ): string { + $owner = self::inlineOwner($source, $root, $segments, $from, $inlineLeaf); + if ($owner === null || $from < $owner[0]) { + return $candidate; + } + list($start, $end) = $owner; + $newEnd = $end - ($to - $from) + $inserted; + if ($newEnd < $start || $newEnd > strlen($candidate)) { + throw EditEnvelope::unsupported('invalid layout range'); + } + $body = substr($candidate, $start, $newEnd - $start); + if ($body === false || !self::isCompoundText($body)) { + return $candidate; + } + $oldWidth = EditEnvelope::countScalars(EditTree::physicalLine($source, $from)); + $limit = max($oldWidth, self::LINE_LIMIT); + $force = EditEnvelope::requiresBlock($c); + if (!$force && EditEnvelope::countScalars(EditTree::physicalLine($candidate, $from)) <= $limit) { + return $candidate; + } + $indent = EditTree::lineIndent($source, $start); + $eol = EditTree::detectEol($source, $end); + $expanded = self::expand( + $body, $indent, $eol, $limit, + [$from - $start, $from - $start + $inserted], $force, 0 + ); + if ($expanded === null) { + return $candidate; // lossless standalone impossible: keep inline + } + return substr($candidate, 0, $start) . $expanded . substr($candidate, $newEnd); + } + + /** Leading § 3.3 inline-whitespace trim (width-correct). */ + private static function trimStartInline(string $s): string + { + $i = 0; + $n = strlen($s); + while ($i < $n && ($l = PureScanner::wsLenAt($s, $i, $n, true)) > 0) { + $i += $l; + } + return $i === 0 ? $s : substr($s, $i); + } + + /** @return bool */ + private static function isCompoundText(string $body): bool + { + return (substr($body, 0, 1) === '{' && substr($body, -1) === '}') + || (substr($body, 0, 1) === '[' && substr($body, -1) === ']'); + } + + /** + * Innermost…outermost inline compound owning the splice position: + * the first single-line compound slot along the path, or the whole + * trimmed physical line for a physically-inline root/leaf chain. + * + * @param string[] $segments + * @return array{int,int}|null + */ + private static function inlineOwner( + string $source, + array $root, + array $segments, + int $pos, + bool $inlineLeaf + ): ?array { + if ($root['inline']) { + return self::lineOwner($source, $pos); + } + $node = $root; + foreach ($segments as $seg) { + if ($node['kind'] === 'object') { + $node = self::childOf($node, $seg); + } elseif ($node['kind'] === 'array') { + $idx = self::parseIndexSoft($seg); + $node = $idx !== null ? ($node['items'][$idx] ?? null) : null; + } else { + $node = null; + } + if ($node === null) { + throw EditEnvelope::notFound(); + } + $slot = $node['slot']; + if ($slot !== null) { + list($start, $end) = self::slotRanges($slot); + $body = substr($source, $start, $end - $start); + if ($node['kind'] !== 'scalar' && $body !== false + && self::isCompoundText($body) + && strpos($body, "\n") === false && strpos($body, "\r") === false) { + return [$start, $end]; + } + } + } + if ($node['inline']) { + return self::lineOwner($source, $pos); + } + return null; + } + + /** Trimmed owner for inline contexts: the whole trimmed line when + * it is itself a compound, otherwise the value body after the + * pair's unescaped colon (core inline_owner final branch). */ + private static function lineOwner(string $source, int $pos): ?array + { + $line = EditTree::physicalLine($source, $pos); + $start = strpos($source, $line, max(0, $pos - strlen($line))); + list($ts, $te) = PureScanner::trimBounds($line, 0, strlen($line)); + $body = substr($line, $ts, $te - $ts); + $bodyStart = $start + $ts; + if (substr($body, 0, 3) === "\xEF\xBB\xBF") { + $body = substr($body, 3); + $bodyStart += 3; + } + if (self::isCompoundText($body)) { + return [$bodyStart, $bodyStart + strlen($body)]; + } + // Unescaped colon on the line: the pair value is the owner. + list($sep, $marker, $unterm) = PureScanner::findSeparator($line, $ts, $te); + if ($sep < 0 || $unterm) { + return null; + } + $vs = $sep + $marker; + while ($vs < $te && ($l = PureScanner::wsLenAt($line, $vs, $te, true)) > 0) { + $vs += $l; + } + $value = substr($line, $vs, $te - $vs); + if (!self::isCompoundText($value)) { + return null; + } + return [$start + $vs, $start + $vs + strlen($value)]; + } + + /** + * Expand one inline compound body into a block. Returns null when + * a moved part has no lossless standalone form (the caller keeps + * the inline candidate). + * + * @param int[] $changed [from, to) relative to $body + */ + private static function expand( + string $body, + string $indent, + string $eol, + int $limit, + array $changed, + bool $force, + int $depth + ): ?string { + if ($depth > 128) { + throw EditEnvelope::unsupported('layout nesting exceeds inline depth limit'); + } + $object = substr($body, 0, 1) === '{'; + $kind = $object ? 'o' : 'a'; + // COORDINATE BASIS: PureInlineScan::scan REQUIRES its region to + // include this compound's own closing delimiter (it stops + // there and reports its offset) — unlike the Rust + // split_top_level, which works on the inner slice. The region + // [1, strlen(body)) covers inner + closer; [1, strlen-1) would + // never see the closer and report "positioning failed" for + // every valid inline body. + $scan = \Ktav\Pure\PureInlineScan::scan($body, $kind, 1, strlen($body)); + if ($scan['badEscape'] !== null || $scan['closer'] === null) { + throw EditEnvelope::unsupported('cannot split inline layout'); + } + $childIndent = $indent . ' '; + $out = $object ? '{' : '['; + foreach ($scan['segs'] as $seg) { + list($ss, $se) = PureScanner::trimBounds($body, $seg[0], $seg[1]); + if ($ss >= $se) { + continue; // trailing comma + } + $part = substr($body, $ss, $se - $ss); + $offset = $ss; + $touched = $offset < $changed[1] && $offset + strlen($part) > $changed[0]; + if ($object) { + list($sep, $marker, $unterm) = PureScanner::findSeparator($part, 0, strlen($part)); + if ($sep < 0 || $unterm) { + throw EditEnvelope::unsupported('inline pair has no separator'); + } + $raw = $marker === 2; + $vs = $sep + $marker; + while ($vs < strlen($part) && ($l = PureScanner::wsLenAt($part, $vs, strlen($part), true)) > 0) { + $vs += $l; + } + $value = substr($part, $vs); + $prefix = substr($part, 0, strlen($part) - strlen($value)); + } else { + $raw = false; + $value = self::trimStartInline($part); + $prefix = ''; + } + $out .= $eol . $childIndent; + $trimmedValue = self::trimStartInline($value); + $isCompoundValue = self::isCompoundText($trimmedValue); + if (!$raw && $isCompoundValue && $touched + && ($force || EditEnvelope::countScalars($childIndent . $part) > $limit)) { + $valueOffset = $offset + strlen($part) - strlen($value); + $local = [$changed[0] - $valueOffset, $changed[1] - $valueOffset]; + $out .= $prefix; + $child = self::expand($trimmedValue, $childIndent, $eol, $limit, $local, $force, $depth + 1); + if ($child === null) { + return null; + } + $out .= $child; + } else { + $moved = self::standalone($part, $prefix, $value, $raw, $object, $childIndent, $eol); + if ($moved === null) { + return null; + } + $out .= $moved; + } + } + $out .= $eol . $indent . ($object ? '}' : ']'); + return $out; + } + + /** + * One untouched (or leaf) inline part moved onto its own line: + * keep the bytes when the standalone form parses to the same value; + * otherwise decode and re-render. Null when no lossless standalone + * form exists. + */ + private static function standalone( + string $part, + string $prefix, + string $body, + bool $raw, + bool $object, + string $indent, + string $eol + ): ?string { + // When inline escapes stop being escapes, semantics must hold. + $inlineText = $object ? '{' . $part . '}' : '[' . $part . ']'; + $expected = EditEnvelope::toCval(PureParser::parse($inlineText, false)); + $lineText = $object ? $part : "[\n" . $part . "\n]"; + $standaloneOk = false; + try { + $moved = EditEnvelope::toCval(PureParser::parse($lineText, false)); + $standaloneOk = EditEnvelope::cvalEqual($moved, $expected); + } catch (KtavException $e) { + $standaloneOk = false; + } + if ($standaloneOk) { + return $part; + } + // Decode the moved value. + if ($raw) { + $d = EditEnvelope::decodeInlineScalar($body); + if ($d === null) { + throw EditEnvelope::unsupported('invalid inline string'); + } + $c = ['t' => 'string', 'v' => $d[0]]; + } else { + try { + $parsed = PureParser::parse('[' . $body . ']', false); + } catch (KtavException $e) { + throw EditEnvelope::unsupported('cannot decode moved inline value'); + } + if (!($parsed instanceof \Ktav\Pure\PureList) || count($parsed->items) !== 1) { + throw EditEnvelope::unsupported('moved inline value is not a single value'); + } + $c = EditEnvelope::toCval($parsed->items[0]); + } + if ($c['t'] === 'string') { + if (strpos($c['v'], "\r") !== false) { + return null; // CR: no lossless standalone string form + } + foreach (explode("\n", $c['v']) as $l) { + if (trim($l, " \t") === '))') { + return null; + } + } + } + $slotKind = $object ? 'pair' : 'item'; + list($newBody, $desiredRaw) = self::planValue( + $c, $slotKind, $raw, false, $indent, $eol, true, false + ); + $out = ''; + if ($object) { + list($sep, $marker, $unterm) = PureScanner::findSeparator($part, 0, strlen($part)); + if ($sep < 0 || $unterm) { + throw EditEnvelope::unsupported('moved pair has no separator'); + } + $out .= substr($part, 0, $sep + 1); + if ($desiredRaw) { + $out .= ':'; + } + $ws = substr($prefix, $sep + ($raw ? 2 : 1)); + $out .= $ws === '' ? ' ' : $ws; + } elseif ($desiredRaw) { + $out .= ':: '; + } + $out .= $newBody; + return $out; + } +} diff --git a/src/Pure/Edit/EditTree.php b/src/Pure/Edit/EditTree.php new file mode 100644 index 0000000..76b0974 --- /dev/null +++ b/src/Pure/Edit/EditTree.php @@ -0,0 +1,770 @@ +'pair', 'colon'=>abs, 'start'=>bodyStart, 'end'=>bodyEnd] + * slot item : ['r'=>'item', 'start'=>marker-zone start, 'body0'=>bodyStart, 'end'=>bodyEnd] + * slot block : ['r'=>'block', 'anchor'=>colonAbs|null, 'start', 'end'] + * + * A node whose value was assembled from dotted-key / reopen lines is + * marked NONCONTIGUOUS by clearing its slot (synthetic intermediate + * objects never get one). Inline nodes carry inline=true ON THE LEAF — + * the physical inline context lives at the leaf, so synthetic dotted + * ancestors and reopen descendants neither inherit nor corrupt it. + * + * Object pairs are kept as an ordered list of [key, node] entries (PHP + * casts numeric-string array keys to int, which would corrupt keys). + */ +final class EditTree +{ + /** @var string */ + private $src; + + /** @var int */ + private $len; + + /** @var array[] frame stack */ + private $stack = []; + + /** @var array|null ['mode'=>'s'|'v', 'opener'=>int] multiline collector */ + private $collecting; + + /** @var int opener byte of the currently open multiline string */ + private $collectingOpener = 0; + + /** @var bool */ + private $rootInitialized = false; + + /** @var bool */ + private $rootConsumed = false; + + /** @var array|null */ + private $rootInline; + + /** @var bool */ + private $rootExplicit = false; + + private function __construct(string $src) + { + $this->src = $src; + $this->len = strlen($src); + } + + // ----------------------------------------------------------------- + // Node / table helpers + // ----------------------------------------------------------------- + + /** @return array */ + private static function scalarNode(): array + { + return ['kind' => 'scalar', 'inline' => false, 'slot' => null]; + } + + /** @return array */ + private static function compoundNode(string $kind): array + { + return [ + 'kind' => $kind, + 'inline' => false, + 'slot' => null, + 'pairs' => $kind === 'object' ? [] : null, + 'items' => $kind === 'array' ? [] : null, + ]; + } + + /** + * Build the positional tree of a valid document. Returns + * [rootNode, rootIsExplicit]. Any inconsistency (unreachable after + * a successful parse) surfaces as an EditUnsupported envelope at + * the splice layer. + * + * @return array{array, bool} + */ + public static function build(string $src): array + { + $b = new self($src); + try { + return $b->run(); + } catch (KtavException $e) { + // Re-throw as unsupported: the positional pass must never + // produce a parser-looking diagnostic of its own. + throw EditEnvelope::unsupported('source text does not parse: ' . $e->getMessage()); + } + } + + /** @return array{array, bool} */ + private function run(): array + { + $src = $this->src; + $start = substr($src, 0, 3) === "\xEF\xBB\xBF" ? 3 : 0; // § 3.1: one BOM + $i = $start; + $lineNum = 0; + while ($i <= $this->len) { + // Find the line terminator: LF, lone CR or CRLF (one EOL). + $j = $i; + $termLen = 0; + while ($j < $this->len) { + $byte = ord($src[$j]); + if ($byte === 0x0A) { + $termLen = 1; + break; + } + if ($byte === 0x0D) { + $termLen = ($j + 1 < $this->len && ord($src[$j + 1]) === 0x0A) ? 2 : 1; + break; + } + ++$j; + } + ++$lineNum; + $this->handleLine($i, $j); + if ($j >= $this->len) { + break; + } + $i = $j + $termLen; + } + if ($this->collecting !== null || count($this->stack) > 1 + || ($this->rootExplicit && !$this->rootConsumed)) { + // Unclosed compound: unreachable after a successful parse. + throw new \RuntimeException('unclosed compound'); + } + if ($this->rootInline !== null) { + return [$this->rootInline, $this->rootExplicit]; + } + $frame = array_pop($this->stack); + if ($frame === null) { + return [self::compoundNode('object'), false]; // empty document + } + // The ROOT node is the frame's VALUE, not the frame itself: a + // frame carries kind/rangeStart/pendingKey bookkeeping, and + // returning it handed descend a node-shaped array WITHOUT the + // pairs/items fields (the "Undefined array key pairs" class). + $node = $frame['node']; + $node['slot'] = null; + return [$node, $this->rootExplicit]; + } + + // ----------------------------------------------------------------- + // Line dispatch + // ----------------------------------------------------------------- + + /** @return void */ + private function handleLine(int $start, int $end) + { + $src = $this->src; + if ($this->collecting !== null) { + list($ts, $te) = PureScanner::trimBounds($src, $start, $end); + $term = $this->collecting['mode'] === 'v' ? '))' : ')'; + if (substr($src, $ts, $te - $ts) === $term) { + $opener = $this->collectingOpener; + $this->collecting = null; + $node = self::scalarNode(); + $node['slot'] = ['r' => 'block', 'anchor' => null, 'start' => $opener, 'end' => $te]; + $this->attachScalar($node); + } + return; + } + + list($ts, $te) = PureScanner::trimBounds($src, $start, $end); + if ($ts >= $te) { + return; // blank + } + if (substr($src, $ts, 2) === '##') { + return; // comment — never part of the value layout + } + if ($this->rootConsumed) { + throw new \RuntimeException('orphan line after root inline'); + } + + if (!$this->rootInitialized) { + $this->rootInitialized = true; + if ($this->detectRoot($ts, $te)) { + return; + } + } + + if ($this->trimmedIs($ts, $te, '}')) { + $this->closeFrame('object', $ts, $te); + return; + } + if ($this->trimmedIs($ts, $te, ']')) { + $this->closeFrame('array', $ts, $te); + return; + } + + $top = $this->stack[count($this->stack) - 1]; + if ($top['kind'] === 'array') { + $this->handleArrayItem($ts, $te); + } else { + $this->handleObjectPair($ts, $te); + } + } + + /** @return bool true when the root line was consumed here */ + private function detectRoot(int $ts, int $te): bool + { + $b0 = ord($this->src[$ts]); + if ($b0 !== 0x7B && $b0 !== 0x5B) { + // Implicit root: § 5.0.1 — a pair candidate opens an Object, + // otherwise the first item line opens an Array. + list($sep, , $unterm) = PureScanner::findSeparator($this->src, $ts, $te); + $kind = ($sep >= 0 && !$unterm) ? 'object' : 'array'; + $this->stack[] = $this->newFrame($kind, 0); + return false; + } + $kind = $b0 === 0x7B ? 'object' : 'array'; + $closer = $kind === 'object' ? '}' : ']'; + if ($this->trimmedIs($ts, $te, $kind === 'object' ? '{' : '[')) { + // Explicit root: lone opener. + $this->rootExplicit = true; + $this->stack[] = $this->newFrame($kind, $ts); + return true; + } + if ($this->trimmedIs($ts, $te, $kind === 'object' ? '{}' : '[]')) { + // Empty inline root. + $this->rootConsumed = true; + $this->rootInline = self::compoundNode($kind); + return true; + } + if ($this->src[$te - 1] === $closer) { + // Closed inline root — position it and consume the document. + $this->rootConsumed = true; + $this->rootInline = $this->positionInline($ts, $te, $kind); + return true; + } + // Unterminated inline root attempt: unreachable post-parse. + throw new \RuntimeException('unterminated inline root'); + } + + /** @return array */ + private function newFrame(string $kind, int $rangeStart): array + { + return [ + 'kind' => $kind, + 'node' => self::compoundNode($kind), + 'rangeStart' => $rangeStart, + 'pendingKey' => null, // [rawKeyText, keySpanStart, keySpanEnd, anchorColon|null] + ]; + } + + // ----------------------------------------------------------------- + // Object pairs (block context) + // ----------------------------------------------------------------- + + /** @return void */ + private function handleObjectPair(int $ts, int $te) + { + $src = $this->src; + list($sep, $marker, $unterm) = PureScanner::findSeparator($src, $ts, $te); + if ($sep < 0 || $unterm) { + throw new \RuntimeException('pair line without separator'); + } + // Raw key text: [ts, sep) trimmed. + list($ks, $ke) = PureScanner::trimBounds($src, $ts, $sep); + $rawKey = substr($src, $ks, $ke - $ks); + $colon = $sep; + $after = $sep + $marker; + $vs = $after; + while ($vs < $te && ($l = PureScanner::wsLenAt($src, $vs, $te, true)) > 0) { + $vs += $l; + } + $this->dispatchValue($rawKey, $ks, $ke, $colon, $marker === 2, $vs, $te, true); + } + + /** @return void */ + private function handleArrayItem(int $ts, int $te) + { + $src = $this->src; + if (substr($src, $ts, 2) === '::') { + $vs = $ts + 2; + while ($vs < $te && ($l = PureScanner::wsLenAt($src, $vs, $te, true)) > 0) { + $vs += $l; + } + $node = self::scalarNode(); + $node['slot'] = ['r' => 'item', 'start' => $ts, 'body0' => $vs, 'end' => $te]; + $this->pushItem($node); + return; + } + $this->dispatchValue(null, $ts, $te, $ts, false, $ts, $te, false); + } + + /** + * Shared value-shape dispatch for pair values and array items. + * + * @return void + */ + private function dispatchValue(?string $rawKey, int $ks, int $ke, int $colon, bool $raw, int $vs, int $te, bool $isPair) + { + $src = $this->src; + if ($vs >= $te) { + // Empty body: empty String scalar (§ 5.2). + $this->attachValue($rawKey, $ks, $ke, $colon, self::scalarNode(), $vs, $te, 'pair', $isPair); + return; + } + $b0 = ord($src[$vs]); + $closer = $b0 === 0x7B ? '}' : ($b0 === 0x5B ? ']' : ''); + $openKind = $b0 === 0x7B ? 'object' : ($b0 === 0x5B ? 'array' : ''); + $bare = $vs + 1 === $te; + + if ($openKind !== '') { + if ($bare) { + // Open multiline compound `{` / `[`. + $frame = $this->newFrame($openKind, $vs); + if ($isPair) { + $this->setPending($rawKey, $ks, $ke, $colon); + } + $this->stack[] = $frame; + return; + } + if ($src[$te - 1] === $closer && $te - $vs >= 2) { + if ($te - $vs === 2) { + // Empty inline compound `{}` / `[]`. + $node = self::compoundNode($openKind); + if ($openKind === 'object') { + $node['inline'] = false; + } + $this->attachValue($rawKey, $ks, $ke, $colon, $node, $vs, $te, 'pair', $isPair); + return; + } + // Closed inline compound — position the whole subtree. + $node = $this->positionInline($vs, $te, $openKind); + $node['inline'] = false; // container lies on the LINE, kids stay inline + $this->attachValue($rawKey, $ks, $ke, $colon, $node, $vs, $te, 'pair', $isPair); + return; + } + } + if ($b0 === 0x28) { // '(' + $body = substr($src, $vs, $te - $vs); + list($bts, $bte) = PureScanner::trimBounds($src, $vs, $te); + $trimmed = substr($src, $bts, $bte - $bts); + if ($trimmed === '(' || $trimmed === '((') { + // Open multiline string (stripped / verbatim). A pair + // keeps its pending key; an array item is frame-less + // (attachScalar appends to the array frame directly). + if ($isPair) { + $this->setPending($rawKey, $ks, $ke, $colon); + } + $this->collecting = ['mode' => $trimmed === '(' ? 's' : 'v']; + $this->collectingOpener = $bts; + return; + } + if ($trimmed === '()' || $trimmed === '(())') { + // § 5.6 / § 5.6.1 empty multiline forms — empty String. + $this->attachValue($rawKey, $ks, $ke, $colon, self::scalarNode(), $vs, $te, 'pair', $isPair); + return; + } + } + // Plain scalar / raw scalar body. + $node = self::scalarNode(); + $this->attachValue($rawKey, $ks, $ke, $colon, $node, $vs, $te, $raw ? 'pair' : 'pair', $isPair); + } + + /** + * Attach a completed value node to the current frame (pair key or + * array item). $slotRole 'pair' gives the node a Pair slot; + * array items get an Item slot. + * + * @return void + */ + private function attachValue(?string $rawKey, int $ks, int $ke, int $colon, array $node, int $vs, int $te, string $slotRole, bool $isPair) + { + if ($isPair) { + $node['slot'] = ['r' => 'pair', 'colon' => $colon, 'start' => $vs, 'end' => $te]; + $this->insertPair($rawKey, $ks, $ke, $node); + return; + } + $node['slot'] = ['r' => 'item', 'start' => $ks, 'body0' => $vs, 'end' => $te]; + $this->pushItem($node); + } + + /** @return void */ + private function attachScalar(array $node) + { + $n = count($this->stack); + if ($n === 0) { + throw new \RuntimeException('multiline closed without a frame'); + } + $frame = &$this->stack[$n - 1]; + if ($frame['kind'] === 'object') { + $pending = $frame['pendingKey']; + if ($pending === null) { + throw new \RuntimeException('multiline closed without pending key'); + } + $frame['pendingKey'] = null; + $node['slot']['anchor'] = $pending[3]; + $this->insertPair($pending[0], $pending[1], $pending[2], $node); + return; + } + $frame['node']['items'][] = $node; + } + + // ----------------------------------------------------------------- + // Insertion: dotted keys, reopen, noncontiguity + // ----------------------------------------------------------------- + + /** @return void */ + private function setPending(?string $rawKey, int $ks, int $ke, int $colon) + { + $n = count($this->stack); + if ($n === 0 || $this->stack[$n - 1]['kind'] !== 'object') { + throw new \RuntimeException('pending key outside object frame'); + } + $this->stack[$n - 1]['pendingKey'] = [$rawKey, $ks, $ke, $colon]; + } + + /** + * Insert a pair value under the raw (undecoded) dotted key: + * decode segments per § 5.3.2 / § 4, descend creating synthetic + * object nodes (slot=null), mark REOPENED positioned objects + * noncontiguous (slot=null), then place the leaf. + * + * @return void + */ + private function insertPair(?string $rawKey, int $ks, int $ke, array $leaf) + { + $n = count($this->stack); + if ($n === 0) { + throw new \RuntimeException('pair outside object frame'); + } + $raw = $rawKey !== null ? $rawKey : substr($this->src, $ks, $ke - $ks); + $segs = PureScanner::splitKeyPath($this->src, $ks, $ke); + $total = count($segs); + $table = &$this->stack[$n - 1]['node']; + foreach ($segs as $i => $rawSeg) { + list($ss, $se) = PureScanner::trimBounds($rawSeg, 0, strlen($rawSeg)); + $trimmed = $ss >= $se ? '' : substr($rawSeg, $ss, $se - $ss); + $d = PureScanner::decodeKeySegment($trimmed); + $key = isset($d['val']) && is_string($d['val']) ? $d['val'] : $trimmed; + $isLeaf = $i + 1 === $total; + $idx = self::findEntry($table, $key); + if ($isLeaf) { + $child = $leaf; + if ($idx !== null) { + // Post-parse unreachable (duplicate / conflict). + $table['pairs'][$idx][1] = $child; + } else { + $table['pairs'][] = [$key, $child]; + } + return; + } + if ($idx !== null) { + $next = $table['pairs'][$idx][1]; + if ($next['kind'] !== 'object') { + throw new \RuntimeException('blocked path'); + } + // Reopen: the intermediate object is noncontiguous. + // IN PLACE on the stored node — PHP arrays are value + // types, so nulling a COPY ($next['slot']) would leave + // the tree's slot valid and route a noncontiguous whole + // target into the splice + semantic guard instead. + $table['pairs'][$idx][1]['slot'] = null; + $table = &$table['pairs'][$idx][1]; + continue; + } + $synthetic = self::compoundNode('object'); + $synthetic['slot'] = null; + $table['pairs'][] = [$key, $synthetic]; + $table = &$table['pairs'][count($table['pairs']) - 1][1]; + } + } + + /** @return int|null index of the entry with $key */ + private static function findEntry(array $table, string $key): ?int + { + foreach ($table['pairs'] as $i => $pair) { + if ($pair[0] === $key) { + return $i; + } + } + return null; + } + + /** @return void */ + private function pushItem(array $node) + { + $n = count($this->stack); + if ($n === 0 || $this->stack[$n - 1]['kind'] !== 'array') { + throw new \RuntimeException('item outside array frame'); + } + $this->stack[$n - 1]['node']['items'][] = $node; + } + + /** @return void */ + private function closeFrame(string $expected, int $ts, int $te) + { + $n = count($this->stack); + if ($n === 1 && $this->rootExplicit) { + $this->rootConsumed = true; + return; + } + if ($n <= 1) { + throw new \RuntimeException('unbalanced bracket'); + } + $frame = array_pop($this->stack); + if ($frame['kind'] !== $expected) { + throw new \RuntimeException('mismatched closer'); + } + $node = $frame['node']; + // anchor: the colon of the pair that opened this block. + $anchor = null; + $top = $this->stack[count($this->stack) - 1]; + if ($top['kind'] === 'object' && $top['pendingKey'] !== null) { + $anchor = $top['pendingKey'][3]; + } + $node['slot'] = ['r' => 'block', 'anchor' => $anchor, 'start' => $frame['rangeStart'], 'end' => $te]; + if ($top['kind'] === 'object') { + if ($top['pendingKey'] === null) { + throw new \RuntimeException('closed compound without pending key'); + } + $pending = $top['pendingKey']; + $this->stack[count($this->stack) - 1]['pendingKey'] = null; + $this->insertPair($pending[0], $pending[1], $pending[2], $node); + return; + } + $this->stack[count($this->stack) - 1]['node']['items'][] = $node; + } + + // ----------------------------------------------------------------- + // Inline positioning (one PureInlineScan per region) + // ----------------------------------------------------------------- + + /** + * Position a closed inline compound spanning [vs, te): scan once, + * build the node tree from the scan segments (the scanner's kid + * map feeds nested compounds — no region is scanned twice). + * + * @return array node with inline=true, slot=null + */ + private function positionInline(int $vs, int $te, string $kind): array + { + $scan = PureInlineScan::scan($this->src, $kind === 'object' ? 'o' : 'a', $vs + 1, $te); + if ($scan['badEscape'] !== null || $scan['closer'] === null) { + throw new \RuntimeException('inline positioning failed'); + } + return $this->buildInline($kind, $scan['segs'], $scan['kids']); + } + + /** + * Build an inline compound node from scan segments (absolute + * offsets) and the kid map segIndex => ['kind','valueStart', + * 'closer','segs','kids']. + * + * @param array[] $segs + * @param array $kids + * @return array + */ + private function buildInline(string $kind, array $segs, array $kids): array + { + $node = self::compoundNode($kind); + $node['inline'] = true; + if ($kind === 'object') { + foreach ($segs as $j => $seg) { + list($ss, $se) = PureScanner::trimBounds($this->src, $seg[0], $seg[1]); + if ($ss >= $se) { + continue; // trailing comma + } + list($sep, $marker, $unterm) = PureScanner::findSeparator($this->src, $ss, $se); + if ($sep < 0 || $unterm) { + throw new \RuntimeException('inline pair without separator'); + } + list($ks, $ke) = PureScanner::trimBounds($this->src, $ss, $sep); + $rawKey = substr($this->src, $ks, $ke - $ks); + $vs2 = $sep + $marker; + while ($vs2 < $se && ($l = PureScanner::wsLenAt($this->src, $vs2, $se, true)) > 0) { + $vs2 += $l; + } + if ($marker === 2) { + // § 5.8.2 raw inline scalar; leaf is PHYSICALLY inline. + $child = self::scalarNode(); + $child['inline'] = true; + $child['slot'] = ['r' => 'pair', 'colon' => $sep, 'start' => $vs2, 'end' => $se]; + $this->insertInlinePair($node, $rawKey, $child); + continue; + } + if ($vs2 >= $se) { + $child = self::scalarNode(); + $child['inline'] = true; + $child['slot'] = ['r' => 'pair', 'colon' => $sep, 'start' => $vs2, 'end' => $se]; + $this->insertInlinePair($node, $rawKey, $child); + continue; + } + $b = ord($this->src[$vs2]); + if ($b === 0x7B || $b === 0x5B) { + $kid = $kids[$j] ?? null; + if ($kid === null) { + throw new \RuntimeException('nested inline compound missing from scan tree'); + } + $child = $this->buildInline( + $b === 0x7B ? 'object' : 'array', + $kid['segs'], + $kid['kids'] + ); + } else { + $child = self::scalarNode(); + $child['inline'] = true; + } + $child['slot'] = ['r' => 'pair', 'colon' => $sep, 'start' => $vs2, 'end' => $se]; + $this->insertInlinePair($node, $rawKey, $child); + } + return $node; + } + // Inline array: `::` is literal DATA here — the item body is + // the whole trimmed segment. + foreach ($segs as $j => $seg) { + list($ss, $se) = PureScanner::trimBounds($this->src, $seg[0], $seg[1]); + if ($ss >= $se) { + continue; // trailing comma + } + $vs2 = $ss; + $b = ord($this->src[$vs2]); + if ($b === 0x7B || $b === 0x5B) { + $kid = $kids[$j] ?? null; + if ($kid === null) { + throw new \RuntimeException('nested inline compound missing from scan tree'); + } + $child = $this->buildInline( + $b === 0x7B ? 'object' : 'array', + $kid['segs'], + $kid['kids'] + ); + } else { + $child = self::scalarNode(); + $child['inline'] = true; + } + $child['slot'] = ['r' => 'item', 'start' => $ss, 'body0' => $vs2, 'end' => $se]; + $node['items'][] = $child; + } + return $node; + } + + /** + * Insert under an already-decoded inline key (no dotted expansion + * inside inline compounds — § 5.8 keys are single segments; + * post-parse duplicates are unreachable). + * + * @return void + */ + private function insertInlinePair(array &$table, string $rawKey, array $child) + { + $d = PureScanner::decodeKeySegment($rawKey); + $key = isset($d['val']) && is_string($d['val']) ? $d['val'] : $rawKey; + $idx = self::findEntry($table, $key); + if ($idx !== null) { + // Reopen / duplicate: post-parse unreachable; mark the slot + // chain noncontiguous like the Rust descend would. + $table['pairs'][$idx][1]['slot'] = null; + if ($table['pairs'][$idx][1]['kind'] === 'object' && $child['kind'] === 'object') { + $sub = &$table['pairs'][$idx][1]; // in place: arrays copy + foreach ($child['pairs'] as $pair) { + $existing = self::findEntry($sub, $pair[0]); + if ($existing !== null) { + $sub['pairs'][$existing][1] = $pair[1]; + } else { + $sub['pairs'][] = $pair; + } + } + unset($sub); + return; + } + $table['pairs'][$idx][1] = $child; + return; + } + $table['pairs'][] = [$key, $child]; + } + + /** @return bool */ + private function trimmedIs(int $ts, int $te, string $tok): bool + { + return $te - $ts === strlen($tok) && substr($this->src, $ts, $te - $ts) === $tok; + } + + // ----------------------------------------------------------------- + // Shared offset helpers (used by the splice engine too) + // ----------------------------------------------------------------- + + /** Leading ' '/'\t' run of the line containing byte $pos. */ + public static function lineIndent(string $text, int $pos): string + { + $start = 0; + $i = $pos - 1; + while ($i >= 0) { + $b = ord($text[$i]); + if ($b === 0x0A || $b === 0x0D) { + $start = $i + 1; + break; + } + --$i; + } + $end = $start; + $n = strlen($text); + while ($end < $n && (ord($text[$end]) === 0x20 || ord($text[$end]) === 0x09)) { + ++$end; + } + return substr($text, $start, $end - $start); + } + + /** EOL style of the first terminator at/after $pos (fallback look + * behind; no terminators at all — LF). */ + public static function detectEol(string $text, int $pos): string + { + $n = strlen($text); + $i = $pos; + while ($i < $n) { + $b = ord($text[$i]); + if ($b === 0x0A) { + return "\n"; + } + if ($b === 0x0D) { + return ($i + 1 < $n && ord($text[$i + 1]) === 0x0A) ? "\r\n" : "\r"; + } + ++$i; + } + $j = min($pos, $n); + while ($j > 0) { + $b = ord($text[$j - 1]); + if ($b === 0x0A) { + return ($j >= 2 && ord($text[$j - 2]) === 0x0D) ? "\r\n" : "\n"; + } + if ($b === 0x0D) { + return "\r"; + } + --$j; + } + return "\n"; + } + + /** Full physical line containing byte $pos. */ + public static function physicalLine(string $text, int $pos): string + { + $n = strlen($text); + $start = $pos; + while ($start > 0) { + $b = ord($text[$start - 1]); + if ($b === 0x0A || $b === 0x0D) { + break; + } + --$start; + } + $end = $pos; + while ($end < $n) { + $b = ord($text[$end]); + if ($b === 0x0A || $b === 0x0D) { + break; + } + ++$end; + } + return substr($text, $start, $end - $start); + } +} diff --git a/src/Pure/PureParser.php b/src/Pure/PureParser.php index 9a904bc..f03e03d 100644 --- a/src/Pure/PureParser.php +++ b/src/Pure/PureParser.php @@ -79,12 +79,16 @@ public static function parse(string $src, bool $strict, ?array &$events = null, // over continuation bytes / to EOF, like the Rust core). $bad = PureScanner::validateUtf8($src); if ($bad !== null) { - list($line, $lineText) = PureScanner::locate($src, $bad[0]); + list($line, ) = PureScanner::locate($src, $bad[0]); + // § 6.15 (0.9): the line NUMBER travels, but line_text stays + // NULL — the source is not valid UTF-8, so a "line text" + // slice would decode replacement chars over bytes that are + // not characters. PureKtav::failEnvelope([ 'error' => 'InvalidUtf8', 'spec_section' => '§6.15', 'line' => $line, - 'line_text' => $lineText, + 'line_text' => null, 'span' => ['start' => $bad[0], 'end' => $bad[1]], 'body' => 'input is not valid UTF-8: invalid utf-8 sequence of ' . max(1, $bad[1] - $bad[0]) . ' byte(s) from index ' . $bad[0], diff --git a/src/PureBackend.php b/src/PureBackend.php index 05da588..cbe4b01 100644 --- a/src/PureBackend.php +++ b/src/PureBackend.php @@ -94,6 +94,33 @@ public static function canonicalFromSource(string $src): string return Pure\PureKtav::canonicalFromSource($src); } + /** + * Pure source edit: replace an existing value at a textual path + * (see \Ktav\Ktav::editValue for the full contract — same rules, + * same envelopes, no native artifact). The source is spliced by + * byte ranges from a positional tree; every patch is re-parsed and + * semantically verified. + * + * @param mixed $replacement + * @throws KtavException + */ + public static function editValue(string $src, string $path, $replacement): string + { + return Pure\Edit\EditSplice::editValue($src, $path, $replacement); + } + + /** + * Pure source edit with pre-decoded literal segments. + * + * @param string[] $segments + * @param mixed $replacement + * @throws KtavException + */ + public static function editValueSegments(string $src, array $segments, $replacement): string + { + return Pure\Edit\EditSplice::editValueSegments($src, $segments, $replacement); + } + /** * Pure-backend version marker (`pure-0.8.1` style) — never a * native version; the native one stays `Ktav::nativeVersion()`. diff --git a/src/WireJson.php b/src/WireJson.php index b61213d..9f60686 100644 --- a/src/WireJson.php +++ b/src/WireJson.php @@ -47,6 +47,31 @@ public static function encode($value): string ); } + /** + * EDIT-ONLY wire encoding: identical to {@see encode} except that + * an EMPTY `stdClass` is accepted as an explicit empty Object + * (`{}` on the wire) — a PHP `[]` cannot express it and the edit + * contract's emptying_block fixture needs the Object identity. + * dumps/writer semantics are NOT extended: wrap() in dumps mode + * keeps rejecting stdClass, and a NON-empty stdClass (including a + * self-referencing one) is a typed Message transport failure here + * too — never a recursion and never a coerced scalar. + * + * @param mixed $value + * @throws KtavException Message envelope on a non-empty stdClass + */ + public static function encodeEditReplacement($value): string + { + if (is_array($value)) { + ArrayGraph::assertAcyclic($value); + } + $wrapped = self::wrap($value, true); + return json_encode( + $wrapped, + JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE, + ); + } + /** * @param mixed $v * @return mixed @@ -90,9 +115,10 @@ private static function reviveInteger(string $digits) /** * @param mixed $v + * @param bool $editObjects edit-only empty-stdClass Object support * @return mixed */ - private static function wrap($v) + private static function wrap($v, bool $editObjects = false) { if ($v === null || is_bool($v) || is_string($v)) { return $v; @@ -123,10 +149,29 @@ private static function wrap($v) if (is_array($v)) { $out = []; foreach ($v as $k => $child) { - $out[$k] = self::wrap($child); + $out[$k] = self::wrap($child, $editObjects); } return $out; } + if ($editObjects && $v instanceof \stdClass) { + // Empty stdClass only; non-empty is a Message, never recursion. + if ((array) $v !== []) { + throw KtavException::fromEnvelope([ + 'error' => 'Message', + 'reason' => null, + 'line' => null, + 'line_text' => null, + 'span' => null, + 'path' => null, + 'canonical' => null, + 'spec_section' => null, + 'body' => null, + 'message' => 'edit replacement transport: only an EMPTY stdClass' + . ' (an explicit empty Object) is supported', + ]); + } + return $v; // json_encode(new stdClass) === '{}' + } throw new KtavException('unsupported value type for Ktav::dumps: ' . gettype($v)); } } diff --git a/tests/EditNativeSpec.php b/tests/EditNativeSpec.php new file mode 100644 index 0000000..ffd817c --- /dev/null +++ b/tests/EditNativeSpec.php @@ -0,0 +1,338 @@ +getError() !== null + && in_array($e->getError(), ['EditInvalidPath', 'EditNotFound', 'EditUnsupported'], true)) { + return true; + } + return false; + } + }; + + $requireNativeEdit = function () use ($editAbiAvailable) { + if (!$editAbiAvailable()) { + // REQUIRED, never skipped: pin the parent Rust C ABI commit. + throw new \RuntimeException( + 'loaded ktav_cabi does not export the ABI-2 source-edit set' + . ' — pin the parent Rust C ABI commit' + ); + } + }; + + it('replaces a scalar like the pure backend', function () use ($requireNativeEdit) { + $requireNativeEdit(); + expect(Ktav::editValue("port: 8080\nhost: db\n", 'port', 9090)) + ->toBe("port: 9090\nhost: db\n"); + }); + + it('is byte-identical on a no-op', function () use ($requireNativeEdit) { + $requireNativeEdit(); + expect(Ktav::editValue("a: 1_000\n", 'a', 1000))->toBe("a: 1_000\n"); + }); + + it('matches the pure backend on layout expansion', function () use ($requireNativeEdit) { + $requireNativeEdit(); + expect(Ktav::editValue("cfg: {a: 1}\n", 'cfg.a', 'a,b,c,d')) + ->toBe(PureBackend::editValue("cfg: {a: 1}\n", 'cfg.a', 'a,b,c,d')); + expect(Ktav::editValue("cfg: {a: 1, b: 2}\n", 'cfg.a', "line1\nline2")) + ->toBe(PureBackend::editValue("cfg: {a: 1, b: 2}\n", 'cfg.a', "line1\nline2")); + }); + + it('matches the pure backend on marker transitions', function () use ($requireNativeEdit) { + $requireNativeEdit(); + foreach ( + [ + ["k:: 5\n", 'k', 'x'], + ["k:: 5\n", 'k', 9090], + ["k: 5\n", 'k', 'null'], + ["arr: [\n 5\n]\n", 'arr.0', 'null'], + ] as $case + ) { + list($src, $path, $repl) = $case; + expect(Ktav::editValue($src, $path, $repl)) + ->toBe(PureBackend::editValue($src, $path, $repl)); + } + }); + + it('reports the same envelope codes and bodies as pure', function () use ($requireNativeEdit) { + $requireNativeEdit(); + // The core noncontiguous reason, byte-exact on BOTH backends. + $noncontiguous = 'target is a noncontiguous value (dotted-key/reopen intermediate);' + . ' no lossless single-range splice exists'; + $cases = [ + ['a: 1' . "\n", 'a.', 2, 'EditInvalidPath', '2'], + ['a: 1' . "\n", 'b', 2, 'EditNotFound', null], + ["a: {}\na.y: 2\n", 'a', ['z' => 9], 'EditUnsupported', $noncontiguous], + ]; + foreach ($cases as $case) { + list($src, $path, $repl, $code, $body) = $case; + $native = null; + $pure = null; + try { + Ktav::editValue($src, $path, $repl); + } catch (KtavException $e) { + $native = $e; + } + try { + PureBackend::editValue($src, $path, $repl); + } catch (KtavException $e) { + $pure = $e; + } + expect($native)->toBeAnInstanceOf(KtavException::class); + expect($pure)->toBeAnInstanceOf(KtavException::class); + expect($native->getError())->toBe($code); + expect($native->getError())->toBe($pure->getError()); + // Body parity is EXACT, never a substring relaxation. + expect($native->getBody())->toBe($body); + expect($pure->getBody())->toBe($body); + if ($code !== 'EditUnsupported') { + // Codes and null-locations must match everywhere. + expect($native->getErrorLine())->toBe($pure->getErrorLine()); + expect($native->getSpan())->toBe($pure->getSpan()); + } + } + }); + + it('keeps the source-first transport order on the native side too', function () use ($requireNativeEdit) { + $requireNativeEdit(); + // Broken SOURCE wins over any path/segments transport failure: + // the 0.9 parser envelope, not an edit or Message code. Native + // and pure must agree on code AND message text. + $outcome = function (callable $fn): array { + try { + $fn(); + return ['ok', '']; + } catch (KtavException $e) { + return [$e->getError(), $e->getMessage()]; + } + }; + $native = $outcome(function () { + Ktav::editValue("a: {\n", 'a.', 2); + }); + $pure = $outcome(function () { + PureBackend::editValue("a: {\n", 'a.', 2); + }); + expect($native)->toBe($pure); + expect($native[0])->toBe('UnclosedCompound'); + // An empty textual path is a transport failure that must not + // mask the broken source either. + $native = $outcome(function () { + Ktav::editValue("a: {\n", '', 2); + }); + $pure = $outcome(function () { + PureBackend::editValue("a: {\n", '', 2); + }); + expect($native)->toBe($pure); + expect($native[0])->toBe('UnclosedCompound'); + }); + + it('aligns transport failures with pure Message envelopes', function () use ($requireNativeEdit) { + $requireNativeEdit(); + // Non-string segment: native path JSON is ["a",5] -> transport + // decode failure (Message); pure raises the same category. + $native = null; + $pure = null; + try { + Ktav::editValueSegments("a: 1\n", ['a', 5], 2); + } catch (KtavException $e) { + $native = $e; + } + try { + PureBackend::editValueSegments("a: 1\n", ['a', 5], 2); + } catch (KtavException $e) { + $pure = $e; + } + expect($native)->toBeAnInstanceOf(KtavException::class); + expect($pure)->toBeAnInstanceOf(KtavException::class); + expect($native->getError())->toBe('Message'); + expect($native->getError())->toBe($pure->getError()); + // Source-less transport diagnostics never invent a location. + expect($native->getErrorLine())->toBe(null); + expect($native->getSpan())->toBe(null); + // Invalid UTF-8 textual path: native transport -> Message, + // same as pure. + $native = null; + $pure = null; + try { + Ktav::editValue("a: 1\n", "a\xFF", 2); + } catch (KtavException $e) { + $native = $e; + } + try { + PureBackend::editValue("a: 1\n", "a\xFF", 2); + } catch (KtavException $e) { + $pure = $e; + } + expect($native)->toBeAnInstanceOf(KtavException::class); + expect($native->getError())->toBe('Message'); + expect($native->getError())->toBe($pure->getError()); + expect($native->getErrorLine())->toBe(null); + }); + + it('gates EVERY native operation on ABI version 2 at load time', function () use ($requireNativeEdit) { + // Mechanism regression: the shared gate rejects any non-2 ABI + // (an ABI-1 binary) for ALL native calls — not just the edit + // set. Static and artifact-free; runs everywhere. + $caught = null; + try { + NativeLib::assertAbiVersion(1); + } catch (KtavException $e) { + $caught = $e; + } + expect($caught)->toBeAnInstanceOf(KtavException::class); + expect(strpos((string) $caught->getMessage(), 'ABI version mismatch') !== false)->toBe(true); + expect(strpos((string) $caught->getMessage(), 'expected 2, got 1') !== false)->toBe(true); + // The gate accepts exactly the expected version. + NativeLib::assertAbiVersion(NativeLib::EXPECTED_ABI_VERSION); + // Positive: the LOADED library passed the shared gate (any + // successful native operation implies ffi() ran it). + $requireNativeEdit(); + expect(Ktav::nativeVersion())->not->toBe(''); + }); + + it('matches pure on strict wire-ingest negative controls', function () use ($requireNativeEdit) { + $requireNativeEdit(); + // Invalid tagged payloads are Message on BOTH backends — never + // a coerced scalar that could produce a false no-op. + $cases = [ + ["a: 0.0\n", 'a', ['$f' => 'abc']], // the false 0.0 no-op regression + ["a: 0\n", 'a', ['$i' => '12a3']], + ["a: 0.0\n", 'a', ['$f' => '5']], // no '.' / exponent + ]; + foreach ($cases as $case) { + list($src, $path, $repl) = $case; + $outcome = function (callable $fn): string { + try { + $fn(); + return 'ok'; + } catch (KtavException $e) { + return $e->getError(); + } + }; + $native = $outcome(function () use ($src, $path, $repl) { + Ktav::editValue($src, $path, $repl); + }); + $pure = $outcome(function () use ($src, $path, $repl) { + PureBackend::editValue($src, $path, $repl); + }); + expect($native)->toBe('Message'); + expect($native)->toBe($pure); + } + // Canonicalized payloads agree byte-for-byte with pure. + foreach ( + [ + ["a: 1\n", 'a', ['$i' => '+0001'], "a: 1\n"], + ["a: 0\n", 'a', ['$i' => '-0'], "a: 0\n"], + ["a: 1\n", 'a', ['$f' => '2.50'], "a: 2.5\n"], + ] as $case + ) { + list($src, $path, $repl, $expected) = $case; + expect(Ktav::editValue($src, $path, $repl))->toBe($expected); + expect(PureBackend::editValue($src, $path, $repl))->toBe($expected); + } + }); + + it('treats a reordered Object as a byte-identical no-op on both backends', function () use ($requireNativeEdit) { + $requireNativeEdit(); + $src = "k: {a: 1, b: 2}\n"; + expect(Ktav::editValue($src, 'k', ['b' => 2, 'a' => 1]))->toBe($src); + expect(PureBackend::editValue($src, 'k', ['b' => 2, 'a' => 1]))->toBe($src); + // Arrays stay ordered: the reorder is a real change, agreeing + // with pure byte-for-byte. + $reordered = "k: {a: [2, 1]}\n"; + expect(Ktav::editValue("k: {a: [1, 2]}\n", 'k.a', [2, 1]))->toBe($reordered); + expect(PureBackend::editValue("k: {a: [1, 2]}\n", 'k.a', [2, 1]))->toBe($reordered); + }); + + it('supports an explicit EMPTY stdClass Object replacement on both backends', function () use ($requireNativeEdit) { + $requireNativeEdit(); + // Core emptying_block_containers fixture: an EMPTY stdClass is + // the explicit empty Object ([] stays the empty LIST). Both + // backends, both entry shapes. + expect(Ktav::editValue("a: {\n x: 1\n}\n", 'a', new \stdClass())) + ->toBe("a: {\n}\n"); + expect(PureBackend::editValue("a: {\n x: 1\n}\n", 'a', new \stdClass())) + ->toBe("a: {\n}\n"); + // Nested empty Object inside an array (domain-supported). + expect(Ktav::editValue("a: [old]\n", 'a', [new \stdClass()])) + ->toBe("a: [{}]\n"); + expect(PureBackend::editValue("a: [old]\n", 'a', [new \stdClass()])) + ->toBe("a: [{}]\n"); + // Non-empty stdClass: typed Message on BOTH backends, fail + // closed — and a SELF-REFERENCING one must fail without + // recursion (these calls return, never hang). + $nonEmpty = new \stdClass(); + $nonEmpty->q = 1; + $selfRef = new \stdClass(); + $selfRef->self = $selfRef; + foreach ([$nonEmpty, $selfRef] as $bad) { + $native = null; + $pure = null; + try { + Ktav::editValue("a: 1\n", 'a', $bad); + } catch (KtavException $e) { + $native = $e; + } + try { + PureBackend::editValue("a: 1\n", 'a', $bad); + } catch (KtavException $e) { + $pure = $e; + } + expect($native)->toBeAnInstanceOf(KtavException::class); + expect($native->getError())->toBe('Message'); + expect($pure)->toBeAnInstanceOf(KtavException::class); + expect($pure->getError())->toBe('Message'); + } + }); + + it('segments variant carries literal keys without unescaping', function () use ($requireNativeEdit) { + $requireNativeEdit(); + expect(Ktav::editValueSegments('"a.b": 1' . "\n", ['a.b'], 2)) + ->toBe('"a.b": 2' . "\n"); + }); + + it('lets the source fail with the ordinary parser envelope', function () use ($requireNativeEdit) { + $requireNativeEdit(); + $caught = null; + try { + Ktav::editValue("a: {\n", 'a', 2); + } catch (KtavException $e) { + $caught = $e; + } + expect($caught)->toBeAnInstanceOf(KtavException::class); + expect($caught->getError())->toBe('UnclosedCompound'); + }); +}); diff --git a/tests/NativeDebugEmptySpec.php b/tests/NativeDebugEmptySpec.php index 7ad0b23..a26fd72 100644 --- a/tests/NativeDebugEmptySpec.php +++ b/tests/NativeDebugEmptySpec.php @@ -30,13 +30,13 @@ if (!extension_loaded('ffi')) { throw new \RuntimeException('ext-ffi required for the native debug ABI regression'); } - // Provenance gate: NativeLib::ffi() already throws - // KtavException on a version mismatch (src/NativeLib.php:89-96), - // so this call fails the run on a stale binary. - if (Ktav::nativeVersion() !== '0.8.1') { - throw new \RuntimeException( - 'loaded ktav_cabi does not report 0.8.1 — stale artifact' - ); + // Provenance gate: NativeLib::ffi() throws KtavException on any + // non-ABI-2 binary (src/NativeLib.php assertAbiVersion), so the + // first native call below fails the run on a stale ABI-1 + // artifact. With the immutable git-core pin the version STRING + // is informational — only non-emptiness is asserted here. + if (Ktav::nativeVersion() === '') { + throw new \RuntimeException('loaded ktav_cabi reports an empty version'); } }); diff --git a/tests/Pure/Corpus09.php b/tests/Pure/Corpus09.php new file mode 100644 index 0000000..14a93d0 --- /dev/null +++ b/tests/Pure/Corpus09.php @@ -0,0 +1,515 @@ + per directory: .ktav/.json/.canonical.ktav + * under valid/, .ktav/.json under invalid/, parseable-unrepresentable/ + * and strict-lossy/, and ONLY a .json oracle under unrepresentable/; + * - the oracle is the SIBLING .json: invalid carries + * {expected_error, expected_location?} with location + * {line, start, end | minimum_end} (raw-byte offsets, exactly one of + * end / minimum_end); strict-lossy carries lax_value + + * {expected_error=LossyScalar, body, canonical}; parseable- + * unrepresentable and unrepresentable carry value + + * {unrepresentable_reason}; + * - diagnostics09/: exactly 30 eof_* + 42 utf8_* invalid fixtures + * (the 72 new 0.9 diagnostics). ALL of them are executed through + * BOTH backends' raw-bytes entrypoints — UTF-8 validation precedes + * grammar, InvalidUtf8 never decodes line_text. + * + * The corpus is REQUIRED: a missing/stale directory FAILS the run, it + * never skips. Expected oracles come only from the sibling .json — + * nothing is substituted. + */ +final class Corpus09 +{ + /** The closed 0.9 category set with pinned counts. */ + public const CATEGORIES = [ + 'valid' => 228, + 'invalid' => 162, + 'unrepresentable' => 5, + 'parseable-unrepresentable' => 4, + 'strict-lossy' => 13, + ]; + + public const TOTAL = 412; + + /** New diagnostics09 inside invalid: 30 EOF + 42 malformed UTF8. */ + public const DIAGNOSTICS09_EOF = 30; + public const DIAGNOSTICS09_UTF8 = 42; + + public const SCHEMA_VERSION = 1; + public const DEFINED_FLAGS = ['raw_bytes']; + + /** @var array|null */ + private static $manifest; + + // ----------------------------------------------------------------- + // Fail-closed manifest guard + // ----------------------------------------------------------------- + + /** @return array */ + public static function manifest(): array + { + if (self::$manifest !== null) { + return self::$manifest; + } + $path = self::dir() . '/manifest.json'; + if (!is_file($path)) { + throw new \RuntimeException('spec 0.9 corpus manifest missing: ' . $path); + } + $manifest = json_decode((string) file_get_contents($path), true, 512, JSON_THROW_ON_ERROR); + if (!is_array($manifest)) { + throw new \RuntimeException('spec 0.9 corpus manifest is not an object'); + } + if (($manifest['schema_version'] ?? null) !== self::SCHEMA_VERSION) { + throw new \RuntimeException( + 'spec 0.9 corpus manifest schema_version must be exactly ' + . self::SCHEMA_VERSION . ', got ' + . json_encode($manifest['schema_version'] ?? null) + ); + } + $categories = $manifest['categories'] ?? null; + if (!is_array($categories)) { + throw new \RuntimeException('spec 0.9 corpus manifest has no categories map'); + } + foreach (self::CATEGORIES as $name => $count) { + if (($categories[$name]['count'] ?? null) !== $count) { + throw new \RuntimeException( + "spec 0.9 manifest declares $name count " + . json_encode($categories[$name]['count'] ?? null) + . ", expected $count" + ); + } + } + $flags = $manifest['fixture_flags'] ?? null; + if (!is_array($flags) || $flags === []) { + throw new \RuntimeException('spec 0.9 corpus manifest has an empty fixture_flags list'); + } + foreach ($flags as $entry) { + if (!is_array($entry)) { + throw new \RuntimeException('spec 0.9 fixture_flags entry is not an object'); + } + $unknown = array_diff((array) ($entry['flags'] ?? []), self::DEFINED_FLAGS); + if ($unknown !== []) { + throw new \RuntimeException( + 'spec 0.9 fixture carries undefined flags: ' . implode(',', $unknown) + ); + } + $cat = $entry['category'] ?? null; + if (!isset(self::CATEGORIES[$cat])) { + throw new \RuntimeException('spec 0.9 fixture_flags entry has unknown category: ' . json_encode($cat)); + } + $fixture = $entry['fixture'] ?? null; + if (!is_string($fixture) || $fixture === '') { + throw new \RuntimeException('spec 0.9 fixture_flags entry has no fixture path'); + } + $suffix = $cat === 'unrepresentable' ? '.json' : '.ktav'; + if (!is_file(self::dir() . '/' . $cat . '/' . $fixture . $suffix)) { + throw new \RuntimeException( + 'flagged spec 0.9 fixture file missing: ' . $cat . '/' . $fixture . $suffix + ); + } + } + self::$manifest = $manifest; + return $manifest; + } + + /** + * Fail-closed directory check. + */ + public static function dir(): string + { + $dir = TestPaths::spec09(); + if (!is_dir($dir)) { + throw new \RuntimeException( + 'spec 0.9 corpus missing at "' . $dir . '" — set KTAV_SPEC09_DIR' + . ' or pin the spec submodule to a45275688557dc9ecc24a82afb491dd46e615d4d' + ); + } + return $dir; + } + + /** + * Recursive $suffix collector under $dir. For `valid` the derived + * .canonical.ktav companions are excluded (they are + * expectations, not counted fixtures). + * + * @return string[] + */ + private static function collectFiles(string $dir, string $suffix, bool $excludeCanonical): array + { + $len = strlen($suffix); + $out = []; + foreach (scandir($dir) ?: [] as $entry) { + if ($entry === '.' || $entry === '..') { + continue; + } + $path = $dir . '/' . $entry; + if (is_dir($path)) { + foreach (self::collectFiles($path, $suffix, $excludeCanonical) as $f) { + $out[] = $f; + } + continue; + } + if (substr($entry, -$len) !== $suffix) { + continue; + } + if ($excludeCanonical && substr($entry, -15) === '.canonical.ktav') { + continue; + } + $out[] = $path; + } + return $out; + } + + /** + * Validate the closed category/count contract and collect fixture + * STEMS. Category shapes differ (unrepresentable has no .ktav). + * Any deviation throws. + * + * @return array category => sorted .ktav paths + * (unrepresentable => oracle .json paths) + */ + public static function fixtures(): array + { + self::manifest(); + $dir = self::dir(); + $out = []; + $total = 0; + foreach (self::CATEGORIES as $cat => $count) { + $catDir = $dir . '/' . $cat; + if (!is_dir($catDir)) { + throw new \RuntimeException('spec 0.9 corpus category dir missing: ' . $catDir); + } + $suffix = $cat === 'unrepresentable' ? '.json' : '.ktav'; + // Fixtures live in NESTED per-topic subdirectories + // (valid/arrays/…, invalid/diagnostics09/…, …) — a flat + // scandir sees only directories and finds ZERO fixtures + // (CI PR1: "declares 228 valid fixtures, found 0"). Walk + // the tree. + $files = self::collectFiles($catDir, $suffix, $cat === 'valid'); + sort($files); + if (count($files) !== $count) { + throw new \RuntimeException( + "spec 0.9 corpus declares $count $cat fixtures, found " . count($files) + . ' in ' . $catDir . ' — corpus and runner disagree' + ); + } + $out[$cat] = $files; + $total += count($files); + } + if ($total !== self::TOTAL) { + throw new \RuntimeException("spec 0.9 corpus total $total != " . self::TOTAL); + } + self::guardDiagnostics09($out['invalid']); + return $out; + } + + /** + * diagnostics09/: exactly 30 eof_* + 42 utf8_* stems among the + * invalid fixtures — the 72 new 0.9 diagnostics. + * + * @param string[] $invalidKtavPaths + */ + public static function guardDiagnostics09(array $invalidKtavPaths): void + { + $dir = self::dir() . '/invalid/diagnostics09'; + if (!is_dir($dir)) { + throw new \RuntimeException('spec 0.9 diagnostics09 dir missing: ' . $dir); + } + $eof = 0; + $utf8 = 0; + foreach ($invalidKtavPaths as $path) { + if (strpos($path, $dir . '/') !== 0) { + continue; + } + $stem = basename($path, '.ktav'); + if (strpos($stem, 'eof_') === 0) { + ++$eof; + } elseif (strpos($stem, 'utf8_') === 0) { + ++$utf8; + } else { + throw new \RuntimeException( + 'unexpected diagnostics09 stem (not eof_*/utf8_*): ' . $stem + ); + } + } + if ($eof !== self::DIAGNOSTICS09_EOF || $utf8 !== self::DIAGNOSTICS09_UTF8) { + throw new \RuntimeException( + "spec 0.9 diagnostics09 counts eof=$eof utf8=$utf8, expected " + . self::DIAGNOSTICS09_EOF . '/' . self::DIAGNOSTICS09_UTF8 + ); + } + } + + // ----------------------------------------------------------------- + // Sibling .json oracles (the REAL schema — no sidecars, no aliases) + // ----------------------------------------------------------------- + + /** Full oracle map for a fixture path, from its sibling .json. */ + public static function oracle(string $fixturePath): array + { + $dot = strrpos($fixturePath, '.'); + $jsonPath = ($dot === false ? $fixturePath : substr($fixturePath, 0, $dot)) . '.json'; + if (!is_file($jsonPath)) { + throw new \RuntimeException('spec 0.9 fixture oracle missing: ' . $jsonPath); + } + $oracle = json_decode((string) file_get_contents($jsonPath), true); + if (!is_array($oracle)) { + throw new \RuntimeException('spec 0.9 fixture oracle is not an object: ' . $jsonPath); + } + return $oracle; + } + + /** + * Normalized expected location or null: + * ['line'=>int, 'start'=>int, 'end'=>int|null, 'minimumEnd'=>int|null]. + * Validates: line >= 1; start present; EXACTLY ONE of end / + * minimum_end; nonempty covering span (end > start; minimum_end + * >= start). An optional end / minimum_end is NEVER ignored. + * + * @param array $oracle + * @return array|null + */ + public static function expectedLocation(array $oracle): ?array + { + $loc = $oracle['expected_location'] ?? null; + if ($loc === null) { + return null; + } + if (!is_array($loc)) { + throw new \RuntimeException('expected_location is not an object'); + } + $line = $loc['line'] ?? null; + $start = $loc['start'] ?? null; + if (!is_int($line) || $line < 1) { + throw new \RuntimeException('expected_location.line must be an int >= 1'); + } + if (!is_int($start) || $start < 0) { + throw new \RuntimeException('expected_location.start must be an int >= 0'); + } + $hasEnd = array_key_exists('end', $loc) && $loc['end'] !== null; + $hasMin = array_key_exists('minimum_end', $loc) && $loc['minimum_end'] !== null; + if ($hasEnd === $hasMin) { + throw new \RuntimeException( + 'expected_location must carry EXACTLY ONE of end / minimum_end' + ); + } + $out = ['line' => $line, 'start' => $start, 'end' => null, 'minimumEnd' => null]; + if ($hasEnd) { + if (!is_int($loc['end']) || $loc['end'] <= $start) { + throw new \RuntimeException('expected_location.end must be an int > start (nonempty covering span)'); + } + $out['end'] = $loc['end']; + } else { + if (!is_int($loc['minimum_end']) || $loc['minimum_end'] < $start) { + throw new \RuntimeException('expected_location.minimum_end must be an int >= start'); + } + $out['minimumEnd'] = $loc['minimum_end']; + } + return $out; + } + + // ----------------------------------------------------------------- + // Execution + // ----------------------------------------------------------------- + + /** + * Execute one fixture through one backend and assert the oracle. + * + * @param bool $native false = pure + */ + public static function run(string $fixturePath, string $category, bool $native): void + { + switch ($category) { + case 'invalid': + self::runInvalid($fixturePath, $native); + return; + case 'unrepresentable': + // No .ktav input exists (oracle-only category): validate + // the oracle shape. + $o = self::oracle($fixturePath); + if (!is_string($o['unrepresentable_reason'] ?? null) || $o['unrepresentable_reason'] === '') { + throw new \RuntimeException( + 'unrepresentable oracle lacks unrepresentable_reason: ' . $fixturePath + ); + } + return; + case 'parseable-unrepresentable': + $o = self::oracle($fixturePath); + if (!is_string($o['unrepresentable_reason'] ?? null) || $o['unrepresentable_reason'] === '') { + throw new \RuntimeException( + 'parseable-unrepresentable oracle lacks unrepresentable_reason: ' . $fixturePath + ); + } + $raw = (string) file_get_contents($fixturePath); + $this2 = $native + ? self::callLoads($raw, true) + : self::callLoads($raw, false); + if ($this2 !== null) { + throw new \RuntimeException( + "parseable-unrepresentable fixture $fixturePath failed to LAX-parse: " + . $this2->getMessage() + ); + } + return; + case 'strict-lossy': + $o = self::oracle($fixturePath); + $raw = (string) file_get_contents($fixturePath); + // Lax parse must succeed (lax_value in the oracle). + $laxErr = self::callLoads($raw, $native); + if ($laxErr !== null) { + throw new \RuntimeException( + "strict-lossy fixture $fixturePath failed the LAX parse: " + . $laxErr->getMessage() + ); + } + // Strict parse must throw LossyScalar with the oracle + // body/canonical. + $caught = self::callLoadsStrict($raw, $native); + if ($caught === null) { + throw new \RuntimeException( + "strict-lossy fixture $fixturePath did not throw under loadsStrict" + ); + } + if ($caught->getError() !== ($o['expected_error'] ?? 'LossyScalar')) { + throw new \RuntimeException( + "fixture $fixturePath: expected_error " + . ($o['expected_error'] ?? 'LossyScalar') . ', got ' . $caught->getError() + ); + } + if (isset($o['body']) && $caught->getBody() !== $o['body']) { + throw new \RuntimeException( + "fixture $fixturePath: body mismatch — expected " + . json_encode($o['body']) . ', got ' . json_encode($caught->getBody()) + ); + } + if (isset($o['canonical']) && $caught->getCanonical() !== $o['canonical']) { + throw new \RuntimeException( + "fixture $fixturePath: canonical mismatch — expected " + . json_encode($o['canonical']) . ', got ' . json_encode($caught->getCanonical()) + ); + } + return; + case 'valid': + $raw = (string) file_get_contents($fixturePath); + $err = self::callLoads($raw, $native); + if ($err !== null) { + throw new \RuntimeException( + "valid fixture $fixturePath failed to parse: " . $err->getMessage() + ); + } + return; + } + } + + /** + * One invalid fixture through one backend: the error category and + * the expected_location oracle are validated strictly. + */ + private static function runInvalid(string $fixturePath, bool $native): void + { + $oracle = self::oracle($fixturePath); + $expectedError = $oracle['expected_error'] ?? null; + if (!is_string($expectedError) || $expectedError === '') { + throw new \RuntimeException('invalid oracle lacks expected_error: ' . $fixturePath); + } + $raw = (string) file_get_contents($fixturePath); // RAW bytes + $caught = self::callLoads($raw, $native); + if ($caught === null) { + throw new \RuntimeException("invalid fixture $fixturePath parsed without an error"); + } + if ($caught->getError() !== $expectedError) { + throw new \RuntimeException( + "fixture $fixturePath: expected_error $expectedError, got " . $caught->getError() + ); + } + $loc = self::expectedLocation($oracle); + if ($loc !== null) { + if ($caught->getErrorLine() !== $loc['line']) { + throw new \RuntimeException( + "fixture $fixturePath: expected line {$loc['line']}, got " + . var_export($caught->getErrorLine(), true) + ); + } + $span = $caught->getSpan(); + if ($loc['end'] !== null) { + if ($span === null + || $span['start'] !== $loc['start'] + || $span['end'] !== $loc['end']) { + throw new \RuntimeException( + "fixture $fixturePath: span mismatch — expected" + . " [{$loc['start']}, {$loc['end']}), got " . json_encode($span) + ); + } + } else { + // minimum_end: start exact, end at least the minimum. + if ($span === null || $span['start'] !== $loc['start']) { + throw new \RuntimeException( + "fixture $fixturePath: span start mismatch — expected" + . " {$loc['start']}, got " . json_encode($span) + ); + } + if ($span['end'] < $loc['minimumEnd']) { + throw new \RuntimeException( + "fixture $fixturePath: span end {$span['end']} is below" + . " minimum_end {$loc['minimumEnd']}" + ); + } + } + } + // InvalidUtf8 never decodes a line_text over bytes that are not + // valid UTF-8 (no replacement-char fantasy). + if ($caught->getError() === 'InvalidUtf8' && $caught->getLineText() !== null) { + throw new \RuntimeException( + "fixture $fixturePath: InvalidUtf8 must not decode line_text" + ); + } + } + + /** loads() over raw bytes; returns the KtavException or null. */ + private static function callLoads(string $raw, bool $native): ?KtavException + { + try { + if ($native) { + Ktav::loads($raw); // native bytes API (FFI buffer, byte-safe) + } else { + PureBackend::loads($raw); // pure parser over raw bytes + } + return null; + } catch (KtavException $e) { + return $e; + } + } + + /** loadsStrict() over raw bytes; returns the KtavException or null. */ + private static function callLoadsStrict(string $raw, bool $native): ?KtavException + { + try { + if ($native) { + Ktav::loadsStrict($raw); + } else { + PureBackend::loadsStrict($raw); + } + return null; + } catch (KtavException $e) { + return $e; + } + } +} diff --git a/tests/Pure/Corpus09Spec.php b/tests/Pure/Corpus09Spec.php new file mode 100644 index 0000000..387bcbe --- /dev/null +++ b/tests/Pure/Corpus09Spec.php @@ -0,0 +1,69 @@ +toBe(array_keys(Corpus09::CATEGORIES)); + $total = 0; + foreach ($fixtures as $files) { + $total += count($files); + } + expect($total)->toBe(Corpus09::TOTAL); + }); + + $runAll = function (bool $native) { + $backends = Corpus::backends(); + if ($native && !in_array('native', $backends, true)) { + // An explicit pure-only RUN (KTAV_ENABLED_BACKENDS=pure, + // the FFI-absence CI job) opts out by scope; this is a + // declared backend list, not a missing artifact. + skipIf(true, 'native backend not enabled (KTAV_ENABLED_BACKENDS) — declared pure-only run'); + } + if (!$native && !in_array('pure', $backends, true)) { + throw new \RuntimeException( + 'KTAV_ENABLED_BACKENDS must include "pure" for the spec 0.9' + . ' corpus run — got "' . implode(',', $backends) . '"' + ); + } + if ($native) { + // Enabled but missing artifact: FAIL, never skip. + Corpus::requireNative(); + } + foreach (Corpus09::fixtures() as $category => $files) { + foreach ($files as $path) { + Corpus09::run($path, $category, $native); + } + } + }; + + it('executes every fixture through the pure parser over raw bytes', function () use ($runAll) { + $runAll(false); + }); + + it('executes every fixture through the native bytes API', function () use ($runAll) { + $runAll(true); + }); +}); diff --git a/tests/Pure/EditLayoutSpec.php b/tests/Pure/EditLayoutSpec.php new file mode 100644 index 0000000..3dae2cf --- /dev/null +++ b/tests/Pure/EditLayoutSpec.php @@ -0,0 +1,248 @@ += 4 threshold counts escape + * sequences, per the core. + */ +describe('source edit layout — core layout.rs fixtures (22)', function () { + + $edit = function (string $src, string $path, $replacement): string { + return PureBackend::editValue($src, $path, $replacement); + }; + + $editSegs = function (string $src, array $segments, $replacement): string { + return PureBackend::editValueSegments($src, $segments, $replacement); + }; + + /** Textual path from decoded segments (escape \ and .). */ + $textual = function (array $path): string { + $out = []; + foreach ($path as $seg) { + $out[] = str_replace(['\\', '.'], ['\\\\', '\\.'], $seg); + } + return implode('.', $out); + }; + + /** The Rust `check`: textual AND segments variants, exact output. */ + $check = function (string $source, array $path, $replacement, string $expected) use ($edit, $editSegs, $textual) { + expect($edit($source, $textual($path), $replacement))->toBe($expected); + expect($editSegs($source, $path, $replacement))->toBe($expected); + }; + + // 1 ----------------------------------------------------------------- + it('deep_leaf_preserves_each_source_form', function () use ($check) { + foreach ([ + "targets.storagehost.address: old\ntargets.storagehost.user: mcp-agent\n", + "targets: {storagehost: {address: old, user: mcp-agent}}\n", + "targets: {\n storagehost: {\n address: old\n user: mcp-agent\n }\n}\n", + ] as $source) { + $check($source, ['targets', 'storagehost', 'address'], '192.168.0.88:22', + str_replace('old', '192.168.0.88:22', $source)); + } + }); + + // 2 ----------------------------------------------------------------- + it('deep_array_leaf_preserves_mixed_forms', function () use ($check) { + foreach ([ + "targets: [{storagehost: [old, keep]}, other]\n", + "targets: [\n {storagehost: [old, keep]}\n other\n]\n", + "targets: [\n {\n storagehost: [\n old\n keep\n ]\n }\n other\n]\n", + ] as $source) { + $check($source, ['targets', '0', 'storagehost', '0'], 'new', + str_replace('old', 'new', $source)); + } + }); + + // 3 ----------------------------------------------------------------- + it('whole_inline_containers_remain_inline', function () use ($check) { + $check("a: {x: 1}\n", ['a'], ['y' => 2], "a: {y: 2}\n"); + $check("a: [1, 2]\n", ['a'], [3, 4], "a: [3, 4]\n"); + $check("a: {}\n", ['a'], ['y' => 2], "a: {y: 2}\n"); + $check("a: []\n", ['a'], [3, 4], "a: [3, 4]\n"); + $check("a: {}\n", ['a'], [3, 4], "a: [3, 4]\n"); + }); + + // 4 ----------------------------------------------------------------- + it('emptying_block_containers_does_not_collapse_them', function () use ($check) { + // parse("{}") = an EMPTY OBJECT; PHP expresses it as stdClass. + $check("a: {\n x: 1\n}\n", ['a'], new \stdClass(), "a: {\n}\n"); + $check("a: [\n 1\n]\n", ['a'], [], "a: [\n]\n"); + }); + + // 5 ----------------------------------------------------------------- + it('newline_unfolds_only_inline_ancestor_chain', function () use ($check) { + $check( + "## top\na: [{x: old, untouched: [1, 2]}, {keep: yes}]\nz: keep\n", + ['a', '0', 'x'], "one\ntwo", + "## top\na: [\n {\n x: ((\none\ntwo\n ))\n untouched: [1, 2]\n }\n {keep: yes}\n]\nz: keep\n" + ); + }); + + // 6 ----------------------------------------------------------------- + it('newline_under_dotted_key_does_not_expand_dotted_ancestors', function () use ($check) { + $check( + "a.b: [old, keep]\na.c: other\n", + ['a', 'b', '0'], "one\ntwo", + "a.b: [\n ((\none\ntwo\n ))\n keep\n]\na.c: other\n" + ); + }); + + // 7 ----------------------------------------------------------------- + it('dense_escapes_unfold_but_sparse_escapes_do_not', function () use ($check) { + $check("a: [old, keep]\n", ['a', '0'], 'a,b,c,d,e', + "a: [\n a,b,c,d,e\n keep\n]\n"); + $check("a: [old]\n", ['a', '0'], 'a,b,c,d', + "a: [a\\,b\\,c\\,d]\n"); + $sparse = str_repeat('x', 30) . 'a,b,c,d,e'; + $check("a: [old]\n", ['a', '0'], $sparse, + "a: [" . str_replace(',', '\\,', $sparse) . "]\n"); + }); + + // 8 ----------------------------------------------------------------- + it('width_boundary_is_in_characters_not_bytes', function () use ($check) { + foreach ([95 => false, 96 => true] as $size => $expanded) { + $value = str_repeat('я', $size); + $expected = $expanded + ? "a: [\n {$value}\n]\n" + : "a: [{$value}]\n"; + $check("a: [old]\n", ['a', '0'], $value, $expected); + } + }); + + // 9 ----------------------------------------------------------------- + it('already_long_line_only_unfolds_when_it_grows', function () use ($check) { + $source = 'a: [' . str_repeat('x', 120) . ", keep]\n"; + $check($source, ['a', '0'], str_repeat('y', 120), str_replace('x', 'y', $source)); + $check($source, ['a', '0'], str_repeat('y', 121), + "a: [\n " . str_repeat('y', 121) . "\n keep\n]\n"); + }); + + // 10 ---------------------------------------------------------------- + it('width_expansion_stops_when_inner_line_fits', function () use ($check) { + $value = str_repeat('x', 91); + $check("a: [{x: old}]\n", ['a', '0', 'x'], $value, + "a: [\n {x: {$value}}\n]\n"); + }); + + // 11 ---------------------------------------------------------------- + it('escape_overhead_boundary_is_exact', function () use ($check) { + $dense = str_repeat('x', 7) . 'a,b,c,d,e'; + $sparse = str_repeat('x', 8) . 'a,b,c,d,e'; + $check("a: [old]\n", ['a', '0'], $dense, "a: [\n {$dense}\n]\n"); + $check("a: [old]\n", ['a', '0'], $sparse, + "a: [" . str_replace(',', '\\,', $sparse) . "]\n"); + }); + + // 12 ---------------------------------------------------------------- + it('long_changed_leaf_does_not_unfold_unrelated_nested_branches', function () use ($check) { + $value = str_repeat('x', 100); + $check("a: [old, {untouched: [1, 2]}]\n", ['a', '0'], $value, + "a: [\n {$value}\n {untouched: [1, 2]}\n]\n"); + }); + + // 13 ---------------------------------------------------------------- + it('promoted_siblings_keep_values_when_inline_escapes_lose_meaning', function () use ($check) { + $check( + "a: [old, a\\,b, \\u00342, :: literal]\n", + ['a', '0'], "x\ny", + "a: [\n ((\nx\ny\n ))\n a,b\n :: 42\n :: :: literal\n]\n" + ); + }); + + // 14 ---------------------------------------------------------------- + it('promoted_pairs_keep_key_spelling_and_raw_markers', function () use ($check) { + $check( + "a: {\"x.y\": old, nested.key:: 42, n: 0007}\n", + ['a', 'x.y'], "x\ny", + "a: {\n \"x.y\": ((\nx\ny\n ))\n nested.key:: 42\n n: 0007\n}\n" + ); + }); + + // 15 ---------------------------------------------------------------- + it('root_inline_promotion_preserves_bom_and_eof', function () use ($check) { + $check( + "\xEF\xBB\xBF[old, keep]", + ['0'], "x\ny", + "\xEF\xBB\xBF[\n ((\nx\ny\n ))\n keep\n]" + ); + }); + + // 16 ---------------------------------------------------------------- + it('promotion_preserves_eol_comments_and_no_final_terminator', function () use ($check) { + foreach (["\n", "\r", "\r\n"] as $eol) { + $source = "## header{$eol}a: [old, keep]"; + $expected = "## header{$eol}a: [{$eol} (({$eol}x{$eol}y{$eol} )){$eol} keep{$eol}]"; + $check($source, ['a', '0'], "x\ny", $expected); + } + }); + + // 17 ---------------------------------------------------------------- + it('comments_outside_promoted_inline_value_remain_byte_exact', function () use ($check) { + $check( + "list: [\n ## before\n {x: old}\n ## after\n]\n", + ['list', '0', 'x'], "x\ny", + "list: [\n ## before\n {\n x: ((\nx\ny\n ))\n }\n ## after\n]\n" + ); + }); + + // 18 ---------------------------------------------------------------- + it('noop_ignores_all_readability_thresholds', function () use ($check) { + $source = 'a: [' . str_repeat('x', 140) . ", a\\,b\\,c\\,d\\,e]\n"; + $check($source, ['a', '0'], str_repeat('x', 140), $source); + $check($source, ['a', '1'], 'a,b,c,d,e', $source); + }); + + // 19 ---------------------------------------------------------------- + it('type_change_does_not_force_unrelated_layout_changes', function () use ($check) { + $check("a: {x: [1], keep: [2]}\n", ['a', 'x'], true, "a: {x: true, keep: [2]}\n"); + $check("a: [1]\n", ['a'], '42', "a:: 42\n"); + }); + + // 20 ---------------------------------------------------------------- + it('reopened_inline_parent_promotes_only_its_physical_definition', function () use ($check) { + $check( + "a: {x: old}\n## gap\na.y: keep\n", + ['a', 'x'], "x\ny", + "a: {\n x: ((\nx\ny\n ))\n}\n## gap\na.y: keep\n" + ); + }); + + // 21 ---------------------------------------------------------------- + it('impossible_block_encoding_keeps_valid_inline_form', function () use ($check) { + foreach (["a\rb", "a\n))\nb"] as $value) { + $encoded = str_replace(["\r", "\n"], ['\\r', '\\n'], $value); + $check("a: [old]\n", ['a', '0'], $value, "a: [{$encoded}]\n"); + } + $check("a: [old, a\\rb]\n", ['a', '0'], "x\ny", "a: [x\\ny, a\\rb]\n"); + }); + + // 22 ---------------------------------------------------------------- + it('unsafe_comment_loss_still_fails_closed', function () use ($editSegs) { + foreach (["a: {\n ## keep\n x: 1\n}\n", "a: [\n ## keep\n 1\n]\n"] as $source) { + $caught = null; + try { + $editSegs($source, ['a'], null); + } catch (KtavException $e) { + $caught = $e; + } + expect($caught)->toBeAnInstanceOf(KtavException::class); + expect($caught->getError())->toBe('EditUnsupported'); + } + }); +}); diff --git a/tests/Pure/EditSpec.php b/tests/Pure/EditSpec.php new file mode 100644 index 0000000..b968074 --- /dev/null +++ b/tests/Pure/EditSpec.php @@ -0,0 +1,528 @@ +toBeAnInstanceOf(KtavException::class); + expect($caught->getError())->toBe('EditInvalidPath'); + expect($caught->getBody())->toBe((string) $offset); + expect($caught->getErrorLine())->toBe(null); + expect($caught->getSpan())->toBe(null); + expect(strpos($caught->getMessage(), 'invalid edit path') === 0)->toBe(true); + }; + + /** Asserts the given edit error code. */ + $fails = function (callable $fn, string $code) { + $caught = null; + try { + $fn(); + } catch (KtavException $e) { + $caught = $e; + } + expect($caught)->toBeAnInstanceOf(KtavException::class); + expect($caught->getError())->toBe($code); + return $caught; + }; + + // ------------------------------------------------------------- + // Path grammar + // ------------------------------------------------------------- + + it('rejects an empty textual path at offset 0', function () use ($invalidPath) { + $invalidPath('', 0); + }); + + it('rejects leading / trailing / doubled dots at the dot offset', function () use ($invalidPath) { + $invalidPath('.a', 0); + $invalidPath('a.', 2); + $invalidPath('a..b', 2); + }); + + it('rejects unknown escapes and dangling backslashes', function () use ($invalidPath) { + $invalidPath('a\\x', 1); // \x is not \. or \\ + $invalidPath('a\\', 1); // trailing lone backslash + }); + + it('accepts literal dot and backslash escapes', function () use ($edit) { + expect($edit('"a.b": 1' . "\n", 'a\\.b', 2))->toBe('"a.b": 2' . "\n"); + expect($edit('"a\\\\b": 1' . "\n", 'a\\\\b', 2))->toBe('"a\\\\b": 2' . "\n"); + }); + + it('segments variant takes decoded literals with no unescaping', function () use ($editSegs) { + // The segment 'a.b' is ONE literal key (the source spells it + // quoted); no second unescape happens. + expect($editSegs('"a.b": 1' . "\n", ['a.b'], 2))->toBe('"a.b": 2' . "\n"); + // Numeric segment: key in an Object, index in an Array. + expect($editSegs('"0": 1' . "\n", ['0'], 2))->toBe('"0": 2' . "\n"); + expect($editSegs("[\n 1\n 2\n]\n", ['1'], 9))->toBe("[\n 1\n 9\n]\n"); + }); + + it('numeric segments allow leading zeros in arrays but reject signs', function () use ($editSegs, $fails) { + expect($editSegs("[\n 1\n 2\n]\n", ['01'], 9))->toBe("[\n 1\n 9\n]\n"); + $fails(function () use ($editSegs) { + $editSegs("[\n 1\n]\n", ['-1'], 9); + }, 'EditNotFound'); + $fails(function () use ($editSegs) { + $editSegs("[\n 1\n]\n", ['+1'], 9); + }, 'EditNotFound'); + }); + + // ------------------------------------------------------------- + // NotFound + // ------------------------------------------------------------- + + it('rejects an empty segments list as NotFound', function () use ($editSegs, $fails) { + $e = $fails(function () use ($editSegs) { + $editSegs('a: 1' . "\n", [], 2); + }, 'EditNotFound'); + expect($e->getBody())->toBe(null); + expect(strpos($e->getMessage(), 'does not address an existing value') !== false)->toBe(true); + }); + + it('reports missing keys, index overflow and scalar descent as NotFound', function () use ($edit, $fails) { + $fails(function () use ($edit) { + $edit('a: 1' . "\n", 'b', 2); + }, 'EditNotFound'); + $fails(function () use ($edit) { + $edit("[\n 1\n]\n", '5', 2); + }, 'EditNotFound'); + $fails(function () use ($edit) { + $edit('a: 1' . "\n", 'a.b', 2); + }, 'EditNotFound'); + // Index segment into an Object is a KEY lookup, and vice versa. + $fails(function () use ($edit) { + $edit("{\n 0: 1\n}\n", '1', 2); + }, 'EditNotFound'); + }); + + // ------------------------------------------------------------- + // Splice invariants + // ------------------------------------------------------------- + + it('replaces a scalar preserving siblings and the final EOL', function () use ($edit) { + expect($edit("port: 8080\nhost: db\n", 'port', 9090))->toBe("port: 9090\nhost: db\n"); + }); + + it('keeps a missing final EOL', function () use ($edit) { + expect($edit('a: 1', 'a', 2))->toBe('a: 2'); + }); + + it('preserves BOM and CRLF outside the edit', function () use ($edit) { + expect($edit("\xEF\xBB\xBFa: 1\r\nb: 2\r\n", 'a', 3))->toBe("\xEF\xBB\xBFa: 3\r\nb: 2\r\n"); + }); + + it('is byte-identical on a no-op even with noncanonical spelling', function () use ($edit) { + expect($edit("a: 1_000\n", 'a', 1000))->toBe("a: 1_000\n"); + expect($edit("a: 0x10\n", 'a', 16))->toBe("a: 0x10\n"); + }); + + it('preserves the dotted form when editing a dotted leaf', function () use ($edit) { + expect($edit("outer.inner: 1\n", 'outer.inner', 2))->toBe("outer.inner: 2\n"); + }); + + it('preserves comments outside the effective edit', function () use ($edit) { + expect($edit("a: 1\n## note\nb: 2\n", 'a', 3))->toBe("a: 3\n## note\nb: 2\n"); + }); + + it('keeps internal comments on a leaf edit inside a container', function () use ($edit) { + expect($edit("c: {\n ## inner\n x: 1\n}\n", 'c.x', 5)) + ->toBe("c: {\n ## inner\n x: 5\n}\n"); + }); + + it('refuses structural replacement of a comment-carrying container', function () use ($edit, $fails) { + $e = $fails(function () use ($edit) { + $edit("c: {\n ## inner\n x: 1\n}\n", 'c', []); + }, 'EditUnsupported'); + expect(strpos((string) $e->getBody(), 'comment') !== false)->toBe(true); + expect(strpos((string) $e->getMessage(), 'no lossless source edit: ') === 0)->toBe(true); + }); + + it('refuses noncontiguous dotted/reopen whole targets', function () use ($edit, $fails) { + $e = $fails(function () use ($edit) { + $edit("a: {}\na.y: 2\n", 'a', ['z' => 9]); + }, 'EditUnsupported'); + // body = the core reason phrase, verbatim. + expect($e->getBody())->toBe('target is a noncontiguous value (dotted-key/reopen intermediate);' + . ' no lossless single-range splice exists'); + // The editable leaf still works. + expect($edit("a: {}\na.y: 2\n", 'a.y', 7))->toBe("a: {}\na.y: 7\n"); + }); + + it('refuses replacement strings containing CR', function () use ($edit, $fails) { + $fails(function () use ($edit) { + $edit("a: 1\n", 'a', "x\r\ny"); + }, 'EditUnsupported'); + }); + + it('refuses a multiline string holding a line ))', function () use ($edit, $fails) { + $fails(function () use ($edit) { + $edit("a: 1\n", 'a', "x\n))\ny"); + }, 'EditUnsupported'); + }); + + it('lets the source itself fail with the ordinary parser envelope', function () use ($edit, $fails) { + $e = $fails(function () use ($edit) { + $edit("a: {\n", 'a', 2); + }, 'UnclosedCompound'); + expect($e->getErrorLine())->toBe(1); + }); + + // ------------------------------------------------------------- + // Source-first transport order (C ABI parity) + // ------------------------------------------------------------- + + it('reports a broken SOURCE before an invalid textual path', function () use ($edit, $fails) { + // C ABI: UTF-8 validate + parse source FIRST, then transport + // decode. The parser envelope wins over EditInvalidPath. + $fails(function () use ($edit) { + $edit("a: {\n", 'a.', 2); + }, 'UnclosedCompound'); + }); + + it('reports a broken SOURCE before an empty segments list', function () use ($editSegs, $fails) { + $fails(function () use ($editSegs) { + $editSegs("a: {\n", [], 2); + }, 'UnclosedCompound'); + }); + + it('reports a broken SOURCE before a non-string segment', function () use ($editSegs, $fails) { + $fails(function () use ($editSegs) { + $editSegs("a: {\n", ['a', 5], 2); + }, 'UnclosedCompound'); + }); + + it('reports a non-string segment as a source-less Message transport failure', function () use ($editSegs, $fails) { + // Aligned with the native JSON transport (invalid path JSON -> + // Message envelope, no invented source location) — NOT + // EditNotFound. + $e = $fails(function () use ($editSegs) { + $editSegs("a: 1\n", ['a', 5], 2); + }, 'Message'); + expect($e->getErrorLine())->toBe(null); + expect($e->getSpan())->toBe(null); + }); + + it('reports an invalid-UTF-8 textual path as a source-less Message', function () use ($edit, $fails) { + $e = $fails(function () use ($edit) { + $edit("a: 1\n", "a\xFF", 2); + }, 'Message'); + expect($e->getErrorLine())->toBe(null); + expect($e->getSpan())->toBe(null); + }); + + it('reports an unsupported replacement type as a Message transport failure', function () use ($edit, $fails) { + $e = $fails(function () use ($edit) { + $edit("a: 1\n", 'a', fopen('php://memory', 'r')); + }, 'Message'); + expect($e->getErrorLine())->toBe(null); + }); + + it('surfaces a non-JSON-encodable segments transport before any native call', function () { + // Ktav::editValueSegments must never swallow a json_encode + // failure into empty path bytes: the typed transport error is + // raised BEFORE the FFI call, so this holds with or without a + // native artifact (no partial edit is possible). + $caught = null; + try { + \Ktav\Ktav::editValueSegments("a: 1\n", ['a', fopen('php://memory', 'r')], 2); + } catch (\Ktav\KtavException $e) { + $caught = $e; + } + expect($caught)->toBeAnInstanceOf(\Ktav\KtavException::class); + expect($caught->getError())->toBe('Message'); + expect(strpos((string) $caught->getMessage(), 'not JSON-encodable') !== false)->toBe(true); + }); + + // ------------------------------------------------------------- + // Marker transitions + // ------------------------------------------------------------- + + it('preserves an existing raw marker for a compatible string body', function () use ($edit) { + expect($edit("k:: 5\n", 'k', 'x'))->toBe("k:: x\n"); + }); + + it('strips only the second colon for a raw pair turning non-string', function () use ($edit) { + expect($edit("k:: 5\n", 'k', 9090))->toBe("k: 9090\n"); + }); + + it('adds the second colon when a plain pair needs raw provenance', function () use ($edit) { + expect($edit("k: 5\n", 'k', 'null'))->toBe("k:: null\n"); + expect($edit("k: 5\n", 'k', 'true'))->toBe("k:: true\n"); + }); + + it('keeps the original separator whitespace through marker changes', function () use ($edit) { + expect($edit("k: 5\n", 'k', 'null'))->toBe("k:: null\n"); + }); + + it('switches raw markers on array items', function () use ($edit) { + expect($edit("arr: [\n 5\n]\n", 'arr.0', 'null'))->toBe("arr: [\n :: null\n]\n"); + expect($edit("arr: [\n :: null\n]\n", 'arr.0', 5))->toBe("arr: [\n 5\n]\n"); + }); + + it('keeps an empty inline array string item as ()', function () use ($edit) { + expect($edit('cfg: {a: [x]}' . "\n", 'cfg.a.0', '')) + ->toBe('cfg: {a: [()]}' . "\n"); + }); + + // ------------------------------------------------------------- + // Types and forms + // ------------------------------------------------------------- + + it('preserves int vs float and string identity', function () use ($edit) { + expect($edit("a: 1\n", 'a', 2.5))->toBe("a: 2.5\n"); + expect($edit("a: 1\n", 'a', '2'))->toBe("a:: 2\n"); + expect($edit("a: 1.5\n", 'a', 2))->toBe("a: 2\n"); + expect($edit("a: 1\n", 'a', null))->toBe("a: null\n"); + expect($edit("a: 1\n", 'a', true))->toBe("a: true\n"); + }); + + it('refuses a tagged >i64 Integer: the spliced body re-parses as a String', function () use ($edit, $fails) { + // An arbitrary-precision $i payload is written digit-exactly, + // but § 5.2 rule 13 re-classifies an >i64 literal as a String — + // the verification target (String) can never equal the typed + // Integer replacement, so core refuses (fail closed). + $fails(function () use ($edit) { + $edit("a: 1\n", 'a', ['$i' => '123456789012345678901234567890']); + }, 'EditUnsupported'); + }); + + it('replaces a whole inline container inline when it fits', function () use ($edit) { + // A PHP [] follows the nested empty-LIST convention (documented + // like the writer's); an Object identity needs nonempty pairs. + expect($edit("k: {a: 1}\n", 'k', [7]))->toBe("k: [7]\n"); + expect($edit("k: {a: 1}\n", 'k', []))->toBe("k: []\n"); + }); + + it('re-opens an emptied block container (empty list convention)', function () use ($edit) { + expect($edit("k: {\n x: 1\n}\n", 'k', []))->toBe("k: [\n]\n"); + }); + + it('empty stdClass is the explicit empty Object; non-empty fails closed', function () use ($edit, $fails) { + // Core emptying_block_containers: the empty OBJECT identity. + expect($edit("a: {\n x: 1\n}\n", 'a', new \stdClass()))->toBe("a: {\n}\n"); + // Nested empty Object inside an array. + expect($edit("a: [old]\n", 'a', [new \stdClass()]))->toBe("a: [{}]\n"); + // Non-empty — INCLUDING a self-referencing one — is a Message + // before any recursion (these calls return, never hang). + $nonEmpty = new \stdClass(); + $nonEmpty->q = 1; + $fails(function () use ($edit, $nonEmpty) { + $edit("a: 1\n", 'a', $nonEmpty); + }, 'Message'); + $selfRef = new \stdClass(); + $selfRef->self = $selfRef; + $fails(function () use ($edit, $selfRef) { + $edit("a: 1\n", 'a', $selfRef); + }, 'Message'); + }); + + it('renders a non-empty compound replacement as a 4-space block', function () use ($edit) { + expect($edit("k: {\n x: 1\n}\n", 'k', ['y' => 2]))->toBe("k: {\n y: 2\n}\n"); + }); + + it('does not reformat ancestors on a type change', function () use ($edit) { + // Scalar -> compound in a block pair renders the compound body + // like the core's render_compound_item(level 1) + reindent: + // children at target indent + 4, closer at the target indent. + expect($edit("k: {\n x: 1\n y: two\n}\n", 'k.x', ['q' => 1])) + ->toBe("k: {\n x: {\n q: 1\n }\n y: two\n}\n"); + // A type change never touches SIBLING bytes (core + // type_change_does_not_force_unrelated_layout_changes). + expect($edit("a: {x: [1], keep: [2]}\n", 'a.x', true))->toBe("a: {x: true, keep: [2]}\n"); + expect($edit("a: [1]\n", 'a', '42'))->toBe("a:: 42\n"); + }); + + it('replaces a multiline block string with a verbatim block', function () use ($edit) { + expect($edit("k: (\n old\n)\n", 'k', "line1\nline2")) + ->toBe("k: ((\nline1\nline2\n))\n"); + }); + + it('converts a block target scalar to multiline via verbatim block', function () use ($edit) { + expect($edit("a: 1\n", 'a', "l1\nl2"))->toBe("a: ((\nl1\nl2\n))\n"); + }); + + it('routes edge-whitespace strings through the multiline form', function () use ($edit) { + expect($edit("a: 1\n", 'a', ' x '))->toBe("a: ((\n x \n))\n"); + }); + + it('multibyte bodies stay literal under a plain marker', function () use ($edit) { + expect($edit("a: 1\n", 'a', 'привет'))->toBe("a: привет\n"); + }); + + it('handles nested inline leaf edits inside inline compounds', function () use ($edit) { + expect($edit("cfg: {a: {x: 1, y: 2}}\n", 'cfg.a.x', 9)) + ->toBe("cfg: {a: {x: 9, y: 2}}\n"); + expect($edit("cfg: {a: [1, 2]}\n", 'cfg.a.1', 9)) + ->toBe("cfg: {a: [1, 9]}\n"); + }); + + it('verifies semantically and refuses a corrupted patch (fail closed)', function () use ($edit, $fails) { + // A no-op-looking replacement of a duplicated-key source is + // unreachable post-parse; the guard here is the mandatory + // verification path, exercised via the noncontiguous case. + $fails(function () use ($edit) { + $edit("a: {}\na.y: 2\n", 'a.y.x', 1); + }, 'EditNotFound'); + }); + + // ------------------------------------------------------------- + // Exact numeric identity (no lossy PHP float arithmetic) + // ------------------------------------------------------------- + + it('treats negative zero float as a byte-identical no-op', function () use ($edit) { + // -0.0 must survive: ini-independent shortest round-trip digits + // keep the sign of zero; a lossy (string) cast or -0.0 === 0.0 + // would collapse it onto +0 and falsely report a no-op or a + // semantic mismatch. + expect($edit("a: -0.0\n", 'a', -0.0))->toBe("a: -0.0\n"); + }); + + it('distinguishes -0.0 from 0.0 on a real change', function () use ($edit) { + expect($edit("a: 0.0\n", 'a', -0.0))->toBe("a: -0.0\n"); + expect($edit("a: -0.0\n", 'a', 0.0))->toBe("a: 0.0\n"); + }); + + it('keeps in-i64 integers beyond 2^53 exact on a no-op', function () use ($edit) { + // 2^53 + 1 is exactly representable in i64 but NOT in double: + // equality must go through digit strings, never float math. + $src = "a: 9007199254740993\n"; + expect($edit($src, 'a', 9007199254740993))->toBe($src); + expect($edit($src, 'a', ['$i' => '9007199254740993']))->toBe($src); + }); + + it('refuses a tagged big Integer replacing a String-typed overflow literal', function () use ($edit, $fails) { + // An i64-overflow literal parses as a STRING (§ 5.2 rule 13); + // a tagged Integer replacement has a different type, so the + // verification correctly refuses (mirrors the Rust core). + $fails(function () use ($edit) { + $edit("a: 123456789012345678901234567890\n", 'a', ['$i' => '123456789012345678901234567890']); + }, 'EditUnsupported'); + }); + + // ------------------------------------------------------------- + // Inline fallback when no lossless standalone form exists + // ------------------------------------------------------------- + + // ------------------------------------------------------------- + // Strict wire ingest (tagged payloads) — native codec parity + // ------------------------------------------------------------- + + it('rejects a non-integer $i payload as a Message, never a coerced scalar', function () use ($edit, $fails) { + $e = $fails(function () use ($edit) { + $edit("a: 0\n", 'a', ['$i' => '12a3']); + }, 'Message'); + expect($e->getErrorLine())->toBe(null); + }); + + it('rejects a non-decimal $f payload — the false 0.0 no-op is closed', function () use ($edit, $fails) { + // REGRESSION: (float)'abc' collapsed to 0.0 and could report a + // byte-identical no-op against `a: 0.0`. It must be a Message + // transport failure, like the native codec. + $e = $fails(function () use ($edit) { + $edit("a: 0.0\n", 'a', ['$f' => 'abc']); + }, 'Message'); + expect($e->getErrorLine())->toBe(null); + }); + + it('rejects an $f payload without a dot or exponent', function () use ($edit, $fails) { + $fails(function () use ($edit) { + $edit("a: 0.0\n", 'a', ['$f' => '5']); + }, 'Message'); + }); + + it('rejects a non-string tag payload', function () use ($edit, $editSegs, $fails) { + $fails(function () use ($edit) { + $edit("a: 0\n", 'a', ['$i' => 5]); + }, 'Message'); + $fails(function () use ($editSegs) { + $editSegs("a: 0\n", ['a'], ['$f' => null]); + }, 'Message'); + }); + + it('canonicalizes $i like the core: +0001, -0, leading zeros', function () use ($edit) { + // +0001 -> 1, -0 -> 0, 000... -> trimmed: equal values are + // byte-identical no-ops regardless of wire spelling. + expect($edit("a: 1\n", 'a', ['$i' => '+0001']))->toBe("a: 1\n"); + expect($edit("a: 0\n", 'a', ['$i' => '-0']))->toBe("a: 0\n"); + expect($edit("a: 9007199254740993\n", 'a', ['$i' => '0009007199254740993'])) + ->toBe("a: 9007199254740993\n"); + // A real change writes the CANONICAL text, not the wire spelling. + expect($edit("a: 1\n", 'a', ['$i' => '+0002']))->toBe("a: 2\n"); + expect($edit("a: 1\n", 'a', ['$i' => '-007']))->toBe("a: -7\n"); + }); + + it('canonicalizes $f like the core: finite shortest round-trip', function () use ($edit, $fails) { + expect($edit("a: 1\n", 'a', ['$f' => '2.50']))->toBe("a: 2.5\n"); + // Non-finite wire-only payload passes through verbatim — and + // then re-parses as a String, so verification correctly refuses. + $fails(function () use ($edit) { + $edit("a: 1\n", 'a', ['$f' => '1e400']); + }, 'EditUnsupported'); + }); + + it('keeps Object equality order-insensitive (Rust IndexMap PartialEq)', function () use ($edit, $editSegs) { + // Equal object, different key order: BYTE-IDENTICAL no-op. + $src = "k: {a: 1, b: 2}\n"; + expect($edit($src, 'k', ['b' => 2, 'a' => 1]))->toBe($src); + expect($editSegs($src, ['k'], ['b' => 2, 'a' => 1]))->toBe($src); + // Arrays stay ORDERED: a reordered array is a real change. + expect($edit("k: {a: [1, 2]}\n", 'k.a', [2, 1]))->toBe("k: {a: [2, 1]}\n"); + // Nested objects compare order-insensitively at every level. + expect($edit("k: {a: {x: 1, y: 2}, b: 3}\n", 'k', ['b' => 3, 'a' => ['y' => 2, 'x' => 1]])) + ->toBe("k: {a: {x: 1, y: 2}, b: 3}\n"); + }); + + it('splices a reordered object when a value actually differs', function () use ($edit) { + expect($edit("k: {a: 1, b: 2}\n", 'k', ['b' => 3, 'a' => 1])) + ->toBe("k: {b: 3, a: 1}\n"); + }); + + it('keeps the inline candidate when a moved string holds CR', function () use ($edit) { + // requires_block fires (\r present); the moved value decodes to + // a CR-bearing string that has no lossless standalone line, so + // the valid INLINE candidate (with \r still escaped) is kept + // instead of a formatting error. + expect($edit("cfg: {a: 1}\n", 'cfg.a', "p\rq")) + ->toBe("cfg: {a: p\\rq}\n"); + }); +}); diff --git a/tests/SmokeSpec.php b/tests/SmokeSpec.php index 6704c96..f8aea18 100644 --- a/tests/SmokeSpec.php +++ b/tests/SmokeSpec.php @@ -227,9 +227,11 @@ } expect($output)->toContain('service=web port=8080'); - // Native 0.8.1 producer (src/NativeLib.php LIB_VERSION '0.8.1'; - // CI run 39718006238: the example now reports 0.8.1). - expect($output)->toContain('ktav_cabi version: 0.8.1'); + // The example prints the LOADED cabi's own version string. With + // the immutable git-core pin (447a00b) the exact number is no + // longer statically known — compatibility is enforced by the + // ABI-2 gate at load time, so only the FORMAT is pinned here. + expect($output)->toMatch('/ktav_cabi version: \d+\.\d+\.\d+/'); }); }); diff --git a/tests/TestPaths.php b/tests/TestPaths.php index 190b81c..d64d476 100644 --- a/tests/TestPaths.php +++ b/tests/TestPaths.php @@ -51,6 +51,22 @@ public static function specSubmodulePath(): string return self::REPO . '/spec/versions/0.8/tests'; } + /** + * Spec 0.9 corpus root (diagnostics09: 30 EOF + 42 malformed-UTF8 + * new invalid fixtures; total 412 = 228 valid / 162 invalid / + * 5 unrepresentable / 4 parseable-unrepresentable / 13 strict-lossy). + * Parent pins the spec submodule to a45275688557dc9ecc24a82afb491dd46e615d4d. + * `KTAV_SPEC09_DIR` overrides (CI / parent checkouts). + */ + public static function spec09(): string + { + $override = getenv('KTAV_SPEC09_DIR'); + if ($override !== false && $override !== '') { + return rtrim($override, '/\\'); + } + return self::REPO . '/spec/versions/0.9/tests'; + } + public static function cabiBuilt(): bool { return is_file(self::cabi()); diff --git a/tests/ValueCycleChild.php b/tests/ValueCycleChild.php index 2663801..0a327a6 100644 --- a/tests/ValueCycleChild.php +++ b/tests/ValueCycleChild.php @@ -80,7 +80,10 @@ function cycleAliasControl($backend, $operation) $lib = getenv('KTAV_LIB_PATH'); if ($lib === false || $lib === '' || !is_file($lib)) { fwrite(STDERR, "KTAV_LIB_PATH must name an existing native artifact\n"); exit(2); } \Ktav\NativeLoader::setLibraryPath($lib); - if (Ktav::nativeVersion() !== '0.8.1') { throw new RuntimeException('nativeVersion is not 0.8.1'); } + // Version STRING is informational since the git-core pin; the + // ABI-2 gate inside any native call is the real compatibility + // check — a successful call below proves the gate passed. + if (Ktav::nativeVersion() === '') { throw new RuntimeException('nativeVersion is empty'); } } $factories = ['assocCycle', 'listCycle', 'mutualCycle', 'nestedRootCycle']; $operations = ['dumps', 'emitCanonical', 'dumpsForceStrings']; foreach ($factories as $factory) foreach ($operations as $operation) {