feat(stdlib): add path and regex product modules

This commit is contained in:
Marqdo Agent committed 2026-11-21 08:32:32 +08:00
1 parent bde6f1d0ba
commit cffb6790be
10 files changed
+758 -8

No files matched your search

+2
View File
@@ -15,6 +15,8 @@ module; a mismatch is a hard error.
- `data/json.mq.md` — recursive JSON parser and formatter (recoverable Result)
- `data/csv.mq.md` — quoted single-row CSV parser and formatter
- `data/table.mq.md` — value-returning table select/filter/sort/append/set/join/group/GFM
- `path.mq.md` — lexical join/basename/dirname/extension/normalize (no I/O)
- `regex.mq.md` — `^ $ . * + ? [...]` matcher and search (no groups)
- `net/http.mq.md` — HTTP/1.1 response parsing (headers/chunked) and request builder
- `tests/` — product self-tests for every public module
(`console`, `demo`, `format`, `http`, `io`, `stream`, `table`, `text`, `utf8`)
+180
View File
@@ -0,0 +1,180 @@
## 是否绝对
`(路径)`
**点 = text.codepoints(路径)**
1. `长度(点) == 0`
*false*
2. `点[0] == 47`
*true*
*false*
## 分割
`(文本, 分隔码点)`
**点 = text.codepoints(文本)**
**结果 = []**
**当前 = ""**
**下标 = 0**
- `下标 < 长度(点)`
1. `点[下标] == 分隔码点`
**结果 = list.append(结果, 当前)**
**当前 = ""**
2. `true`
**当前 = 当前 + text.from_codepoint(点[下标])**
**下标 = 下标 + 1**
*list.append(结果, 当前)*
## 连接
`(片段列表)`
空片段被跳过,片段之间只插入一个 `/`。
**结果 = ""**
**下标 = 0**
- `下标 < 长度(片段列表)`
**片段 = 文本(片段列表[下标])**
1. `片段 != ""`
1. `结果 == ""`
**结果 = 片段**
2. `true`
**结果 = 结果 + "/" + 片段**
2. `true`
**忽略 = 0**
**下标 = 下标 + 1**
*结果*
## 文件名
`(路径)`
**片段 = 分割(路径, 47)**
*片段[长度(片段) - 1]*
## 目录名
`(路径)`
**片段 = 分割(路径, 47)**
**结果 = []**
**下标 = 0**
- `下标 < 长度(片段) - 1`
**结果 = list.append(结果, 片段[下标])**
**下标 = 下标 + 1**
*连接(结果)*
## 扩展名
`(路径)`
**名字 = 文件名(路径)**
**点列表 = 分割(名字, 46)**
1. `长度(点列表) <= 1`
*""*
*点列表[长度(点列表) - 1]*
## 去尾
`(列表)`
**结果 = []**
**下标 = 0**
- `下标 < 长度(列表) - 1`
**结果 = list.append(结果, 列表[下标])**
**下标 = 下标 + 1**
*结果*
## 规范
`(路径)`
词法规范化:折叠 `.`、空片段和重复 `/`,用 `..` 消去前一片段;
绝对路径保留前导 `/`。不访问文件系统。
**片段 = 分割(路径, 47)**
**绝对 = 是否绝对(路径)**
**栈 = []**
**下标 = 0**
- `下标 < 长度(片段)`
**段 = 片段[下标]**
1. `段 == "" or 段 == "."`
**忽略 = 0**
2. `段 == ".."`
1. `长度(栈) > 0 and 栈[长度(栈) - 1] != ".."`
**栈 = 去尾(栈)**
2. `not 绝对`
**栈 = list.append(栈, 段)**
3. `true`
**忽略绝对根 = 0**
3. `true`
**栈 = list.append(栈, 段)**
**下标 = 下标 + 1**
1. `绝对`
*"/" + 连接(栈)*
*连接(栈)*
+434
View File
@@ -0,0 +1,434 @@
## 成功
`(值)`
*{ok: true, value: 值}*
## 失败
`(代码, 消息)`
*{ok: false, code: 代码, message: 消息}*
## 包含
`(列表, 值)`
**结果 = false**
**下标 = 0**
- `下标 < 长度(列表) and not 结果`
1. `列表[下标] == 值`
**结果 = true**
2. `true`
**忽略 = 0**
**下标 = 下标 + 1**
*结果*
## 去头
`(列表)`
**结果 = []**
**下标 = 1**
- `下标 < 长度(列表)`
**结果 = list.append(结果, 列表[下标])**
**下标 = 下标 + 1**
*结果*
## 设置最后
`(列表, 值)`
**结果 = []**
**下标 = 0**
- `下标 < 长度(列表)`
1. `下标 + 1 == 长度(列表)`
**结果 = list.append(结果, 值)**
2. `true`
**结果 = list.append(结果, 列表[下标])**
**下标 = 下标 + 1**
*结果*
## 片段
`(点, 起点, 终点)`
**结果 = ""**
**下标 = 起点**
- `下标 < 终点`
**结果 = 结果 + text.from_codepoint(点[下标])**
**下标 = 下标 + 1**
*结果*
## 是数字
`(码点)`
*码点 >= 48 and 码点 <= 57*
## 字面令牌
`(码点)`
包含数量词,默认恰好一次。
*{kind: "literal", value: 码点, neg: false, min: 1, max: 1}*
## 任意令牌
`()`
*{kind: "any", value: 0, neg: false, min: 1, max: 1}*
## 类令牌
`(集合, 否定)`
*{kind: "class", value: 集合, neg: 否定, min: 1, max: 1}*
## 锚点令牌
`(方向)`
*{kind: "anchor", value: 方向, neg: false, min: 1, max: 1}*
## 设置量词
`(令牌, 码点)`
1. `码点 == 42`
*{kind: 令牌.kind, value: 令牌.value, neg: 令牌.neg, min: 0, max: -1}*
2. `码点 == 43`
*{kind: 令牌.kind, value: 令牌.value, neg: 令牌.neg, min: 1, max: -1}*
*{kind: 令牌.kind, value: 令牌.value, neg: 令牌.neg, min: 0, max: 1}*
## 编译类
`(点, 位置)`
**当前 = 位置 + 1**
**否定 = false**
1. `当前 < 长度(点) and 点[当前] == 94`
**否定 = true**
**当前 = 当前 + 1**
2. `true`
**忽略否定 = 0**
**集合 = []**
**完成 = false**
**错误 = ""**
- `当前 < 长度(点) and not 完成 and 错误 == ""`
**码点 = 点[当前]**
1. `码点 == 93`
**完成 = true**
**当前 = 当前 + 1**
2. `码点 == 92`
1. `当前 + 1 >= 长度(点)`
**错误 = "regex.invalid"**
2. `true`
**集合 = list.append(集合, 点[当前 + 1])**
**当前 = 当前 + 2**
3. `当前 + 2 < 长度(点) and 点[当前 + 1] == 45 and 点[当前 + 2] != 93`
**开始 = 码点**
**结束 = 点[当前 + 2]**
1. `开始 > 结束`
**错误 = "regex.invalid"**
2. `true`
**值 = 开始**
- `值 <= 结束`
**集合 = list.append(集合, 值)**
**值 = 值 + 1**
**当前 = 当前 + 3**
4. `true`
**集合 = list.append(集合, 码点)**
**当前 = 当前 + 1**
1. `not 完成`
*失败("regex.invalid", "字符类未闭合")*
1. `错误 != ""`
*失败(错误, "字符类范围无效")*
*成功({set: 集合, neg: 否定, next: 当前})*
## 编译
`(点, 位置)`
**令牌表 = []**
**当前 = 位置**
**错误 = ""**
**代码 = ""**
- `当前 < 长度(点) and 错误 == ""`
**码点 = 点[当前]**
1. `码点 == 94`
**令牌表 = list.append(令牌表, 锚点令牌("start"))**
**当前 = 当前 + 1**
2. `码点 == 36`
**令牌表 = list.append(令牌表, 锚点令牌("end"))**
**当前 = 当前 + 1**
3. `码点 == 91`
**类结果 = 编译类(点, 当前)**
1. `not 类结果.ok`
**错误 = 类结果.code**
**代码 = 类结果.message**
2. `true`
**令牌表 = list.append(令牌表, 类令牌(类结果.value.set, 类结果.value.neg))**
**当前 = 类结果.value.next**
4. `码点 == 92`
1. `当前 + 1 >= 长度(点)`
**错误 = "regex.invalid"**
**代码 = "转义缺少字符"**
2. `true`
**令牌表 = list.append(令牌表, 字面令牌(点[当前 + 1]))**
**当前 = 当前 + 2**
5. `码点 == 46`
**令牌表 = list.append(令牌表, 任意令牌())**
**当前 = 当前 + 1**
6. `码点 == 40 or 码点 == 41 or 码点 == 124`
**错误 = "regex.unsupported"**
**代码 = "不支持分组或选择"**
7. `码点 == 42 or 码点 == 43 or 码点 == 63`
**错误 = "regex.invalid"**
**代码 = "量词没有前一项"**
8. `true`
**令牌表 = list.append(令牌表, 字面令牌(码点))**
**当前 = 当前 + 1**
- `错误 == "" and 当前 < 长度(点) and (点[当前] == 42 or 点[当前] == 43 or 点[当前] == 63)`
**令牌表 = 设置最后(令牌表, 设置量词(令牌表[长度(令牌表) - 1], 点[当前]))**
**当前 = 当前 + 1**
- `当前 < 长度(点) and (点[当前] == 42 or 点[当前] == 43 or 点[当前] == 63)`
**错误 = "regex.invalid"**
**代码 = "连续量词无效"**
1. `错误 != ""`
*失败(错误, 代码)*
*成功(令牌表)*
## 单字符匹配
`(令牌, 码点)`
**类别 = 令牌.kind**
1. `类别 == "any"`
*true*
2. `类别 == "literal"`
*令牌.value == 码点*
3. `类别 == "class"`
*包含(令牌.value, 码点) != 令牌.neg*
*false*
## 尝试匹配
`(令牌, 下标, 当前, 点, 位置, 次数, 要求结束)`
**下限 = 当前.min**
**上限 = 当前.max**
1. `次数 >= 下限 and 匹配序列(令牌, 下标 + 1, 点, 位置, 要求结束)`
*true*
1. `上限 >= 0 and 次数 >= 上限`
*false*
1. `位置 < 长度(点) and 单字符匹配(当前, 点[位置])`
*尝试匹配(令牌, 下标, 当前, 点, 位置 + 1, 次数 + 1, 要求结束)*
*false*
## 匹配序列
`(令牌, 下标, 点, 位置, 要求结束)`
1. `下标 >= 长度(令牌)`
1. `要求结束`
*位置 == 长度(点)*
*true*
**当前 = 令牌[下标]**
1. `当前.kind == "anchor"`
1. `当前.value == "start" and 位置 == 0`
*匹配序列(令牌, 下标 + 1, 点, 位置, 要求结束)*
2. `当前.value == "end" and 位置 == 长度(点)`
*匹配序列(令牌, 下标 + 1, 点, 位置, 要求结束)*
*false*
*尝试匹配(令牌, 下标, 当前, 点, 位置, 0, 要求结束)*
## 匹配
`(模式, 文本)`
编译后要求整个文本匹配。支持 `^`、`$`、`.`、`*`、`+`、`?`、
`[...]` 和 `\\` 转义;不支持分组和选择。
**编译结果 = 编译(text.codepoints(模式), 0)**
1. `not 编译结果.ok`
*编译结果*
*{ok: true, value: 匹配序列(编译结果.value, 0, text.codepoints(文本), 0, true)}*
## 搜索
`(模式, 文本)`
**编译结果 = 编译(text.codepoints(模式), 0)**
1. `not 编译结果.ok`
*编译结果*
**令牌 = 编译结果.value**
**点 = text.codepoints(文本)**
**下标 = 0**
**结果 = false**
- `下标 <= 长度(点) and not 结果`
1. `匹配序列(令牌, 0, 点, 下标, false)`
**结果 = true**
2. `true`
**忽略 = 0**
**下标 = 下标 + 1**
*成功(结果)*
+1 -1
View File
@@ -1,5 +1,5 @@
{
"name": "marqdo-stdlib",
"version": "0.1.0",
"digest": "sha256:dc0ab593afeaabde0c1f688b039f35d6efac28936412ba624a408cf0d587aeb8"
"digest": "sha256:b692e264f046098b83c8046ae9c19ba03c116a078eaac3bdcf207d121e794ffb"
}
+8
View File
@@ -0,0 +1,8 @@
---
import:
path: "std:path"
---
## main
*[path.连接(["a", "", "b", "c"]), path.文件名("a/b/c.txt"), path.目录名("a/b/c.txt"), path.扩展名("a/b/c.txt"), path.规范("./a//b/../c"), path.规范("/a/./b/../c"), path.规范("../a/b"), path.是否绝对("/a"), path.是否绝对("a")]*
+30
View File
@@ -0,0 +1,30 @@
---
import:
regex: "std:regex"
---
## main
**`星号 = regex.匹配("a*b", "aaab")`**
**`加号 = regex.匹配("a+b", "b")`**
**`锚点 = regex.匹配("^a.*z$", "abcz")`**
**`类 = regex.匹配("[a-c]+", "abc")`**
**`否定类 = regex.匹配("[^0-9]", "a")`**
**`任意 = regex.匹配("a.a", "aba")`**
**`可选 = regex.匹配("a?b", "b")`**
**`转义 = regex.匹配("\\.", ".")`**
**`搜索 = regex.搜索("b+c", "xxbbcyy")`**
**`无效 = regex.匹配("[a", "a")`**
**`不支持 = regex.匹配("(a)", "a")`**
*[星号.value, 加号.value, 锚点.value, 类.value, 否定类.value, 任意.value, 可选.value, 转义.value, 搜索.value, 无效.ok, 无效.code, 不支持.code]*