Version française
Home     About     Download     Resources     Contact us    
Browse thread
camlp4: processing lists
[ 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: processing lists
On 3/20/07, Hugo Ferreira <hmf@inescporto.pt> wrote:
> Hello,
>
> I have sent this post to the new ocaml-developer list but am resending
> it here in the hopes that someone can help me. I have come across a
> weirdness in camlp4. Maybe someone can explain this puzzle to me.
>
> I have the following extension:
>
> let expand_term_list loc l =
>             let nl = <:expr< [] >>                                      in
>             let nl = List.fold_right (fun h t ->
>                     <:expr< [$h$::$t$] >>
>                     ) l nl                              in
>             <:expr<
>             do {
>                    List.iter (fun i -> print_endline i) $nl$
>              }
>             >>
>
> EXTEND
>     Pcaml.expr: [
>       [
>       "|" ; "[" ; l = LIST0 Pcaml.expr  SEP ";" ; "]" ; "|"->

Pcaml.expr is too greedy here. Since 1; 2 is a valid ocaml expression.
You need to choose another level ("expr1" in your case and "top" in 3.10).

>       "|" ; "[" ; l = LIST0 Pcaml.expr  LEVEL "expr1" SEP ";" ; "]" ; "|"->

>                    expand_term_list loc l ]  ];
> END;;
>

[...]

>
>     let t1 =
>       do {
>         print_string "Not implemented for list ";

Here you reached a bug (fixed for 3.10)

>         List.iter (fun i -> print_endline i)
>           [do { "1"; "f(X,Y)"; "g(X,Y)"; "4" }]
>       }
>     in
>     ()
>
> which ... to say the least is *not* what I expected. I initially I
> though I had but one Pcaml.expr and not a list of those.  What seems
> to be happening is that the revised syntax is used to generate the
> code and this is wrong.

The in revised 1; 2 is not a valid expression you need to add do { ...
} thus by printing it in the revised syntax you see that was not what
you want.
BTW it was a bug in camlp4 to print [1; 2] where it was [(1; 2)].

Hope this helps,

-- 
Nicolas Pouillard