From 3c14251c28b2dedb29057fe5f26d44667385551a Mon Sep 17 00:00:00 2001 From: cflmy Date: Mon, 30 Nov 2026 12:18:07 +0800 Subject: [PATCH] feat: add explicit strict JSON entry point MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implement marqdo-std-data#13 without changing the default `json.解析` behavior: - `json.严格解析(文本)` rejects duplicate keys in one object level after decoding (`"\u0061"` equals `"a"`) with `json.duplicate_key`, and numbers that cannot be represented as finite binary64 values with `json.non_finite_number` (1e400 / -1e400 / 1.8e308 reject; 1e308 / 1.79e308 stay finite). - The strict validator reuses the existing tokenizer/parser helpers and the 128-level depth limit; no second JSON grammar or parser is introduced. - Candidate-input self-test: `tests/json-strict.mq.md` + `tests/cases-strict/json-strict.json` + `scripts/test-strict.sh`, kept separate from the default suite because the frozen ecosystem Bundle predates the new function. `run_cases.py` accepts `MARQDO_STDLIB_CASES_DIR`. - README documents the new entry, error codes and candidate-Bundle test lane; docs/evidence records the real CLI runs and digests. No cross-repo dependency, no CLI change, no release. --- README.md | 8 +- docs/evidence-2026-11-30-strict-json.md | 60 ++++ modules/data/json.mq.md | 383 ++++++++++++++++++++++++ scripts/run_cases.py | 3 + scripts/test-strict.sh | 15 + tests/cases-strict/json-strict.json | 65 ++++ tests/json-strict.mq.md | 28 ++ 7 files changed, 561 insertions(+), 1 deletion(-) create mode 100644 docs/evidence-2026-11-30-strict-json.md create mode 100755 scripts/test-strict.sh create mode 100644 tests/cases-strict/json-strict.json create mode 100644 tests/json-strict.mq.md diff --git a/README.md b/README.md index f6922bc..060598d 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ | 发布路径 | 公开导入 | 职责 | | --- | --- | --- | -| `data/json.mq.md` | `std:data/json` | 递归 JSON 解析/格式化(128 层上限、多值) | +| `data/json.mq.md` | `std:data/json` | 递归 JSON 解析/格式化(128 层上限、多值);显式严格入口 `json.严格解析` 拒绝同层重复键(按解码后键比较,含转义等价)与无法表示为有限值的数字(`json.duplicate_key` / `json.non_finite_number`),普通 `json.解析` 行为不变。 | | `data/csv.mq.md` | `std:data/csv` | 引号单行/多行 CSV 解析与格式化 | | `data/table.mq.md` | `std:data/table` | Table 序列化:`连接片段`、`连接行`、`转义单元格`、`转GFM` | @@ -26,6 +26,12 @@ MARQDO_BIN=/path/to/verified/marqdo \ MARQDO_STDLIB_DIR=/path/to/installed/bundle \ scripts/test.sh + +# Candidate-input strict suite: requires a Bundle built from this component's +# current gitlink (the frozen ecosystem Bundle predates `json.严格解析`). +MARQDO_BIN=/path/to/verified/marqdo \ +MARQDO_STDLIB_DIR=/path/to/candidate/bundle \ +scripts/test-strict.sh ``` 期望值取自迁移前基线 Bundle 的真实执行(JSON/CSV 分项断言、Table GFM 输出);修改期望值等于声明公开行为变化,必须单独评审。 diff --git a/docs/evidence-2026-11-30-strict-json.md b/docs/evidence-2026-11-30-strict-json.md new file mode 100644 index 0000000..3e5b620 --- /dev/null +++ b/docs/evidence-2026-11-30-strict-json.md @@ -0,0 +1,60 @@ +# std-data 严格 JSON 入口 — 2026-11-30 + +Issue: [marqdo-std-data#13](https://git.cflmy.cn/cflmy/marqdo-std-data/issues/13) +(consumer: `marqdo-ext-ai` AI-U2b). Branch `codex/issue-13-strict-json`. + +## 交付 + +* `data/json.mq.md` 新增显式严格入口 `json.严格解析(文本)`: + - 同一对象层重复键(按解码后键比较,`"a"` 与 `"\u0061"` 等价)→ + `json.duplicate_key`; + - 无法表示为 binary64 有限值的数字(如 `1e400`、`-1e400`、`1.8e308`)→ + `json.non_finite_number`;`1e308`、`1.79e308` 仍为有限值; + - 复用现有 tokenizer/解析器与 128 层深度上限,不新增第二套 JSON 语法。 +* 普通 `json.解析` 公开行为不变(默认套件 PASS)。 +* 候选输入自测:`tests/json-strict.mq.md` + `tests/cases-strict/json-strict.json` + + `scripts/test-strict.sh`。严格入口只存在于本仓当前模块,冻结生态 Bundle + 尚未含它,因此该套件与默认 `scripts/test.sh` 分离,由调用方显式传入候选 + Bundle(根 Bundle 生产者/晋升流水线)。 +* `scripts/run_cases.py` 支持 `MARQDO_STDLIB_CASES_DIR` 选择用例目录(默认不变)。 + +## 验证(真实执行) + +环境:Linux x86_64 / glibc 2.43;CLI `marqdo 3.0.0-rc.6`(CLI candidate +`0f87f856…` 构建);本仓模块 `sha256:0f099d0279e0ea8c5d40b00ff3f2ff3ff8bccad551d61898f1c995e1d6a29407`。 + +候选 Bundle:pinned `marqdo-stdlib 0.2.0` 叠加本仓当前 `modules/` 后重算 tree +digest,得到 `sha256:4e47b0ff4b1e17d252cc74f20837014298fed4831050d65ca0c69f9cbdac55e9` +(17 个 `.mq.md`;仅本地候选,不是官方 manifest)。 + +```text +$ MARQDO_BIN= MARQDO_STDLIB_DIR= sh scripts/test.sh +PASS csv +PASS json +PASS table +3 cases passed + +$ MARQDO_BIN= MARQDO_STDLIB_DIR= sh scripts/test-strict.sh +PASS json-strict +1 cases passed +``` + +严格入口行为(真实 CLI 运行,逐项断言在 `tests/cases-strict/json-strict.json`): + +| 输入 | 结果 | +|---|---| +| `{"b":3,"c":[1,2.5,true,null]}` | ok | +| `[1,2.5,true,null,"x",{"b":3}]` | ok | +| `{"a":1,"a":2}` | `json.duplicate_key` | +| `{"a":1,"\u0061":2}` | `json.duplicate_key` | +| `1e400` / `-1e400` | `json.non_finite_number` | +| `1e308` / `1.79e308` | ok | +| `1.8e308` | `json.non_finite_number` | +| 普通 `json.解析("{\"a\":1,\"a\":2}")` | ok,`value={"a":2}`(行为不变) | + +## 未验证 / 边界 + +* 冻结生态 Bundle 0.2.0 不含严格入口,仓库默认 CI 套件不覆盖新入口;候选 + Bundle 由根生产者/晋升流水线提供,本仓不自建第二份官方 manifest。 +* 未修改 CLI/其它组件;未新增跨仓依赖;未创建 release/tag。 +* 转义等价按解码后键比较(`\u0061`);未额外做 Unicode 规范化,与普通键语义一致。 diff --git a/modules/data/json.mq.md b/modules/data/json.mq.md index 76d8eac..ebbabdc 100644 --- a/modules/data/json.mq.md +++ b/modules/data/json.mq.md @@ -918,3 +918,386 @@ JSON 数字:可选负号、整数位、可选小数位、可选指数;含 ./ `end` `return {ok: true, value: 结果}` + +## 严格解析 + +`params(文本)` + +在普通解析之外显式选择严格语义:拒绝同一对象层内的重复键(按解码后 +键比较)与无法表示为有限值的数字;普通 json.解析 的公开行为不变。 + +**点 = text.codepoints(文本)** + +`if 嵌套深度(文本) > 128` +`return 失败("json.depth", "嵌套深度超过 128")` +`end` + +**检查 = 严格检查值(点, 0)** + +`if not 检查.ok` +`return 检查` +`end` + +**位置 = 跳过空白(点, 检查.next)** + +`if 位置 != 长度(点)` +`return 失败("json.trailing", "值后存在多余输入")` +`end` + +`return 解析(文本)` + +## 严格检查值 + +`params(点, 位置)` + +**位置 = 跳过空白(点, 位置)** + +`if 位置 >= 长度(点)` +`return 失败("json.unexpected_eof", "需要值")` +`end` + +**码点 = 点[位置]** + +`if 码点 == 123` +`return 严格检查对象(点, 位置 + 1)` +`else` +`if 码点 == 91` +`return 严格检查数组(点, 位置 + 1)` +`else` +`if 码点 == 34` +**文本结果 = 解析字符串(点, 位置)** +`if not 文本结果.ok` +`return 文本结果` +`end` +`return 成功值(None, 文本结果.next)` +`else` +`if 码点 == 45 or 是数字(码点)` +**开始 = 位置** +**数字结果 = 解析数字(点, 位置)** +`if not 数字结果.ok` +`return 数字结果` +`end` +**原文 = 片段(点, 开始, 数字结果.next)** +`if not 有限数字文本(原文)` +`return 失败("json.non_finite_number", "数字无法表示为有限值")` +`end` +`return 成功值(None, 数字结果.next)` +`else` +`return 解析值(点, 位置)` +`end` +`end` +`end` +`end` + +## 严格检查对象 + +`params(点, 位置)` +`return 严格检查对象项(点, 位置, {})` + +## 严格检查对象项 + +`params(点, 位置, 已见)` + +**位置 = 跳过空白(点, 位置)** + +`if 位置 >= 长度(点)` +`return 失败("json.unexpected_eof", "对象未闭合")` +`end` + +**当前 = text.from_codepoint(点[位置])** + +`if 当前 == "}"` +`return 成功值(None, 位置 + 1)` +`end` + +**键结果 = 解析字符串(点, 位置)** + +`if not 键结果.ok` +`return 键结果` +`end` + +**键 = 键结果.value** + +`if map.has(已见, 键)` +`return 失败("json.duplicate_key", "同一对象层内存在重复键")` +`end` + +**更新的已见 = map.set(已见, 键, true)** + +**位置 = 跳过空白(点, 键结果.next)** + +`if 位置 >= 长度(点)` +`return 失败("json.unexpected_eof", "对象缺少冒号")` +`else` +`if text.from_codepoint(点[位置]) != ":"` +`return 失败("json.invalid_object", "对象缺少冒号")` +`end` +`end` + +**值检查 = 严格检查值(点, 位置 + 1)** + +`if not 值检查.ok` +`return 值检查` +`end` + +**位置 = 跳过空白(点, 值检查.next)** + +`if 位置 >= 长度(点)` +`return 失败("json.unexpected_eof", "对象未闭合")` +`end` + +**分隔 = text.from_codepoint(点[位置])** + +`if 分隔 == ","` +`return 严格检查对象项(点, 位置 + 1, 更新的已见)` +`else` +`if 分隔 == "}"` +`return 成功值(None, 位置 + 1)` +`else` +`return 失败("json.invalid_object", "对象缺少逗号或 }")` +`end` +`end` + +## 严格检查数组 + +`params(点, 位置)` +`return 严格检查数组项(点, 位置)` + +## 严格检查数组项 + +`params(点, 位置)` + +**位置 = 跳过空白(点, 位置)** + +`if 位置 >= 长度(点)` +`return 失败("json.unexpected_eof", "数组未闭合")` +`end` + +**当前 = text.from_codepoint(点[位置])** + +`if 当前 == "]"` +`return 成功值(None, 位置 + 1)` +`end` + +**项检查 = 严格检查值(点, 位置)** + +`if not 项检查.ok` +`return 项检查` +`end` + +**位置 = 跳过空白(点, 项检查.next)** + +`if 位置 >= 长度(点)` +`return 失败("json.unexpected_eof", "数组未闭合")` +`end` + +**分隔 = text.from_codepoint(点[位置])** + +`if 分隔 == ","` +`return 严格检查数组项(点, 位置 + 1)` +`else` +`if 分隔 == "]"` +`return 成功值(None, 位置 + 1)` +`else` +`return 失败("json.invalid_array", "数组缺少逗号或 ]")` +`end` +`end` + +## 有限数字文本 + +`params(原文)` + +判断 JSON 数字原文是否可用 binary64 有限值表示:十进制量级超过 308 +即溢出;量级正好 308 时与 binary64 最大值 1.7976931348623157e308 的 +显著数字比较。下溢为 0 仍属有限值。 + +**点 = text.codepoints(原文)** + +**下标 = 0** + +`if 下标 < 长度(点) and 点[下标] == 45` +**下标 = 下标 + 1** +`else` +`if true` +**忽略符号 = 0** +`end` +`end` + +**整数起 = 下标** + +`while 下标 < 长度(点) and 是数字(点[下标])` +**下标 = 下标 + 1** +`end` + +**整数终 = 下标** + +**小数起 = 下标** + +`if 下标 < 长度(点) and 点[下标] == 46` +**下标 = 下标 + 1** +**小数起 = 下标** +`while 下标 < 长度(点) and 是数字(点[下标])` +**下标 = 下标 + 1** +`end` +`else` +`if true` +**忽略小数 = 0** +`end` +`end` + +**小数终 = 下标** + +**指数 = 0** + +`if 下标 < 长度(点) and (点[下标] == 101 or 点[下标] == 69)` +**下标 = 下标 + 1** +**指数负 = false** +`if 下标 < 长度(点) and 点[下标] == 43` +**下标 = 下标 + 1** +`else` +`if 下标 < 长度(点) and 点[下标] == 45` +**指数负 = true** +**下标 = 下标 + 1** +`else` +`if true` +**忽略指数符号 = 0** +`end` +`end` +`end` +`while 下标 < 长度(点) and 是数字(点[下标])` +`if 指数 < 100000` +**指数 = 指数 * 10 + (点[下标] - 48)** +`else` +`if true` +**指数 = 100000** +`end` +`end` +**下标 = 下标 + 1** +`end` +`if 指数负` +**指数 = 0 - 指数** +`end` +`else` +`if true` +**忽略指数 = 0** +`end` +`end` + +**有效 = 严格有效数字(点, 整数起, 整数终, 小数起, 小数终)** + +`if 长度(有效) == 0` +`return true` +`end` + +**量级 = 0** + +`if 有效首位整数(点, 整数起, 整数终)` +**量级 = (整数终 - 有效起点(点, 整数起, 整数终)) - 1 + 指数** +`else` +**量级 = (0 - (有效起点(点, 小数起, 小数终) - 小数起 + 1)) + 指数** +`end` + +`if 量级 > 308` +`return false` +`end` + +`if 量级 < 308` +`return true` +`end` + +`return 有效数字不超过上限(有效)` + +## 有效首位整数 + +`params(点, 起点, 终点)` + +**下标 = 起点** + +`while 下标 < 终点` +`if 点[下标] != 48` +`return true` +`end` +**下标 = 下标 + 1** +`end` +`return false` + +## 有效起点 + +`params(点, 起点, 终点)` + +**下标 = 起点** + +`while 下标 < 终点` +`if 点[下标] != 48` +`return 下标` +`end` +**下标 = 下标 + 1** +`end` +`return 终点` + +## 严格有效数字 + +`params(点, 整数起, 整数终, 小数起, 小数终)` + +**有效 = []** + +**开始 = false** + +**下标 = 整数起** + +`while 下标 < 整数终` +`if 开始 or 点[下标] != 48` +**开始 = true** +`if 长度(有效) < 17` +**有效 = list.append(有效, 点[下标] - 48)** +`else` +`if true` +**忽略多余 = 0** +`end` +`end` +`end` +**下标 = 下标 + 1** +`end` + +**下标 = 小数起** + +`while 下标 < 小数终` +`if 开始 or 点[下标] != 48` +**开始 = true** +`if 长度(有效) < 17` +**有效 = list.append(有效, 点[下标] - 48)** +`else` +`if true` +**忽略多余 = 0** +`end` +`end` +`end` +**下标 = 下标 + 1** +`end` + +`return 有效` + +## 有效数字不超过上限 + +`params(有效)` + +**上限 = text.codepoints("17976931348623157")** + +**下标 = 0** + +`while 下标 < 17` +**数字 = 0** +`if 下标 < 长度(有效)` +**数字 = 有效[下标]** +`end` +**限制 = 上限[下标] - 48** +`if 数字 > 限制` +`return false` +`end` +`if 数字 < 限制` +`return true` +`end` +**下标 = 下标 + 1** +`end` + +`return true` diff --git a/scripts/run_cases.py b/scripts/run_cases.py index 0e38c3d..6c67eb9 100755 --- a/scripts/run_cases.py +++ b/scripts/run_cases.py @@ -157,6 +157,9 @@ def _dig(value, pointer: str): def main() -> int: + global CASES + if os.environ.get("MARQDO_STDLIB_CASES_DIR"): + CASES = Path(os.environ["MARQDO_STDLIB_CASES_DIR"]).resolve() parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("cases", nargs="*", help="case names to run") args = parser.parse_args() diff --git a/scripts/test-strict.sh b/scripts/test-strict.sh new file mode 100755 index 0000000..e4d32cc --- /dev/null +++ b/scripts/test-strict.sh @@ -0,0 +1,15 @@ +#!/bin/sh +# Candidate-input strict-mode self-test. +# +# Strict JSON rejects duplicate keys and non-finite numbers. Those symbols only +# exist in a Bundle that contains this component's current modules, so this +# suite is separate from the default `scripts/test.sh` (which must keep running +# against the frozen ecosystem Bundle). Callers pass an explicit candidate +# Bundle produced from the current component gitlink, e.g. by the root repo's +# `scripts/build-stdlib.py` / acceptance pipeline. +set -eu +: "${MARQDO_BIN:?set MARQDO_BIN to the verified marqdo executable}" +: "${MARQDO_STDLIB_DIR:?set MARQDO_STDLIB_DIR to a candidate Bundle containing current modules}" +MARQDO_STDLIB_CASES_DIR="$(CDPATH= cd -- "$(dirname -- "$0")/../tests/cases-strict" && pwd)" +export MARQDO_STDLIB_CASES_DIR +exec python3 "$(dirname "$0")/run_cases.py" "$@" diff --git a/tests/cases-strict/json-strict.json b/tests/cases-strict/json-strict.json new file mode 100644 index 0000000..088dea9 --- /dev/null +++ b/tests/cases-strict/json-strict.json @@ -0,0 +1,65 @@ +{ + "schema": "marqdo.stdlib-test-case", + "schema_version": 1, + "name": "json-strict", + "file": "json-strict.mq.md", + "require_module": "data/json.mq.md", + "args": [], + "stdin": "", + "env": {}, + "expected_stdout": "", + "expected_terminal": { + "type": "list", + "items": [ + { + "type": "bool", + "value": true + }, + { + "type": "bool", + "value": true + }, + { + "type": "bool", + "value": false + }, + { + "type": "text", + "value": "json.duplicate_key" + }, + { + "type": "text", + "value": "json.duplicate_key" + }, + { + "type": "text", + "value": "json.non_finite_number" + }, + { + "type": "text", + "value": "json.non_finite_number" + }, + { + "type": "bool", + "value": true + }, + { + "type": "bool", + "value": false + }, + { + "type": "bool", + "value": true + }, + { + "type": "map", + "entries": { + "a": { + "type": "int", + "decimal": "2" + } + } + } + ] + } +} diff --git a/tests/json-strict.mq.md b/tests/json-strict.mq.md new file mode 100644 index 0000000..b3fa4a5 --- /dev/null +++ b/tests/json-strict.mq.md @@ -0,0 +1,28 @@ +--- +import: + json: "std:data/json" +--- + +# @exec 严格解析 + +## main + +**`合法对象 = json.严格解析("{\"b\":3,\"c\":[1,2.5,true,null]}")`** + +**`合法数组 = json.严格解析("[1,2.5,true,null,\"x\",{\"b\":3}]")`** + +**`重复键 = json.严格解析("{\"a\":1,\"a\":2}")`** + +**`转义等价重复 = json.严格解析("{\"a\":1,\"\\u0061\":2}")`** + +**`溢出 = json.严格解析("1e400")`** + +**`负溢出 = json.严格解析("-1e400")`** + +**`最大量级 = json.严格解析("1.79e308")`** + +**`超出最大量级 = json.严格解析("1.8e308")`** + +**`普通入口 = json.解析("{\"a\":1,\"a\":2}")`** + +`return [合法对象.ok, 合法数组.ok, 重复键.ok, 重复键.code, 转义等价重复.code, 溢出.code, 负溢出.code, 最大量级.ok, 超出最大量级.ok, 普通入口.ok, 普通入口.value]` -- 2.54.1