2

我是 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,但我无法摆脱它。

有没有人有解决方案?

4

2 回答 2

2

这是最近在 3.12.1 Mantis之后修复的一个众所周知的错误。请注意,您的解决方案可能不适用于已修复该错误的下一个版本。

于 2012-02-16T22:45:10.350 回答
1

我终于找到了解决方案。这似乎是 ocaml 3.12.1 中的一种不同的语法。将匹配案例替换为

 <:str_item< type $lid:tid$ = $ors$  >>

将会成功。

不知道为什么。

于 2012-01-26T22:25:03.703 回答