我是 ocamlp4 的新手。我正在阅读Jake Donham 的博客以开始使用它。
我正在尝试编写一个小的 CamlP4 程序,它将获得一个简单的类型:
type t = Foo | Bar | Baz
并生成一个t_of_string
和一个t_to_string
函数。
按照博客上的代码,我应该能够将类型与:
let wrap_str_item si =
let _loc = Ast.loc_of_str_item si in
<:str_item< $si$ >>
match wrap_str_item si with
| <:str_item< type $lid:tid$ = $Ast.TySum (_, ors)$ >> ->
但这不起作用。当我查看 ASTcampl4of xx.ml -printer o
并将其简化为有趣的部分时:
(Ast.TyDcl (_, tid, [],
(Ast.TySum (_,
(Ast.TySum (_, ors)))), [])
但我需要匹配类似的东西
(Ast.TyDcl (_loc, "t", [],
(Ast.TySum (_loc,
(Ast.TyOr (_loc,
(Ast.TyOr (_loc, (Ast.TyId (_loc, (Ast.IdUid (_loc, "Foo")))),
(Ast.TyId (_loc, (Ast.IdUid (_loc, "Bar")))))),
(Ast.TyId (_loc, (Ast.IdUid (_loc, "Baz")))))))),
[]))
似乎比赛案例中的 AST 有一个虚假的TySum
,但我无法摆脱它。
有没有人有解决方案?