From 09c70c7a314a91e3365a9b66cf0f98e78e81c761 Mon Sep 17 00:00:00 2001 From: Marat K Date: Sun, 25 Oct 2026 19:27:49 +0100 Subject: [PATCH 1/6] =?UTF-8?q?feat(edit):=20native=20pure=20path=20API=20?= =?UTF-8?q?=D0=B8=20=D0=B4=D0=B8=D0=B0=D0=B3=D0=BD=D0=BE=D1=81=D1=82=D0=B8?= =?UTF-8?q?=D0=BA=D0=B0=200.9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Добавить lossless source-edit по текстовому пути и literal segments, native ABI2 gate и самостоятельный pure engine с post-splice verification. Проверять source locations по corpus 0.9 и общие Rust layout regressions на обоих backend. Обновить README/changelog EN/RU/ZH, сохранить версии пакетов. Co-Authored-By: Claude Opus 5.5 --- .gitattributes | 3 + CHANGELOG.md | 6 + Cargo.toml | 3 +- README.md | 44 + docs/ru/CHANGELOG.ru.md | 6 + docs/ru/README.ru.md | 44 + docs/zh/CHANGELOG.zh.md | 6 + docs/zh/README.zh.md | 35 + lib/src/main/java/lang/ktav/Ktav.java | 119 ++ .../java/lang/ktav/internal/NativeLib.java | 47 +- .../java/lang/ktav/internal/WireJson.java | 19 + .../main/java/lang/ktav/pure/EditError.java | 55 + .../main/java/lang/ktav/pure/EditPath.java | 89 ++ .../main/java/lang/ktav/pure/EditSplice.java | 1015 +++++++++++++++++ .../main/java/lang/ktav/pure/EditTree.java | 726 ++++++++++++ lib/src/main/java/lang/ktav/pure/Layout.java | 669 +++++++++++ .../main/java/lang/ktav/pure/PureKtav.java | 230 +++- .../main/java/lang/ktav/pure/PureParser.java | 6 +- .../main/java/lang/ktav/pure/SourceError.java | 14 + lib/src/main/java/lang/ktav/pure/Utf8.java | 5 +- lib/src/test/java/lang/ktav/Backend.java | 29 + .../test/java/lang/ktav/CorpusHarness.java | 113 +- .../test/java/lang/ktav/EditValueTest.java | 549 +++++++++ .../java/lang/ktav/NativeAbiVersionTest.java | 32 + lib/src/test/java/lang/ktav/PureApiTest.java | 5 +- .../test/java/lang/ktav/PureNegativeTest.java | 48 +- .../test/java/lang/ktav/PureNoNativeTest.java | 3 +- lib/src/test/java/lang/ktav/TestPaths.java | 2 +- root-docs/CHANGELOG/manifest.js | 1 + root-docs/CHANGELOG/source-edit09/body-1.md | 21 + root-docs/CHANGELOG/source-edit09/meta.js | 7 + root-docs/README/editing/body-1.md | 71 ++ root-docs/README/editing/body-2.md | 52 + root-docs/README/editing/meta.js | 7 + root-docs/README/manifest.js | 2 + root-docs/README/source-edit-status/body-1.md | 9 + root-docs/README/source-edit-status/meta.js | 7 + spec | 2 +- 38 files changed, 4063 insertions(+), 38 deletions(-) create mode 100644 lib/src/main/java/lang/ktav/pure/EditError.java create mode 100644 lib/src/main/java/lang/ktav/pure/EditPath.java create mode 100644 lib/src/main/java/lang/ktav/pure/EditSplice.java create mode 100644 lib/src/main/java/lang/ktav/pure/EditTree.java create mode 100644 lib/src/main/java/lang/ktav/pure/Layout.java create mode 100644 lib/src/test/java/lang/ktav/EditValueTest.java create mode 100644 lib/src/test/java/lang/ktav/NativeAbiVersionTest.java create mode 100644 root-docs/CHANGELOG/source-edit09/body-1.md create mode 100644 root-docs/CHANGELOG/source-edit09/meta.js create mode 100644 root-docs/README/editing/body-1.md create mode 100644 root-docs/README/editing/body-2.md create mode 100644 root-docs/README/editing/meta.js create mode 100644 root-docs/README/source-edit-status/body-1.md create mode 100644 root-docs/README/source-edit-status/meta.js diff --git a/.gitattributes b/.gitattributes index d697e9e..351caf5 100644 --- a/.gitattributes +++ b/.gitattributes @@ -3,6 +3,9 @@ # `git status` from Windows clients. * text=auto eol=lf +# Polydoc требует пустую строку в конце промежуточных source parts. +root-docs/**/body-*.md whitespace=-blank-at-eof + # Windows-only scripts must stay CRLF — cmd.exe refuses to parse .bat # files with LF-only endings. *.bat text eol=crlf diff --git a/CHANGELOG.md b/CHANGELOG.md index b5e6e80..a860c5d 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 — 2026-10-22 diff --git a/Cargo.toml b/Cargo.toml index d24ad5e..aa95f96 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,8 @@ repository = "https://github.com/ktav-lang/java" rust-version = "1.71" [workspace.dependencies] -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 e872195..2598deb 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,50 @@ equals `Ktav.emitCanonical(Ktav.loads(src))`. The stronger condition is deliberate: blank lines are no more part of the `Value` model than comments are, so the canonical writer drops them and `format` does not. +> **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. + +### Editing source text + +`Ktav.editValue()` replaces an **existing** value in Ktav source text +and returns new source text — a lossless, byte-level splice, not a +parse→render round-trip: + +```java +String out = Ktav.editValue("port: 8080\nhost: db\n", "port", + new Value.Int("9090")); +// port: 9090 +// host: db +``` + +Only the target's bytes change. Comments, blank lines, key spelling +and order, the BOM, and the document's LF / CR / CRLF style outside the +effective edit are preserved exactly. Dotted, inline, and block forms +survive as they are written; inline wrappers expand only when the +replacement would hurt readability (a multiline string, line growth +past `max(100 Unicode scalar values, the old physical line length)`, or +a replacement needing many escape sequences), and only the touched +branch expands — untouched nested branches stay inline. A replacement +equal to the current value is a byte-identical no-op, even for +noncanonical scalar spellings. + +Paths are dotted (`key1.key2.0.key4`); inside a segment `\.` is a +literal dot and `\\` a literal backslash. `editValueSegments` takes +already-decoded literal segments instead — a numeric segment is a +string key inside an object and a zero-based index inside an array. +There is no create, remove, or rename: only replacement of an existing +value. + +Errors are structured: `EditInvalidPath` (body: the UTF-8 byte offset +inside the path argument where the defect is), `EditNotFound` (missing +key, out-of-bounds index, descent through a scalar, empty path), and +`EditUnsupported` (body: the reason — e.g. a noncontiguous +dotted-key/reopen target, a structural replacement that would drop +interior comment lines, or a multiline string containing a bare `))` +line). Source-level problems still surface as the ordinary parse +envelopes. The pure backend exposes the same operations as +`PureKtav.editValue` / `PureKtav.editValueSegments` with identical +output. + ### Errors `KtavException` is thrown on any parse or render failure. Beyond a diff --git a/docs/ru/CHANGELOG.ru.md b/docs/ru/CHANGELOG.ru.md index cf899cd..8e60126 100644 --- a/docs/ru/CHANGELOG.ru.md +++ b/docs/ru/CHANGELOG.ru.md @@ -11,6 +11,12 @@ формата 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 — 2026-10-22 diff --git a/docs/ru/README.ru.md b/docs/ru/README.ru.md index f1810b6..7db9c85 100644 --- a/docs/ru/README.ru.md +++ b/docs/ru/README.ru.md @@ -176,6 +176,50 @@ System.out.print(Ktav.format("## the server\nserver: {host: a, port: 80}\n")); пустые строки входят в модель `Value` не больше, чем комментарии, поэтому канонический writer их отбрасывает, а `format` — нет. +> **Ещё не выпущено (подготовка 0.9):** Эти API редактирования и обновлённая диагностика не входят в опубликованные пакеты 0.8.1. Native-backend требует закреплённое ядро 0.9 (C ABI 2 для C-FFI); pure-backend остаётся самостоятельным. + +### Правка исходного текста + +`Ktav.editValue()` заменяет **существующее** значение в Ktav-исходном +тексте и возвращает новый исходный текст — это lossless-сплайс на +уровне байтов, а не круг parse→render: + +```java +String out = Ktav.editValue("port: 8080\nhost: db\n", "port", + new Value.Int("9090")); +// port: 9090 +// host: db +``` + +Меняются только байты цели. Комментарии, пустые строки, написание и +порядок ключей, BOM и стиль LF / CR / CRLF вне эффективной правки +сохраняются дословно. Dotted-, inline- и блочные формы переживают +правку как написано; inline-обёртки раскрываются только тогда, когда +замена ухудшает читаемость (многострочная строка, рост строки сверх +`max(100 Unicode scalar values, прежняя длина строки)`, замена, +требующая множества escape-последовательностей), и раскрывается только +затронутая ветвь — нетронутые вложенные ветви остаются inline. Замена, +равная текущему значению, — byte-identical no-op даже для +неканонических написаний скаляров. + +Пути — через точку (`key1.key2.0.key4`); внутри сегмента `\.` — +literal точка, `\\` — literal backslash. `editValueSegments` принимает +уже декодированные literal-сегменты: числовой сегмент — это ключ +внутри объекта и zero-based индекс внутри массива. Создание, удаление +и переименование не поддерживаются — только замена существующего +значения. + +Ошибки структурированы: `EditInvalidPath` (body: UTF-8 byte offset +внутри аргумента пути, где обнаружен дефект), `EditNotFound` +(отсутствующий ключ, индекс вне диапазона, спуск через скаляр, пустой +путь) и `EditUnsupported` (body: причина — например noncontiguous-цель +из dotted-ключей и reopen, структурная замена, которая потеряла бы +комментарии внутри цели, или многострочная строка со строкой `))`). +Проблемы самого исходника по-прежнему приходят обычными parse +envelope'ами. Pure-бэкенд предоставляет те же операции — +`PureKtav.editValue` / `PureKtav.editValueSegments` — с идентичным +выводом. + ### Ошибки `KtavException` бросается на любой ошибке разбора или рендеринга. Помимо diff --git a/docs/zh/CHANGELOG.zh.md b/docs/zh/CHANGELOG.zh.md index 76864c0..dd96e4b 100644 --- a/docs/zh/CHANGELOG.zh.md +++ b/docs/zh/CHANGELOG.zh.md @@ -10,6 +10,12 @@ MINOR 递增视为破坏性变更。 本 changelog 跟踪 **绑定发布**,不覆盖 Ktav 格式自身的变更 —— 后者见 [`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 — 2026-10-22 diff --git a/docs/zh/README.zh.md b/docs/zh/README.zh.md index bd22464..471bb5d 100644 --- a/docs/zh/README.zh.md +++ b/docs/zh/README.zh.md @@ -168,6 +168,41 @@ System.out.print(Ktav.format("## the server\nserver: {host: a, port: 80}\n")); 与注释一样都不属于 `Value` 模型,所以规范 writer 会丢弃它们,而 `format` 不会。 +> **尚未发布(0.9 准备版本):** 这些编辑 API 和更新后的诊断不包含在已发布的 0.8.1 包中。原生后端需要固定的 0.9 核心(C-FFI 使用 C ABI 2);纯实现仍完全独立。 + +### 编辑源文本 + +`Ktav.editValue()` 替换 Ktav 源文本中**已存在**的值并返回新的源文本 +—— 这是字节级的无损拼接(splice),而不是 parse→render 往返: + +```java +String out = Ktav.editValue("port: 8080\nhost: db\n", "port", + new Value.Int("9090")); +// port: 9090 +// host: db +``` + +只有目标的字节会改变。注释、空行、键的拼写与顺序、BOM 以及有效编辑 +范围之外的 LF / CR / CRLF 行尾风格都原样保留。dotted、inline 和块 +形式按原样保留;仅当替换会损害可读性时(多行字符串、行长超过 +`max(100 个 Unicode 标量值, 原物理行长)`、或需要大量转义序列的替换), +inline 包装才会展开,且只展开受影响的分支——未触及的嵌套分支保持 +inline。与当前值相等的替换是字节完全一致的 no-op,即使标量拼写不 +规范也一样。 + +路径以点分隔(`key1.key2.0.key4`);段内 `\.` 表示字面点号,`\\` +表示字面反斜杠。`editValueSegments` 直接接收已解码的字面段——数字段 +在对象中是字符串键,在数组中是从零开始的索引。不支持创建、删除或重 +命名——只支持替换已存在的值。 + +错误是结构化的:`EditInvalidPath`(body:路径参数中发现缺陷处的 +UTF-8 字节偏移)、`EditNotFound`(缺少键、索引越界、穿过标量下降、 +空路径)和 `EditUnsupported`(body:原因——例如由 dotted 键与 reopen +拼成的非连续目标、会丢失容器内部注释的结构性替换,或包含独立成行的 +`))` 的多行字符串)。源文本本身的问题仍以普通解析错误 envelope 报告。 +纯 Java 后端提供相同操作——`PureKtav.editValue` / +`PureKtav.editValueSegments`,输出完全一致。 + ### 错误 解析或渲染失败时抛出 `KtavException`。除了人类可读的 `getMessage()` diff --git a/lib/src/main/java/lang/ktav/Ktav.java b/lib/src/main/java/lang/ktav/Ktav.java index a86d5dc..241e04f 100644 --- a/lib/src/main/java/lang/ktav/Ktav.java +++ b/lib/src/main/java/lang/ktav/Ktav.java @@ -10,6 +10,7 @@ import lang.ktav.internal.WireJson; import java.nio.charset.StandardCharsets; +import java.util.List; /** * Public facade for the Ktav configuration format. Thin wrapper around @@ -232,6 +233,124 @@ public static String nativeVersion() { return v == null ? "" : v; } + // ------------------------------------------------------------------ + // Lossless source edits (spec 0.9) + // ------------------------------------------------------------------ + + /** + * Replace an existing value addressed by the textual dotted path + * ({@code key1.key2.0.key4}; {@code \.} literal dot, {@code \\} + * literal backslash) with {@code replacement}, splicing only the + * target's bytes. See {@link lang.ktav.pure.PureKtav#editValue} for + * the full contract; the native and pure backends produce identical + * output. + * + * @throws KtavException {@code EditInvalidPath} (body: UTF-8 byte + * offset into the path), {@code EditNotFound}, + * {@code EditUnsupported}, or an ordinary parse envelope + */ + public static String editValue(String src, String path, + Value replacement) { + return editValue(bytes(src, "src"), bytes(path, "path"), + replacement, false); + } + + /** Byte-level variant of {@link #editValue(String, String, Value)}. */ + public static String editValue(byte[] src, String path, + Value replacement) { + return editValue(src, bytes(path, "path"), replacement, false); + } + + /** + * {@link #editValue(String, String, Value)} with pre-decoded literal + * segments (no additional unescaping; numeric segment = object key / + * zero-based ASCII-digit array index). + */ + public static String editValueSegments(String src, + List segments, Value replacement) { + return editValue(bytes(src, "src"), WireJson.encodePath(segments), + replacement, true); + } + + /** Byte-level variant of {@link #editValueSegments(String, List, Value)}. */ + public static String editValueSegments(byte[] src, + List segments, Value replacement) { + return editValue(src, WireJson.encodePath(segments), replacement, + true); + } + + private static byte[] bytes(String s, String arg) { + if (s == null) { + throw new NullPointerException(arg); + } + return s.getBytes(StandardCharsets.UTF_8); + } + + private static String editValue(byte[] src, byte[] path, + Value replacement, boolean segments) { + if (src == null) { + throw new NullPointerException("src"); + } + if (replacement == null) { + throw new NullPointerException("replacement"); + } + byte[] replacementWire = WireJson.encode(replacement); + NativeLib lib = NativeLib.get(); + + try (Memory srcMem = new Memory(Math.max(1, src.length)); + Memory pathMem = new Memory(Math.max(1, path.length)); + Memory replMem = new Memory( + Math.max(1, replacementWire.length))) { + if (src.length > 0) { + srcMem.write(0, src, 0, src.length); + } + if (path.length > 0) { + pathMem.write(0, path, 0, path.length); + } + if (replacementWire.length > 0) { + replMem.write(0, replacementWire, 0, replacementWire.length); + } + + PointerByReference outBuf = new PointerByReference(); + LongByReference outLen = new LongByReference(); + PointerByReference outErr = new PointerByReference(); + LongByReference outErrLen = new LongByReference(); + + int rc; + try { + rc = segments + ? lib.ktav_edit_value_segments(srcMem, src.length, + pathMem, path.length, replMem, + replacementWire.length, outBuf, outLen, + outErr, outErrLen) + : lib.ktav_edit_value(srcMem, src.length, pathMem, + path.length, replMem, replacementWire.length, + outBuf, outLen, outErr, outErrLen); + } catch (UnsatisfiedLinkError | NoSuchMethodError e) { + // An old native binary simply lacks the 0.9 symbols: + // report that clearly instead of silently falling back. + throw new KtavException( + "the loaded ktav_cabi native library does not provide" + + " the source-edit entry points; a 0.9 (or" + + " newer) native library is required", + e); + } + + if (rc != 0) { + byte[] payload = copyAndFreeBytes(lib, outErr.getValue(), + outErrLen.getValue()); + if (payload.length == 0) { + throw new KtavException( + "native call failed with code " + rc); + } + throw KtavException.fromEnvelope(ErrorEnvelope.parse(payload)); + } + + return new String(copyAndFreeBytes(lib, outBuf.getValue(), + outLen.getValue()), StandardCharsets.UTF_8); + } + } + private enum NativeOp { LOADS, LOADS_STRICT, diff --git a/lib/src/main/java/lang/ktav/internal/NativeLib.java b/lib/src/main/java/lang/ktav/internal/NativeLib.java index 7e2b3c4..cf7a837 100644 --- a/lib/src/main/java/lang/ktav/internal/NativeLib.java +++ b/lib/src/main/java/lang/ktav/internal/NativeLib.java @@ -72,16 +72,61 @@ int ktav_format( PointerByReference outErr, LongByReference outErrLen); + int ktav_edit_value( + Pointer src, + long srcLen, + Pointer path, + long pathLen, + Pointer replacement, + long replacementLen, + PointerByReference outBuf, + LongByReference outLen, + PointerByReference outErr, + LongByReference outErrLen); + + int ktav_edit_value_segments( + Pointer src, + long srcLen, + Pointer path, + long pathLen, + Pointer replacement, + long replacementLen, + PointerByReference outBuf, + LongByReference outLen, + PointerByReference outErr, + LongByReference outErrLen); + void ktav_free(Pointer ptr, long len); String ktav_version(); + /** ABI version reported by the loaded native library. */ + int ktav_abi_version(); + + /** + * The ABI version this binding is built against. 0.9 raises it from + * 1 to 2 (the exported set grew by the source-edit symbols); an + * ABI-1 native binary is REJECTED with a clear version-mismatch + * error — never silently used, never hidden behind a pure fallback. + */ + int EXPECTED_ABI_VERSION = 2; + final class Holder { private static final NativeLib INSTANCE = loadSingleton(); private static NativeLib loadSingleton() { String path = NativeLoader.resolve(); - return Native.load(path, NativeLib.class); + NativeLib lib = Native.load(path, NativeLib.class); + int abi = lib.ktav_abi_version(); + if (abi != EXPECTED_ABI_VERSION) { + throw new IllegalStateException( + "ktav_cabi ABI version mismatch: loaded library " + + "reports ABI " + abi + " but this binding" + + " requires ABI " + EXPECTED_ABI_VERSION + + " (source-edit entry points need the 0.9" + + " native library): " + path); + } + return lib; } private Holder() { diff --git a/lib/src/main/java/lang/ktav/internal/WireJson.java b/lib/src/main/java/lang/ktav/internal/WireJson.java index 25ea16b..fd2ceb5 100644 --- a/lib/src/main/java/lang/ktav/internal/WireJson.java +++ b/lib/src/main/java/lang/ktav/internal/WireJson.java @@ -62,6 +62,25 @@ public static byte[] encode(Value v) { return out.toByteArray(); } + /** + * Encode an edit path as the UTF-8 JSON transport array of literal + * (already-decoded) segment strings — the second-argument wire form + * of {@code ktav_edit_value_segments}. + */ + public static byte[] encodePath(List segments) { + ByteArrayOutputStream out = new ByteArrayOutputStream(64); + try (JsonGenerator g = FACTORY.createGenerator(out)) { + g.writeStartArray(); + for (String s : segments) { + g.writeString(s); + } + g.writeEndArray(); + } catch (IOException e) { + throw new KtavException("encode JSON path: " + e.getMessage(), e); + } + return out.toByteArray(); + } + private static Value readValue(JsonParser p, JsonToken t) throws IOException { return switch (t) { case VALUE_NULL -> Value.Null.NULL; diff --git a/lib/src/main/java/lang/ktav/pure/EditError.java b/lib/src/main/java/lang/ktav/pure/EditError.java new file mode 100644 index 0000000..cf14bd6 --- /dev/null +++ b/lib/src/main/java/lang/ktav/pure/EditError.java @@ -0,0 +1,55 @@ +package lang.ktav.pure; + +/** + * Everything that can go wrong with a lossless pure-Java source edit — + * the behavioural twin of the Rust core's {@code ktav::edit::EditError}. + * + *
    + *
  • {@code InvalidPath} — the textual path is malformed; {@code + * offset} is the UTF-8 byte offset within the path argument where + * the problem was detected.
  • + *
  • {@code NotFound} — the path does not address an existing value + * (missing key, bad/out-of-bounds array index, descent through a + * scalar, empty path).
  • + *
  • {@code Unsupported} — no lossless single-range patch exists for + * this (path, replacement) pair.
  • + *
+ */ +final class EditError extends RuntimeException { + private static final long serialVersionUID = 1L; + + enum Code { + INVALID_PATH, + NOT_FOUND, + UNSUPPORTED + } + + final Code code; + /** UTF-8 byte offset into the path argument (InvalidPath only). */ + final int offset; + /** Human-readable limitation description (Unsupported only). */ + final String reason; + + private EditError(Code code, int offset, String reason, String message) { + super(message); + this.code = code; + this.offset = offset; + this.reason = reason; + } + + static EditError invalidPath(int offset) { + return new EditError(Code.INVALID_PATH, offset, null, + "invalid edit path (bad escape or empty segment) at byte " + + offset); + } + + static EditError notFound() { + return new EditError(Code.NOT_FOUND, -1, null, + "edit path does not address an existing value"); + } + + static EditError unsupported(String reason) { + return new EditError(Code.UNSUPPORTED, -1, reason, + "no lossless source edit: " + reason); + } +} diff --git a/lib/src/main/java/lang/ktav/pure/EditPath.java b/lib/src/main/java/lang/ktav/pure/EditPath.java new file mode 100644 index 0000000..ef99415 --- /dev/null +++ b/lib/src/main/java/lang/ktav/pure/EditPath.java @@ -0,0 +1,89 @@ +package lang.ktav.pure; + +import java.util.ArrayList; +import java.util.List; + +/** + * Textual edit-path parsing ({@code key1.key2.0.key4}): the separator + * is {@code .}; inside a segment {@code \.} is a literal dot and + * {@code \\} a literal backslash; any other escape (or a trailing lone + * backslash, or an empty segment) is + * {@link EditError.Code#INVALID_PATH} with the UTF-8 byte offset of the + * defect inside the path argument. Mirrors the Rust core's + * {@code edit::parse_path} byte-for-byte. + */ +final class EditPath { + + private EditPath() { + } + + /** Decode a textual path into literal segments. */ + static List parse(byte[] path) { + if (path.length == 0) { + throw EditError.invalidPath(0); + } + List segments = new ArrayList<>(); + int segStart = 0; + StringBuilder cur = null; // non-null after the first escape + int i = 0; + while (i < path.length) { + byte c = path[i]; + if (c == '.') { + if (segStart == i && cur == null) { + throw EditError.invalidPath(i); + } + pushSegment(segments, cur, path, segStart, i); + cur = null; + segStart = i + 1; + i++; + } else if (c == '\\') { + if (i + 1 < path.length + && (path[i + 1] == '.' || path[i + 1] == '\\')) { + if (cur == null) { + cur = new StringBuilder(Bytes.text(path, segStart, i)); + } + cur.append(path[i + 1] == '.' ? '.' : '\\'); + i += 2; + } else { + throw EditError.invalidPath(i); + } + } else { + int len = utf8Len(c); + if (cur != null) { + cur.append(Bytes.text(path, i, Math.min(i + len, path.length))); + } + i += len; + } + } + if (segStart == path.length && cur == null) { + // Empty last segment (trailing '.'). + throw EditError.invalidPath(path.length); + } + segments.add(cur != null ? cur.toString() + : Bytes.text(path, segStart, path.length)); + return segments; + } + + private static void pushSegment(List segments, StringBuilder cur, + byte[] path, int from, int to) { + segments.add(cur != null ? cur.toString() : Bytes.text(path, from, to)); + } + + /** UTF-8 sequence length by lead byte (1 for ASCII / stray continuation). */ + private static int utf8Len(byte b) { + int c = b & 0xFF; + if (c < 0x80) { + return 1; + } + if (c >= 0xC0 && c <= 0xDF) { + return 2; + } + if (c >= 0xE0 && c <= 0xEF) { + return 3; + } + if (c >= 0xF0 && c <= 0xF7) { + return 4; + } + return 1; + } +} diff --git a/lib/src/main/java/lang/ktav/pure/EditSplice.java b/lib/src/main/java/lang/ktav/pure/EditSplice.java new file mode 100644 index 0000000..ea11ad3 --- /dev/null +++ b/lib/src/main/java/lang/ktav/pure/EditSplice.java @@ -0,0 +1,1015 @@ +package lang.ktav.pure; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; + +import lang.ktav.KtavException; +import lang.ktav.Value; + +/** + * Source-splice engine for the pure-Java source edit — the behavioural + * twin of the Rust core's {@code document/parser/edit/splice.rs} + + * {@code splice/layout.rs}: replace only the target's bytes via the + * positional tree ({@link EditTree}), preserve comments / whitespace / + * key spelling / order / BOM / EOLs outside the effective edit, expand + * inline wrappers only when the layout criteria demand it, and verify + * the patch semantically (exactly the target replaced, rest of the tree + * equal). A matching replacement is a byte-identical no-op. + */ +final class EditSplice { + + private EditSplice() { + } + + // ── Public entry ──────────────────────────────────────────────────── + + /** + * Replace the value addressed by {@code segments} in validated + * source {@code b} (whose grammar begins at {@code bomStart}) with + * {@code replacement}. Returns the patched source text; the input is + * never mutated. + */ + static String apply(byte[] b, int bomStart, List segments, + Value replacement) { + Value originalValue = PureParser.parse(b, bomStart, false); + Value target = lookupValue(originalValue, segments); + if (target == null) { + throw EditError.notFound(); + } + if (target.equals(replacement)) { + return Bytes.text(b, 0, b.length); + } + + EditTree.Result tree = EditTree.build(b); + Descend d = descend(tree.root, segments, tree.rootExplicit); + EditTree.Node node = d.node; + + EditTree.Slot slot = node.slot; + if (slot == null) { + throw EditError.unsupported( + "target is a noncontiguous value (dotted-key/reopen" + + " intermediate); no lossless single-range splice" + + " exists"); + } + + int slotKind = slot.kind; + int a; // anchor of the marker zone + int b0 = slot.b0; + int b1 = slot.b1; + boolean blockItemRole = false; + switch (slotKind) { + case EditTree.Slot.PAIR -> a = slot.anchor; + case EditTree.Slot.ITEM -> a = slot.anchor; + default -> { + a = slot.anchor >= 0 ? slot.anchor : b0; + blockItemRole = slot.anchor < 0; + } + } + if (a > b1 || b0 > b1 || a < 0) { + throw EditError.unsupported("internal: invalid marker/body offsets"); + } + + boolean inlineItem = d.inline && slotKind == EditTree.Slot.ITEM; + boolean existingRaw; + String wsRaw = ""; + String wsPlain = ""; + switch (slotKind) { + case EditTree.Slot.BLOCK -> existingRaw = false; + default -> { + if (inlineItem) { + existingRaw = false; + } else { + existingRaw = b[a + 1] == ':'; + if (existingRaw) { + if (a + 2 > b0) { + throw EditError.unsupported( + "internal: invalid raw marker range"); + } + wsRaw = Bytes.text(b, a + 2, b0); + } + if (slotKind == EditTree.Slot.PAIR) { + if (a + 1 > b0) { + throw EditError.unsupported( + "internal: invalid plain marker range"); + } + wsPlain = Bytes.text(b, a + 1, b0); + } + } + } + } + + // Structural rewrite of a container must not lose interior + // comment lines; multiline scalar blocks are data, not markup. + if (node.kind == EditTree.Node.OBJECT || node.kind == EditTree.Node.ARRAY) { + List ranges = new ArrayList<>(); + collectMultilineScalarRanges(node, ranges); + if (hasCommentLine(b, b0, b1, ranges)) { + throw EditError.unsupported( + "target container contains comment lines; structural" + + " replacement cannot preserve them losslessly"); + } + } + + String indent = lineIndent(b, a); + String eol = detectEol(b, b1); + PlanCtx pc = new PlanCtx(); + pc.slot = slotKind; + pc.roleItemBlock = slotKind == EditTree.Slot.BLOCK && blockItemRole; + pc.existingRaw = existingRaw; + pc.rootFirst = d.rootFirst; + pc.indent = indent; + pc.eol = eol; + pc.inlineCompound = slotKind != EditTree.Slot.BLOCK + && (node.kind == EditTree.Node.OBJECT + || node.kind == EditTree.Node.ARRAY); + pc.blockCompound = slotKind == EditTree.Slot.BLOCK + && (node.kind == EditTree.Node.OBJECT + || node.kind == EditTree.Node.ARRAY); + + String wsPairBlock = (slotKind == EditTree.Slot.BLOCK && !blockItemRole) + ? Bytes.text(b, a + 1, b0) + : ""; + + Plan plan; + if (d.inline) { + if (slotKind == EditTree.Slot.ITEM + && replacement instanceof Value.Str s && s.value().isEmpty()) { + plan = new Plan(b0, "", "", "()"); + } else if (slotKind == EditTree.Slot.ITEM) { + plan = new Plan(b0, "", "", inlineBody(replacement, true)); + } else if (existingRaw && !(replacement instanceof Value.Str)) { + plan = new Plan(a + 1, wsRaw, "", + inlineBody(replacement, false)); + } else { + plan = new Plan(b0, "", "", + inlineBody(replacement, !existingRaw)); + } + } else if (slotKind == EditTree.Slot.BLOCK) { + plan = blockReplacement(replacement, pc, a, b0, wsPairBlock); + } else { + Planned planned = planValue(replacement, pc); + if (existingRaw == planned.desiredRaw) { + String prefix = (b0 == b1 && wsPlain.isEmpty() && wsRaw.isEmpty()) + ? " " : ""; + plan = new Plan(b0, prefix, "", planned.body); + } else if (existingRaw && slotKind == EditTree.Slot.PAIR) { + plan = new Plan(a + 1, sepWs(b0, b1, wsRaw), "", planned.body); + } else if (existingRaw) { + plan = new Plan(a, wsRaw, "", planned.body); + } else if (slotKind == EditTree.Slot.PAIR) { + plan = new Plan(a + 1, ":", sepWs(b0, b1, wsPlain), + planned.body); + } else { + plan = new Plan(a, ":: ", "", planned.body); + } + } + + int from = plan.from; + if (from > b1) { + throw EditError.unsupported("internal: splice range out of bounds"); + } + String result = Bytes.text(b, 0, from) + plan.prefix0 + plan.prefix1 + + plan.body + Bytes.text(b, b1, b.length); + + result = Layout.adjust(b, result, tree.root, segments, replacement, + from, b1, plan.prefix0.length() + plan.prefix1.length() + + plan.body.length()); + + // Obligatory semantic verification: reparse and check that + // exactly the target was replaced. + byte[] rb = result.getBytes(java.nio.charset.StandardCharsets.UTF_8); + Value newDoc; + try { + newDoc = PureParser.parse(rb, bomStart, false); + } catch (SourceError e) { + throw EditError.unsupported( + "patch produced an invalid document: " + e.getMessage()); + } + boolean targetMatches; + try { + Value actual = lookupValue(newDoc, segments); + targetMatches = actual != null && actual.equals(replacement); + } catch (EditError e) { + targetMatches = false; + } + Value expected = nullTargets(originalValue, segments); + Value actual = nullTargets(newDoc, segments); + if (!targetMatches || !actual.equals(expected)) { + throw EditError.unsupported( + "semantic mismatch after splice; no lossless patch" + + " produced"); + } + return result; + } + + private static final class Plan { + final int from; + final String prefix0; + final String prefix1; + final String body; + + Plan(int from, String prefix0, String prefix1, String body) { + this.from = from; + this.prefix0 = prefix0; + this.prefix1 = prefix1; + this.body = body; + } + } + + private static final class PlanCtx { + int slot; + boolean roleItemBlock; + boolean existingRaw; + boolean rootFirst; + String indent; + String eol; + boolean inlineCompound; + boolean blockCompound; + } + + private static final class Planned { + final String body; + final boolean desiredRaw; + + Planned(String body, boolean desiredRaw) { + this.body = body; + this.desiredRaw = desiredRaw; + } + } + + // ── Path helpers ──────────────────────────────────────────────────── + + static final class Descend { + final EditTree.Node node; + final boolean inline; + final boolean rootFirst; + + Descend(EditTree.Node node, boolean inline, boolean rootFirst) { + this.node = node; + this.inline = inline; + this.rootFirst = rootFirst; + } + } + + private static Descend descend(EditTree.Node root, List segments, + boolean rootExplicit) { + boolean containerIsRoot = true; + EditTree.Node cur = root; + boolean rootFirst = false; + for (String seg : segments) { + if (cur.kind == EditTree.Node.OBJECT) { + rootFirst = false; + cur = cur.child(seg); + if (cur == null) { + throw EditError.notFound(); + } + } else if (cur.kind == EditTree.Node.ARRAY) { + int idx = parseIndex(seg); + rootFirst = containerIsRoot && !rootExplicit && idx == 0; + cur = cur.item(idx); + if (cur == null) { + throw EditError.notFound(); + } + } else { + throw EditError.notFound(); + } + containerIsRoot = false; + } + return new Descend(cur, cur.inline, rootFirst); + } + + static Value lookupValue(Value v, List segments) { + Value cur = v; + for (String seg : segments) { + if (cur instanceof Value.Obj o) { + cur = o.entries().get(seg); + } else if (cur instanceof Value.Arr arr) { + int idx = parseIndexOrNull(seg); + cur = idx < 0 ? null + : (idx < arr.items().size() ? arr.items().get(idx) + : null); + } else { + return null; + } + if (cur == null) { + return null; + } + } + return cur; + } + + /** Copy of {@code v} with the value at {@code segments} replaced by null. */ + static Value nullTargets(Value v, List segments) { + if (segments.isEmpty()) { + return v; + } + String head = segments.get(0); + List rest = segments.subList(1, segments.size()); + if (v instanceof Value.Obj o) { + LinkedHashMap out = new LinkedHashMap<>(); + o.entries().forEach((k, val) -> out.put(k, + k.equals(head) ? nullTargets(val, rest) : val)); + return new Value.Obj(out); + } + if (v instanceof Value.Arr arr) { + int idx = parseIndexOrNull(head); + List out = new ArrayList<>(arr.items().size()); + for (int i = 0; i < arr.items().size(); i++) { + out.add(i == idx ? nullTargets(arr.items().get(i), rest) + : arr.items().get(i)); + } + return new Value.Arr(out); + } + return v; + } + + /** ASCII digits only; leading zeros allowed, signs are not indexes. */ + static int parseIndex(String seg) { + int idx = parseIndexOrNull(seg); + if (idx < 0) { + throw EditError.notFound(); + } + return idx; + } + + private static int parseIndexOrNull(String seg) { + if (seg.isEmpty()) { + return -1; + } + long acc = 0; + for (int i = 0; i < seg.length(); i++) { + char c = seg.charAt(i); + if (c < '0' || c > '9') { + return -1; + } + acc = acc * 10 + (c - '0'); + if (acc > Integer.MAX_VALUE) { + return -1; // out of bounds anyway + } + } + return (int) acc; + } + + // ── Comment detection ─────────────────────────────────────────────── + + private static void collectMultilineScalarRanges(EditTree.Node node, + List out) { + if (node.kind == EditTree.Node.SCALAR && node.slot != null + && node.slot.kind == EditTree.Slot.BLOCK) { + out.add(new int[] { node.slot.b0, node.slot.b1 }); + return; + } + if (node.kind == EditTree.Node.OBJECT) { + for (EditTree.Node child : node.entries.values()) { + collectMultilineScalarRanges(child, out); + } + } else if (node.kind == EditTree.Node.ARRAY) { + for (EditTree.Node child : node.items) { + collectMultilineScalarRanges(child, out); + } + } + } + + /** Any line in [from, to) starting (after § 3.3 ws) with '##' outside skip ranges. */ + private static boolean hasCommentLine(byte[] b, int from, int to, + List skip) { + skip.sort((x, y) -> Integer.compare(x[0], y[0])); + int skipI = 0; + int i = from; + while (i < to) { + int j = i; + while (j < to && b[j] != '\n' && b[j] != '\r') { + j++; + } + int[] t = Bytes.trim(b, i, j); + while (skipI < skip.size() && skip.get(skipI)[1] <= t[0]) { + skipI++; + } + boolean inData = false; + if (skipI < skip.size() && skip.get(skipI)[0] <= t[0] + && t[0] < skip.get(skipI)[1]) { + inData = true; + } + if (!inData && t[1] - t[0] >= 2 && b[t[0]] == '#' + && b[t[0] + 1] == '#') { + return true; + } + i = j; + while (i < to && (b[i] == '\n' || b[i] == '\r')) { + i++; + } + } + return false; + } + + // ── Body encoding ─────────────────────────────────────────────────── + + private static String scalarBody(Value v) { + if (v == Value.Null.NULL) { + return "null"; + } + if (v instanceof Value.Bool bo) { + return bo.value() ? "true" : "false"; + } + if (v instanceof Value.Int i) { + return i.text(); + } + if (v instanceof Value.Flt f) { + return f.text(); + } + throw EditError.unsupported( + "internal: scalar_body called on a compound value"); + } + + private static String inlineBody(Value replacement, boolean forceString) { + if (replacement instanceof Value.Str s) { + return encodeInline(s.value(), forceString && needsRawMarker(s.value())); + } + if (replacement instanceof Value.Obj || replacement instanceof Value.Arr) { + StringBuilder out = new StringBuilder(); + pushInlineValue(replacement, 0, out); + return out.toString(); + } + return scalarBody(replacement); + } + + static String encodeInline(String s, boolean forceString) { + StringBuilder out = new StringBuilder(s.length() + 8); + pushInlineString(s, forceString, out); + return out.toString(); + } + + private static void pushInlineString(String s, boolean forceString, + StringBuilder out) { + int last = s.isEmpty() ? 0 : lastCodePointStart(s); + int i = 0; + boolean first = true; + while (i < s.length()) { + int cp = s.codePointAt(i); + if (forceString && first) { + out.append(String.format("\\u%04X", cp)); + } else { + encodeInlineChar(out, cp, first || i == last); + } + first = false; + i += Character.charCount(cp); + } + } + + private static int lastCodePointStart(String s) { + int i = s.length() - 1; + while (i > 0 && Character.isLowSurrogate(s.charAt(i))) { + i--; + } + return i; + } + + private static void encodeInlineChar(StringBuilder out, int cp, + boolean edge) { + switch (cp) { + case '\\' -> out.append("\\\\"); + case ',' -> out.append("\\,"); + case '}' -> out.append("\\}"); + case ']' -> out.append("\\]"); + case '{' -> out.append("\\{"); + case '[' -> out.append("\\["); + case '\n' -> out.append("\\n"); + case '\r' -> out.append("\\r"); + default -> { + if (edge && isWsCp(cp)) { + out.append(String.format("\\u%04X", cp)); + } else { + out.appendCodePoint(cp); + } + } + } + } + + private static boolean isWsCp(int cp) { + switch (cp) { + case 0x09: case 0x0B: case 0x0C: case 0x20: case 0x85: + case 0xA0: case 0x1680: case 0x2028: case 0x2029: + case 0x202F: case 0x205F: case 0x3000: + return true; + default: + return cp >= 0x2000 && cp <= 0x200A; + } + } + + static void appendInlineValue(Value v, StringBuilder out) { + pushInlineValue(v, 0, out); + } + + private static void pushInlineValue(Value v, int depth, StringBuilder out) { + if (depth > PureParser.MAX_DEPTH) { + throw EditError.unsupported( + "replacement nesting exceeds inline depth limit (" + + PureParser.MAX_DEPTH + ")"); + } + if (v instanceof Value.Obj o) { + out.append('{'); + boolean first = true; + for (var e : o.entries().entrySet()) { + if (!first) { + out.append(", "); + } + first = false; + out.append(encodeKeySegment(e.getKey())); + out.append(": "); + if (e.getValue() instanceof Value.Str s) { + pushInlineString(s.value(), needsRawMarker(s.value()), out); + } else { + pushInlineValue(e.getValue(), depth + 1, out); + } + } + out.append('}'); + } else if (v instanceof Value.Arr arr) { + out.append('['); + boolean first = true; + for (Value item : arr.items()) { + if (!first) { + out.append(", "); + } + first = false; + if (item instanceof Value.Str s && s.value().isEmpty()) { + out.append("()"); + } else if (item instanceof Value.Str s) { + pushInlineString(s.value(), needsRawMarker(s.value()), out); + } else { + pushInlineValue(item, depth + 1, out); + } + } + out.append(']'); + } else if (v instanceof Value.Str s) { + pushInlineString(s.value(), needsRawMarker(s.value()), out); + } else { + out.append(scalarBody(v)); + } + } + + /** § 5.9.10 key emission for inline compounds: bare, else quoted+escaped. */ + private static String encodeKeySegment(String key) { + StringBuilder out = new StringBuilder(); + pushEscapedKeySegment(key, false, out); + return out.toString(); + } + + private static void pushEscapedKeySegment(String key, boolean rootFirst, + StringBuilder out) { + if (isBareKeyCandidate(key, rootFirst)) { + out.append(key); + return; + } + // Quoted form with re-escaping; double quotes preferred. + out.append('"'); + for (int i = 0; i < key.length(); ) { + int cp = key.codePointAt(i); + if (cp == '"' || cp == '\\') { + out.append('\\').appendCodePoint(cp); + } else if (cp == '\n') { + out.append("\\n"); + } else if (cp == '\r') { + out.append("\\r"); + } else if (cp == '\t') { + out.append("\\t"); + } else { + out.appendCodePoint(cp); + } + i += Character.charCount(cp); + } + out.append('"'); + } + + /** + * A bare key is non-empty § 4 key text: no whitespace edges, no + * structural characters, no dotted/quoted look-alikes. The full + * parser is the oracle; this predicate is conservative — anything + * questionable is quoted. + */ + private static boolean isBareKeyCandidate(String key, boolean rootFirst) { + if (key.isEmpty()) { + return false; + } + for (int i = 0; i < key.length(); ) { + int cp = key.codePointAt(i); + if (isWsCp(cp) || Character.isISOControl(cp)) { + return false; + } + switch (cp) { + case ':': case '.': case ',': case '{': case '}': + case '[': case ']': case '(': case ')': case '"': + case '\'': case '`': case '\\': case '#': + return false; + default: + i += Character.charCount(cp); + } + } + // Leading BOM cannot appear bare (root-dispatch protection). + return key.codePointAt(0) != 0xFEFF; + } + + // ── Planning ──────────────────────────────────────────────────────── + + private static String sepWs(int b0, int b1, String ws) { + return (b0 == b1 && ws.isEmpty()) ? " " : ws; + } + + private static Planned planValue(Value replacement, PlanCtx pc) { + if (replacement instanceof Value.Str s) { + return planString(s.value(), pc); + } + if (replacement instanceof Value.Obj || replacement instanceof Value.Arr) { + if (pc.inlineCompound) { + StringBuilder out = new StringBuilder(); + pushInlineValue(replacement, 0, out); + return new Planned(out.toString(), false); + } + if (replacement instanceof Value.Obj o && o.entries().isEmpty()) { + return new Planned(pc.blockCompound + ? "{" + pc.eol + pc.indent + "}" : "{}", false); + } + if (replacement instanceof Value.Arr a && a.items().isEmpty()) { + return new Planned(pc.blockCompound + ? "[" + pc.eol + pc.indent + "]" : "[]", false); + } + String rendered; + try { + rendered = PureWriter.dumps(replacement); + } catch (KtavException e) { + throw EditError.unsupported( + "replacement is not representable: " + e.getMessage()); + } + return new Planned(reindent(rendered, pc.indent, pc.eol), false); + } + return new Planned(scalarBody(replacement), false); + } + + private static Planned planString(String s, PlanCtx pc) { + if (s.indexOf('\r') >= 0) { + throw EditError.unsupported( + "replacement string contains CR (U+000D)"); + } + boolean edgeWs = (!s.isEmpty() && isWsCp(s.codePointAt(0))) + || (!s.isEmpty() && isWsCp(s.codePointBefore(s.length()))); + if (s.indexOf('\n') >= 0 || edgeWs) { + return new Planned(verbatimBlockBody(s, pc.indent, pc.eol), false); + } + boolean desiredRaw; + if (pc.slot == EditTree.Slot.ITEM) { + desiredRaw = itemNeedsRawMarker(s) || s.isEmpty(); + } else { + desiredRaw = needsRawMarker(s); + } + if (pc.slot == EditTree.Slot.ITEM && pc.rootFirst) { + desiredRaw = desiredRaw || bareItemIsPairCandidate(s) + || s.startsWith("\uFEFF"); + } + if (pc.existingRaw) { + desiredRaw = true; + } + return new Planned(s, desiredRaw); + } + + private static String verbatimBlockBody(String s, String indent, + String eol) { + for (String line : s.split("\n", -1)) { + if (trim(line).equals("))")) { + throw EditError.unsupported( + "multiline string contains a line equal to '))';" + + " no lossless verbatim form exists"); + } + } + StringBuilder out = new StringBuilder(s.length() + 16); + out.append("((").append(eol); + boolean first = true; + for (String line : s.split("\n", -1)) { + if (!first) { + out.append(eol); + } + first = false; + out.append(line); + } + out.append(eol).append(indent).append("))"); + return out.toString(); + } + + private static String trim(String s) { + int b = 0; + int e = s.length(); + while (b < e && isWsCp(s.codePointAt(b))) { + b += Character.charCount(s.codePointAt(b)); + } + while (e > b && isWsCp(s.codePointBefore(e))) { + e = s.offsetByCodePoints(e, -1); + } + return s.substring(b, e); + } + + private static Plan blockReplacement(Value replacement, PlanCtx pc, + int a, int b0, String wsPair) { + if (replacement instanceof Value.Str s) { + String sv = s.value(); + if (sv.indexOf('\r') >= 0) { + throw EditError.unsupported( + "replacement string contains CR (U+000D)"); + } + boolean multiline = sv.indexOf('\n') >= 0 + || (!sv.isEmpty() && isWsCp(sv.codePointAt(0))) + || (!sv.isEmpty() && isWsCp(sv.codePointBefore(sv.length()))); + if (multiline) { + return new Plan(b0, "", "", + verbatimBlockBody(sv, pc.indent, pc.eol)); + } + if (pc.roleItemBlock && sv.isEmpty()) { + return new Plan(b0, "", "", "()"); + } + boolean itemRaw = pc.roleItemBlock + && (itemNeedsRawMarker(sv) + || (pc.rootFirst + && (bareItemIsPairCandidate(sv) + || sv.startsWith("\uFEFF")))); + boolean pairRaw = !pc.roleItemBlock && needsRawMarker(sv); + if (itemRaw) { + return new Plan(b0, ":: ", "", sv); + } + if (pairRaw) { + return new Plan(a + 1, ":", wsPair, sv); + } + return new Plan(b0, "", "", sv); + } + PlanCtx sub = pc; + sub.slot = EditTree.Slot.BLOCK; + Planned p = planValue(replacement, sub); + return new Plan(b0, "", "", p.body); + } + + /** Pretty render re-indented under the target's indent; verbatim block interiors untouched. */ + private static String reindent(String doc, String indent, String eol) { + StringBuilder out = new StringBuilder(doc.length()); + boolean inVerbatim = false; + String[] lines = doc.split("\n", -1); + for (int i = 0; i < lines.length; i++) { + String line = lines[i]; + if (line.startsWith(" ")) { + line = line.substring(4); + } + boolean verbatimOpen = line.endsWith("(("); + boolean verbatimClose = trim(line).equals("))"); + if (i > 0) { + out.append(eol); + boolean skipPrefix = inVerbatim || line.isEmpty(); + if (!skipPrefix) { + out.append(indent); + } + } + out.append(line); + if (verbatimOpen) { + inVerbatim = true; + } else if (verbatimClose) { + inVerbatim = false; + } + } + return out.toString(); + } + + // ── Line helpers ──────────────────────────────────────────────────── + + private static String lineIndent(byte[] b, int pos) { + int start = pos; + while (start > 0 && b[start - 1] != '\n' && b[start - 1] != '\r') { + start--; + } + int end = start; + while (end < b.length && (b[end] == ' ' || b[end] == '\t')) { + end++; + } + return Bytes.text(b, start, end); + } + + private static String detectEol(byte[] b, int pos) { + int i = pos; + while (i < b.length) { + if (b[i] == '\n') { + return "\n"; + } + if (b[i] == '\r') { + return (i + 1 < b.length && b[i + 1] == '\n') ? "\r\n" : "\r"; + } + i++; + } + for (int j = pos - 1; j >= 0; j--) { + if (b[j] == '\r') { + return (j + 1 < pos && b[j + 1] == '\n') ? "\r\n" : "\r"; + } + if (b[j] == '\n') { + return "\n"; + } + } + return "\n"; + } + + // ── Raw-marker predicates (mirror render/helpers.rs) ──────────────── + + static boolean itemNeedsRawMarker(String s) { + return needsRawMarker(s) || s.startsWith("##") || s.startsWith("::") + || s.equals("]") || s.equals("}"); + } + + static boolean bareItemIsPairCandidate(String body) { + byte[] kb = body.getBytes(java.nio.charset.StandardCharsets.UTF_8); + Keys.ColonResult cr = Keys.scanColon(kb, 0, kb.length); + if (cr.kind != Keys.Colon.FOUND) { + return false; + } + int ci = cr.index; + int[] pre = Bytes.trim(kb, 0, ci); + if (pre[0] >= pre[1]) { + return false; + } + if (ci + 1 < kb.length && kb[ci + 1] == ':') { + return true; + } + return ci + 1 >= kb.length || Bytes.wsAt(kb, ci + 1, kb.length) > 0; + } + + static boolean needsRawMarker(String s) { + if (s.isEmpty()) { + return false; + } + char c0 = s.charAt(0); + if (c0 == ' ' || c0 == '\t') { + String t = s.stripLeading(); + return t.startsWith("{") || t.startsWith("[") + || needsRawMarkerContent(t); + } + if (c0 == '{' || c0 == '[') { + return true; + } + return needsRawMarkerContent(s); + } + + private static boolean needsRawMarkerContent(String s) { + if (s.equals("null") || s.equals("true") || s.equals("false") + || s.equals("(") || s.equals("((") || s.equals("()") + || s.equals("(())")) { + return true; + } + return matchesIntegerGrammar(s) || matchesFloatGrammar(s); + } + + /** § 3.6 integer grammar match (grammar-level, NOT bounded to i64). */ + static boolean matchesIntegerGrammar(String s) { + int n = s.length(); + int i = 0; + if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) { + i++; + } + if (i >= n) { + return false; + } + if (s.charAt(i) == '0' && i + 1 < n) { + switch (s.charAt(i + 1)) { + case 'x' -> { + return validDigitRun(s, i + 2, 16); + } + case 'o' -> { + return validDigitRun(s, i + 2, 8); + } + case 'b' -> { + return validDigitRun(s, i + 2, 2); + } + default -> { + } + } + } + return validDigitRun(s, i, 10); + } + + private static boolean validDigitRun(String s, int from, int radix) { + int n = s.length(); + if (from >= n) { + return false; + } + if (!digitFor(s.charAt(from), radix)) { + return false; + } + boolean prevUnderscore = false; + for (int i = from; i < n; i++) { + char c = s.charAt(i); + if (c == '_') { + if (prevUnderscore) { + return false; + } + prevUnderscore = true; + continue; + } + if (!digitFor(c, radix)) { + return false; + } + prevUnderscore = false; + } + return !prevUnderscore; + } + + private static boolean digitFor(char c, int radix) { + int v; + if (c >= '0' && c <= '9') { + v = c - '0'; + } else if (c >= 'a' && c <= 'f') { + v = c - 'a' + 10; + } else if (c >= 'A' && c <= 'F') { + v = c - 'A' + 10; + } else { + return false; + } + return v < radix; + } + + /** § 3.6 float grammar match. */ + static boolean matchesFloatGrammar(String s) { + int n = s.length(); + if (n == 0) { + return false; + } + char c0 = s.charAt(0); + if (!(c0 >= '0' && c0 <= '9') && c0 != '+' && c0 != '-') { + return false; + } + if (s.indexOf('.') < 0 && s.indexOf('e') < 0 && s.indexOf('E') < 0) { + return false; + } + int i = 0; + if (s.charAt(i) == '+' || s.charAt(i) == '-') { + i++; + } + int[] r = scanDecPart(s, i); + if (r[1] == 0) { + return false; + } + i = r[0]; + if (i < n && s.charAt(i) == '.') { + i++; + r = scanDecPart(s, i); + if (r[1] == 0) { + return false; + } + i = r[0]; + if (i < n && (s.charAt(i) == 'e' || s.charAt(i) == 'E')) { + r = scanExponent(s, i); + if (r[1] == 0) { + return false; + } + i = r[0]; + } + return i == n; + } + if (i < n && (s.charAt(i) == 'e' || s.charAt(i) == 'E')) { + r = scanExponent(s, i); + if (r[1] == 0) { + return false; + } + return r[0] == n; + } + return false; + } + + private static int[] scanDecPart(String s, int from) { + int n = s.length(); + if (from >= n || !Character.isDigit(s.charAt(from)) + || s.charAt(from) > '9') { + return new int[] { from, 0 }; + } + boolean prevUnderscore = false; + int i = from; + while (i < n) { + char c = s.charAt(i); + if (c == '_') { + if (prevUnderscore) { + return new int[] { i, 0 }; + } + prevUnderscore = true; + i++; + continue; + } + if (c < '0' || c > '9') { + break; + } + prevUnderscore = false; + i++; + } + if (prevUnderscore) { + return new int[] { i, 0 }; + } + return new int[] { i, 1 }; + } + + private static int[] scanExponent(String s, int from) { + int n = s.length(); + int i = from + 1; // skip 'e'/'E' + if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) { + i++; + } + return scanDecPart(s, i); + } +} diff --git a/lib/src/main/java/lang/ktav/pure/EditTree.java b/lib/src/main/java/lang/ktav/pure/EditTree.java new file mode 100644 index 0000000..3202a21 --- /dev/null +++ b/lib/src/main/java/lang/ktav/pure/EditTree.java @@ -0,0 +1,726 @@ +package lang.ktav.pure; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * Positional tree of the source: a line-by-line pass mirroring the + * value parser's dispatch decisions, but recording BYTE RANGES of every + * value instead of building trivia. Ported from the Rust core's + * {@code document/parser/edit/tree.rs}: same line splitting (LF / lone + * CR / CRLF, one EOL each), same root-shape classification, same + * dotted-key / reopen insertion, same inline-compound positioning. + * + *

All offsets are ORIGINAL byte offsets into the source (a leading + * BOM is skipped for content but stays in the coordinates). + */ +final class EditTree { + + private EditTree() { + } + + // ── Slots ─────────────────────────────────────────────────────────── + + /** How a value lies in the source; determines the splice bounds. */ + static final class Slot { + static final int PAIR = 0; + static final int ITEM = 1; + static final int BLOCK = 2; + + final int kind; + /** PAIR: first ':' byte. ITEM: first content byte (incl. "::"). */ + final int anchor; // -1 when absent + /** Exact body bytes (BLOCK: opener..end of closing line). */ + final int b0; + final int b1; + + private Slot(int kind, int anchor, int b0, int b1) { + this.kind = kind; + this.anchor = anchor; + this.b0 = b0; + this.b1 = b1; + } + + static Slot pair(int colon, int b0, int b1) { + return new Slot(PAIR, colon, b0, b1); + } + + static Slot item(int start, int b0, int b1) { + return new Slot(ITEM, start, b0, b1); + } + + static Slot block(int anchor, int start, int end) { + return new Slot(BLOCK, anchor, start, end); + } + } + + // ── Nodes ─────────────────────────────────────────────────────────── + + static final class Node { + static final int SCALAR = 0; + static final int OBJECT = 1; + static final int ARRAY = 2; + + final int kind; + /** Built from an inline compound ({@code { ... }} / {@code [ ... ]}). */ + boolean inline; + /** null — synthetic (dotted intermediate, root, or unattached inline). */ + Slot slot; + final Map entries; // OBJECT + final List items; // ARRAY + + Node(int kind, boolean inline, Slot slot) { + this.kind = kind; + this.inline = inline; + this.slot = slot; + this.entries = kind == OBJECT ? new LinkedHashMap<>() : null; + this.items = kind == ARRAY ? new ArrayList<>() : null; + } + + Node child(String key) { + return kind == OBJECT ? entries.get(key) : null; + } + + Node item(int i) { + return kind == ARRAY ? items.get(i) : null; + } + } + + // ── Builder ───────────────────────────────────────────────────────── + + private static final int ML_STRIPPED = 0; + private static final int ML_VERBATIM = 1; + + private static final class Frame { + final boolean isObject; + final Node node; + String pendingKey; + /** ':' of the pair that opened the current pending block. */ + int pendingAnchor = -1; + /** Opener byte of the block this frame will close. */ + final int rangeStart; + + Frame(boolean isObject, Node node, int rangeStart) { + this.isObject = isObject; + this.node = node; + this.rangeStart = rangeStart; + } + } + + private final byte[] b; + private final List stack = new ArrayList<>(); + private int collecting = -1; + private int collectingOpener; + private boolean rootInitialized; + private boolean rootConsumed; + private Node rootInline; + private boolean rootIsExplicit; + + private EditTree(byte[] b) { + this.b = b; + } + + /** Build the positional tree; returns (root, rootIsExplicit). */ + static Result build(byte[] b) { + EditTree t = new EditTree(b); + int n = b.length; + int lineStart = (n >= 3 && (b[0] & 0xFF) == 0xEF + && (b[1] & 0xFF) == 0xBB && (b[2] & 0xFF) == 0xBF) ? 3 : 0; + int lineNum = 0; + while (lineStart < n || (lineStart == n && n == 0)) { + int pos = lineStart; + while (pos < n && b[pos] != '\n' && b[pos] != '\r') { + pos++; + } + lineNum++; + t.handleLine(lineStart, pos, lineNum); + if (pos >= n) { + break; + } + lineStart = (b[pos] == '\r' && pos + 1 < n && b[pos + 1] == '\n') + ? pos + 2 : pos + 1; + } + return new Result(t.finish(), t.rootIsExplicit); + } + + static final class Result { + final Node root; + final boolean rootExplicit; + + Result(Node root, boolean rootExplicit) { + this.root = root; + this.rootExplicit = rootExplicit; + } + } + + // ── Line handling ─────────────────────────────────────────────────── + + private void handleLine(int rawStart, int rawEnd, int lineNum) { + if (collecting >= 0) { + int[] t = Bytes.trim(b, rawStart, rawEnd); + String trimmed = Bytes.text(b, t[0], t[1]); + String terminator = collecting == ML_VERBATIM ? "))" : ")"; + if (trimmed.equals(terminator)) { + int opener = collectingOpener; + collecting = -1; + int end = t[1]; + Node node = new Node(Node.SCALAR, false, + Slot.block(-1, opener, end)); + attachScalar(node, lineNum); + } + // Content lines of the block are data — no tree effect. + return; + } + + int[] t = Bytes.trim(b, rawStart, rawEnd); + int ts = t[0]; + int te = t[1]; + if (ts == te || (te - ts >= 2 && b[ts] == '#' && b[ts + 1] == '#')) { + return; // blank / comment + } + + if (rootConsumed) { + throw EditError.unsupported( + "internal: orphan line after the document root"); + } + + if (!rootInitialized) { + rootInitialized = true; + if (!(te - ts == 1 && (b[ts] == '}' || b[ts] == ']'))) { + if (b[ts] == '{' && te - ts == 1) { + rootIsExplicit = true; + pushObject(ts); + return; + } + if (b[ts] == '[' && te - ts == 1) { + rootIsExplicit = true; + pushArray(ts); + return; + } + if (b[ts] == '{' || b[ts] == '[') { + rootConsumed = true; + rootInline = positionInlineTop(ts, te); + return; + } + Keys.ColonResult cr = Keys.scanColon(b, ts, te); + if (cr.kind == Keys.Colon.FOUND) { + int ci = cr.index; + int[] pre = Bytes.trim(b, ts, ci); + boolean nonEmptyPrefix = pre[0] < pre[1]; + boolean sepOk; + if (ci + 1 < te && b[ci + 1] == ':') { + sepOk = true; + } else { + sepOk = ci + 1 >= te || Bytes.wsAt(b, ci + 1, te) > 0; + } + if (nonEmptyPrefix && sepOk) { + // Implicit Object root; the line is its first pair. + pushObject(0); + handleObjectPair(ts, te, lineNum); + return; + } + } + pushArray(0); + handleArrayItem(ts, te, lineNum); + return; + } + // Bare '}' / ']' as first content line — the real parser + // already rejected the document; unreachable after parse. + throw EditError.unsupported("internal: unbalanced bracket"); + } + + if (te - ts == 1 && b[ts] == '}') { + closeFrame(true, lineNum, ts, te); + return; + } + if (te - ts == 1 && b[ts] == ']') { + closeFrame(false, lineNum, ts, te); + return; + } + + if (!stack.isEmpty() && !top().isObject) { + handleArrayItem(ts, te, lineNum); + } else { + handleObjectPair(ts, te, lineNum); + } + } + + private Frame top() { + return stack.get(stack.size() - 1); + } + + private void pushObject(int rangeStart) { + stack.add(new Frame(true, new Node(Node.OBJECT, false, null), + rangeStart)); + } + + private void pushArray(int rangeStart) { + stack.add(new Frame(false, new Node(Node.ARRAY, false, null), + rangeStart)); + } + + // ── Object pairs ──────────────────────────────────────────────────── + + private void handleObjectPair(int ts, int te, int lineNum) { + Keys.ColonResult cr = Keys.scanColon(b, ts, te); + if (cr.kind != Keys.Colon.FOUND) { + throw EditError.unsupported( + "internal: pair without separator after a successful parse"); + } + int colon = cr.index; + int[] pre = Bytes.trim(b, ts, colon); + String key = Bytes.text(b, pre[0], pre[1]); + int colonOff = colon; + + if (colonOff + 1 < te && b[colonOff + 1] == ':') { + // Raw marker '::'. + if (colonOff + 2 < te && Bytes.wsAt(b, colonOff + 2, te) == 0) { + throw EditError.unsupported( + "internal: separator glued to body"); + } + int[] body = Bytes.trim(b, colonOff + 2, te); + insertPair(key, new Node(Node.SCALAR, false, + Slot.pair(colonOff, body[0], te)), lineNum); + return; + } + if (colonOff + 1 < te && Bytes.wsAt(b, colonOff + 1, te) == 0) { + throw EditError.unsupported("internal: separator glued to body"); + } + int vs = Bytes.skipWs(b, colonOff + 1, te); + + // classify_value_shape over [vs, te). + int shape = classifyValueShape(vs, te); + switch (shape) { + case SHAPE_SCALAR: + case SHAPE_EMPTY_STRING: + insertPair(key, new Node(Node.SCALAR, false, + Slot.pair(colonOff, vs, te)), lineNum); + return; + case SHAPE_EMPTY_OBJECT: + insertPair(key, new Node(Node.OBJECT, false, + Slot.pair(colonOff, vs, te)), lineNum); + return; + case SHAPE_EMPTY_ARRAY: + insertPair(key, new Node(Node.ARRAY, false, + Slot.pair(colonOff, vs, te)), lineNum); + return; + case SHAPE_OPEN_OBJECT: + case SHAPE_OPEN_ARRAY: { + setPending(key, colonOff); + if (shape == SHAPE_OPEN_OBJECT) { + pushObject(vs); + } else { + pushArray(vs); + } + return; + } + case SHAPE_OPEN_ML_STRIPPED: + case SHAPE_OPEN_ML_VERBATIM: { + setPending(key, colonOff); + collecting = shape == SHAPE_OPEN_ML_VERBATIM + ? ML_VERBATIM : ML_STRIPPED; + collectingOpener = vs; + return; + } + default: { // SHAPE_INLINE_VALUE + Node node = positionInlineTop(vs, te); + node.inline = false; + node.slot = Slot.pair(colonOff, vs, te); + insertPair(key, node, lineNum); + } + } + } + + private void setPending(String key, int anchor) { + top().pendingKey = key; + top().pendingAnchor = anchor; + } + + private void insertPair(String key, Node value, int lineNum) { + insertValue(top().node, key, value); + } + + // ── Array items ───────────────────────────────────────────────────── + + private void handleArrayItem(int ts, int te, int lineNum) { + Frame f = top(); + if (te - ts >= 2 && b[ts] == ':' && b[ts + 1] == ':') { + if (te > ts + 2 && Bytes.wsAt(b, ts + 2, te) == 0) { + throw EditError.unsupported("internal: separator glued to body"); + } + int[] body = Bytes.trim(b, ts + 2, te); + f.node.items.add(new Node(Node.SCALAR, false, + Slot.item(ts, body[0], te))); + return; + } + int shape = classifyValueShape(ts, te); + int bodyStart = Bytes.skipWs(b, ts, te); + switch (shape) { + case SHAPE_SCALAR: + case SHAPE_EMPTY_STRING: + f.node.items.add(new Node(Node.SCALAR, false, + Slot.item(ts, bodyStart, te))); + return; + case SHAPE_EMPTY_OBJECT: + f.node.items.add(new Node(Node.OBJECT, false, + Slot.item(ts, bodyStart, te))); + return; + case SHAPE_EMPTY_ARRAY: + f.node.items.add(new Node(Node.ARRAY, false, + Slot.item(ts, bodyStart, te))); + return; + case SHAPE_OPEN_OBJECT: + pushObject(bodyStart); + return; + case SHAPE_OPEN_ARRAY: + pushArray(bodyStart); + return; + case SHAPE_OPEN_ML_STRIPPED: + case SHAPE_OPEN_ML_VERBATIM: + collecting = shape == SHAPE_OPEN_ML_VERBATIM + ? ML_VERBATIM : ML_STRIPPED; + collectingOpener = bodyStart; + return; + default: { + Node node = positionInlineTop(bodyStart, te); + node.inline = false; + node.slot = Slot.item(ts, bodyStart, te); + f.node.items.add(node); + } + } + } + + private void attachScalar(Node node, int lineNum) { + Frame f = top(); + if (f.isObject) { + String key = f.pendingKey; + if (key == null) { + throw EditError.unsupported( + "internal: multiline closed without pending key"); + } + f.pendingKey = null; + int anchor = f.pendingAnchor; + f.pendingAnchor = -1; + if (node.slot != null && node.slot.kind == Slot.BLOCK) { + node = new Node(Node.SCALAR, false, + Slot.block(anchor, node.slot.b0, node.slot.b1)); + } + insertValue(f.node, key, node); + } else { + f.node.items.add(node); + } + } + + private void closeFrame(boolean object, int lineNum, int ts, int te) { + if (stack.size() == 1 && rootIsExplicit) { + rootConsumed = true; + return; + } + if (stack.size() <= 1) { + throw EditError.unsupported("internal: unbalanced bracket"); + } + Frame frame = stack.remove(stack.size() - 1); + if (frame.isObject != object) { + throw EditError.unsupported("internal: mismatched closer"); + } + int anchor = -1; + if (!stack.isEmpty() && top().isObject) { + anchor = top().pendingAnchor; + top().pendingAnchor = -1; + } + Node node = new Node(frame.node.kind, false, + Slot.block(anchor, frame.rangeStart, te)); + Frame parent = top(); + if (parent.isObject) { + String key = parent.pendingKey; + if (key == null) { + throw EditError.unsupported( + "internal: closed compound without pending key"); + } + parent.pendingKey = null; + insertValue(parent.node, key, node); + } else { + parent.node.items.add(node); + } + } + + private Node finish() { + if (collecting >= 0 || stack.size() > 1 + || (rootIsExplicit && !rootConsumed && !stack.isEmpty())) { + throw EditError.unsupported( + "internal: unclosed compound after a successful parse"); + } + if (rootInline != null) { + return rootInline; + } + if (stack.isEmpty()) { + return new Node(Node.OBJECT, false, null); // empty/comment-only + } + return stack.remove(stack.size() - 1).node; + } + + // ── classify_value_shape over a byte range ────────────────────────── + + static final int SHAPE_SCALAR = 0; + static final int SHAPE_EMPTY_STRING = 1; + static final int SHAPE_OPEN_OBJECT = 2; + static final int SHAPE_OPEN_ARRAY = 3; + static final int SHAPE_EMPTY_OBJECT = 4; + static final int SHAPE_EMPTY_ARRAY = 5; + static final int SHAPE_OPEN_ML_STRIPPED = 6; + static final int SHAPE_OPEN_ML_VERBATIM = 7; + static final int SHAPE_INLINE_VALUE = 8; + + /** {@code from} is already the first non-whitespace byte. */ + static int classifyValueShape(int from, int end) { + int len = end - from; + if (len == 1) { + byte c = b0(from); + if (c == '{') { + return SHAPE_OPEN_OBJECT; + } + if (c == '[') { + return SHAPE_OPEN_ARRAY; + } + if (c == '(') { + return SHAPE_OPEN_ML_STRIPPED; + } + } + if (len == 2 && b0(from) == '(' && b0(from + 1) == '(') { + return SHAPE_OPEN_ML_VERBATIM; + } + if (len == 2 && b0(from) == '(' && b0(from + 1) == ')') { + return SHAPE_EMPTY_STRING; + } + if (len == 4 && b0(from) == '(' && b0(from + 1) == '(' + && b0(from + 2) == ')' && b0(from + 3) == ')') { + return SHAPE_EMPTY_STRING; + } + byte c = b0(from); + if (c == '{') { + int[] inner = Bytes.trim(b, from + 1, end - 1); + if (b0(end - 1) == '}' && inner[0] >= inner[1]) { + return SHAPE_EMPTY_OBJECT; + } + return SHAPE_INLINE_VALUE; + } + if (c == '[') { + int[] inner = Bytes.trim(b, from + 1, end - 1); + if (b0(end - 1) == ']' && inner[0] >= inner[1]) { + return SHAPE_EMPTY_ARRAY; + } + return SHAPE_INLINE_VALUE; + } + return SHAPE_SCALAR; + } + + private byte b0(int i) { + return b[i]; + } + + // ── Inline positioning ────────────────────────────────────────────── + + /** + * Position an inline compound whose body occupies {@code [from, to)} + * (starts with the opener, ends with the matching closer). Root of + * the recursion: builds the segment list once, then recurses. + */ + private Node positionInlineTop(int from, int to) { + return positionInline(from, to, 0); + } + + private Node positionInline(int from, int to, int depth) { + boolean isObject = b[from] == '{'; + List segments = new ArrayList<>(); + splitTopLevel(from + 1, to - 1, isObject, segments); + if (isObject) { + Node table = new Node(Node.OBJECT, true, null); + for (int[] seg : segments) { + int s = seg[0]; + int e = seg[1]; + if (s >= e) { + continue; // trailing comma + } + Keys.ColonResult cr = Keys.scanColon(b, s, e); + if (cr.kind != Keys.Colon.FOUND) { + throw EditError.unsupported( + "internal: inline pair has no separator"); + } + int colon = cr.index; + int[] keyRange = Bytes.trim(b, s, colon); + String key = Bytes.text(b, keyRange[0], keyRange[1]); + int v; + boolean raw = colon + 1 < e && b[colon + 1] == ':'; + if (raw) { + v = Bytes.skipWs(b, colon + 2, e); + } else { + v = Bytes.skipWs(b, colon + 1, e); + } + Node child = inlineChild(v, e, depth); + child.slot = Slot.pair(colon, v, e); + insertValue(table, key, child); + } + return table; + } + Node arr = new Node(Node.ARRAY, true, null); + for (int[] seg : segments) { + int s = seg[0]; + int e = seg[1]; + if (s >= e) { + continue; + } + int v = Bytes.skipWs(b, s, e); + Node child = inlineChild(v, e, depth); + child.slot = Slot.item(s, v, e); + arr.items.add(child); + } + return arr; + } + + private Node inlineChild(int v, int e, int depth) { + if (v < e && (b[v] == '{' || b[v] == '[')) { + return positionInline(v, e, depth + 1); + } + return new Node(Node.SCALAR, true, null); + } + + /** + * § 5.8.5-aware top-level comma split of an inline compound body + * {@code [from, to)}: only a value-start {@code {}/[} nests; quoted + * key segments (object mode, segment-start positions) are + * comma-opaque; escapes are skipped; a trailing comma yields no + * final segment. Input already parsed successfully — any surprise is + * an internal error. + */ + private void splitTopLevel(int from, int to, boolean isObject, + List out) { + int i = from; + int segAt = from; + int depth = 0; + int[] scopeObj = new int[64]; + int top = -1; + boolean inKey = isObject; + boolean valueStart = !isObject; + while (i < to) { + byte c = b[i]; + if (c == '\\') { + i += 2; // escape lead consumes the next byte + continue; + } + if (inKey && (c == '"' || c == '\'' || c == '`')) { + int close = Keys.quotedSpanEnd(b, i, to); + if (close < 0) { + throw EditError.unsupported( + "internal: unterminated quoted key segment"); + } + i = close + 1; + continue; + } + if (inKey) { + if (c == ':') { + inKey = false; + valueStart = true; + i++; + continue; + } + if (c == '.') { + i = Bytes.skipWs(b, i + 1, to); + continue; + } + i++; + continue; + } + if (valueStart && (c == '{' || c == '[')) { + depth++; + top++; + scopeObj[top] = c == '{' ? 1 : 0; + inKey = c == '{'; + valueStart = c != '{'; + i++; + continue; + } + if (depth > 0 && (c == '}' || c == ']')) { + depth--; + top--; + inKey = false; // the parent was in a value position + valueStart = false; + i++; + continue; + } + if (c == ',' && depth == 0) { + out.add(new int[] { segAt, i }); + i = Bytes.skipWs(b, i + 1, to); + segAt = i; + valueStart = true; + inKey = isObject; + continue; + } + valueStart = false; + i++; + } + int[] tail = Bytes.trim(b, segAt, to); + if (tail[0] >= tail[1] && segAt != from) { + // Only whitespace after the last comma — trailing comma. + return; + } + out.add(new int[] { segAt, to }); + } + + // ── Dotted-key insertion ──────────────────────────────────────────── + + /** + * Dotted-key expansion with reopen semantics: every intermediate + * segment descends through (or creates) a synthetic object whose + * slot is invalidated; the final segment inserts the value. + * Occupied-by-non-object and duplicate collisions are unreachable + * after a successful parse — reported as internal Unsupported. + */ + private static void insertValue(Node table, String key, Node value) { + List ranges = splitKeyRanges(key); + Node cur = table; + for (int i = 0; i < ranges.size(); i++) { + String seg = decodeSegment(key, ranges.get(i)); + boolean last = i == ranges.size() - 1; + if (last) { + if (cur.entries.containsKey(seg)) { + // Reopen: keep the existing entry, mark noncontiguous. + cur.entries.get(seg).slot = null; + } else { + cur.entries.put(seg, value); + } + } else { + Node next = cur.entries.get(seg); + if (next == null) { + next = new Node(Node.OBJECT, false, null); + cur.entries.put(seg, next); + } else if (next.kind != Node.OBJECT) { + throw EditError.unsupported( + "internal: dotted descent through a scalar"); + } else { + next.slot = null; // reopen / dotted intermediate + } + cur = next; + } + } + } + + /** Raw dotted-path segment ranges of an already-decoded key. */ + private static List splitKeyRanges(String key) { + // The key here is the raw (post-trim) key spelling decoded from + // source bytes; re-split on unescaped dots using the byte view. + byte[] kb = key.getBytes(java.nio.charset.StandardCharsets.UTF_8); + return Keys.splitPath(kb, 0, kb.length); + } + + private static String decodeSegment(String key, int[] range) { + byte[] kb = key.getBytes(java.nio.charset.StandardCharsets.UTF_8); + Keys.Segment s = Keys.checkAndDecode(kb, range[0], range[1], + 1, "", 0, 0); + if (s.kind == Keys.Segment.OK) { + return s.decoded; + } + throw EditError.unsupported("internal: invalid dotted key segment"); + } +} diff --git a/lib/src/main/java/lang/ktav/pure/Layout.java b/lib/src/main/java/lang/ktav/pure/Layout.java new file mode 100644 index 0000000..0769ee6 --- /dev/null +++ b/lib/src/main/java/lang/ktav/pure/Layout.java @@ -0,0 +1,669 @@ +package lang.ktav.pure; + +import java.util.ArrayList; +import java.util.List; + +import lang.ktav.Value; + +/** + * Minimal inline-wrapper expansion when a splice would cost readability + * — the port of the Rust core's {@code splice/layout.rs}: expand the + * containing inline wrapper (and touched nested wrappers) only while a + * criterion holds (multiline replacement, line growth past + * max(100 Unicode scalar values, the old physical line length), or + * string replacements needing ≥ 4 escapes with ≥ 25% overhead);untouched + * nested branches stay inline. If a lossless standalone form does not + * exist, the valid inline candidate is kept instead of failing. + */ +final class Layout { + + private Layout() { + } + + static String adjust(byte[] source, String candidate, EditTree.Node root, + List path, Value replacement, int from, int to, + int inserted) { + long[] owner = inlineOwner(source, root, path, from); + if (owner == null) { + return candidate; + } + int start = (int) owner[0]; + int end = (int) owner[1]; + if (from < start) { + return candidate; + } + int newEnd = end - (to - from) + inserted; + if (newEnd < start) { + throw EditError.unsupported("invalid layout range"); + } + byte[] cb = candidate.getBytes(java.nio.charset.StandardCharsets.UTF_8); + if (newEnd > cb.length || end > cb.length) { + throw EditError.unsupported("invalid layout boundaries"); + } + String body = Bytes.text(cb, start, newEnd); + if (!isCompound(body)) { + return candidate; + } + int oldWidth = codePoints(physicalLineText(cb, from)); + int limit = Math.max(oldWidth, 100); + boolean force = requiresBlock(replacement, 0); + if (!force && codePoints(physicalLineText(cb, from)) <= limit) { + return candidate; + } + String indent = lineIndent(source, start); + String eol = detectEol(source, end); + int ch0 = from - start; + int ch1 = from - start + inserted; + String expanded = expand(cb, start, newEnd, indent, eol, limit, + ch0, ch1, force, 0); + if (expanded == null) { + return candidate; + } + return Bytes.text(cb, 0, start) + expanded + + Bytes.text(cb, newEnd, cb.length); + } + + // ── Owner discovery ───────────────────────────────────────────────── + + private static long[] inlineOwner(byte[] source, EditTree.Node root, + List path, int pos) { + EditTree.Node node = root; + if (root.inline) { + long[] lr = physicalLineRange(source, pos); + int[] t = Bytes.trim(source, (int) lr[0], (int) lr[1]); + int start = t[0]; + if (start + 2 < source.length + && (source[start] & 0xFF) == 0xEF + && (source[start + 1] & 0xFF) == 0xBB + && (source[start + 2] & 0xFF) == 0xBF) { + start += 3; + } + return new long[] { start, t[1] }; + } + for (String segment : path) { + if (node.kind == EditTree.Node.OBJECT) { + node = node.child(segment); + } else if (node.kind == EditTree.Node.ARRAY) { + node = node.item(EditSplice.parseIndex(segment)); + } else { + node = null; + } + if (node == null) { + throw EditError.notFound(); + } + if (node.slot != null) { + int start; + int end; + if (node.slot.kind == EditTree.Slot.BLOCK) { + start = node.slot.b0; + end = node.slot.b1; + } else { + start = node.slot.b0; + end = node.slot.b1; + } + if ((node.kind == EditTree.Node.OBJECT + || node.kind == EditTree.Node.ARRAY) + && start >= 0 && end <= source.length + && isCompound(Bytes.text(source, start, end)) + && !hasEol(source, start, end)) { + return new long[] { start, end }; + } + } + } + if (node.inline) { + long[] lr = physicalLineRange(source, pos); + int[] t = Bytes.trim(source, (int) lr[0], (int) lr[1]); + int lineStart = t[0]; + if (lineStart + 2 < source.length + && (source[lineStart] & 0xFF) == 0xEF + && (source[lineStart + 1] & 0xFF) == 0xBB + && (source[lineStart + 2] & 0xFF) == 0xBF) { + lineStart += 3; + } + byte[] lb = Bytes.text(source, lineStart, t[1]) + .getBytes(java.nio.charset.StandardCharsets.UTF_8); + String line = Bytes.text(source, lineStart, t[1]); + byte[] bodyB; + if (isCompound(line)) { + bodyB = lb; + } else { + Keys.ColonResult cr = Keys.scanColon(lb, 0, lb.length); + if (cr.kind != Keys.Colon.FOUND) { + return null; + } + int[] v = Bytes.trim(lb, cr.index + 1, lb.length); + if (v[0] >= v[1]) { + return null; + } + byte[] sub = new byte[v[1] - v[0]]; + System.arraycopy(lb, v[0], sub, 0, sub.length); + bodyB = sub; + } + String body = Bytes.text(bodyB, 0, bodyB.length); + if (isCompound(body)) { + int off = indexOfBytes(lb, bodyB); + if (off < 0) { + return null; + } + return new long[] { lineStart + off, lineStart + off + bodyB.length }; + } + } + return null; + } + + private static int indexOfBytes(byte[] hay, byte[] needle) { + outer: + for (int i = 0; i + needle.length <= hay.length; i++) { + for (int j = 0; j < needle.length; j++) { + if (hay[i + j] != needle[j]) { + continue outer; + } + } + return i; + } + return -1; + } + + // ── Block criteria ────────────────────────────────────────────────── + + private static boolean requiresBlock(Value value, int depth) { + if (depth > PureParser.MAX_DEPTH) { + throw EditError.unsupported( + "replacement nesting exceeds inline depth limit (" + + PureParser.MAX_DEPTH + ")"); + } + if (value instanceof Value.Str s) { + String sv = s.value(); + if (sv.indexOf('\n') >= 0 || sv.indexOf('\r') >= 0) { + return true; + } + String encoded = EditSplice.encodeInline(sv, false); + int escapes = 0; + for (int i = 0; i < encoded.length(); i++) { + if (encoded.charAt(i) == '\\') { + escapes++; + i++; + } + } + int plain = codePoints(sv); + int overhead = codePoints(encoded) - plain; + int quarter = plain / 4 + ((plain % 4 != 0) ? 1 : 0); + return escapes >= 4 && overhead >= Math.max(quarter, 0); + } + if (value instanceof Value.Obj o) { + for (Value child : o.entries().values()) { + if (requiresBlock(child, depth + 1)) { + return true; + } + } + return false; + } + if (value instanceof Value.Arr a) { + for (Value child : a.items()) { + if (requiresBlock(child, depth + 1)) { + return true; + } + } + return false; + } + return false; + } + + // ── Expansion ─────────────────────────────────────────────────────── + + /** + * Expand the inline compound occupying cb[bodyStart..bodyEnd). + * {@code changed} is the splice range relative to the body start. + * Returns null when no lossless standalone form exists. + */ + private static String expand(byte[] cb, int bodyStart, int bodyEnd, + String indent, String eol, int limit, int ch0, int ch1, + boolean force, int depth) { + if (depth > PureParser.MAX_DEPTH) { + throw EditError.unsupported("layout nesting exceeds inline depth limit"); + } + boolean object = cb[bodyStart] == '{'; + List parts = new ArrayList<>(); + splitTopLevel(cb, bodyStart + 1, bodyEnd - 1, object, parts); + String childIndent = indent + " "; + StringBuilder out = new StringBuilder(bodyEnd - bodyStart); + out.append(object ? '{' : '['); + for (int[] part : parts) { + int ps = part[0]; + int pe = part[1]; + int[] t = Bytes.trim(cb, ps, pe); + if (t[0] >= t[1]) { + continue; // trailing comma + } + ps = t[0]; + pe = t[1]; + int offset = ps - bodyStart; + int partLen = pe - ps; + boolean touched = offset < ch1 && offset + partLen > ch0; + Keys.ColonResult cr = null; + boolean raw = false; + if (object) { + cr = Keys.scanColon(cb, ps, pe); + if (cr.kind != Keys.Colon.FOUND) { + throw EditError.unsupported("inline pair has no separator"); + } + raw = cr.index + 1 < pe && cb[cr.index + 1] == ':'; + } + out.append(eol); + out.append(childIndent); + boolean partCompound = isCompound(Bytes.text(cb, ps, pe)); + if (!raw && partCompound && touched + && (force || codePoints(childIndent) + + codePoints(Bytes.text(cb, ps, pe)) > limit)) { + int local0 = Math.max(ch0 - offset, 0); + int local1 = Math.max(ch1 - offset, 0); + String child = expand(cb, ps, pe, childIndent, eol, limit, + local0, local1, force, depth + 1); + if (child == null) { + return null; + } + out.append(child); + } else { + String child = standalone(cb, ps, pe, raw, object, + childIndent, eol); + if (child == null) { + return null; + } + out.append(child); + } + } + out.append(eol); + out.append(indent); + out.append(object ? '}' : ']'); + return out.toString(); + } + + /** + * Move one inline part to its own line: keep the spelling when the + * standalone form parses identically; otherwise decode and re-plan. + * Returns null when no lossless standalone form exists. + */ + private static String standalone(byte[] cb, int ps, int pe, + boolean raw, boolean object, String indent, String eol) { + String part = Bytes.text(cb, ps, pe); + String inline = object ? "{" + part + "}" : "[" + part + "]"; + // A probe failure is not fatal: fall through to decode+re-plan, + // which re-emits the value canonically (the reference keeps the + // part when the probe proves the spelling lossless). + Value expected = tryParseInlineFragment(inline); + // The standalone-form probe uses the REAL parser on the would-be + // line (a pair line for object parts, a block array for array + // parts) — escapes are NOT decoded there, exactly like the + // reference's `parse(&line)`: if the standalone line re-parses + // to the same value, the spelling moves up verbatim. + Value parsedLine = expected == null + ? null : tryParseLineFragment(part, object); + if (parsedLine != null && parsedLine.equals(expected)) { + return part; + } + int vs; + int colon = -1; + if (object) { + Keys.ColonResult cr = Keys.scanColon(cb, ps, pe); + if (cr.kind != Keys.Colon.FOUND) { + throw EditError.unsupported("moved pair has no separator"); + } + colon = cr.index; + vs = raw ? Bytes.skipWs(cb, colon + 2, pe) + : Bytes.skipWs(cb, colon + 1, pe); + } else { + vs = Bytes.skipWs(cb, ps, pe); + } + Value value; + if (raw) { + String decoded = decodeInlineEscapes(Bytes.text(cb, vs, pe)); + if (decoded == null) { + throw EditError.unsupported("invalid inline string"); + } + value = new Value.Str(decoded); + } else { + Value parsed = parseInlineFragment("[" + part + "]", false, + "cannot decode moved inline value"); + if (!(parsed instanceof Value.Arr arr) || arr.items().size() != 1) { + throw EditError.unsupported( + "moved inline value is not a single value"); + } + value = arr.items().get(0); + } + if (value instanceof Value.Str s) { + String sv = s.value(); + if (sv.indexOf('\r') >= 0) { + return null; + } + for (String l : sv.split("\n", -1)) { + if (trimSpacesTabs(l).equals("))")) { + return null; + } + } + } + EditSplicePlan p = new EditSplicePlan(); + p.slot = object ? EditTree.Slot.PAIR : EditTree.Slot.ITEM; + p.existingRaw = raw; + p.indent = indent; + p.eol = eol; + p.inlineCompound = true; + Planned planned = planValuePublic(value, p); + StringBuilder out = new StringBuilder(); + if (object) { + out.append(Bytes.text(cb, ps, colon + 1)); + if (planned.desiredRaw) { + out.append(':'); + } + int wsFrom = raw ? colon + 2 : colon + 1; + String ws = Bytes.text(cb, wsFrom, vs); + out.append(ws.isEmpty() ? " " : ws); + } else if (planned.desiredRaw) { + out.append(":: "); + } + out.append(planned.body); + return out.toString(); + } + + // ── Shared small helpers ──────────────────────────────────────────── + + private static final class EditSplicePlan { + int slot; + boolean existingRaw; + String indent; + String eol; + boolean inlineCompound; + } + + private static Planned planValuePublic(Value v, EditSplicePlan p) { + // Local re-export of planValue with the reduced context used by + // standalone(): mirrors layout.rs's PlanCtx construction. + if (v instanceof Value.Str s) { + String sv = s.value(); + if (sv.indexOf('\r') >= 0) { + throw EditError.unsupported( + "replacement string contains CR (U+000D)"); + } + boolean edgeWs = (!sv.isEmpty() && isWsCp(sv.codePointAt(0))) + || (!sv.isEmpty() + && isWsCp(sv.codePointBefore(sv.length()))); + if (sv.indexOf('\n') >= 0 || edgeWs) { + return new Planned(verbatimBlock(sv, p.indent, p.eol), false); + } + boolean desiredRaw; + if (p.slot == EditTree.Slot.ITEM) { + desiredRaw = EditSplice.itemNeedsRawMarker(sv) || sv.isEmpty(); + } else { + desiredRaw = EditSplice.needsRawMarker(sv); + } + if (p.existingRaw) { + desiredRaw = true; + } + return new Planned(sv, desiredRaw); + } + if (p.inlineCompound && (v instanceof Value.Obj || v instanceof Value.Arr)) { + StringBuilder out = new StringBuilder(); + EditSplice.appendInlineValue(v, out); + return new Planned(out.toString(), false); + } + throw EditError.unsupported("internal: unexpected layout plan value"); + } + + private static String verbatimBlock(String s, String indent, String eol) { + for (String line : s.split("\n", -1)) { + if (line.trim().equals("))")) { + throw EditError.unsupported( + "multiline string contains a line equal to '))';" + + " no lossless verbatim form exists"); + } + } + StringBuilder out = new StringBuilder(s.length() + 16); + out.append("((").append(eol); + boolean first = true; + for (String line : s.split("\n", -1)) { + if (!first) { + out.append(eol); + } + first = false; + out.append(line); + } + out.append(eol).append(indent).append("))"); + return out.toString(); + } + + /** Top-level comma split over a byte range of the inline body. */ + private static void splitTopLevel(byte[] b, int from, int to, + boolean isObject, List out) { + int i = from; + int segAt = from; + int depth = 0; + int[] scopeObj = new int[64]; + int top = -1; + boolean inKey = isObject; + boolean valueStart = !isObject; + while (i < to) { + byte c = b[i]; + if (c == '\\') { + i += 2; + continue; + } + if (inKey && (c == '"' || c == '\'' || c == '`')) { + int close = Keys.quotedSpanEnd(b, i, to); + if (close < 0) { + throw EditError.unsupported("cannot split inline layout"); + } + i = close + 1; + continue; + } + if (inKey) { + if (c == ':') { + inKey = false; + valueStart = true; + i++; + continue; + } + if (c == '.') { + i = Bytes.skipWs(b, i + 1, to); + continue; + } + i++; + continue; + } + if (valueStart && (c == '{' || c == '[')) { + depth++; + top++; + scopeObj[top] = c == '{' ? 1 : 0; + inKey = c == '{'; + valueStart = c != '{'; + i++; + continue; + } + if (depth > 0 && (c == '}' || c == ']')) { + depth--; + top--; + inKey = false; + valueStart = false; + i++; + continue; + } + if (c == ',' && depth == 0) { + out.add(new int[] { segAt, i }); + i = Bytes.skipWs(b, i + 1, to); + segAt = i; + valueStart = true; + inKey = isObject; + continue; + } + valueStart = false; + i++; + } + int[] tail = Bytes.trim(b, segAt, to); + if (tail[0] >= tail[1] && segAt != from) { + return; // trailing comma + } + out.add(new int[] { segAt, to }); + } + + /** Parse a synthetic inline fragment; throws Unsupported on failure. */ + private static Value parseInlineFragment(String text, boolean object, + String why) { + Value v = tryParseInlineFragment(text); + if (v == null) { + throw EditError.unsupported(why); + } + return v; + } + + private static Value tryParseInlineFragment(String text) { + try { + byte[] fb = text.getBytes(java.nio.charset.StandardCharsets.UTF_8); + Ctx ctx = new Ctx(fb, 0, false); + ctx.line = 1; + ctx.lineText = text; + ctx.spanStart = 0; + ctx.spanEnd = fb.length; + Inline.Parsed p = fb[0] == '{' + ? Inline.parseObject(ctx, 0, fb.length, 1) + : Inline.parseArray(ctx, 0, fb.length, 1); + if (p.next != fb.length) { + return null; + } + return Builders.toValue(p.value, 0); + } catch (RuntimeException e) { + return null; + } + } + + /** + * Parse the would-be STANDALONE form with the real document parser: + * an object part becomes a one-pair document, an array part becomes + * a {@code [\n part \n]} block array. Escape sequences in plain + * bodies are NOT decoded there — this is the reference's + * {@code parse(&line)} probe. + */ + private static Value tryParseLineFragment(String part, boolean object) { + try { + String doc = object ? part + "\n" : "[\n" + part + "\n]"; + byte[] fb = doc.getBytes(java.nio.charset.StandardCharsets.UTF_8); + Value v = PureParser.parse(fb, 0, false); + // Reject shape mismatches (e.g. a comment-only object part). + return v; + } catch (RuntimeException e) { + return null; + } + } + + /** Decode § 3.7 escapes of an inline string body; null when invalid. */ + private static String decodeInlineEscapes(String body) { + try { + byte[] bb = body.getBytes(java.nio.charset.StandardCharsets.UTF_8); + StringBuilder out = new StringBuilder(bb.length); + Escapes.decode(bb, 0, bb.length, out, 1, body, 0, bb.length); + return out.toString(); + } catch (RuntimeException e) { + return null; + } + } + + /** Trim only ' ' and '\t' (the reference's `trim_matches(' '|'\t')`). */ + private static String trimSpacesTabs(String s) { + int b = 0; + int e = s.length(); + while (b < e && (s.charAt(b) == ' ' || s.charAt(b) == '\t')) { + b++; + } + while (e > b && (s.charAt(e - 1) == ' ' || s.charAt(e - 1) == '\t')) { + e--; + } + return s.substring(b, e); + } + + private static boolean isCompound(String body) { + return (body.startsWith("{") && body.endsWith("}")) + || (body.startsWith("[") && body.endsWith("]")); + } + + private static boolean hasEol(byte[] b, int from, int to) { + for (int i = from; i < to && i < b.length; i++) { + if (b[i] == '\n' || b[i] == '\r') { + return true; + } + } + return false; + } + + private static long[] physicalLineRange(byte[] b, int pos) { + int start = pos; + while (start > 0 && b[start - 1] != '\n' && b[start - 1] != '\r') { + start--; + } + int end = pos; + while (end < b.length && b[end] != '\n' && b[end] != '\r') { + end++; + } + return new long[] { start, end }; + } + + private static String physicalLineText(byte[] b, int pos) { + long[] r = physicalLineRange(b, pos); + return Bytes.text(b, (int) r[0], (int) r[1]); + } + + private static int codePoints(String s) { + return s.codePointCount(0, s.length()); + } + + private static String lineIndent(byte[] b, int pos) { + int start = pos; + while (start > 0 && b[start - 1] != '\n' && b[start - 1] != '\r') { + start--; + } + int end = start; + while (end < b.length && (b[end] == ' ' || b[end] == '\t')) { + end++; + } + return Bytes.text(b, start, end); + } + + private static String detectEol(byte[] b, int pos) { + int i = pos; + while (i < b.length) { + if (b[i] == '\n') { + return "\n"; + } + if (b[i] == '\r') { + return (i + 1 < b.length && b[i + 1] == '\n') ? "\r\n" : "\r"; + } + i++; + } + for (int j = pos - 1; j >= 0; j--) { + if (b[j] == '\r') { + return (j + 1 < pos && b[j + 1] == '\n') ? "\r\n" : "\r"; + } + if (b[j] == '\n') { + return "\n"; + } + } + return "\n"; + } + + private static boolean isWsCp(int cp) { + switch (cp) { + case 0x09: case 0x0B: case 0x0C: case 0x20: case 0x85: + case 0xA0: case 0x1680: case 0x2028: case 0x2029: + case 0x202F: case 0x205F: case 0x3000: + return true; + default: + return cp >= 0x2000 && cp <= 0x200A; + } + } + + private static final class Planned { + final String body; + final boolean desiredRaw; + + Planned(String body, boolean desiredRaw) { + this.body = body; + this.desiredRaw = desiredRaw; + } + } +} diff --git a/lib/src/main/java/lang/ktav/pure/PureKtav.java b/lib/src/main/java/lang/ktav/pure/PureKtav.java index 9763665..3787f46 100644 --- a/lib/src/main/java/lang/ktav/pure/PureKtav.java +++ b/lib/src/main/java/lang/ktav/pure/PureKtav.java @@ -1,6 +1,9 @@ package lang.ktav.pure; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; import lang.ktav.Value; import lang.ktav.internal.ErrorEnvelope; @@ -183,7 +186,232 @@ public static String format(String src) { * implements. */ public static String version() { - return "0.8.1"; + return "0.9.0"; + } + + // ------------------------------------------------------------------ + // Lossless source edits (spec 0.9) + // ------------------------------------------------------------------ + + /** + * Replace an existing value addressed by the textual dotted path + * ({@code key1.key2.0.key4}; {@code \.} is a literal dot, {@code \\} + * a literal backslash, other escapes and empty segments are + * {@code EditInvalidPath}) with {@code replacement}, splicing only + * the target's bytes: comments, whitespace, key spelling and order, + * BOM, EOL style, and dotted/inline/block forms outside the + * effective edit are preserved. The source is never mutated; a + * replacement equal to the current value is a byte-identical no-op. + * + *

Errors surface as {@link KtavException} with + * {@code getError()} equal to {@code EditInvalidPath} (body: the + * UTF-8 byte offset inside the path argument), + * {@code EditNotFound}, or {@code EditUnsupported} (body: the + * reason). Parse errors of the source are the ordinary 0.9 parser + * envelopes. + */ + public static String editValue(String src, String path, + Value replacement) { + return editValue(encoded(src), path, replacement); + } + + /** Byte-level variant of {@link #editValue(String, String, Value)}. */ + public static String editValue(byte[] src, String path, + Value replacement) { + if (src == null) { + throw new NullPointerException("src"); + } + if (replacement == null) { + throw new NullPointerException("replacement"); + } + // Source-first, mirroring the C ABI contract: UTF-8 validation + // and a full grammar parse run BEFORE the path/transport decode, + // so a damaged source reports its parse envelope even when the + // path argument is also malformed. + Validated v = parseSourceFirst(src); + replacement = canonicalizeWire(replacement); + List segments = EditPath.parse(pathBytes(path)); + return applyEdit(v, segments, replacement); + } + + /** + * {@link #editValue(String, String, Value)} with pre-decoded + * literal segments: no additional unescaping is performed. A + * numeric segment addresses a string key inside an Object and a + * zero-based ASCII-digit index inside an Array. + */ + public static String editValueSegments(String src, + List segments, Value replacement) { + return editValueSegments(encoded(src), segments, replacement); + } + + /** Byte-level variant of {@link #editValueSegments(String, List, Value)}. */ + public static String editValueSegments(byte[] src, + List segments, Value replacement) { + if (src == null) { + throw new NullPointerException("src"); + } + if (segments == null) { + throw new NullPointerException("segments"); + } + if (replacement == null) { + throw new NullPointerException("replacement"); + } + // Source-first (C ABI contract): the source is validated and + // parsed before the transport decode (replacement canonicalize + // and the empty-segments NotFound decision), so a damaged source + // wins over every transport-level error. + Validated v = parseSourceFirst(src); + replacement = canonicalizeWire(replacement); + if (segments.isEmpty()) { + throw editFailure(lang.ktav.internal.ErrorEnvelope.of( + "EditNotFound", null, null, null, null, null, null, + null, null, null, + "edit path does not address an existing value")); + } + return applyEdit(v, segments, replacement); + } + + /** Validate UTF-8, locate the BOM, and parse the source — first. */ + private static Validated parseSourceFirst(byte[] src) { + try { + Validated v = validated(src); + PureParser.parse(v.b, v.start, false); + return v; + } catch (SourceError e) { + throw KtavException.fromEnvelope(ErrorEnvelope.of(e.error, + e.reason, e.line, e.lineText, e.spanStart, e.spanEnd, + e.path, e.body, e.canonical, e.specSection, + e.getMessage())); + } + } + + /** Splice an already-validated source; converts edit errors. */ + private static String applyEdit(Validated v, List segments, + Value replacement) { + try { + return EditSplice.apply(v.b, v.start, segments, replacement); + } catch (EditError e) { + throw editFailure(envelopeFor(e)); + } catch (SourceError e) { + throw KtavException.fromEnvelope(ErrorEnvelope.of(e.error, + e.reason, e.line, e.lineText, e.spanStart, e.spanEnd, + e.path, e.body, e.canonical, e.specSection, + e.getMessage())); + } + } + + /** + * Canonicalize a replacement the way the C ABI does when it decodes + * the wire ({@code cabi::wire canonical_integer / canonical_float}): + * {@link Value.Int} loses any {@code +} sign, redundant leading + * zeros, and signed zero (string-level — arbitrary precision is + * preserved, never i64-limited); {@link Value.Flt} must contain a + * {@code .} or exponent and re-rounds to the crate's canonical + * shortest form, with a non-finite result passing through verbatim + * (the writers reject it later, fail closed). An invalid payload is + * a wire-transport violation: a {@code Message} envelope without an + * invented source location. Value semantics elsewhere (the records + * themselves, the dumps guard) are untouched — this runs ONLY at the + * edit boundary, so {@code new Value.Int("+0001")} over {@code n: 1} + * is the same byte-identical no-op the native backend produces. + */ + static Value canonicalizeWire(Value v) { + if (v instanceof Value.Int i) { + return new Value.Int(canonicalInteger(i.text())); + } + if (v instanceof Value.Flt f) { + return new Value.Flt(canonicalFloatPayload(f.text())); + } + if (v instanceof Value.Obj o) { + LinkedHashMap out = new LinkedHashMap<>(); + o.entries().forEach((k, val) -> out.put(k, canonicalizeWire(val))); + return new Value.Obj(out); + } + if (v instanceof Value.Arr a) { + List out = new ArrayList<>(a.items().size()); + for (Value item : a.items()) { + out.add(canonicalizeWire(item)); + } + return new Value.Arr(out); + } + return v; + } + + /** Mirror of {@code canonical_integer}: string-level, not i64-parsed. */ + private static String canonicalInteger(String s) { + String sign; + String digits; + if (s.startsWith("-")) { + sign = "-"; + digits = s.substring(1); + } else if (s.startsWith("+")) { + sign = ""; + digits = s.substring(1); + } else { + sign = ""; + digits = s; + } + if (digits.isEmpty() + || !digits.chars().allMatch(c -> c >= '0' && c <= '9')) { + throw wireMessage("$i payload not an integer literal: \"" + + s + "\""); + } + String trimmed = digits.replaceFirst("^0+", ""); + return trimmed.isEmpty() ? "0" : sign + trimmed; + } + + /** Mirror of {@code canonical_float}: f64 parse + canonical shortest form. */ + private static String canonicalFloatPayload(String s) { + double val; + try { + val = Double.parseDouble(s); + } catch (NumberFormatException e) { + throw wireMessage("$f payload not a finite decimal: \"" + + s + "\""); + } + if (s.indexOf('.') < 0 && s.indexOf('e') < 0 && s.indexOf('E') < 0) { + throw wireMessage("$f payload must contain '.' or exponent: \"" + + s + "\""); + } + if (!Double.isFinite(val)) { + // Deliberate wire-only capability: passed through verbatim; + // the § 5.9.0 writers reject it at render time (fail closed). + return s; + } + return Scalars.canonicalFloat(val); + } + + private static KtavException wireMessage(String text) { + return KtavException.fromEnvelope(ErrorEnvelope.of( + "Message", null, null, null, null, null, null, null, null, + null, text)); + } + + static byte[] pathBytes(String path) { + if (path == null) { + throw new NullPointerException("path"); + } + return path.getBytes(StandardCharsets.UTF_8); + } + + private static lang.ktav.internal.ErrorEnvelope envelopeFor(EditError e) { + return switch (e.code) { + case INVALID_PATH -> ErrorEnvelope.of( + "EditInvalidPath", null, null, null, null, null, null, + String.valueOf(e.offset), null, null, e.getMessage()); + case NOT_FOUND -> ErrorEnvelope.of( + "EditNotFound", null, null, null, null, null, null, + null, null, null, e.getMessage()); + case UNSUPPORTED -> ErrorEnvelope.of( + "EditUnsupported", null, null, null, null, null, null, + e.reason, null, null, e.getMessage()); + }; + } + + private static KtavException editFailure( + lang.ktav.internal.ErrorEnvelope env) { + return KtavException.fromEnvelope(env); } private static byte[] encoded(String src) { diff --git a/lib/src/main/java/lang/ktav/pure/PureParser.java b/lib/src/main/java/lang/ktav/pure/PureParser.java index 00fda5a..257c3c3 100644 --- a/lib/src/main/java/lang/ktav/pure/PureParser.java +++ b/lib/src/main/java/lang/ktav/pure/PureParser.java @@ -622,7 +622,11 @@ private void finish() { && !f.explicit) { continue; // implicit root needs no closer (0.8.1 errata) } - throw SourceError.unpositioned("UnclosedCompound", "§6.1", + // 0.9 location contract: innermost opener..EOF, line and + // line_text of the opener (the source is valid UTF-8 here, + // so the opener line always decodes). + throw SourceError.unclosedCompound(f.openerLine, + Utf8.lineTextAt(ctx.b, f.openerOffset), f.openerOffset, ctx.b.length, "UnclosedCompound: compound opened at line " + f.openerLine + " is never closed"); diff --git a/lib/src/main/java/lang/ktav/pure/SourceError.java b/lib/src/main/java/lang/ktav/pure/SourceError.java index f65bd12..d48a890 100644 --- a/lib/src/main/java/lang/ktav/pure/SourceError.java +++ b/lib/src/main/java/lang/ktav/pure/SourceError.java @@ -66,6 +66,20 @@ static SourceError unpositioned(String error, String specSection, specSection); } + /** + * UnclosedCompound with the 0.9 location contract: the span covers + * the innermost opener through EOF and the line/line_text describe + * the opener's 1-based line (decoded from the VALID source — the + * opener offset always sits inside valid UTF-8 here). + */ + static SourceError unclosedCompound(int openerLine, String openerLineText, + int spanStart, int spanEnd, String message) { + return new SourceError(message, "UnclosedCompound", null, + (long) openerLine, openerLineText, + (long) spanStart, (long) spanEnd, + null, null, null, "§6.1"); + } + /** Error carrying decoded/raw key path segments. */ static SourceError withPath(String error, String specSection, int line, String lineText, int spanStart, int spanEnd, List path, diff --git a/lib/src/main/java/lang/ktav/pure/Utf8.java b/lib/src/main/java/lang/ktav/pure/Utf8.java index c7924fd..b6016b3 100644 --- a/lib/src/main/java/lang/ktav/pure/Utf8.java +++ b/lib/src/main/java/lang/ktav/pure/Utf8.java @@ -81,8 +81,9 @@ static void validate(byte[] bytes) { private static void fail(byte[] bytes, int start, int len) { int end = Math.min(start + Math.max(len, 1), bytes.length); int line = lineAt(bytes, start); - String lineText = lineTextAt(bytes, start); - throw SourceError.invalidUtf8(line, lineText, start, end); + // 0.9: line_text is null for malformed UTF-8 — the source is not + // valid text, so decoding a line would invent characters. + throw SourceError.invalidUtf8(line, null, start, end); } /** 1-based line containing byte offset {@code pos}, over raw § 3.2 terminators. */ diff --git a/lib/src/test/java/lang/ktav/Backend.java b/lib/src/test/java/lang/ktav/Backend.java index f18adf0..74c5346 100644 --- a/lib/src/test/java/lang/ktav/Backend.java +++ b/lib/src/test/java/lang/ktav/Backend.java @@ -33,6 +33,13 @@ interface Backend { String format(String src); + /** Lossless source edit by textual path (0.9). */ + String editValue(String src, String path, Value replacement); + + /** Lossless source edit by decoded segments (0.9). */ + String editValueSegments(String src, java.util.List segments, + Value replacement); + static Backend nativeBackend() { return new Backend() { @Override public String name() { @@ -74,6 +81,16 @@ static Backend nativeBackend() { @Override public String format(String src) { return Ktav.format(src); } + + @Override public String editValue(String src, String path, + Value replacement) { + return Ktav.editValue(src, path, replacement); + } + + @Override public String editValueSegments(String src, + java.util.List segments, Value replacement) { + return Ktav.editValueSegments(src, segments, replacement); + } }; } @@ -118,6 +135,18 @@ static Backend pureBackend() { @Override public String format(String src) { return lang.ktav.pure.PureKtav.format(src); } + + @Override public String editValue(String src, String path, + Value replacement) { + return lang.ktav.pure.PureKtav.editValue(src, path, + replacement); + } + + @Override public String editValueSegments(String src, + java.util.List segments, Value replacement) { + return lang.ktav.pure.PureKtav.editValueSegments(src, + segments, replacement); + } }; } } diff --git a/lib/src/test/java/lang/ktav/CorpusHarness.java b/lib/src/test/java/lang/ktav/CorpusHarness.java index 1ed87ae..aad3090 100644 --- a/lib/src/test/java/lang/ktav/CorpusHarness.java +++ b/lib/src/test/java/lang/ktav/CorpusHarness.java @@ -52,7 +52,7 @@ final class CorpusHarness { private static final Map EXPECTED_CATEGORIES = new LinkedHashMap<>(Map.of( "valid", 228, - "invalid", 90, + "invalid", 162, "unrepresentable", 5, "parseable-unrepresentable", 4, "strict-lossy", 13)); @@ -62,7 +62,7 @@ final class CorpusHarness { * {@code raw_bytes} (spec § 6.15). A different count is a corpus * shape the runner does not understand. */ - private static final int EXPECTED_RAW_BYTES_FLAGS = 9; + private static final int EXPECTED_RAW_BYTES_FLAGS = 51; private CorpusHarness() { } @@ -384,42 +384,113 @@ static Stream invalidFixtures(Backend backend) throws IOException { private static void runInvalid(Backend backend, Path ktavPath, Set rawByteFixtures) throws IOException { Path oraclePath = oracleFor(ktavPath); - String expected = expectedError(oraclePath, ktavPath); + Value oracle = WireJson.decode(Files.readAllBytes(oraclePath)); + Value field = ((Value.Obj) oracle).entries().get("expected_error"); + if (!(field instanceof Value.Str)) { + fail("oracle for " + ktavPath + " has no string \"expected_error\": " + oraclePath); + } + String expected = ((Value.Str) field).value(); + long[] location = expectedLocation(oracle, oraclePath, ktavPath); String stem = TestPaths.SPEC.relativize(ktavPath).toString().replace('\\', '/'); stem = stem.substring(0, stem.length() - ".ktav".length()); byte[] raw = Files.readAllBytes(ktavPath); + KtavException e; if (rawByteFixtures.contains(stem)) { try { strictDecodeUtf8(raw); fail("fixture " + ktavPath + " is flagged raw_bytes in manifest.json" + " but decodes as UTF-8 — the flag is wrong, not the decoder"); - } catch (CharacterCodingException e) { + } catch (CharacterCodingException ex) { // Expected: the strict decoder refuses the bytes. } assertEquals("InvalidUtf8", expected, "oracle for raw_bytes fixture " + ktavPath + " must expect InvalidUtf8"); - KtavException e = assertThrowsKtav(() -> backend.loads(raw), + e = assertThrowsKtav(() -> backend.loads(raw), "byte[] entry must reject invalid UTF-8: " + ktavPath); - assertEquals("InvalidUtf8", e.getError(), - "error class for raw_bytes fixture " + ktavPath); - assertNotNull(e.getSpanStart(), "span start for " + ktavPath); - assertNotNull(e.getSpanEnd(), "span end for " + ktavPath); - assertTrue(e.getSpanEnd() > e.getSpanStart(), - "InvalidUtf8 span must be non-empty: " + ktavPath); - return; + } else { + try { + strictDecodeUtf8(raw); + } catch (CharacterCodingException ex) { + fail("fixture " + ktavPath + " is not valid UTF-8 but is not flagged raw_bytes" + + " in manifest.json — add a fixture_flags entry for it"); + } + String src = new String(raw, StandardCharsets.UTF_8); + e = assertThrowsKtav(() -> backend.loads(src), + "expected parse error for " + ktavPath); } - try { - strictDecodeUtf8(raw); - } catch (CharacterCodingException e) { - fail("fixture " + ktavPath + " is not valid UTF-8 but is not flagged raw_bytes" - + " in manifest.json — add a fixture_flags entry for it"); - } - String src = new String(raw, StandardCharsets.UTF_8); - KtavException e = assertThrowsKtav(() -> backend.loads(src), - "expected parse error for " + ktavPath); assertEquals(expected, e.getError(), "error class for " + ktavPath); + if (location != null) { + // 0.9 expected_location: line/start and exactly one end or + // minimum_end; raw-byte UTF-8 offsets, BOM included. + assertTrue(location[0] >= 1, "line must be >= 1 for " + ktavPath); + assertEquals(location[0], e.getLine() == null ? -1 : e.getLine(), + "line for " + ktavPath); + assertEquals(location[1], e.getSpanStart() == null ? -1 : e.getSpanStart(), + "span start for " + ktavPath); + if (location[3] != 0) { // minimum_end variant + assertTrue(e.getSpanEnd() != null + && e.getSpanEnd() >= location[3], + "span end must be >= minimum_end " + location[3] + + " for " + ktavPath + " (got " + e.getSpanEnd() + ")"); + } else { + assertEquals(location[2], e.getSpanEnd() == null ? -1 : e.getSpanEnd(), + "span end for " + ktavPath); + } + assertTrue(e.getSpanEnd() == null || e.getSpanEnd() > e.getSpanStart(), + "span must be non-empty: " + ktavPath); + if ("InvalidUtf8".equals(expected)) { + assertTrue(e.getLineText() == null, + "line_text must be null for invalid UTF-8: " + ktavPath); + } + } + } + + /** + * Read the oracle's optional {@code expected_location} object: + * {@code line}, {@code start}, and EXACTLY one of {@code end} / + * {@code minimum_end}. Returns {line, start, end, minimumEndFlag} + * or null when the oracle carries no location metadata (pre-0.9 + * fixtures). + */ + private static long[] expectedLocation(Value oracle, Path oraclePath, + Path ktavPath) { + Value loc = ((Value.Obj) oracle).entries().get("expected_location"); + if (loc == null) { + return null; + } + assertTrue(loc instanceof Value.Obj, + "expected_location must be an object: " + oraclePath); + Map m = ((Value.Obj) loc).entries(); + long line = intField(m, "line", oraclePath, ktavPath); + long start = intField(m, "start", oraclePath, ktavPath); + Value end = m.get("end"); + Value minEnd = m.get("minimum_end"); + assertTrue(end == null || end instanceof Value.Int, + "end must be a number: " + oraclePath); + assertTrue(minEnd == null || minEnd instanceof Value.Int, + "minimum_end must be a number: " + oraclePath); + assertTrue(end != null ^ minEnd != null, + "expected_location must carry exactly one of end/" + + "minimum_end: " + oraclePath); + if (end instanceof Value.Int e) { + long[] out = { line, start, e.text().isEmpty() ? -1 + : Long.parseLong(e.text()), 0 }; + assertTrue(out[2] > start, "end must exceed start: " + oraclePath); + return out; + } + long min = Long.parseLong(((Value.Int) minEnd).text()); + assertTrue(min > start, "minimum_end must exceed start: " + oraclePath); + return new long[] { line, start, -1, min }; + } + + private static long intField(Map m, String name, + Path oraclePath, Path ktavPath) { + Value v = m.get(name); + assertTrue(v instanceof Value.Int, + "expected_location." + name + " must be a number: " + oraclePath); + return Long.parseLong(((Value.Int) v).text()); } // ── Shared helpers ────────────────────────────────────────────────── diff --git a/lib/src/test/java/lang/ktav/EditValueTest.java b/lib/src/test/java/lang/ktav/EditValueTest.java new file mode 100644 index 0000000..a27836c --- /dev/null +++ b/lib/src/test/java/lang/ktav/EditValueTest.java @@ -0,0 +1,549 @@ +package lang.ktav; + +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestFactory; + +import java.util.List; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * Verbatim port of the reference layout regressions + * ({@code tests/suite/api/text/edit_value/layout.rs}, 22 named tests, + * every tabular subcase, exact source/expected bytes). Every case runs + * BOTH API forms (textual path + decoded segments) on BOTH backends; + * the engine's own semantic verification covers the reference's + * {@code parse(result) == wanted} assertion. + */ +final class EditValueTest { + + /** The reference {@code check}: both forms must yield exactly {@code expected}. */ + private static void check(Backend backend, String source, + List path, Value replacement, String expected, + String name) { + String textual = textual(path); + assertEquals(expected, backend.editValue(source, textual, replacement), + name + " [textual]"); + assertEquals(expected, + backend.editValueSegments(source, path, replacement), + name + " [segments]"); + } + + /** Escape each segment and join with '.', like the reference helper. */ + private static String textual(List segments) { + StringBuilder out = new StringBuilder(); + for (int i = 0; i < segments.size(); i++) { + if (i > 0) { + out.append('.'); + } + out.append(segments.get(i).replace("\\", "\\\\") + .replace(".", "\\.")); + } + return out.toString(); + } + + private static Value s(String v) { + return new Value.Str(v); + } + + private static Value obj(Object... kv) { + java.util.LinkedHashMap m = new java.util.LinkedHashMap<>(); + for (int i = 0; i < kv.length; i += 2) { + m.put((String) kv[i], (Value) kv[i + 1]); + } + return new Value.Obj(m); + } + + private static Value arr(Value... items) { + return new Value.Arr(List.of(items)); + } + + // ── 1 ─────────────────────────────────────────────────────────────── + + private static void deepLeafPreservesEachSourceForm(Backend b) { + String name = "deep_leaf_preserves_each_source_form"; + for (String source : new String[] { + "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"}) { + check(b, source, + List.of("targets", "storagehost", "address"), + s("192.168.0.88:22"), + source.replace("old", "192.168.0.88:22"), name); + } + } + + // ── 2 ─────────────────────────────────────────────────────────────── + + private static void deepArrayLeafPreservesMixedForms(Backend b) { + String name = "deep_array_leaf_preserves_mixed_forms"; + for (String source : new String[] { + "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"}) { + check(b, source, + List.of("targets", "0", "storagehost", "0"), + s("new"), source.replace("old", "new"), name); + } + } + + // ── 3 ─────────────────────────────────────────────────────────────── + + private static void wholeInlineContainersRemainInline(Backend b) { + String name = "whole_inline_containers_remain_inline"; + Object[][] cases = { + {"a: {x: 1}\n", obj("y", new Value.Int("2")), "a: {y: 2}\n"}, + {"a: [1, 2]\n", arr(new Value.Int("3"), new Value.Int("4")), + "a: [3, 4]\n"}, + {"a: {}\n", obj("y", new Value.Int("2")), "a: {y: 2}\n"}, + {"a: []\n", arr(new Value.Int("3"), new Value.Int("4")), + "a: [3, 4]\n"}, + {"a: {}\n", arr(new Value.Int("3"), new Value.Int("4")), + "a: [3, 4]\n"}, + }; + for (Object[] c : cases) { + check(b, (String) c[0], List.of("a"), (Value) c[1], + (String) c[2], name); + } + } + + // ── 4 ─────────────────────────────────────────────────────────────── + + private static void emptyingBlockContainersDoesNotCollapseThem(Backend b) { + String name = "emptying_block_containers_does_not_collapse_them"; + check(b, "a: {\n x: 1\n}\n", List.of("a"), obj(), + "a: {\n}\n", name); + check(b, "a: [\n 1\n]\n", List.of("a"), arr(), + "a: [\n]\n", name); + } + + // ── 5 ─────────────────────────────────────────────────────────────── + + private static void newlineUnfoldsOnlyInlineAncestorChain(Backend b) { + check(b, + "## top\na: [{x: old, untouched: [1, 2]}, {keep: yes}]\nz: keep\n", + List.of("a", "0", "x"), s("one\ntwo"), + "## top\na: [\n {\n x: ((\none\ntwo\n ))\n" + + " untouched: [1, 2]\n }\n {keep: yes}\n]" + + "\nz: keep\n", + "newline_unfolds_only_inline_ancestor_chain"); + } + + // ── 6 ─────────────────────────────────────────────────────────────── + + private static void newlineUnderDottedKeyDoesNotExpandDottedAncestors( + Backend b) { + check(b, + "a.b: [old, keep]\na.c: other\n", + List.of("a", "b", "0"), s("one\ntwo"), + "a.b: [\n ((\none\ntwo\n ))\n keep\n]\na.c: other\n", + "newline_under_dotted_key_does_not_expand_dotted_ancestors"); + } + + // ── 7 ─────────────────────────────────────────────────────────────── + + private static void denseEscapesUnfoldButSparseEscapesDoNot(Backend b) { + String name = "dense_escapes_unfold_but_sparse_escapes_do_not"; + check(b, "a: [old, keep]\n", List.of("a", "0"), s("a,b,c,d,e"), + "a: [\n a,b,c,d,e\n keep\n]\n", name); + check(b, "a: [old]\n", List.of("a", "0"), s("a,b,c,d"), + "a: [a\\,b\\,c\\,d]\n", name); + String sparse = "x".repeat(30) + "a,b,c,d,e"; + check(b, "a: [old]\n", List.of("a", "0"), s(sparse), + "a: [" + sparse.replace(",", "\\,") + "]\n", name); + } + + // ── 8 ─────────────────────────────────────────────────────────────── + + private static void widthBoundaryIsInCharactersNotBytes(Backend b) { + String name = "width_boundary_is_in_characters_not_bytes"; + for (Object[] b2 : new Object[][] {{95, false}, {96, true}}) { + int size = (Integer) b2[0]; + boolean expanded = (Boolean) b2[1]; + String value = "я".repeat(size); + String expected = expanded + ? "a: [\n " + value + "\n]\n" + : "a: [" + value + "]\n"; + check(b, "a: [old]\n", List.of("a", "0"), s(value), expected, name); + } + } + + // ── 9 ─────────────────────────────────────────────────────────────── + + private static void alreadyLongLineOnlyUnfoldsWhenItGrows(Backend b) { + String name = "already_long_line_only_unfolds_when_it_grows"; + String source = "a: [" + "x".repeat(120) + ", keep]\n"; + check(b, source, List.of("a", "0"), s("y".repeat(120)), + source.replace("x", "y"), name); + check(b, source, List.of("a", "0"), s("y".repeat(121)), + "a: [\n " + "y".repeat(121) + "\n keep\n]\n", name); + } + + // ── 10 ────────────────────────────────────────────────────────────── + + private static void widthExpansionStopsWhenInnerLineFits(Backend b) { + String value = "x".repeat(91); + check(b, "a: [{x: old}]\n", List.of("a", "0", "x"), s(value), + "a: [\n {x: " + value + "}\n]\n", + "width_expansion_stops_when_inner_line_fits"); + } + + // ── 11 ────────────────────────────────────────────────────────────── + + private static void escapeOverheadBoundaryIsExact(Backend b) { + String name = "escape_overhead_boundary_is_exact"; + String dense = "x".repeat(7) + "a,b,c,d,e"; + String sparse = "x".repeat(8) + "a,b,c,d,e"; + check(b, "a: [old]\n", List.of("a", "0"), s(dense), + "a: [\n " + dense + "\n]\n", name); + check(b, "a: [old]\n", List.of("a", "0"), s(sparse), + "a: [" + sparse.replace(",", "\\,") + "]\n", name); + } + + // ── 12 ────────────────────────────────────────────────────────────── + + private static void longChangedLeafDoesNotUnfoldUnrelatedNestedBranches( + Backend b) { + String value = "x".repeat(100); + check(b, "a: [old, {untouched: [1, 2]}]\n", List.of("a", "0"), + s(value), + "a: [\n " + value + "\n {untouched: [1, 2]}\n]\n", + "long_changed_leaf_does_not_unfold_unrelated_nested_branches"); + } + + // ── 13 ────────────────────────────────────────────────────────────── + + private static void promotedSiblingsKeepValuesWhenInlineEscapesLoseMeaning( + Backend b) { + check(b, + "a: [old, a\\,b, \\u00342, :: literal]\n", + List.of("a", "0"), s("x\ny"), + "a: [\n ((\nx\ny\n ))\n a,b\n :: 42\n" + + " :: :: literal\n]\n", + "promoted_siblings_keep_values_when_inline_escapes_lose_meaning"); + } + + // ── 14 ────────────────────────────────────────────────────────────── + + private static void promotedPairsKeepKeySpellingAndRawMarkers(Backend b) { + check(b, + "a: {\"x.y\": old, nested.key:: 42, n: 0007}\n", + List.of("a", "x.y"), s("x\ny"), + "a: {\n \"x.y\": ((\nx\ny\n ))\n" + + " nested.key:: 42\n n: 0007\n}\n", + "promoted_pairs_keep_key_spelling_and_raw_markers"); + } + + // ── 15 ────────────────────────────────────────────────────────────── + + private static void rootInlinePromotionPreservesBomAndEof(Backend b) { + check(b, "\uFEFF[old, keep]", List.of("0"), s("x\ny"), + "\uFEFF[\n ((\nx\ny\n ))\n keep\n]", + "root_inline_promotion_preserves_bom_and_eof"); + } + + // ── 16 ────────────────────────────────────────────────────────────── + + private static void promotionPreservesEolCommentsAndNoFinalTerminator( + Backend b) { + String name = "promotion_preserves_eol_comments_and_no_final_terminator"; + for (String eol : new String[] {"\n", "\r", "\r\n"}) { + String source = "## header" + eol + "a: [old, keep]"; + String expected = "## header" + eol + "a: [" + eol + " ((" + + eol + "x" + eol + "y" + eol + " ))" + eol + + " keep" + eol + "]"; + check(b, source, List.of("a", "0"), s("x\ny"), expected, name); + } + } + + // ── 17 ────────────────────────────────────────────────────────────── + + private static void commentsOutsidePromotedInlineValueRemainByteExact( + Backend b) { + check(b, + "list: [\n ## before\n {x: old}\n ## after\n]\n", + List.of("list", "0", "x"), s("x\ny"), + "list: [\n ## before\n {\n x: ((\nx\ny\n ))\n" + + " }\n ## after\n]\n", + "comments_outside_promoted_inline_value_remain_byte_exact"); + } + + // ── 18 ────────────────────────────────────────────────────────────── + + private static void noopIgnoresAllReadabilityThresholds(Backend b) { + String name = "noop_ignores_all_readability_thresholds"; + String source = "a: [" + "x".repeat(140) + ", a\\,b\\,c\\,d\\,e]\n"; + check(b, source, List.of("a", "0"), s("x".repeat(140)), source, name); + check(b, source, List.of("a", "1"), s("a,b,c,d,e"), source, name); + } + + // ── 19 ────────────────────────────────────────────────────────────── + + private static void typeChangeDoesNotForceUnrelatedLayoutChanges( + Backend b) { + String name = "type_change_does_not_force_unrelated_layout_changes"; + check(b, "a: {x: [1], keep: [2]}\n", List.of("a", "x"), + Value.Bool.TRUE, "a: {x: true, keep: [2]}\n", name); + check(b, "a: [1]\n", List.of("a"), s("42"), "a:: 42\n", name); + } + + // ── 20 ────────────────────────────────────────────────────────────── + + private static void reopenedInlineParentPromotesOnlyItsPhysicalDefinition( + Backend b) { + check(b, + "a: {x: old}\n## gap\na.y: keep\n", + List.of("a", "x"), s("x\ny"), + "a: {\n x: ((\nx\ny\n ))\n}\n## gap\na.y: keep\n", + "reopened_inline_parent_promotes_only_its_physical_definition"); + } + + // ── 21 ────────────────────────────────────────────────────────────── + + private static void impossibleBlockEncodingKeepsValidInlineForm(Backend b) { + String name = "impossible_block_encoding_keeps_valid_inline_form"; + for (String value : new String[] {"a\rb", "a\n))\nb"}) { + String encoded = value.replace("\r", "\\r").replace("\n", "\\n"); + check(b, "a: [old]\n", List.of("a", "0"), s(value), + "a: [" + encoded + "]\n", name); + } + check(b, "a: [old, a\\rb]\n", List.of("a", "0"), s("x\ny"), + "a: [x\\ny, a\\rb]\n", name); + } + + // ── 22 ────────────────────────────────────────────────────────────── + + private static void unsafeCommentLossStillFailsClosed(Backend b) { + String name = "unsafe_comment_loss_still_fails_closed"; + for (String source : new String[] { + "a: {\n ## keep\n x: 1\n}\n", + "a: [\n ## keep\n 1\n]\n"}) { + KtavException e = assertThrows(KtavException.class, + () -> b.editValueSegments(source, List.of("a"), + Value.Null.NULL), + name + " " + source); + assertEquals("EditUnsupported", e.getError(), name + " " + source); + } + } + + // ── runners ───────────────────────────────────────────────────────── + + private static final String[] NAMES = { + "deep_leaf_preserves_each_source_form", + "deep_array_leaf_preserves_mixed_forms", + "whole_inline_containers_remain_inline", + "emptying_block_containers_does_not_collapse_them", + "newline_unfolds_only_inline_ancestor_chain", + "newline_under_dotted_key_does_not_expand_dotted_ancestors", + "dense_escapes_unfold_but_sparse_escapes_do_not", + "width_boundary_is_in_characters_not_bytes", + "already_long_line_only_unfolds_when_it_grows", + "width_expansion_stops_when_inner_line_fits", + "escape_overhead_boundary_is_exact", + "long_changed_leaf_does_not_unfold_unrelated_nested_branches", + "promoted_siblings_keep_values_when_inline_escapes_lose_meaning", + "promoted_pairs_keep_key_spelling_and_raw_markers", + "root_inline_promotion_preserves_bom_and_eof", + "promotion_preserves_eol_comments_and_no_final_terminator", + "comments_outside_promoted_inline_value_remain_byte_exact", + "noop_ignores_all_readability_thresholds", + "type_change_does_not_force_unrelated_layout_changes", + "reopened_inline_parent_promotes_only_its_physical_definition", + "impossible_block_encoding_keeps_valid_inline_form", + "unsafe_comment_loss_still_fails_closed", + }; + + private static void runCase(Backend b, String name) { + switch (name) { + case "deep_leaf_preserves_each_source_form" -> + deepLeafPreservesEachSourceForm(b); + case "deep_array_leaf_preserves_mixed_forms" -> + deepArrayLeafPreservesMixedForms(b); + case "whole_inline_containers_remain_inline" -> + wholeInlineContainersRemainInline(b); + case "emptying_block_containers_does_not_collapse_them" -> + emptyingBlockContainersDoesNotCollapseThem(b); + case "newline_unfolds_only_inline_ancestor_chain" -> + newlineUnfoldsOnlyInlineAncestorChain(b); + case "newline_under_dotted_key_does_not_expand_dotted_ancestors" -> + newlineUnderDottedKeyDoesNotExpandDottedAncestors(b); + case "dense_escapes_unfold_but_sparse_escapes_do_not" -> + denseEscapesUnfoldButSparseEscapesDoNot(b); + case "width_boundary_is_in_characters_not_bytes" -> + widthBoundaryIsInCharactersNotBytes(b); + case "already_long_line_only_unfolds_when_it_grows" -> + alreadyLongLineOnlyUnfoldsWhenItGrows(b); + case "width_expansion_stops_when_inner_line_fits" -> + widthExpansionStopsWhenInnerLineFits(b); + case "escape_overhead_boundary_is_exact" -> + escapeOverheadBoundaryIsExact(b); + case "long_changed_leaf_does_not_unfold_unrelated_nested_branches" -> + longChangedLeafDoesNotUnfoldUnrelatedNestedBranches(b); + case "promoted_siblings_keep_values_when_inline_escapes_lose_meaning" -> + promotedSiblingsKeepValuesWhenInlineEscapesLoseMeaning(b); + case "promoted_pairs_keep_key_spelling_and_raw_markers" -> + promotedPairsKeepKeySpellingAndRawMarkers(b); + case "root_inline_promotion_preserves_bom_and_eof" -> + rootInlinePromotionPreservesBomAndEof(b); + case "promotion_preserves_eol_comments_and_no_final_terminator" -> + promotionPreservesEolCommentsAndNoFinalTerminator(b); + case "comments_outside_promoted_inline_value_remain_byte_exact" -> + commentsOutsidePromotedInlineValueRemainByteExact(b); + case "noop_ignores_all_readability_thresholds" -> + noopIgnoresAllReadabilityThresholds(b); + case "type_change_does_not_force_unrelated_layout_changes" -> + typeChangeDoesNotForceUnrelatedLayoutChanges(b); + case "reopened_inline_parent_promotes_only_its_physical_definition" -> + reopenedInlineParentPromotesOnlyItsPhysicalDefinition(b); + case "impossible_block_encoding_keeps_valid_inline_form" -> + impossibleBlockEncodingKeepsValidInlineForm(b); + case "unsafe_comment_loss_still_fails_closed" -> + unsafeCommentLossStillFailsClosed(b); + default -> throw new AssertionError("unknown case " + name); + } + } + + @TestFactory + Stream native() { + Backend backend = Backend.nativeBackend(); + CorpusHarness.requireCabiFor(backend); + return factor(backend); + } + + @TestFactory + Stream pure() { + return factor(Backend.pureBackend()); + } + + private static Stream factor(Backend backend) { + return java.util.Arrays.stream(NAMES) + .map(n -> DynamicTest.dynamicTest(backend.name() + ": " + n, + () -> runCase(backend, n))); + } + + // ── numeric wire parity (canonical_integer / canonical_float) ─────── + + private static void runNumericParity(Backend b) { + // Non-canonical but VALID numeric tags are normalized exactly + // like the C ABI's wire decode, so the edit is a byte-identical + // no-op — not a change, not an Unsupported. + check(b, "n: 1\n", List.of("n"), new Value.Int("+0001"), "n: 1\n", + "int plus/leading-zeros no-op"); + check(b, "n: 1.5\n", List.of("n"), new Value.Flt("1.50"), "n: 1.5\n", + "float trailing-zeros no-op"); + check(b, "n: 0\n", List.of("n"), new Value.Int("-0"), "n: 0\n", + "int signed zero no-op"); + check(b, "n: -0.0\n", List.of("n"), new Value.Flt("-0.0"), "n: -0.0\n", + "float signed zero no-op"); + // Arbitrary precision is preserved on the WIRE (never i64-clamped), + // but the SOURCE parser's i64 domain routes a 30-digit bare literal + // to String — so a typed Integer over it can only fail closed: + // EditUnsupported (semantic guard), NOT Message, NOT a silent + // String-typed write. This is the proof the wire was not truncated. + String huge = "9".repeat(30); + KtavException hugeNoop = assertThrows(KtavException.class, + () -> b.editValueSegments("n: " + huge + "\n", List.of("n"), + new Value.Int(huge)), + "30-digit typed Integer over a String literal"); + assertEquals("EditUnsupported", hugeNoop.getError(), + "wire keeps arbitrary digits; the semantic guard fails closed"); + KtavException hugeTextual = assertThrows(KtavException.class, + () -> b.editValue("n: " + huge + "\n", "n", new Value.Int(huge)), + "30-digit typed Integer over a String literal [textual]"); + assertEquals("EditUnsupported", hugeTextual.getError()); + // Positive precision control: > 2^53 but within i64 — the value IS + // a typed Integer in the source, so it round-trips exactly. + String big = "9007199254740993"; + check(b, "n: " + big + "\n", List.of("n"), new Value.Int(big), + "n: " + big + "\n", "bigint no-op (>2^53, within i64)"); + check(b, "n: 1\n", List.of("n"), new Value.Int(big), + "n: " + big + "\n", "bigint change"); + // Malformed payloads are wire-transport Message errors, never + // Edit* and never an invented source location. + for (Value bad : new Value[] { + new Value.Int("12a3"), new Value.Int("+"), new Value.Int(""), + new Value.Flt("abc"), new Value.Flt("12")}) { + KtavException e = assertThrows(KtavException.class, + () -> b.editValueSegments("n: 1\n", List.of("n"), bad), + "malformed numeric payload " + bad); + assertEquals("Message", e.getError(), + "malformed numeric payload " + bad); + assertNull(e.getSpecSection(), + "Message carries no invented spec section"); + } + // Non-finite wire floats stay verbatim and fail closed downstream + // (the writers refuse them) — Unsupported, not a silent write. + KtavException inf = assertThrows(KtavException.class, + () -> b.editValueSegments("n: 1.0\n", List.of("n"), + new Value.Flt("1e400")), + "non-finite float payload"); + assertEquals("EditUnsupported", inf.getError(), + "non-finite float payload fails closed"); + } + + @TestFactory + Stream numericParityNative() { + Backend backend = Backend.nativeBackend(); + CorpusHarness.requireCabiFor(backend); + return Stream.of(DynamicTest.dynamicTest("native: numeric wire parity", + () -> runNumericParity(backend))); + } + + @TestFactory + Stream numericParityPure() { + Backend backend = Backend.pureBackend(); + return Stream.of(DynamicTest.dynamicTest("pure: numeric wire parity", + () -> runNumericParity(backend))); + } + + // ── source-first contract over raw bytes ──────────────────────────── + + /** + * Source-first contract over RAW bytes: an invalid-UTF-8 source must + * report the InvalidUtf8 parse envelope even when the path argument + * is also malformed, and even with an empty segments list — never + * the transport-level {@code EditInvalidPath} / {@code + * EditNotFound}. + */ + private static void runSourceFirstRawBytes(byte[] damaged) { + byte[] bad = java.util.Arrays.copyOf(damaged, damaged.length); + KtavException badPath = assertThrows(KtavException.class, + () -> lang.ktav.pure.PureKtav.editValue(bad, "a\\q", + new Value.Int("1"))); + assertEquals("InvalidUtf8", badPath.getError(), + "UTF-8 validation precedes the path decode"); + KtavException emptySegs = assertThrows(KtavException.class, + () -> lang.ktav.pure.PureKtav.editValueSegments(bad, + List.of(), new Value.Int("1"))); + assertEquals("InvalidUtf8", emptySegs.getError(), + "UTF-8 validation precedes the empty-segments NotFound"); + } + + @Test + void pureSourceFirstOverRawBytes() { + byte[] damaged = "k: 1\n".getBytes(java.nio.charset.StandardCharsets.UTF_8); + byte[] raw = java.util.Arrays.copyOf(damaged, damaged.length + 1); + raw[raw.length - 1] = (byte) 0xFF; + runSourceFirstRawBytes(raw); + } + + @Test + void nativeSourceFirstOverRawBytes() { + org.junit.jupiter.api.Assumptions.assumeTrue(TestPaths.cabiBuilt(), + CorpusHarness.CABI_MISSING + " (" + TestPaths.CABI + ")"); + byte[] damaged = "k: 1\n".getBytes(java.nio.charset.StandardCharsets.UTF_8); + byte[] raw = java.util.Arrays.copyOf(damaged, damaged.length + 1); + raw[raw.length - 1] = (byte) 0xFF; + KtavException e = assertThrows(KtavException.class, + () -> Ktav.editValue(raw, "a\\q", new Value.Int("1"))); + assertEquals("InvalidUtf8", e.getError(), + "the C ABI validates and parses the source first"); + } + + private EditValueTest() { + } +} diff --git a/lib/src/test/java/lang/ktav/NativeAbiVersionTest.java b/lib/src/test/java/lang/ktav/NativeAbiVersionTest.java new file mode 100644 index 0000000..648dc39 --- /dev/null +++ b/lib/src/test/java/lang/ktav/NativeAbiVersionTest.java @@ -0,0 +1,32 @@ +package lang.ktav; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * ABI gate: a loaded native library must report ABI version 2 — the 0.9 + * exported set (with the source-edit symbols). An ABI-1 binary must be + * rejected with a clear version-mismatch error, never silently used and + * never hidden behind a pure fallback. Runs only where the cabi exists; + * the rejection path itself is exercised by the loader, so this test + * pins the positive side and the expected constant. + */ +final class NativeAbiVersionTest { + + @Test + void loadedLibraryReportsAbi2() { + org.junit.jupiter.api.Assumptions.assumeTrue( + TestPaths.cabiBuilt(), + CorpusHarness.CABI_MISSING + " (" + TestPaths.CABI + ")"); + TestPaths.init(); + // The loader itself enforces the version; reaching the call + // proves the gate accepted the library. The value is re-read + // here so a stale EXPECTED_ABI_VERSION constant fails loudly. + int abi = lang.ktav.internal.NativeLib.get().ktav_abi_version(); + assertEquals(2, abi, + "ktav_cabi must report ABI 2 for 0.9 (source-edit exports)"); + assertTrue(Ktav.nativeVersion() != null); + } +} diff --git a/lib/src/test/java/lang/ktav/PureApiTest.java b/lib/src/test/java/lang/ktav/PureApiTest.java index b874aa8..d24029c 100644 --- a/lib/src/test/java/lang/ktav/PureApiTest.java +++ b/lib/src/test/java/lang/ktav/PureApiTest.java @@ -25,7 +25,10 @@ final class PureApiTest { @Test void versionMatchesImplementedSpec() { - assertEquals("0.8.1", lang.ktav.pure.PureKtav.version()); + // The SPEC version the pure backend implements (0.9 source + // diagnostics + lossless source edit) — deliberately NOT the + // package version, which stays 0.8.1 until release. + assertEquals("0.9.0", lang.ktav.pure.PureKtav.version()); } // ── Entry points: byte[] vs String agree ──────────────────────────── diff --git a/lib/src/test/java/lang/ktav/PureNegativeTest.java b/lib/src/test/java/lang/ktav/PureNegativeTest.java index 694baab..e36cd1b 100644 --- a/lib/src/test/java/lang/ktav/PureNegativeTest.java +++ b/lib/src/test/java/lang/ktav/PureNegativeTest.java @@ -33,15 +33,49 @@ private static KtavException err(String src) { // ── Compound structure ────────────────────────────────────────────── @Test - void unclosedExplicitRootSpansOpenerToEofWithoutALine() { + void unclosedExplicitRootSpansOpenerToEofWithTheOpenerLine() { KtavException e = err("a: ["); assertEquals("UnclosedCompound", e.getError()); assertEquals("§6.1", e.getSpecSection()); - assertNull(e.getLine(), "UnclosedCompound carries a span, not a line"); + // 0.9 location contract: the span is opener..EOF, and the line + // number AND line_text describe the opener (valid UTF-8 source, + // so the opener line always decodes). + assertEquals(1L, e.getLine(), "line is the opener's line"); + assertEquals("a: [", e.getLineText(), "line_text is the opener line"); assertEquals(3L, e.getSpanStart(), "span starts at the opener byte"); assertEquals(4L, e.getSpanEnd(), "span ends at EOF"); } + /** + * 0.9 opener-line regressions: line counting is over raw § 3.2 + * terminators (LF, lone CR, CRLF — one EOL each) and the line_text + * strips exactly one trailing CR; the BOM does not rebase offsets. + */ + @Test + void unclosedCompoundOpenerLineOverLfCrCrlfAndBom() { + KtavException lf = err("k: 1\na: ["); + assertEquals(2L, lf.getLine(), "LF: opener on line 2"); + assertEquals("a: [", lf.getLineText()); + assertEquals(6L, lf.getSpanStart()); + + KtavException cr = err("k: 1\ra: ["); + assertEquals(2L, cr.getLine(), "lone CR: opener on line 2"); + assertEquals("a: [", cr.getLineText()); + assertEquals(6L, cr.getSpanStart()); + + KtavException crlf = err("k: 1\r\na: ["); + assertEquals(2L, crlf.getLine(), "CRLF is ONE terminator"); + assertEquals("a: [", crlf.getLineText(), + "line_text strips exactly one trailing CR"); + assertEquals(7L, crlf.getSpanStart()); + + KtavException bom = err("\uFEFFk: 1\na: ["); + assertEquals(2L, bom.getLine()); + assertEquals("a: [", bom.getLineText()); + assertEquals(10L, bom.getSpanStart(), + "BOM stays in the raw coordinates (3 + 6 + 1)"); + } + @Test void crossedCloserIsUnbalancedBracket() { KtavException e = err("a: [\n}\n"); @@ -347,9 +381,10 @@ void invalidUtf8SpanCoversTheMalformedSequence() { assertEquals("InvalidUtf8", e.getError()); assertEquals("§6.15", e.getSpecSection()); assertEquals(1L, e.getLine()); - // lineText is the raw line DECODED as UTF-8; the invalid byte - // becomes U+FFFD, and the text ends where the line ends. - assertEquals("a: [1, \uFFFD]", e.getLineText()); + // 0.9: line_text is NULL for invalid UTF-8 — the source is not + // valid text, so no line may be decoded (a lossily decoded line + // with U+FFFD would pretend the source was valid). + assertNull(e.getLineText(), "line_text must be null for InvalidUtf8"); assertEquals(7L, e.getSpanStart(), "span starts at the first invalid byte"); assertEquals(8L, e.getSpanEnd()); } @@ -376,8 +411,7 @@ void truncatedMultibyteAtEofSpansToEof() { assertEquals("InvalidUtf8", e.getError()); assertEquals(3L, e.getSpanStart(), "span starts at the truncated lead byte"); assertEquals(5L, e.getSpanEnd(), "truncated sequence clamps at EOF"); - assertEquals("a: \uFFFD", e.getLineText(), - "raw line decoded with replacement for the truncated bytes"); + assertNull(e.getLineText(), "0.9: line_text null for InvalidUtf8"); } @Test diff --git a/lib/src/test/java/lang/ktav/PureNoNativeTest.java b/lib/src/test/java/lang/ktav/PureNoNativeTest.java index 5678858..b7c9dd6 100644 --- a/lib/src/test/java/lang/ktav/PureNoNativeTest.java +++ b/lib/src/test/java/lang/ktav/PureNoNativeTest.java @@ -64,7 +64,8 @@ static void restoreLoaderForOtherTests() { @Test void versionNeedsNoNative() { - assertEquals("0.8.1", lang.ktav.pure.PureKtav.version()); + // Spec version (0.9), not the package version. + assertEquals("0.9.0", lang.ktav.pure.PureKtav.version()); } @Test diff --git a/lib/src/test/java/lang/ktav/TestPaths.java b/lib/src/test/java/lang/ktav/TestPaths.java index aa561fa..d380b3f 100644 --- a/lib/src/test/java/lang/ktav/TestPaths.java +++ b/lib/src/test/java/lang/ktav/TestPaths.java @@ -20,7 +20,7 @@ final class TestPaths { private static final Path REPO = Paths.get("..").toAbsolutePath().normalize(); static final Path CABI = cabiPath(); - static final Path SPEC = REPO.resolve("spec").resolve("versions").resolve("0.8").resolve("tests"); + static final Path SPEC = REPO.resolve("spec").resolve("versions").resolve("0.9").resolve("tests"); static { String override = System.getenv("KTAV_LIB_PATH"); diff --git a/root-docs/CHANGELOG/manifest.js b/root-docs/CHANGELOG/manifest.js index 8e2acd3..ef9131b 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-1", "v0-8-0", 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/editing/body-1.md b/root-docs/README/editing/body-1.md new file mode 100644 index 0000000..53d2590 --- /dev/null +++ b/root-docs/README/editing/body-1.md @@ -0,0 +1,71 @@ +>>>>> lang=en +### Editing source text + +`Ktav.editValue()` replaces an **existing** value in Ktav source text +and returns new source text — a lossless, byte-level splice, not a +parse→render round-trip: + +```java +String out = Ktav.editValue("port: 8080\nhost: db\n", "port", + new Value.Int("9090")); +// port: 9090 +// host: db +``` + +Only the target's bytes change. Comments, blank lines, key spelling +and order, the BOM, and the document's LF / CR / CRLF style outside the +effective edit are preserved exactly. Dotted, inline, and block forms +survive as they are written; inline wrappers expand only when the +replacement would hurt readability (a multiline string, line growth +past `max(100 Unicode scalar values, the old physical line length)`, or +a replacement needing many escape sequences), and only the touched +branch expands — untouched nested branches stay inline. A replacement +equal to the current value is a byte-identical no-op, even for +noncanonical scalar spellings. + +>>>>> lang=ru +### Правка исходного текста + +`Ktav.editValue()` заменяет **существующее** значение в Ktav-исходном +тексте и возвращает новый исходный текст — это lossless-сплайс на +уровне байтов, а не круг parse→render: + +```java +String out = Ktav.editValue("port: 8080\nhost: db\n", "port", + new Value.Int("9090")); +// port: 9090 +// host: db +``` + +Меняются только байты цели. Комментарии, пустые строки, написание и +порядок ключей, BOM и стиль LF / CR / CRLF вне эффективной правки +сохраняются дословно. Dotted-, inline- и блочные формы переживают +правку как написано; inline-обёртки раскрываются только тогда, когда +замена ухудшает читаемость (многострочная строка, рост строки сверх +`max(100 Unicode scalar values, прежняя длина строки)`, замена, +требующая множества escape-последовательностей), и раскрывается только +затронутая ветвь — нетронутые вложенные ветви остаются inline. Замена, +равная текущему значению, — byte-identical no-op даже для +неканонических написаний скаляров. + +>>>>> lang=zh +### 编辑源文本 + +`Ktav.editValue()` 替换 Ktav 源文本中**已存在**的值并返回新的源文本 +—— 这是字节级的无损拼接(splice),而不是 parse→render 往返: + +```java +String out = Ktav.editValue("port: 8080\nhost: db\n", "port", + new Value.Int("9090")); +// port: 9090 +// host: db +``` + +只有目标的字节会改变。注释、空行、键的拼写与顺序、BOM 以及有效编辑 +范围之外的 LF / CR / CRLF 行尾风格都原样保留。dotted、inline 和块 +形式按原样保留;仅当替换会损害可读性时(多行字符串、行长超过 +`max(100 个 Unicode 标量值, 原物理行长)`、或需要大量转义序列的替换), +inline 包装才会展开,且只展开受影响的分支——未触及的嵌套分支保持 +inline。与当前值相等的替换是字节完全一致的 no-op,即使标量拼写不 +规范也一样。 + diff --git a/root-docs/README/editing/body-2.md b/root-docs/README/editing/body-2.md new file mode 100644 index 0000000..a782460 --- /dev/null +++ b/root-docs/README/editing/body-2.md @@ -0,0 +1,52 @@ +>>>>> lang=en +Paths are dotted (`key1.key2.0.key4`); inside a segment `\.` is a +literal dot and `\\` a literal backslash. `editValueSegments` takes +already-decoded literal segments instead — a numeric segment is a +string key inside an object and a zero-based index inside an array. +There is no create, remove, or rename: only replacement of an existing +value. + +Errors are structured: `EditInvalidPath` (body: the UTF-8 byte offset +inside the path argument where the defect is), `EditNotFound` (missing +key, out-of-bounds index, descent through a scalar, empty path), and +`EditUnsupported` (body: the reason — e.g. a noncontiguous +dotted-key/reopen target, a structural replacement that would drop +interior comment lines, or a multiline string containing a bare `))` +line). Source-level problems still surface as the ordinary parse +envelopes. The pure backend exposes the same operations as +`PureKtav.editValue` / `PureKtav.editValueSegments` with identical +output. + +>>>>> lang=ru +Пути — через точку (`key1.key2.0.key4`); внутри сегмента `\.` — +literal точка, `\\` — literal backslash. `editValueSegments` принимает +уже декодированные literal-сегменты: числовой сегмент — это ключ +внутри объекта и zero-based индекс внутри массива. Создание, удаление +и переименование не поддерживаются — только замена существующего +значения. + +Ошибки структурированы: `EditInvalidPath` (body: UTF-8 byte offset +внутри аргумента пути, где обнаружен дефект), `EditNotFound` +(отсутствующий ключ, индекс вне диапазона, спуск через скаляр, пустой +путь) и `EditUnsupported` (body: причина — например noncontiguous-цель +из dotted-ключей и reopen, структурная замена, которая потеряла бы +комментарии внутри цели, или многострочная строка со строкой `))`). +Проблемы самого исходника по-прежнему приходят обычными parse +envelope'ами. Pure-бэкенд предоставляет те же операции — +`PureKtav.editValue` / `PureKtav.editValueSegments` — с идентичным +выводом. + +>>>>> lang=zh +路径以点分隔(`key1.key2.0.key4`);段内 `\.` 表示字面点号,`\\` +表示字面反斜杠。`editValueSegments` 直接接收已解码的字面段——数字段 +在对象中是字符串键,在数组中是从零开始的索引。不支持创建、删除或重 +命名——只支持替换已存在的值。 + +错误是结构化的:`EditInvalidPath`(body:路径参数中发现缺陷处的 +UTF-8 字节偏移)、`EditNotFound`(缺少键、索引越界、穿过标量下降、 +空路径)和 `EditUnsupported`(body:原因——例如由 dotted 键与 reopen +拼成的非连续目标、会丢失容器内部注释的结构性替换,或包含独立成行的 +`))` 的多行字符串)。源文本本身的问题仍以普通解析错误 envelope 报告。 +纯 Java 后端提供相同操作——`PureKtav.editValue` / +`PureKtav.editValueSegments`,输出完全一致。 + diff --git a/root-docs/README/editing/meta.js b/root-docs/README/editing/meta.js new file mode 100644 index 0000000..7e0a98b --- /dev/null +++ b/root-docs/README/editing/meta.js @@ -0,0 +1,7 @@ +export default { + "kind": "frontmatter", + "number": null, + "level": null, + "title": null, + "bodyParts": 2 +} diff --git a/root-docs/README/manifest.js b/root-docs/README/manifest.js index 8848537..fcf1259 100644 --- a/root-docs/README/manifest.js +++ b/root-docs/README/manifest.js @@ -6,6 +6,8 @@ export default [ "build-render", "api", "formatting", + "source-edit-status", + "editing", "errors", "error-writer-rejections", "type-mapping", 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 From c75b002138fb96151172de6bad413f06edb57afe Mon Sep 17 00:00:00 2001 From: Marat K Date: Sun, 25 Oct 2026 19:37:13 +0100 Subject: [PATCH 2/6] =?UTF-8?q?fix(edit):=20=D1=81=D0=B2=D1=8F=D0=B7=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B8=D1=84=D0=B8?= =?UTF-8?q?=D0=BA=D0=B0=D1=82=D0=BE=D1=80=20=D1=81=20=D0=B8=D1=81=D1=85?= =?UTF-8?q?=D0=BE=D0=B4=D0=BD=D1=8B=D0=BC=20buffer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Устранить 14 javac ошибок: classifyValueShape читает instance bytes и вызывается только instance parser methods, поэтому не должен быть static. Проверки CI не ослаблены. Co-Authored-By: Claude Opus 5.5 --- lib/src/main/java/lang/ktav/pure/EditTree.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/main/java/lang/ktav/pure/EditTree.java b/lib/src/main/java/lang/ktav/pure/EditTree.java index 3202a21..77f6f67 100644 --- a/lib/src/main/java/lang/ktav/pure/EditTree.java +++ b/lib/src/main/java/lang/ktav/pure/EditTree.java @@ -474,7 +474,7 @@ private Node finish() { static final int SHAPE_INLINE_VALUE = 8; /** {@code from} is already the first non-whitespace byte. */ - static int classifyValueShape(int from, int end) { + private int classifyValueShape(int from, int end) { int len = end - from; if (len == 1) { byte c = b0(from); From c218460a4f87e1e724619d9e318899cbdebbf4f2 Mon Sep 17 00:00:00 2001 From: Marat K Date: Sun, 25 Oct 2026 19:45:55 +0100 Subject: [PATCH 3/6] =?UTF-8?q?fix(edit):=20=D1=83=D0=B1=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D1=8C=20=D0=BD=D0=B5=D0=B8=D0=BD=D0=B8=D1=86=D0=B8=D0=B0=D0=BB?= =?UTF-8?q?=D0=B8=D0=B7=D0=B8=D1=80=D1=83=D1=8E=D1=89=D0=B8=D0=B9=20defaul?= =?UTF-8?q?t=20ctor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit EditTree создаётся только с исходным byte buffer; неиспользуемый no-arg constructor нарушал final-field invariant и блокировал javac CI. Co-Authored-By: Claude Opus 5.5 --- lib/src/main/java/lang/ktav/pure/EditTree.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/src/main/java/lang/ktav/pure/EditTree.java b/lib/src/main/java/lang/ktav/pure/EditTree.java index 77f6f67..711fbba 100644 --- a/lib/src/main/java/lang/ktav/pure/EditTree.java +++ b/lib/src/main/java/lang/ktav/pure/EditTree.java @@ -18,9 +18,6 @@ */ final class EditTree { - private EditTree() { - } - // ── Slots ─────────────────────────────────────────────────────────── /** How a value lies in the source; determines the splice bounds. */ From ed530e5ae34ea99f40753ba3b5d7126a97fe8799 Mon Sep 17 00:00:00 2001 From: Marat K Date: Sun, 25 Oct 2026 19:57:24 +0100 Subject: [PATCH 4/6] =?UTF-8?q?fix(test):=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D1=82=D1=8C=20Java=20factory=20=D0=B8=20spec=20ver?= =?UTF-8?q?sion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Не использовать reserved keyword native как имя метода. В packaged pure smoke проверять реализуемую spec 0.9, сохранив package version 0.8.1. Co-Authored-By: Claude Opus 5.5 --- lib/src/pureCheck/java/lang/ktav/PureSmokeMain.java | 6 +++--- lib/src/test/java/lang/ktav/EditValueTest.java | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/src/pureCheck/java/lang/ktav/PureSmokeMain.java b/lib/src/pureCheck/java/lang/ktav/PureSmokeMain.java index 949f7a8..a35cd95 100644 --- a/lib/src/pureCheck/java/lang/ktav/PureSmokeMain.java +++ b/lib/src/pureCheck/java/lang/ktav/PureSmokeMain.java @@ -40,7 +40,7 @@ public static void main(String[] args) { failures += check("loads(byte[]) rejects invalid UTF-8", PureSmokeMain::checkInvalidUtf8); failures += check("grammar error reports exact UTF-8 byte span", PureSmokeMain::checkGrammarSpan); failures += check("runs against the packaged jar, not loose classes", PureSmokeMain::checkJarSource); - failures += check("version is 0.8.1", PureSmokeMain::checkVersion); + failures += check("version is 0.9.0", PureSmokeMain::checkVersion); if (failures > 0) { System.out.println("FAIL pure smoke: " + failures + " check(s) failed"); System.exit(1); @@ -227,8 +227,8 @@ private static void checkJarSource() { } private static void checkVersion() { - if (!"0.8.1".equals(PureKtav.version())) { - throw new AssertionError("expected version 0.8.1, got: " + PureKtav.version()); + if (!"0.9.0".equals(PureKtav.version())) { + throw new AssertionError("expected version 0.9.0, got: " + PureKtav.version()); } } diff --git a/lib/src/test/java/lang/ktav/EditValueTest.java b/lib/src/test/java/lang/ktav/EditValueTest.java index a27836c..bc8244e 100644 --- a/lib/src/test/java/lang/ktav/EditValueTest.java +++ b/lib/src/test/java/lang/ktav/EditValueTest.java @@ -408,7 +408,7 @@ private static void runCase(Backend b, String name) { } @TestFactory - Stream native() { + Stream nativeEdits() { Backend backend = Backend.nativeBackend(); CorpusHarness.requireCabiFor(backend); return factor(backend); From 7b8998e711c2569e858df968b1a7acb60ff7f469 Mon Sep 17 00:00:00 2001 From: Marat K Date: Sun, 25 Oct 2026 20:16:54 +0100 Subject: [PATCH 5/6] =?UTF-8?q?fix(edit):=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D1=82=D1=8C=20Java=20pure=20splice=20=D0=B8=20EOF?= =?UTF-8?q?=20spans?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Маскировать конечный target при semantic guard, сохранять children блочного frame и value-start после whitespace в inline scanner. Считать multiline EOF span от скобки и актуализировать mutation baselines corpus 0.9. Co-Authored-By: Claude Opus 5.5 --- .../main/java/lang/ktav/pure/EditSplice.java | 9 ++- .../main/java/lang/ktav/pure/EditTree.java | 12 +++- lib/src/main/java/lang/ktav/pure/Layout.java | 5 ++ .../main/java/lang/ktav/pure/PureParser.java | 7 +- .../lang/ktav/CorpusGuardMutationTest.java | 64 ++++++++++--------- .../test/java/lang/ktav/PureNegativeTest.java | 19 ++++-- 6 files changed, 75 insertions(+), 41 deletions(-) diff --git a/lib/src/main/java/lang/ktav/pure/EditSplice.java b/lib/src/main/java/lang/ktav/pure/EditSplice.java index ea11ad3..1848c4d 100644 --- a/lib/src/main/java/lang/ktav/pure/EditSplice.java +++ b/lib/src/main/java/lang/ktav/pure/EditSplice.java @@ -306,17 +306,22 @@ static Value nullTargets(Value v, List segments) { } String head = segments.get(0); List rest = segments.subList(1, segments.size()); + boolean last = rest.isEmpty(); if (v instanceof Value.Obj o) { LinkedHashMap out = new LinkedHashMap<>(); o.entries().forEach((k, val) -> out.put(k, - k.equals(head) ? nullTargets(val, rest) : val)); + k.equals(head) + ? (last ? Value.Null.NULL : nullTargets(val, rest)) + : val)); return new Value.Obj(out); } if (v instanceof Value.Arr arr) { int idx = parseIndexOrNull(head); List out = new ArrayList<>(arr.items().size()); for (int i = 0; i < arr.items().size(); i++) { - out.add(i == idx ? nullTargets(arr.items().get(i), rest) + out.add(i == idx + ? (last ? Value.Null.NULL + : nullTargets(arr.items().get(i), rest)) : arr.items().get(i)); } return new Value.Arr(out); diff --git a/lib/src/main/java/lang/ktav/pure/EditTree.java b/lib/src/main/java/lang/ktav/pure/EditTree.java index 711fbba..1b98d7c 100644 --- a/lib/src/main/java/lang/ktav/pure/EditTree.java +++ b/lib/src/main/java/lang/ktav/pure/EditTree.java @@ -427,8 +427,11 @@ private void closeFrame(boolean object, int lineNum, int ts, int te) { anchor = top().pendingAnchor; top().pendingAnchor = -1; } - Node node = new Node(frame.node.kind, false, - Slot.block(anchor, frame.rangeStart, te)); + // Attach the POPULATED frame node — a fresh node would drop all + // children parsed inside the block. + Node node = frame.node; + node.inline = false; + node.slot = Slot.block(anchor, frame.rangeStart, te); Frame parent = top(); if (parent.isObject) { String key = parent.pendingKey; @@ -654,6 +657,11 @@ private void splitTopLevel(int from, int to, boolean isObject, inKey = isObject; continue; } + int w = Bytes.wsAt(b, i, to); + if (w > 0) { + i += w; // § 3.3 whitespace keeps the value-start armed + continue; + } valueStart = false; i++; } diff --git a/lib/src/main/java/lang/ktav/pure/Layout.java b/lib/src/main/java/lang/ktav/pure/Layout.java index 0769ee6..87d653c 100644 --- a/lib/src/main/java/lang/ktav/pure/Layout.java +++ b/lib/src/main/java/lang/ktav/pure/Layout.java @@ -493,6 +493,11 @@ private static void splitTopLevel(byte[] b, int from, int to, inKey = isObject; continue; } + int w = Bytes.wsAt(b, i, to); + if (w > 0) { + i += w; // § 3.3 whitespace keeps the value-start armed + continue; + } valueStart = false; i++; } diff --git a/lib/src/main/java/lang/ktav/pure/PureParser.java b/lib/src/main/java/lang/ktav/pure/PureParser.java index 257c3c3..5ff66e3 100644 --- a/lib/src/main/java/lang/ktav/pure/PureParser.java +++ b/lib/src/main/java/lang/ktav/pure/PureParser.java @@ -342,7 +342,7 @@ private void handleBody(Keys.RawPath key, int vs, int ve) { case Scalars.Cls.OPEN_ML_STRIPPED: case Scalars.Cls.OPEN_ML_VERBATIM: openMultiline(key, - cl.opener == Scalars.Cls.OPEN_ML_VERBATIM); + cl.opener == Scalars.Cls.OPEN_ML_VERBATIM, vs); break; default: attachLeaf(key, cl.value); @@ -375,13 +375,14 @@ private void openCompound(Keys.RawPath key, int opener) { } } - private void openMultiline(Keys.RawPath key, boolean verbatim) { + private void openMultiline(Keys.RawPath key, boolean verbatim, + int openerOffset) { Builders.ObjBuilder top = stack.peek().obj; Builders.Slot slot = Builders.leafSlot(top, key, key.size() == 1, ctx.line, ctx.lineText, ctx.spanStart, ctx.spanEnd); pushDepth(); stack.push(new Frame(Frame.ML, false, null, null, - new Mline(verbatim, slot), ctx.spanStart, ctx.line)); + new Mline(verbatim, slot), openerOffset, ctx.line)); } // ------------------------------------------------------------------ diff --git a/lib/src/test/java/lang/ktav/CorpusGuardMutationTest.java b/lib/src/test/java/lang/ktav/CorpusGuardMutationTest.java index e3724eb..ac9e421 100644 --- a/lib/src/test/java/lang/ktav/CorpusGuardMutationTest.java +++ b/lib/src/test/java/lang/ktav/CorpusGuardMutationTest.java @@ -20,30 +20,36 @@ */ final class CorpusGuardMutationTest { - private static final String KNOWN_GOOD = """ - { - "$comment": "known-good manifest shape (spec § 8.5 / § 6.15)", - "schema_version": 1, - "categories": { - "valid": {"count": 228}, - "invalid": {"count": 90}, - "unrepresentable": {"count": 5}, - "parseable-unrepresentable": {"count": 4}, - "strict-lossy": {"count": 13} - }, - "fixture_flags": [ - {"category": "invalid", "fixture": "invalid_utf8/f1", "flags": ["raw_bytes"]}, - {"category": "invalid", "fixture": "invalid_utf8/f2", "flags": ["raw_bytes"]}, - {"category": "invalid", "fixture": "invalid_utf8/f3", "flags": ["raw_bytes"]}, - {"category": "invalid", "fixture": "invalid_utf8/f4", "flags": ["raw_bytes"]}, - {"category": "invalid", "fixture": "invalid_utf8/f5", "flags": ["raw_bytes"]}, - {"category": "invalid", "fixture": "invalid_utf8/f6", "flags": ["raw_bytes"]}, - {"category": "invalid", "fixture": "invalid_utf8/f7", "flags": ["raw_bytes"]}, - {"category": "invalid", "fixture": "invalid_utf8/f8", "flags": ["raw_bytes"]}, - {"category": "invalid", "fixture": "invalid_utf8/f9", "flags": ["raw_bytes"]} - ] - } - """; + private static final String KNOWN_GOOD = buildKnownGood(); + + /** + * 0.9 baseline: invalid grew 90 → 162 (30 EOF + 42 malformed-UTF-8 + * diagnostics09) and raw_bytes grew 9 → 51 (9 legacy + 42 new). The + * synthetic manifest must mirror the real 0.9 corpus shape, or the + * "known-good" control would itself be a mutation. + */ + private static String buildKnownGood() { + StringBuilder sb = new StringBuilder(""" + { + "$comment": "known-good manifest shape (spec § 8.5 / § 6.15, 0.9)", + "schema_version": 1, + "categories": { + "valid": {"count": 228}, + "invalid": {"count": 162}, + "unrepresentable": {"count": 5}, + "parseable-unrepresentable": {"count": 4}, + "strict-lossy": {"count": 13} + }, + "fixture_flags": [ + """); + for (int i = 1; i <= 51; i++) { + sb.append(" {\"category\": \"invalid\", \"fixture\": \"raw/f") + .append(i).append("\", \"flags\": [\"raw_bytes\"]}"); + sb.append(i == 51 ? "\n" : ",\n"); + } + sb.append(" ]\n }"); + return sb.toString(); + } @TempDir Path temp; @@ -87,7 +93,7 @@ void unimplementedSchemaVersionIsNamed() throws IOException { @Test void missingCategoryIsNamedByClosedSetMessage() throws IOException { assertReadFailsNaming(KNOWN_GOOD.replace( - " \"invalid\": {\"count\": 90},\n", ""), + " \"invalid\": {\"count\": 162},\n", ""), "do not match the closed set", "expected exactly"); } @@ -110,17 +116,17 @@ void extraCategoryIsNamedByClosedSetMessage() throws IOException { @Test void unimplementedFlagIsNamed() throws IOException { assertReadFailsNaming(KNOWN_GOOD.replace( - "\"fixture\": \"invalid_utf8/f1\", \"flags\": [\"raw_bytes\"]", - "\"fixture\": \"invalid_utf8/f1\", \"flags\": [\"raw_bytes_x\"]"), + "\"fixture\": \"raw/f1\", \"flags\": [\"raw_bytes\"]", + "\"fixture\": \"raw/f1\", \"flags\": [\"raw_bytes_x\"]"), "raw_bytes_x", "only raw_bytes"); } @Test void wrongRawBytesFlagCountIsNamed() throws IOException { assertReadFailsNaming(KNOWN_GOOD.replace( - "{\"category\": \"invalid\", \"fixture\": \"invalid_utf8/f8\"," + "{\"category\": \"invalid\", \"fixture\": \"raw/f50\"," + " \"flags\": [\"raw_bytes\"]},\n", ""), - "exactly 9"); + "exactly 51"); } // ── Canonical-companion guard (spec § 5.9.10) ────────────────────── diff --git a/lib/src/test/java/lang/ktav/PureNegativeTest.java b/lib/src/test/java/lang/ktav/PureNegativeTest.java index e36cd1b..ec8a5b1 100644 --- a/lib/src/test/java/lang/ktav/PureNegativeTest.java +++ b/lib/src/test/java/lang/ktav/PureNegativeTest.java @@ -56,24 +56,33 @@ void unclosedCompoundOpenerLineOverLfCrCrlfAndBom() { KtavException lf = err("k: 1\na: ["); assertEquals(2L, lf.getLine(), "LF: opener on line 2"); assertEquals("a: [", lf.getLineText()); - assertEquals(6L, lf.getSpanStart()); + assertEquals(8L, lf.getSpanStart(), "opener is the '[' byte, not the line start"); KtavException cr = err("k: 1\ra: ["); assertEquals(2L, cr.getLine(), "lone CR: opener on line 2"); assertEquals("a: [", cr.getLineText()); - assertEquals(6L, cr.getSpanStart()); + assertEquals(8L, cr.getSpanStart()); KtavException crlf = err("k: 1\r\na: ["); assertEquals(2L, crlf.getLine(), "CRLF is ONE terminator"); assertEquals("a: [", crlf.getLineText(), "line_text strips exactly one trailing CR"); - assertEquals(7L, crlf.getSpanStart()); + assertEquals(9L, crlf.getSpanStart()); KtavException bom = err("\uFEFFk: 1\na: ["); assertEquals(2L, bom.getLine()); assertEquals("a: [", bom.getLineText()); - assertEquals(10L, bom.getSpanStart(), - "BOM stays in the raw coordinates (3 + 6 + 1)"); + assertEquals(11L, bom.getSpanStart(), + "BOM stays in the raw coordinates (3 + 8)"); + + // Multiline opener: the span starts at the '(' byte itself + // (0.9 diagnostics09), not at the opening pair's line start. + KtavException ml = err("k: (\nx"); + assertEquals("UnclosedCompound", ml.getError()); + assertEquals(1L, ml.getLine()); + assertEquals("k: (", ml.getLineText()); + assertEquals(3L, ml.getSpanStart()); + assertEquals(6L, ml.getSpanEnd()); } @Test From eb0ca4b2cc7c95d9848651a3cac82b96056e2682 Mon Sep 17 00:00:00 2001 From: Marat K Date: Sun, 25 Oct 2026 20:31:12 +0100 Subject: [PATCH 6/6] =?UTF-8?q?fix(edit):=20=D0=B2=D1=8B=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=BD=D1=8F=D1=82=D1=8C=20Java=20inline=20promotion=20=D1=81?= =?UTF-8?q?=20core?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Измерять исходный лимит строки отдельно от candidate, передавать UTF-8 byte length, декодировать только value slice и сохранять CRLF при обратном поиске EOL. Co-Authored-By: Claude Opus 5.5 --- lib/src/main/java/lang/ktav/pure/EditSplice.java | 14 +++++++++++--- lib/src/main/java/lang/ktav/pure/Layout.java | 15 +++++++++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/src/main/java/lang/ktav/pure/EditSplice.java b/lib/src/main/java/lang/ktav/pure/EditSplice.java index 1848c4d..1ed3d3c 100644 --- a/lib/src/main/java/lang/ktav/pure/EditSplice.java +++ b/lib/src/main/java/lang/ktav/pure/EditSplice.java @@ -173,8 +173,7 @@ static String apply(byte[] b, int bomStart, List segments, + plan.body + Bytes.text(b, b1, b.length); result = Layout.adjust(b, result, tree.root, segments, replacement, - from, b1, plan.prefix0.length() + plan.prefix1.length() - + plan.body.length()); + from, b1, insertedBytes(plan)); // Obligatory semantic verification: reparse and check that // exactly the target was replaced. @@ -203,6 +202,14 @@ static String apply(byte[] b, int bomStart, List segments, return result; } + /** Insertion size in UTF-8 BYTES — Layout works in byte space. */ + private static int insertedBytes(Plan plan) { + java.nio.charset.Charset utf8 = java.nio.charset.StandardCharsets.UTF_8; + return plan.prefix0.getBytes(utf8).length + + plan.prefix1.getBytes(utf8).length + + plan.body.getBytes(utf8).length; + } + private static final class Plan { final int from; final String prefix0; @@ -808,7 +815,8 @@ private static String detectEol(byte[] b, int pos) { return (j + 1 < pos && b[j + 1] == '\n') ? "\r\n" : "\r"; } if (b[j] == '\n') { - return "\n"; + // CRLF: the LF's preceding CR makes the terminator CRLF. + return (j > 0 && b[j - 1] == '\r') ? "\r\n" : "\n"; } } return "\n"; diff --git a/lib/src/main/java/lang/ktav/pure/Layout.java b/lib/src/main/java/lang/ktav/pure/Layout.java index 87d653c..09d6b00 100644 --- a/lib/src/main/java/lang/ktav/pure/Layout.java +++ b/lib/src/main/java/lang/ktav/pure/Layout.java @@ -44,8 +44,10 @@ static String adjust(byte[] source, String candidate, EditTree.Node root, if (!isCompound(body)) { return candidate; } - int oldWidth = codePoints(physicalLineText(cb, from)); - int limit = Math.max(oldWidth, 100); + // Reference parity: the limit comes from the SOURCE line, the + // growth check from the CANDIDATE line — comparing the candidate + // against itself would never fire. + int limit = Math.max(codePoints(physicalLineText(source, from)), 100); boolean force = requiresBlock(replacement, 0); if (!force && codePoints(physicalLineText(cb, from)) <= limit) { return candidate; @@ -322,7 +324,11 @@ private static String standalone(byte[] cb, int ps, int pe, } value = new Value.Str(decoded); } else { - Value parsed = parseInlineFragment("[" + part + "]", false, + // Decode the VALUE slice only — for an object part the whole + // part still carries the key prefix, which is not part of + // the value being re-planned. + String valueText = Bytes.text(cb, vs, pe); + Value parsed = parseInlineFragment("[" + valueText + "]", false, "cannot decode moved inline value"); if (!(parsed instanceof Value.Arr arr) || arr.items().size() != 1) { throw EditError.unsupported( @@ -645,7 +651,8 @@ private static String detectEol(byte[] b, int pos) { return (j + 1 < pos && b[j + 1] == '\n') ? "\r\n" : "\r"; } if (b[j] == '\n') { - return "\n"; + // CRLF: the LF's preceding CR makes the terminator CRLF. + return (j > 0 && b[j - 1] == '\r') ? "\r\n" : "\n"; } } return "\n";