px普贤教程PuXian
普贤教程 › 包参考

parser 包

解析器组合子

项值
包名parser
版本0.2.0
可用版本0.1.0 / 0.2.0
入口parser.px
分类数据格式与编解码
文件数 / 行数1 / 352

安装与引入

bash
pxpkg init          # 首次
pxpkg add parser@0.2.0
pxpkg install
px
import parser
所属分类教程:数据格式与编解码

公开接口

函数参数
p_anyinput, pos
p_charc
p_strs
p_digitinput, pos
p_letterinput, pos
p_alnuminput, pos
p_spaceinput, pos
p_take_whilepred
p_endinput, pos
p_seqparsers
p_altparsers
p_manyp
p_many1p
p_optp
p_mapp, f
p_betweenopenp, inner, closep
p_sep_byp, sep
p_sep_by1p, sep
p_wsinput, pos
p_intinput, pos
其余 9 个 def(内部与辅助,可直接调用但不属稳定接口)
函数参数
p_errmsgpos, msg
ps_is_wsc
p_char_atinput, pos
p_identinput, pos
ps_is_alnum_underc
p_parse_intinput, pos
p_parse_floatinput, pos
p_parse_quotedinput, pos
p_optionalinput, pos, parser_fn

包内说明(源码头注释)

text
============================================================
普贤生态库:registry/parser —— 解析器组合子
------------------------------------------------------------
纯函数库(无 IO、无状态),编译(px build)与解释(px run)双模式一致。
约定:解析器 = 函数 (input, pos) → Ok([新pos, 值]) | Err(错误信息)
基本原语:
p_char(c) / p_str(s) / p_any / p_digit / p_letter / p_alnum
p_space / p_take_while(pred) / p_end
组合子:
p_seq(parsers)      → [值...](全部成功)
p_alt(parsers)      → 首个成功分支的值
p_many(p) / p_many1(p) → [值...](零次/一次以上)
p_opt(p)            → 值 或 null(不失败)
p_map(p, f)         → 转换结果
p_between(open, inner, close)
p_sep_by(p, sep) / p_sep_by1(p, sep)
p_ws                → 跳过空白(永不失败)
p_int               → 有符号整数
说明:
- 错误信息带位置("位置 N: ..."),alt 只保存最后分支的错误
- 解析器以函数值传递(闭包捕获按引用,M160 修复后三轨一致)
============================================================
---- 内部 ----
错误信息