Files
marqdo-cli/tests/conformance/semantics/62-json-array-parser.mq.md
T
cflmy 92bfbf2568 feat(rfc-cm): baseline P0-P3 compute machine, stdlib and resource slices
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.
2026-11-20 21:16:54 +08:00

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 结果

    1. 位置 + 下标 >= 长度(点)

      结果 = false

    2. 点[位置 + 下标] != 目标[下标]

      结果 = false

    3. true

      继续 = 0

    下标 = 下标 + 1

结果

解析数字

(点, 位置)

负数 = false

当前 = 位置

  1. text.from_codepoint(点[当前]) == "-"

    负数 = true

    当前 = 当前 + 1

  2. true

    非负 = 0

值 = 0

开始 = 当前

  • 当前 < 长度(点) and 是数字(点[当前])

    值 = 值 * 10 + 点[当前] - 48

    当前 = 当前 + 1

  1. 当前 == 开始

    失败("json.invalid_number", "数字缺少位数")

  2. 负数

    成功值(0 - 值, 当前)

成功值(值, 当前)

十六进制值

(码点)

  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", "字符串转义无效")

  2. not 完成

    失败("json.unexpected_eof", "字符串未闭合")

成功值(结果, 当前)

解析对象

(点, 位置)

解析对象项(点, 位置 + 1, {})

解析对象项

(点, 位置, 已解析)

位置 = 跳过空白(点, 位置)

  1. 位置 >= 长度(点)

    失败("json.unexpected_eof", "对象未闭合")

当前 = text.from_codepoint(点[位置])

  1. 当前 == "}"

    成功值(已解析, 位置 + 1)

键结果 = 解析字符串(点, 位置)

  1. not 键结果.ok

    键结果

键 = 键结果.value

位置 = 跳过空白(点, 键结果.next)

  1. 位置 >= 长度(点)

    失败("json.unexpected_eof", "对象缺少冒号")

  2. 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", "数组缺少逗号或 ]")

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), 对象结果, 字符串结果]