feat(rfc-cm): add JSON floats and compact formatting
- Replace the JSON number scanner with sign/integer/fraction/exponent
scanning; return Int or Float via 整数/小数 and reject missing digits.
- Add 片段/格式化文本/格式化列表/格式化对象/格式化 recursive formatter.
- Extend fixture 62 with floats ({1,2.5,-3e2,4E-1,{a:1.0},x}) and a compact
round-trip string.
- Update P2 status: only the separate stdlib package and frozen-CLI closure
lane remain.
Gate: fmt, clippy -D warnings, 407 tests passed.
This commit is contained in:
1 parent
5d3a5fb13b
commit
1a978c7a42
7 files changed
+280
-30
No files matched your search
+6
-6
@@ -142,13 +142,13 @@ by:
|
||||
* semantic fixture 61: a `Memory`-backed graph adjacency list with 16-byte nodes
|
||||
and 8-byte edges, head-inserted edges and a neighbor traversal; no Rust graph
|
||||
container or new `Value` type;
|
||||
* semantic fixture 62: a recursive `.mq.md` JSON subset parser for nested
|
||||
* semantic fixture 62: a recursive `.mq.md` JSON parser/formatter for nested
|
||||
arrays and objects, strings with standard escapes including `\uXXXX`,
|
||||
integers (including negative), `true`/`false`/`null`, whitespace, `next`
|
||||
positions and `Result` errors (`json.unexpected_eof`, `json.invalid_array`,
|
||||
`json.invalid_object`, `json.invalid_string`, `json.invalid_number`,
|
||||
`json.invalid_literal`, `json.unexpected_token`). Floats are explicitly out
|
||||
of scope;
|
||||
ints and floats (including exponents), `true`/`false`/`null`, whitespace,
|
||||
`next` positions, compact output and `Result` errors
|
||||
(`json.unexpected_eof`, `json.invalid_array`, `json.invalid_object`,
|
||||
`json.invalid_string`, `json.invalid_number`, `json.invalid_literal`,
|
||||
`json.unexpected_token`);
|
||||
* semantic fixture 63: a `Memory`-backed stack bytecode VM with instruction
|
||||
decode, PUSH/ADD/MUL/JMP/JZ/HALT, a stack region and two programs (arithmetic
|
||||
and conditional branch); no Rust VM or interpreter loop was added;
|
||||
|
||||
@@ -135,6 +135,57 @@
|
||||
}
|
||||
},
|
||||
"type": "map"
|
||||
},
|
||||
{
|
||||
"entries": {
|
||||
"next": {
|
||||
"decimal": "37",
|
||||
"type": "int"
|
||||
},
|
||||
"ok": {
|
||||
"type": "bool",
|
||||
"value": true
|
||||
},
|
||||
"value": {
|
||||
"items": [
|
||||
{
|
||||
"decimal": "1",
|
||||
"type": "int"
|
||||
},
|
||||
{
|
||||
"type": "float",
|
||||
"value": 2.5
|
||||
},
|
||||
{
|
||||
"type": "float",
|
||||
"value": -300.0
|
||||
},
|
||||
{
|
||||
"type": "float",
|
||||
"value": 0.4
|
||||
},
|
||||
{
|
||||
"entries": {
|
||||
"a": {
|
||||
"type": "float",
|
||||
"value": 1.0
|
||||
}
|
||||
},
|
||||
"type": "map"
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"value": "x"
|
||||
}
|
||||
],
|
||||
"type": "list"
|
||||
}
|
||||
},
|
||||
"type": "map"
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"value": "[1, 2.5, -300.0, 0.4, {\"a\": 1.0}, \"x\"]"
|
||||
}
|
||||
],
|
||||
"type": "list"
|
||||
|
||||
@@ -62,43 +62,113 @@
|
||||
|
||||
*结果*
|
||||
|
||||
## 片段
|
||||
|
||||
`(点, 起点, 终点)`
|
||||
|
||||
**结果 = ""**
|
||||
|
||||
**下标 = 起点**
|
||||
|
||||
- `下标 < 终点`
|
||||
|
||||
**结果 = 结果 + text.from_codepoint(点[下标])**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果*
|
||||
|
||||
## 解析数字
|
||||
|
||||
`(点, 位置)`
|
||||
|
||||
**负数 = false**
|
||||
JSON 数字:可选负号、整数位、可选小数位、可选指数;含 `.`/`e` 返回 Float。
|
||||
|
||||
**开始 = 位置**
|
||||
|
||||
**当前 = 位置**
|
||||
|
||||
1. `text.from_codepoint(点[当前]) == "-"`
|
||||
|
||||
**负数 = true**
|
||||
|
||||
**当前 = 当前 + 1**
|
||||
|
||||
2. `true`
|
||||
|
||||
**非负 = 0**
|
||||
|
||||
**值 = 0**
|
||||
|
||||
**开始 = 当前**
|
||||
**整数位 = 0**
|
||||
|
||||
- `当前 < 长度(点) and 是数字(点[当前])`
|
||||
|
||||
**值 = 值 * 10 + 点[当前] - 48**
|
||||
**整数位 = 整数位 + 1**
|
||||
|
||||
**当前 = 当前 + 1**
|
||||
|
||||
1. `当前 == 开始`
|
||||
**有小数 = false**
|
||||
|
||||
*失败("json.invalid_number", "数字缺少位数")*
|
||||
**小数位 = 0**
|
||||
|
||||
1. `负数`
|
||||
1. `当前 < 长度(点) and text.from_codepoint(点[当前]) == "."`
|
||||
|
||||
*成功值(0 - 值, 当前)*
|
||||
**有小数 = 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. `有小数`
|
||||
|
||||
*成功值(小数(原文), 当前)*
|
||||
|
||||
*成功值(整数(原文), 当前)*
|
||||
|
||||
## 十六进制值
|
||||
|
||||
@@ -422,10 +492,136 @@
|
||||
|
||||
*失败("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"*
|
||||
|
||||
## 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), 对象结果, 字符串结果]*
|
||||
**`浮点结果 = 解析值(text.codepoints("[1, 2.5, -3e2, 4E-1, {\"a\": 1.0}, \"x\"]"), 0)`**
|
||||
|
||||
**格式化结果 = 格式化(浮点结果.value)**
|
||||
|
||||
*[解析值(text.codepoints(" [1, 23, [4, 5], -6] "), 0), 解析值(text.codepoints("[1 2]"), 0), 解析值(text.codepoints("[1,"), 0), 对象结果, 字符串结果, 浮点结果, 格式化结果]*
|
||||
Reference in new issue
Block a user