Implements the first vertical slices of RFC-CM-0001/0002 on top of a113d89:
- P1: opaque Memory regions (64 KiB pages, budgets, grow-only), byte
read/write, Memory-index place assignment, wire/host-boundary rejection.
- CM-A04/A05: value-semantics List/Map/Text/Table primitives and the
recoverable Result map convention.
- CM-A06: opaque Resource handles, resource.open/read/write/close, stdout
write, granted project-file read and buffered write, strict fail-closed.
- CM-A07 Compat-A: explicit .mq.md format/utf8/io chain matching builtin
print for the covered value types; builtin remains default.
- P2 fixtures: allocators, list/tree/graph/hash, integer codecs, UTF-8
encode/decode, CSV/JSON subsets, table filter/sort/join, bytecode VM.
- ADRs 0022-0027, spec updates, evidence reports and 68 semantic goldens.
Gate: cargo fmt --check, clippy -D warnings, 407 tests passed.
7.3 KiB
成功值
(值, 下一位置)
{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 结果-
位置 + 下标 >= 长度(点)结果 = false
-
点[位置 + 下标] != 目标[下标]结果 = false
-
true继续 = 0
下标 = 下标 + 1
-
结果
解析数字
(点, 位置)
负数 = false
当前 = 位置
-
text.from_codepoint(点[当前]) == "-"负数 = true
当前 = 当前 + 1
-
true非负 = 0
值 = 0
开始 = 当前
-
当前 < 长度(点) and 是数字(点[当前])值 = 值 * 10 + 点[当前] - 48
当前 = 当前 + 1
-
当前 == 开始失败("json.invalid_number", "数字缺少位数")
-
负数成功值(0 - 值, 当前)
成功值(值, 当前)
十六进制值
(码点)
-
码点 >= 48 and 码点 <= 57码点 - 48
-
码点 >= 65 and 码点 <= 70码点 - 55
-
码点 >= 97 and 码点 <= 102码点 - 87
0 - 1
解析字符串
(点, 位置)
当前 = 位置 + 1
结果 = ""
完成 = false
错误 = ""
-
当前 < 长度(点) and not 完成 and 错误 == ""码点 = 点[当前]
-
码点 == 34完成 = true
当前 = 当前 + 1
-
码点 == 92-
当前 + 1 >= 长度(点)错误 = "json.invalid_escape"
-
true转义码点 = 点[当前 + 1]
-
转义码点 == 34结果 = 结果 + text.from_codepoint(34)
当前 = 当前 + 2
-
转义码点 == 92结果 = 结果 + text.from_codepoint(92)
当前 = 当前 + 2
-
转义码点 == 47结果 = 结果 + text.from_codepoint(47)
当前 = 当前 + 2
-
转义码点 == 98结果 = 结果 + text.from_codepoint(8)
当前 = 当前 + 2
-
转义码点 == 102结果 = 结果 + text.from_codepoint(12)
当前 = 当前 + 2
-
转义码点 == 110结果 = 结果 + text.from_codepoint(10)
当前 = 当前 + 2
-
转义码点 == 114结果 = 结果 + text.from_codepoint(13)
当前 = 当前 + 2
-
转义码点 == 116结果 = 结果 + text.from_codepoint(9)
当前 = 当前 + 2
-
转义码点 == 117-
当前 + 5 >= 长度(点)错误 = "json.invalid_escape"
-
true数字 = 十六进制值(点[当前 + 2]) * 4096 + 十六进制值(点[当前 + 3]) * 256 + 十六进制值(点[当前 + 4]) * 16 + 十六进制值(点[当前 + 5])
-
数字 < 0 or (数字 >= 55296 and 数字 <= 57343)错误 = "json.invalid_escape"
-
true结果 = 结果 + text.from_codepoint(数字)
当前 = 当前 + 6
-
-
-
true错误 = "json.invalid_escape"
-
-
-
true结果 = 结果 + text.from_codepoint(码点)
当前 = 当前 + 1
-
-
错误 != ""失败("json.invalid_string", "字符串转义无效")
-
not 完成失败("json.unexpected_eof", "字符串未闭合")
成功值(结果, 当前)
解析对象
(点, 位置)
解析对象项(点, 位置 + 1, {})
解析对象项
(点, 位置, 已解析)
位置 = 跳过空白(点, 位置)
-
位置 >= 长度(点)失败("json.unexpected_eof", "对象未闭合")
当前 = text.from_codepoint(点[位置])
-
当前 == "}"成功值(已解析, 位置 + 1)
键结果 = 解析字符串(点, 位置)
-
not 键结果.ok键结果
键 = 键结果.value
位置 = 跳过空白(点, 键结果.next)
-
位置 >= 长度(点)失败("json.unexpected_eof", "对象缺少冒号")
-
text.from_codepoint(点[位置]) != ":"失败("json.invalid_object", "对象缺少冒号")
位置 = 跳过空白(点, 位置 + 1)
值结果 = 解析值(点, 位置)
-
not 值结果.ok值结果
新的已解析 = map.set(已解析, 键, 值结果.value)
位置 = 跳过空白(点, 值结果.next)
-
位置 >= 长度(点)失败("json.unexpected_eof", "对象未闭合")
分隔 = text.from_codepoint(点[位置])
-
分隔 == ","解析对象项(点, 位置 + 1, 新的已解析)
-
分隔 == "}"成功值(新的已解析, 位置 + 1)
-
true失败("json.invalid_object", "对象缺少逗号或 }")
解析值
(点, 位置)
位置 = 跳过空白(点, 位置)
-
位置 >= 长度(点)失败("json.unexpected_eof", "需要值")
首 = text.from_codepoint(点[位置])
首码点 = 点[位置]
-
首码点 == 34解析字符串(点, 位置)
-
首码点 == 123解析对象(点, 位置)
-
首 == "["解析数组(点, 位置)
-
首 == "-" or 是数字(点[位置])解析数字(点, 位置)
-
首 == "t"-
匹配(点, 位置, "true")成功值(true, 位置 + 4)
-
true失败("json.invalid_literal", "无效字面量")
-
-
首 == "f"-
匹配(点, 位置, "false")成功值(false, 位置 + 5)
-
true失败("json.invalid_literal", "无效字面量")
-
-
首 == "n"-
匹配(点, 位置, "null")成功值(None, 位置 + 4)
-
true失败("json.invalid_literal", "无效字面量")
-
-
true失败("json.unexpected_token", "不支持的值")
解析数组
(点, 位置)
解析数组项(点, 位置 + 1, [])
解析数组项
(点, 位置, 已解析)
位置 = 跳过空白(点, 位置)
-
位置 >= 长度(点)失败("json.unexpected_eof", "数组未闭合")
当前 = text.from_codepoint(点[位置])
-
当前 == "]"成功值(已解析, 位置 + 1)
项 = 解析值(点, 位置)
-
not 项.ok项
新的已解析 = list.append(已解析, 项.value)
位置 = 跳过空白(点, 项.next)
-
位置 >= 长度(点)失败("json.unexpected_eof", "数组未闭合")
分隔 = text.from_codepoint(点[位置])
-
分隔 == ","解析数组项(点, 位置 + 1, 新的已解析)
-
分隔 == "]"成功值(新的已解析, 位置 + 1)
-
true失败("json.invalid_array", "数组缺少逗号或 ]")
main
对象结果 = 解析值(text.codepoints("{\"a\": 1, \"b\": [true, null, \"说\\\"好\"]}"), 0)
字符串结果 = 解析值(text.codepoints("\"a\\nb\""), 0)
[解析值(text.codepoints(" [1, 23, [4, 5], -6] "), 0), 解析值(text.codepoints("[1 2]"), 0), 解析值(text.codepoints("[1,"), 0), 对象结果, 字符串结果]