From bde6f1d0ba6f3ab19829ea63e5e85f1df84979a4 Mon Sep 17 00:00:00 2001 From: Marqdo Agent Date: Sat, 21 Nov 2026 08:23:54 +0800 Subject: [PATCH] feat(stdlib): add JSON and CSV product parsers --- crates/cli/tests/stdlib_product.rs | 47 ++ docs/STATUS.md | 11 +- docs/architecture/CURRENT.md | 6 +- docs/evidence/2026-11-21-json-csv-product.md | 49 ++ stdlib/README.md | 2 + stdlib/data/csv.mq.md | 163 +++++ stdlib/data/json.mq.md | 638 +++++++++++++++++++ stdlib/stdlib.json | 2 +- stdlib/tests/csv.mq.md | 16 + stdlib/tests/json.mq.md | 16 + 10 files changed, 942 insertions(+), 8 deletions(-) create mode 100644 docs/evidence/2026-11-21-json-csv-product.md create mode 100644 stdlib/data/csv.mq.md create mode 100644 stdlib/data/json.mq.md create mode 100644 stdlib/tests/csv.mq.md create mode 100644 stdlib/tests/json.mq.md diff --git a/crates/cli/tests/stdlib_product.rs b/crates/cli/tests/stdlib_product.rs index 1283bd3..147ea3c 100644 --- a/crates/cli/tests/stdlib_product.rs +++ b/crates/cli/tests/stdlib_product.rs @@ -128,11 +128,13 @@ fn every_public_stdlib_module_has_a_product_test() { names, vec![ "console.mq.md", + "csv.mq.md", "demo.mq.md", "format.mq.md", "fs.mq.md", "http.mq.md", "io.mq.md", + "json.mq.md", "stream.mq.md", "table.mq.md", "text.mq.md", @@ -184,6 +186,51 @@ fn fs_test_exercises_the_file_operation_surface() { ); } +#[test] +fn json_test_parses_and_formats_nested_values() { + let report = run_product_test( + product_tests() + .iter() + .find(|file| file.name == "json.mq.md") + .expect("json test"), + ); + let items = report["value"]["items"].as_array().expect("list"); + assert_eq!(items[0]["value"], true); + assert_eq!(items[1]["decimal"], "1"); + assert_eq!(items[2]["value"], "说\"好"); + assert_eq!( + items[3]["value"], + "[1, 2.5, -300.0, 0.4, {\"a\": 1.0}, \"x\"]" + ); + assert_eq!(items[4]["value"], false); + assert_eq!(items[5]["value"], "json.invalid_array"); + assert_eq!(items[6]["value"], "json.trailing"); +} + +#[test] +fn csv_test_parses_and_formats_quoted_rows() { + let report = run_product_test( + product_tests() + .iter() + .find(|file| file.name == "csv.mq.md") + .expect("csv test"), + ); + let items = report["value"]["items"].as_array().expect("list"); + let row = items[0]["items"].as_array().expect("row"); + assert_eq!(row.len(), 3); + assert_eq!(row[0]["value"], "甲"); + assert_eq!(row[1]["value"], "乙,丙"); + assert_eq!(row[2]["value"], "说\"好"); + assert_eq!(items[1]["value"], "甲,\"乙,丙\",\"说\"\"好\""); + let empty = items[2]["items"].as_array().expect("empty row"); + assert_eq!(empty.len(), 1); + assert_eq!(empty[0]["value"], ""); + assert_eq!(items[3]["value"], "csv.unterminated_quote"); + let quoted = items[4]["items"].as_array().expect("quoted row"); + assert_eq!(quoted[0]["value"], "a"); + assert_eq!(quoted[1]["value"], "b"); +} + #[test] fn format_test_pins_value_semantics_and_handle_observation() { let report = run_product_test( diff --git a/docs/STATUS.md b/docs/STATUS.md index de31705..ca2bdf3 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -58,7 +58,7 @@ byte-identical). cargo fmt --all -- --check PASS cargo clippy --workspace --all-targets -- -D warnings PASS cargo test --workspace --locked PASS - 467 passed, 0 failed, 2 ignored; includes 7/7 Linux security tests + 469 passed, 0 failed, 2 ignored; includes 7/7 Linux security tests ``` The documentation and test migration is local work; re-run this block after it @@ -200,10 +200,11 @@ This is still a **local VM lane**, not a self-hosted GitHub Actions security URL `Value::Table`; inputs are never mutated; GFM AST laziness and physical T0 storage are unchanged. `std:fs/file` provides bounded `读文本`/`写文本`/`复制文件` over the existing `resource.*` ABI plus `存在`/`元数据`/`列出目录`/`创建目录`/ - `删除`/`重命名` over `resource.request` (ADR-0041). Append/seek/watch and a - recursive walk remain open. + `删除`/`重命名` over `resource.request` (ADR-0041); `data/json`/`data/csv` + provide recursive JSON and quoted single-row CSV product parsers. Append/seek/ + watch, recursive walk, streaming JSON/CSV and a JSON depth bound remain open. - Bundled standard library (CM-A10, ADR-0028): `stdlib/` ships - `format`/`text`/`io`/`fs`; explicit `std:` imports resolve only inside the bundle; + `format`/`text`/`io`/`fs`/`data`; explicit `std:` imports resolve only inside the bundle; a SHA-256 digest over `*.mq.md` is verified before loading; Analysis records `project.stdlib`; a frozen-binary test asserts the CLI hash is unchanged after running a `std:io` project and that tampering is rejected. `打印/print` is @@ -229,7 +230,7 @@ This is still a **local VM lane**, not a self-hosted GitHub Actions security URL status/index links and SHA-pinned evidence; `crates/cli/tests/architecture_boundaries.rs` pins the workspace graph, `marqdo-runtime -> marqdo-host`, the bundled stdlib digest and the stdio/file/TCP endpoints; `crates/cli/tests/stdlib_product.rs` - runs every public stdlib module (`console/demo/format/fs/http/io/stream/table/text/utf8`) + runs every public stdlib module (`console/csv/demo/format/fs/http/io/json/stream/table/text/utf8`) through `std:` imports over `stdlib/tests/*.mq.md`. `tools/pack-stdlib.py` now sorts `.mq.md` by the same relative path key as the CLI. Source/test baseline `0d40de7`; later docs-only commits keep code/test bytes identical. Evidence: diff --git a/docs/architecture/CURRENT.md b/docs/architecture/CURRENT.md index b7d512d..ab0e973 100644 --- a/docs/architecture/CURRENT.md +++ b/docs/architecture/CURRENT.md @@ -18,7 +18,7 @@ platform". | Dimension | Current state | Source | |---|---|---| | Implemented | Rust workspace `0.1.0`: Markdown/frontend/IR, tree-walk runtime, `Memory`, capability host, Tool/Analysis/Trace, Plugin ABI, Foreign kernels, bundled stdlib, CLI composition root | `Cargo.toml`, `crates/`, `stdlib/` | -| Tested locally | `cargo test --workspace --locked`: **467 passed, 0 failed, 2 ignored** on Linux x86_64 at the baseline | `docs/STATUS.md` §3 | +| Tested locally | `cargo test --workspace --locked`: **469 passed, 0 failed, 2 ignored** on Linux x86_64 at the baseline | `docs/STATUS.md` §3 | | Bundled stdlib released | In-repo bundle `marqdo-stdlib@0.1.0`, SHA-256 digest verified before loading; it is **not** an external registry package | `stdlib/stdlib.json`, `stdlib/README.md` | | Multi-platform verified | Linux local gate is the only executed lane here; Windows/macOS native lanes are **UNVERIFIED**, and the GitHub security lane remains a **VM-only** gate | `docs/STATUS.md` §1, §4 | @@ -88,6 +88,8 @@ review RFC, not a new crate per provider. | project file write | `resource.open("file", path, ["write"])`; buffers until `close`, never flush-on-drop | implemented, project mode | `resource_streams`, `stdlib_product` | | `std:fs/file` | `fs.读文本`, `fs.写文本`, `fs.复制文件` over `resource.open/read/write/close` and `Memory`; bounded at 8 MiB | implemented (read/write/copy) | `stdlib_product` | | `resource.request` | `resource.request("fs.exists|metadata|list|mkdir|remove|rename", paths)`; project mode, grant-checked, recoverable fs Result | implemented `fs.*`; no seek/append/watch | `resource_streams`, `stdlib_product` | +| `std:data/json` | `json.解析(文本)` -> Result with nested Value; `json.格式化(值)` -> Text | implemented (recursive, recoverable errors) | `stdlib_product` | +| `std:data/csv` | `csv.解析行(文本)` -> Result with List; `csv.格式化行(列表)` -> Text | implemented (single row, quoted) | `stdlib_product` | | file metadata / directory / rename / remove / mkdir | not part of the current Resource contract | **not implemented**; needs a separate Resource Operation ADR | — | | TCP | `resource.open("tcp", ip:port, ["read","write"])`; exact IP grant, 5 s timeout | implemented; no DNS/TLS/server socket/UDP | `tcp_endpoint`, `http_over_tcp` | | HTTP/1.1 | `stdlib/net/http.mq.md`: status/header/chunked-byte parse, request build | implemented over an already-authorized TCP stream; not a DNS/TLS client | `stdlib_http`, `http_over_tcp`, `stdlib_product` | @@ -115,7 +117,7 @@ that a phase is complete. | P0 | Documentation drift between `STATUS.md`, the architecture index and the evidence | being fixed by this migration and the docs gate | | P1 | `std:fs/file` read/write/copy and `resource.request` fs metadata/directory operations are implemented; append/seek/watch/recursive walk remain open | open | | P1 | No file append/seek/watch/recursive directory walk | open; future Resource Operation extension | -| P1 | JSON/CSV/UTF-8 decode remain mainly under `tests/conformance` | open as product stdlib API | +| P1 | JSON/CSV product parsers are implemented; no streaming/multi-line CSV or depth-bounded JSON walker | open | | P1 | `crates/cli/src/commands.rs` and `crates/marqdo-host/src/host.rs` still mix multiple responsibilities | open refactor, behavior-preserving | | P2 | HTTP parsing is Unicode-codepoint oriented; a general byte body is not the only path | open; convert before a general network release | | P2 | No DNS, TLS, listener or UDP provider | open | diff --git a/docs/evidence/2026-11-21-json-csv-product.md b/docs/evidence/2026-11-21-json-csv-product.md new file mode 100644 index 0000000..e650954 --- /dev/null +++ b/docs/evidence/2026-11-21-json-csv-product.md @@ -0,0 +1,49 @@ +# Evidence: `std:data/json` and `std:data/csv` + +Date: 2026-11-21 (Asia/Shanghai) +Source baseline: `fbe36bcc431ccadcb95b5aba74ecd7c39113f9ae` +Repository: `marqdo-cli` +RFC item: P1 JSON/CSV product stdlib APIs + +## Implemented + +`stdlib/data/json.mq.md` is a recursive JSON parser/formatter written only in +`.mq.md`: + +- `解析(文本)` -> `{ok:true, value}` or `{ok:false, code, message}`; + rejects malformed numbers, strings, escapes, arrays, objects and trailing + input (`json.trailing`). +- `格式化(值)` -> canonical text: strings escaped, lists/objects comma-space + separated, `None` as `null`, Text/Int/Float/Bool by their value form. + +`stdlib/data/csv.mq.md` is a quoted single-row CSV parser/formatter: + +- `解析行(文本)` -> `{ok:true, value:List}`; `""` is a literal quote; + unterminated quotes and a quote inside an unquoted field are recoverable + `csv.unterminated_quote`/`csv.invalid_quote`. +- `格式化行(列表)` -> quotes fields containing comma/quote/CR/LF and doubles + inner quotes. + +No Rust builtin, `HostOp` or parser change was added. Both modules are pure +`.mq.md` value functions. + +## Tests + +`stdlib/tests/json.mq.md` and `stdlib/tests/csv.mq.md` run through +`std:data/json` and `std:data/csv` and are pinned by +`crates/cli/tests/stdlib_product.rs` (8 product tests total). + +```text +cargo fmt --all -- --check PASS +cargo clippy --workspace --all-targets -- -D warnings PASS +cargo test --workspace 469 passed, 0 failed, 2 ignored +cargo test -p marqdo-cli --test stdlib_product 8 passed +python3 tools/pack-stdlib.py PASS +``` + +## Not covered + +No streaming/incremental JSON, no depth bound (deep nesting can exhaust the +interpreter stack), no multi-line CSV fields, no duplicate-key policy beyond +last-write-wins, and no `std:data/json` conversion to `Table`. Those remain +open product work. diff --git a/stdlib/README.md b/stdlib/README.md index 3dfaa14..3070f0b 100644 --- a/stdlib/README.md +++ b/stdlib/README.md @@ -12,6 +12,8 @@ module; a mismatch is a hard error. - `io/stream.mq.md` — `写全部` / `写文本` with short-write and close checking - `io/console.mq.md` — `读取行` / `打印` over `stdio/stdin` and `stdio/stdout` - `demo.mq.md` — side-by-side comparison with builtin `打印` +- `data/json.mq.md` — recursive JSON parser and formatter (recoverable Result) +- `data/csv.mq.md` — quoted single-row CSV parser and formatter - `data/table.mq.md` — value-returning table select/filter/sort/append/set/join/group/GFM - `net/http.mq.md` — HTTP/1.1 response parsing (headers/chunked) and request builder - `tests/` — product self-tests for every public module diff --git a/stdlib/data/csv.mq.md b/stdlib/data/csv.mq.md new file mode 100644 index 0000000..6da22bf --- /dev/null +++ b/stdlib/data/csv.mq.md @@ -0,0 +1,163 @@ +## 成功 + +`(值)` + +*{ok: true, value: 值}* + +## 失败 + +`(代码, 消息)` + +*{ok: false, code: 代码, message: 消息}* + +## 解析行 + +`(文本)` + +解析单行 CSV:逗号分隔、双引号包裹、`""` 表示一个字面双引号。 +不处理换行、多行字段或首尾空白。未闭合引号和未加引号字段中的双引号 +返回可恢复错误。 + +**点 = text.codepoints(文本)** + +**下标 = 0** + +**字段 = ""** + +**字段表 = []** + +**引号内 = false** + +**错误 = ""** + +- `下标 < 长度(点) and 错误 == ""` + + **字符 = text.from_codepoint(点[下标])** + + 1. `引号内 and 字符 == "\""` + + 1. `下标 + 1 < 长度(点) and text.from_codepoint(点[下标 + 1]) == "\""` + + **`字段 = 字段 + "\""`** + + **下标 = 下标 + 2** + + 2. `true` + + **引号内 = false** + + **下标 = 下标 + 1** + + 2. `引号内` + + **字段 = 字段 + 字符** + + **下标 = 下标 + 1** + + 3. `字符 == "\""` + + 1. `字段 == ""` + + **引号内 = true** + + **下标 = 下标 + 1** + + 2. `true` + + **错误 = "csv.invalid_quote"** + + 4. `字符 == ","` + + **字段表 = list.append(字段表, 字段)** + + **字段 = ""** + + **下标 = 下标 + 1** + + 5. `true` + + **字段 = 字段 + 字符** + + **下标 = 下标 + 1** + +1. `错误 != ""` + + *失败(错误, "未加引号字段中出现双引号")* + +1. `引号内` + + *失败("csv.unterminated_quote", "引号未闭合")* + +*成功(list.concat(字段表, [字段]))* + +## 格式化字段 + +`(文本)` + +含逗号、双引号、换行或回车的字段加引号,内部双引号写成 `""`。 + +**点 = text.codepoints(文本)** + +**下标 = 0** + +**需要引号 = false** + +- `下标 < 长度(点) and not 需要引号` + + **码点 = 点[下标]** + + 1. `码点 == 34 or 码点 == 44 or 码点 == 10 or 码点 == 13` + + **需要引号 = true** + + 2. `true` + + **忽略 = 0** + + **下标 = 下标 + 1** + +1. `not 需要引号` + + *文本* + +**`结果 = "\""`** + +**下标 = 0** + +- `下标 < 长度(点)` + + 1. `点[下标] == 34` + + **`结果 = 结果 + "\"\""`** + + 2. `true` + + **结果 = 结果 + text.from_codepoint(点[下标])** + + **下标 = 下标 + 1** + +*`结果 + "\""`* + +## 格式化行 + +`(列表)` + +**结果 = ""** + +**下标 = 0** + +- `下标 < 长度(列表)` + + 1. `下标 > 0` + + **结果 = 结果 + ","** + + 2. `true` + + **忽略 = 0** + + **结果 = 结果 + 格式化字段(文本(列表[下标]))** + + **下标 = 下标 + 1** + +*结果* diff --git a/stdlib/data/json.mq.md b/stdlib/data/json.mq.md new file mode 100644 index 0000000..6f58d5a --- /dev/null +++ b/stdlib/data/json.mq.md @@ -0,0 +1,638 @@ +## 解析 + +`(文本)` + +解析一个完整的 JSON 文本。值之后只允许空白;多余输入返回 +`json.trailing`。成功结果只保留 `{ok, value}`,不暴露内部游标。 + +**点 = text.codepoints(文本)** + +**结果 = 解析值(点, 0)** + +1. `not 结果.ok` + + *结果* + +**位置 = 跳过空白(点, 结果.next)** + +1. `位置 != 长度(点)` + + *失败("json.trailing", "值后存在多余输入")* + +*{ok: true, value: 结果.value}* + +## 成功值 + +`(值, 下一位置)` + +*{ok: true, value: 值, next: 下一位置}* + +## 失败 + +`(代码, 消息)` + +*{ok: false, code: 代码, message: 消息}* + +## 是空白 + +`(码点)` + +*码点 == 32 or 码点 == 9 or 码点 == 10 or 码点 == 13* + +## 是数字 + +`(码点)` + +*码点 >= 48 and 码点 <= 57* + +## 跳过空白 + +`(点, 位置)` + +**当前 = 位置** + +- `当前 < 长度(点) and 是空白(点[当前])` + + **当前 = 当前 + 1** + +*当前* + +## 匹配 + +`(点, 位置, 文本)` + +**目标 = text.codepoints(文本)** + +**下标 = 0** + +**结果 = true** + +- `下标 < 长度(目标) and 结果` + + 1. `位置 + 下标 >= 长度(点)` + + **结果 = false** + + 2. `点[位置 + 下标] != 目标[下标]` + + **结果 = false** + + 3. `true` + + **继续 = 0** + + **下标 = 下标 + 1** + +*结果* + +## 片段 + +`(点, 起点, 终点)` + +**结果 = ""** + +**下标 = 起点** + +- `下标 < 终点` + + **结果 = 结果 + text.from_codepoint(点[下标])** + + **下标 = 下标 + 1** + +*结果* + +## 解析数字 + +`(点, 位置)` + +JSON 数字:可选负号、整数位、可选小数位、可选指数;含 `.`/`e` 返回 Float。 + +**开始 = 位置** + +**当前 = 位置** + +1. `text.from_codepoint(点[当前]) == "-"` + + **当前 = 当前 + 1** + +2. `true` + + **非负 = 0** + +**整数位 = 0** + +- `当前 < 长度(点) and 是数字(点[当前])` + + **整数位 = 整数位 + 1** + + **当前 = 当前 + 1** + +**有小数 = false** + +**小数位 = 0** + +1. `当前 < 长度(点) and text.from_codepoint(点[当前]) == "."` + + **有小数 = true** + + **当前 = 当前 + 1** + + - `当前 < 长度(点) and 是数字(点[当前])` + + **小数位 = 小数位 + 1** + + **当前 = 当前 + 1** + +2. `true` + + **忽略 = 0** + +**指数位 = 0** + +**指数标记 = false** + +1. `当前 < 长度(点) and (text.from_codepoint(点[当前]) == "e" or text.from_codepoint(点[当前]) == "E")` + + **有小数 = true** + + **指数标记 = true** + + **当前 = 当前 + 1** + + 1. `当前 < 长度(点) and (text.from_codepoint(点[当前]) == "+" or text.from_codepoint(点[当前]) == "-")` + + **当前 = 当前 + 1** + + 2. `true` + + **忽略指数符号 = 0** + + - `当前 < 长度(点) and 是数字(点[当前])` + + **指数位 = 指数位 + 1** + + **当前 = 当前 + 1** + +1. `整数位 == 0` + + *失败("json.invalid_number", "数字缺少整数位")* + +1. `有小数 and not 指数标记 and 小数位 == 0` + + *失败("json.invalid_number", "小数缺少位数")* + +1. `指数标记 and 指数位 == 0` + + *失败("json.invalid_number", "指数缺少位数")* + +**原文 = 片段(点, 开始, 当前)** + +1. `有小数` + + *成功值(小数(原文), 当前)* + +*成功值(整数(原文), 当前)* + +## 十六进制值 + +`(码点)` + +1. `码点 >= 48 and 码点 <= 57` + + *码点 - 48* + +2. `码点 >= 65 and 码点 <= 70` + + *码点 - 55* + +3. `码点 >= 97 and 码点 <= 102` + + *码点 - 87* + +*0 - 1* + +## 解析字符串 + +`(点, 位置)` + +**当前 = 位置 + 1** + +**结果 = ""** + +**完成 = false** + +**错误 = ""** + +- `当前 < 长度(点) and not 完成 and 错误 == ""` + + **码点 = 点[当前]** + + 1. `码点 == 34` + + **完成 = true** + + **当前 = 当前 + 1** + + 2. `码点 == 92` + + 1. `当前 + 1 >= 长度(点)` + + **错误 = "json.invalid_escape"** + + 2. `true` + + **转义码点 = 点[当前 + 1]** + + 1. `转义码点 == 34` + + **结果 = 结果 + text.from_codepoint(34)** + + **当前 = 当前 + 2** + + 2. `转义码点 == 92` + + **结果 = 结果 + text.from_codepoint(92)** + + **当前 = 当前 + 2** + + 3. `转义码点 == 47` + + **结果 = 结果 + text.from_codepoint(47)** + + **当前 = 当前 + 2** + + 4. `转义码点 == 98` + + **结果 = 结果 + text.from_codepoint(8)** + + **当前 = 当前 + 2** + + 5. `转义码点 == 102` + + **结果 = 结果 + text.from_codepoint(12)** + + **当前 = 当前 + 2** + + 6. `转义码点 == 110` + + **结果 = 结果 + text.from_codepoint(10)** + + **当前 = 当前 + 2** + + 7. `转义码点 == 114` + + **结果 = 结果 + text.from_codepoint(13)** + + **当前 = 当前 + 2** + + 8. `转义码点 == 116` + + **结果 = 结果 + text.from_codepoint(9)** + + **当前 = 当前 + 2** + + 9. `转义码点 == 117` + + 1. `当前 + 5 >= 长度(点)` + + **错误 = "json.invalid_escape"** + + 2. `true` + + **数字 = 十六进制值(点[当前 + 2]) * 4096 + 十六进制值(点[当前 + 3]) * 256 + 十六进制值(点[当前 + 4]) * 16 + 十六进制值(点[当前 + 5])** + + 1. `数字 < 0 or (数字 >= 55296 and 数字 <= 57343)` + + **错误 = "json.invalid_escape"** + + 2. `true` + + **结果 = 结果 + text.from_codepoint(数字)** + + **当前 = 当前 + 6** + + 10. `true` + + **错误 = "json.invalid_escape"** + + 3. `true` + + **结果 = 结果 + text.from_codepoint(码点)** + + **当前 = 当前 + 1** + +1. `错误 != ""` + + *失败("json.invalid_string", "字符串转义无效")* + +1. `not 完成` + + *失败("json.unexpected_eof", "字符串未闭合")* + +*成功值(结果, 当前)* + +## 解析对象 + +`(点, 位置)` + +*解析对象项(点, 位置 + 1, {})* + +## 解析对象项 + +`(点, 位置, 已解析)` + +**位置 = 跳过空白(点, 位置)** + +1. `位置 >= 长度(点)` + + *失败("json.unexpected_eof", "对象未闭合")* + +**当前 = text.from_codepoint(点[位置])** + +1. `当前 == "}"` + + *成功值(已解析, 位置 + 1)* + +**键结果 = 解析字符串(点, 位置)** + +1. `not 键结果.ok` + + *键结果* + +**键 = 键结果.value** + +**位置 = 跳过空白(点, 键结果.next)** + +1. `位置 >= 长度(点)` + + *失败("json.unexpected_eof", "对象缺少冒号")* + +1. `text.from_codepoint(点[位置]) != ":"` + + *失败("json.invalid_object", "对象缺少冒号")* + +**位置 = 跳过空白(点, 位置 + 1)** + +**值结果 = 解析值(点, 位置)** + +1. `not 值结果.ok` + + *值结果* + +**新的已解析 = map.set(已解析, 键, 值结果.value)** + +**位置 = 跳过空白(点, 值结果.next)** + +1. `位置 >= 长度(点)` + + *失败("json.unexpected_eof", "对象未闭合")* + +**分隔 = text.from_codepoint(点[位置])** + +1. `分隔 == ","` + + *解析对象项(点, 位置 + 1, 新的已解析)* + +2. `分隔 == "}"` + + *成功值(新的已解析, 位置 + 1)* + +3. `true` + + *失败("json.invalid_object", "对象缺少逗号或 }")* + +## 解析值 + +`(点, 位置)` + +**位置 = 跳过空白(点, 位置)** + +1. `位置 >= 长度(点)` + + *失败("json.unexpected_eof", "需要值")* + +**首 = text.from_codepoint(点[位置])** + +**首码点 = 点[位置]** + +1. `首码点 == 34` + + *解析字符串(点, 位置)* + +2. `首码点 == 123` + + *解析对象(点, 位置)* + +3. `首 == "["` + + *解析数组(点, 位置)* + +4. `首 == "-" or 是数字(点[位置])` + + *解析数字(点, 位置)* + +5. `首 == "t"` + + 1. `匹配(点, 位置, "true")` + + *成功值(true, 位置 + 4)* + + 2. `true` + + *失败("json.invalid_literal", "无效字面量")* + +6. `首 == "f"` + + 1. `匹配(点, 位置, "false")` + + *成功值(false, 位置 + 5)* + + 2. `true` + + *失败("json.invalid_literal", "无效字面量")* + +7. `首 == "n"` + + 1. `匹配(点, 位置, "null")` + + *成功值(None, 位置 + 4)* + + 2. `true` + + *失败("json.invalid_literal", "无效字面量")* + +8. `true` + + *失败("json.unexpected_token", "不支持的值")* + +## 解析数组 + +`(点, 位置)` + +*解析数组项(点, 位置 + 1, [])* + +## 解析数组项 + +`(点, 位置, 已解析)` + +**位置 = 跳过空白(点, 位置)** + +1. `位置 >= 长度(点)` + + *失败("json.unexpected_eof", "数组未闭合")* + +**当前 = text.from_codepoint(点[位置])** + +1. `当前 == "]"` + + *成功值(已解析, 位置 + 1)* + +**项 = 解析值(点, 位置)** + +1. `not 项.ok` + + *项* + +**新的已解析 = list.append(已解析, 项.value)** + +**位置 = 跳过空白(点, 项.next)** + +1. `位置 >= 长度(点)` + + *失败("json.unexpected_eof", "数组未闭合")* + +**分隔 = text.from_codepoint(点[位置])** + +1. `分隔 == ","` + + *解析数组项(点, 位置 + 1, 新的已解析)* + +2. `分隔 == "]"` + + *成功值(新的已解析, 位置 + 1)* + +3. `true` + + *失败("json.invalid_array", "数组缺少逗号或 ]")* + +## 格式化文本 + +`(文本)` + +**点 = text.codepoints(文本)** + +**`结果 = "\""`** + +**下标 = 0** + +- `下标 < 长度(点)` + + **码点 = 点[下标]** + + 1. `码点 == 34` + + **`结果 = 结果 + "\\\""`** + + 2. `码点 == 92` + + **`结果 = 结果 + "\\\\"`** + + 3. `码点 == 10` + + **`结果 = 结果 + "\\n"`** + + 4. `码点 == 9` + + **`结果 = 结果 + "\\t"`** + + 5. `码点 == 13` + + **`结果 = 结果 + "\\r"`** + + 6. `true` + + **结果 = 结果 + text.from_codepoint(码点)** + + **下标 = 下标 + 1** + +*`结果 + "\""`* + +## 格式化列表 + +`(列表)` + +**结果 = "["** + +**下标 = 0** + +- `下标 < 长度(列表)` + + 1. `下标 > 0` + + **结果 = 结果 + ", "** + + 2. `true` + + **忽略 = 0** + + **结果 = 结果 + 格式化(列表[下标])** + + **下标 = 下标 + 1** + +*结果 + "]"* + +## 格式化对象 + +`(映射)` + +**键列表 = map.keys(映射)** + +**结果 = "{"** + +**下标 = 0** + +- `下标 < 长度(键列表)` + + 1. `下标 > 0` + + **结果 = 结果 + ", "** + + 2. `true` + + **忽略 = 0** + + **当前键 = 键列表[下标]** + + **结果 = 结果 + 格式化文本(当前键) + ": " + 格式化(map.get(映射, 当前键))** + + **下标 = 下标 + 1** + +*结果 + "}"* + +## 格式化 + +`(值)` + +**类别 = 类型(值)** + +1. `类别 == "Text"` + + *格式化文本(值)* + +2. `类别 == "None"` + + *"null"* + +3. `类别 == "Bool" or 类别 == "Int" or 类别 == "Float"` + + *文本(值)* + +4. `类别 == "List"` + + *格式化列表(值)* + +5. `类别 == "Map"` + + *格式化对象(值)* + +*"null"* diff --git a/stdlib/stdlib.json b/stdlib/stdlib.json index 9a264bb..124f127 100644 --- a/stdlib/stdlib.json +++ b/stdlib/stdlib.json @@ -1,5 +1,5 @@ { "name": "marqdo-stdlib", "version": "0.1.0", - "digest": "sha256:3a1544c355ad4b08d679126b4c51f7bc79d282a1b22c9a2bda805f24cc3d4023" + "digest": "sha256:dc0ab593afeaabde0c1f688b039f35d6efac28936412ba624a408cf0d587aeb8" } diff --git a/stdlib/tests/csv.mq.md b/stdlib/tests/csv.mq.md new file mode 100644 index 0000000..ba7475c --- /dev/null +++ b/stdlib/tests/csv.mq.md @@ -0,0 +1,16 @@ +--- +import: + csv: "std:data/csv" +--- + +## main + +**`行 = csv.解析行("甲,\"乙,丙\",\"说\"\"好\"")`** + +**`空 = csv.解析行("")`** + +**`坏 = csv.解析行("\"a")`** + +**`框 = csv.解析行("a,\"b\"")`** + +*[行.value, csv.格式化行(行.value), 空.value, 坏.code, 框.value]* diff --git a/stdlib/tests/json.mq.md b/stdlib/tests/json.mq.md new file mode 100644 index 0000000..2377f76 --- /dev/null +++ b/stdlib/tests/json.mq.md @@ -0,0 +1,16 @@ +--- +import: + json: "std:data/json" +--- + +## main + +**`对象 = json.解析("{\"a\": 1, \"b\": [true, null, \"说\\\"好\"]}")`** + +**`浮点 = json.解析("[1, 2.5, -3e2, 4E-1, {\"a\": 1.0}, \"x\"]")`** + +**`失败 = json.解析("[1 2]")`** + +**`尾随 = json.解析("1 2")`** + +*[对象.ok, 对象.value.a, 对象.value.b[2], json.格式化(浮点.value), 失败.ok, 失败.code, 尾随.code]*