camp4: How to match Ast.TySum to retrieve Ast.TyOr

93 views Asked by At

I'm new with ocamlp4. I'm reading Jake Donham's blog to get started with it.

I'm trying to write a small CamlP4 program wich will get a simple type as:

type t = Foo | Bar | Baz

and generate a t_of_string and a t_to_string function.

Following the code on the blog I should be able to match the type with:

 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)$  >> -> 

But this doesn't work. When I look at the AST with campl4of xx.ml -printer o and I reduce it to the interesting part:

(Ast.TyDcl (_, tid, [],
      (Ast.TySum (_,
        (Ast.TySum (_, ors)))), [])

But I need to match something like

(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")))))))),
        []))

It seem like the AST in the match case has a spurious TySum, but I wasn't able to get rid of it.

Does anyone have a solution for that ?

2

There are 2 answers

0
Fabrice Le Fessant On BEST ANSWER

That's a well known bug that has been fixed recently after 3.12.1 Mantis. Beware that your solution might not work with the next version where the bug has been fixed.

0
Marc Simon On

I finally found a solution. It's seems to be a deferente syntax in ocaml 3.12.1. Replacing the match case by

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

will succeed.

Not sure why though.