Version française
Home     About     Download     Resources     Contact us    
Browse thread
try .. finally using new camlp4
[ 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] try .. finally using new camlp4
On 7/8/07, Jon Harrop <jon@ffconsultancy.com> wrote:
>
> I'm just getting my toe wet with the new camlp4 and I can't get this simple
> syntax extension to work. AFAICT, the following code:
>

Let's go

>
> open Camlp4.PreCast;;
Useless since you use the functor based approach.

> let expr : Camlp4.PreCast.Ast.expr Camlp4.PreCast.Gram.Entry.t =
>   Gram.Entry.mk "expression"
Useless too why make a new (empty) rule, there is already one that one
wants to extend.

> module Example (Syntax : Camlp4.Sig.Camlp4Syntax) = struct
>   open Camlp4.PreCast
Useless too

>   include Syntax
>
>   let printer =
>     let module P = Camlp4.Printers.OCaml.Make(Syntax) in
>     new P.printer ()
Useless too

>   EXTEND Gram
>     expr: LEVEL "top"
>     [[ "try"; f=expr; "finally"; g=expr ->
Here one extend a rule. So in order to have something working properly
one *must* look at the original rule.

There is the up-to-date rule:

$ cvs ann -r release310
camlp4/Camlp4Parsers/Camlp4OCamlRevisedParser.ml | grep '"try"'
Annotations for camlp4/Camlp4Parsers/Camlp4OCamlRevisedParser.ml
***************
1.2.2.11     (pouillar 04-Apr-07):         | "try"; e = sequence;
"with"; a = match_case ->

So the correct rule is:
     [[ "try"; f=sequence; "finally"; g=expr ->

>          <:expr<
>            ((function
>              | `Val v, g -> g(); v
>              | `Exn e, g -> g(); raise e)
>               ((try `Val($f$) with e -> `Exn e), (fun () -> $g$)))
>          >>]];
>   END
> end
>
> module M = Camlp4.Register.OCamlSyntaxExtension(Id)(Example)

The Id module is missing...

So there is the fixed code:

module Id = struct
  let name = "pa_tryfinally"
  let version = "$Id:$"
end

module Example (Syntax : Camlp4.Sig.Camlp4Syntax) = struct
 include Syntax

 EXTEND Gram
   expr: LEVEL "top"
   [[ "try"; f=sequence; "finally"; g=expr ->
        <:expr<
          ((function
            | `Val v, g -> g(); v
            | `Exn e, g -> g(); raise e)
             ((try `Val($f$) with e -> `Exn e), (fun () -> $g$)))
        >>]];
 END
end

module M = Camlp4.Register.OCamlSyntaxExtension(Id)(Example)

>
> should provide the syntax extension. Compile with:
>
> $ ocamlc -dtypes -pp camlp4of -I /usr/lib/ocaml/3.10.0/camlp4 -linkall
> camlp4lib.cma unix.cma Camlp4Parsers/Camlp4OCamlRevisedParser.cmo
> Camlp4Parsers/Camlp4OCamlParser.cmo Camlp4Printers/Camlp4AutoPrinter.cmo
> Camlp4Bin.cmo pa_tryfinally.ml -o pa_tryfinally

Why link, with "-c" it's a lot simpler.

ocamlc -c -dtypes -pp camlp4of -I +camlp4 pa_tryfinally.ml

Cheers,

-- 
Nicolas Pouillard