Version française
Home     About     Download     Resources     Contact us    
Browse thread
camlp4 3.10: <:ctyp< ( $list:tys$ )
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Nicolas Pouillard <nicolas.pouillard@g...>
Subject: Re: [Caml-list] camlp4 3.10: <:ctyp< ( $list:tys$ )
On 5/1/07, Joel Reymont <joelr1@gmail.com> wrote:
> This is the old code:
>
>      | <:ctyp< ( $list:tys$ ) >> -> tupl _loc (List.map aux tys)
>
> I figure it tries to match something like "int * int".
>
> Following Nicolas's suggestion I thought I would see what this
> expands to:
>
> camlp4of -str '<:ctyp< ( int * int ) >>'
> Ast.TyTup (_loc,
>    Ast.TySta (_loc, Ast.TyId (_loc, Ast.IdLid (_loc, "int")),
>      Ast.TyId (_loc, Ast.IdLid (_loc, "int"))))
>
> How do I work back from this to the right expression within parens in
> the original pattern above? I tried
>
>      | <:ctyp< ( $tup:tys$ ) >> -> tupl _loc (List.map aux tys)
>
> but this produces ctyp instead of ctyp list so it's not suitable for
> the tupl.
>

There is one simple answer about camlp4 AST. There is no more lists inside.

Then how to guess, how to pattern match these AST.

Take tuples for instance...

( t1 * ... * tN )

Hum that a list of types separated by stars, so in camlp4 the
constructor node will be the star.

t ::= ... | ( $tup:t$ ) | $t1$ * $t2$

So that's the representation. Now if you really want a list you can
use a function like Ast.ctyp_of_list

match t with
| <:ctyp< ( $tup:t$ ) >> -> Ast.ctyp_of_list t
...

Regards,

-- 
Nicolas Pouillard