feat(json,csv): bound depth and add multi-value/multi-row APIs
This commit is contained in:
1 parent
efbb8007e1
commit
94a13880c4
10 files changed
+316
-12
No files matched your search
+2
-2
@@ -12,8 +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/json.mq.md` — recursive JSON parser/formatter, 128-depth bound, multi-value
|
||||
- `data/csv.mq.md` — quoted single/multi-row CSV parser and formatter
|
||||
- `data/table.mq.md` — value-returning table select/filter/sort/append/set/join/group/GFM
|
||||
- `env.mq.md` — explicitly granted `env.get` names over `resource.request`
|
||||
- `process.mq.md` — explicitly granted `process.run` with timeout/output caps
|
||||
|
||||
@@ -161,3 +161,131 @@
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*结果*
|
||||
|
||||
## 解析
|
||||
|
||||
`(文本)`
|
||||
|
||||
解析多行 CSV(逗号分隔、双引号、`""` 转义、CRLF/LF 行尾,引号内可含
|
||||
换行)。返回 `{ok:true, value:List<List<Text>>}`;未闭合引号和不合法
|
||||
引号返回可恢复错误。
|
||||
|
||||
**点 = 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. `字符 == "\n"`
|
||||
|
||||
**当前行 = list.append(当前行, 字段)**
|
||||
|
||||
**行 = list.append(行, 当前行)**
|
||||
|
||||
**当前行 = []**
|
||||
|
||||
**字段 = ""**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
6. `字符 == "\r"`
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
7. `true`
|
||||
|
||||
**字段 = 字段 + 字符**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
1. `错误 != ""`
|
||||
|
||||
*失败(错误, "未加引号字段中出现双引号")*
|
||||
|
||||
1. `引号内`
|
||||
|
||||
*失败("csv.unterminated_quote", "引号未闭合")*
|
||||
|
||||
1. `字段 != "" or 长度(当前行) > 0`
|
||||
|
||||
**当前行 = list.append(当前行, 字段)**
|
||||
|
||||
**行 = list.append(行, 当前行)**
|
||||
|
||||
*成功(行)*
|
||||
|
||||
## 格式化
|
||||
|
||||
`(行列表)`
|
||||
|
||||
**结果 = ""**
|
||||
|
||||
**行号 = 0**
|
||||
|
||||
- `行号 < 长度(行列表)`
|
||||
|
||||
1. `行号 > 0`
|
||||
|
||||
**结果 = 结果 + "\n"**
|
||||
|
||||
2. `true`
|
||||
|
||||
**忽略 = 0**
|
||||
|
||||
**结果 = 结果 + 格式化行(行列表[行号])**
|
||||
|
||||
**行号 = 行号 + 1**
|
||||
|
||||
*结果*
|
||||
+107
-1
@@ -1,12 +1,91 @@
|
||||
## 嵌套深度
|
||||
|
||||
`(文本)`
|
||||
|
||||
按未转义字符串之外的 `{`/`[` 计算最大嵌套深度。
|
||||
|
||||
**点 = text.codepoints(文本)**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
**深度 = 0**
|
||||
|
||||
**最大 = 0**
|
||||
|
||||
**字符串内 = false**
|
||||
|
||||
**转义 = false**
|
||||
|
||||
- `下标 < 长度(点)`
|
||||
|
||||
**码点 = 点[下标]**
|
||||
|
||||
1. `字符串内`
|
||||
|
||||
1. `转义`
|
||||
|
||||
**转义 = false**
|
||||
|
||||
2. `码点 == 92`
|
||||
|
||||
**转义 = true**
|
||||
|
||||
3. `码点 == 34`
|
||||
|
||||
**字符串内 = false**
|
||||
|
||||
4. `true`
|
||||
|
||||
**忽略 = 0**
|
||||
|
||||
2. `码点 == 34`
|
||||
|
||||
**字符串内 = true**
|
||||
|
||||
3. `码点 == 123 or 码点 == 91`
|
||||
|
||||
**深度 = 深度 + 1**
|
||||
|
||||
1. `深度 > 最大`
|
||||
|
||||
**最大 = 深度**
|
||||
|
||||
2. `true`
|
||||
|
||||
**忽略 = 0**
|
||||
|
||||
4. `码点 == 125 or 码点 == 93`
|
||||
|
||||
1. `深度 > 0`
|
||||
|
||||
**深度 = 深度 - 1**
|
||||
|
||||
2. `true`
|
||||
|
||||
**忽略 = 0**
|
||||
|
||||
5. `true`
|
||||
|
||||
**忽略 = 0**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
*最大*
|
||||
|
||||
## 解析
|
||||
|
||||
`(文本)`
|
||||
|
||||
解析一个完整的 JSON 文本。值之后只允许空白;多余输入返回
|
||||
`json.trailing`。成功结果只保留 `{ok, value}`,不暴露内部游标。
|
||||
`json.trailing`,嵌套超过 128 层返回 `json.depth`。成功结果只保留
|
||||
`{ok, value}`,不暴露内部游标。
|
||||
|
||||
**点 = text.codepoints(文本)**
|
||||
|
||||
1. `嵌套深度(文本) > 128`
|
||||
|
||||
*失败("json.depth", "嵌套深度超过 128")*
|
||||
|
||||
**结果 = 解析值(点, 0)**
|
||||
|
||||
1. `not 结果.ok`
|
||||
@@ -636,3 +715,30 @@ JSON 数字:可选负号、整数位、可选小数位、可选指数;含 `.
|
||||
*格式化对象(值)*
|
||||
|
||||
*"null"*
|
||||
|
||||
## 解析多个
|
||||
|
||||
`(文本)`
|
||||
|
||||
解析连续多个 JSON 值(空白分隔),返回 `{ok:true, value:List}`。
|
||||
任一项失败时返回该项的错误,不返回部分结果。
|
||||
|
||||
**点 = text.codepoints(文本)**
|
||||
|
||||
**位置 = 跳过空白(点, 0)**
|
||||
|
||||
**结果 = []**
|
||||
|
||||
- `位置 < 长度(点)`
|
||||
|
||||
**项 = 解析值(点, 位置)**
|
||||
|
||||
1. `not 项.ok`
|
||||
|
||||
*项*
|
||||
|
||||
**结果 = list.append(结果, 项.value)**
|
||||
|
||||
**位置 = 跳过空白(点, 项.next)**
|
||||
|
||||
*{ok: true, value: 结果}*
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "marqdo-stdlib",
|
||||
"version": "0.1.0",
|
||||
"digest": "sha256:1e5087c05a3c1f5ce0581c834a061d12b88cc0e9eed098b766d4ff1a2ecf859e"
|
||||
"digest": "sha256:770a63bbd1d3c8a62471c0f0c3cb9ee5647656c137a528442f16e3bd63505d5f"
|
||||
}
|
||||
@@ -13,4 +13,8 @@ import:
|
||||
|
||||
**`框 = csv.解析行("a,\"b\"")`**
|
||||
|
||||
*[行.value, csv.格式化行(行.value), 空.value, 坏.code, 框.value]*
|
||||
**`多行 = csv.解析("甲,\"乙,丙\"\n\"说\"\"好\",2")`**
|
||||
|
||||
**格式化 = csv.格式化(多行.value)**
|
||||
|
||||
*[行.value, csv.格式化行(行.value), 空.value, 坏.code, 框.value, 多行.ok, 多行.value, 格式化]*
|
||||
+23
-1
@@ -13,4 +13,26 @@ import:
|
||||
|
||||
**`尾随 = json.解析("1 2")`**
|
||||
|
||||
*[对象.ok, 对象.value.a, 对象.value.b[2], json.格式化(浮点.value), 失败.ok, 失败.code, 尾随.code]*
|
||||
**深度文本 = ""**
|
||||
|
||||
**深度下标 = 0**
|
||||
|
||||
- `深度下标 < 130`
|
||||
|
||||
**深度文本 = 深度文本 + "["**
|
||||
|
||||
**深度下标 = 深度下标 + 1**
|
||||
|
||||
**深度下标 = 0**
|
||||
|
||||
- `深度下标 < 130`
|
||||
|
||||
**深度文本 = 深度文本 + "]"**
|
||||
|
||||
**深度下标 = 深度下标 + 1**
|
||||
|
||||
**深度 = json.解析(深度文本)**
|
||||
|
||||
**`多个 = json.解析多个("1 2 [3]")`**
|
||||
|
||||
*[对象.ok, 对象.value.a, 对象.value.b[2], json.格式化(浮点.value), 失败.ok, 失败.code, 尾随.code, 深度.ok, 深度.code, 多个.ok, 多个.value]*
|
||||
Reference in new issue
Block a user