ane*_*eal 5 ocaml abstract-syntax-tree camlp4
我希望能够获得给定OCaml程序的AST(例如,我想走AST并生成代码的检测版本或进行某种转换).任何OCaml工具都支持此功能吗?
从 OCaml 4.02.1 开始,可以使用Alain Frisch 编写的 PPX 工具来精确地执行此操作。例子:
% ocamlfind ppx_tools/dumpast -e "1 + 2"
1 + 2
==>
{pexp_desc =
Pexp_apply ({pexp_desc = Pexp_ident {txt = Lident "+"}},
[("", {pexp_desc = Pexp_constant (Const_int 1)});
("", {pexp_desc = Pexp_constant (Const_int 2)})])}
=========
Run Code Online (Sandbox Code Playgroud)
也可以使用该程序转储普通代码文件的 AST,并且各种选项控制转储的精度。例如,在上面的示例中,AST 的位置参数被隐藏。