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
@@ -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