caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Nicolas Pouillard" <nicolas.pouillard@gmail.com>
To: "Jon Harrop" <jon@ffconsultancy.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] try .. finally using new camlp4
Date: Mon, 9 Jul 2007 15:06:47 +0200	[thread overview]
Message-ID: <cd67f63a0707090606y4edc4bbbl9fa8bd9a8a85f97d@mail.gmail.com> (raw)
In-Reply-To: <200707080114.42213.jon@ffconsultancy.com>

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


  parent reply	other threads:[~2007-07-09 13:06 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-08  0:14 Jon Harrop
2007-07-08  3:28 ` [Caml-list] " Julien Moutinho
2007-07-09  9:01 ` Vincent Hanquez
2007-07-09  9:43   ` Jeremy Yallop
2007-07-09  9:33     ` Vincent Hanquez
2007-07-09 13:06 ` Nicolas Pouillard [this message]
2007-07-10  8:28   ` Bruno De Fraine
2007-07-10 12:44     ` Nicolas Pouillard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cd67f63a0707090606y4edc4bbbl9fa8bd9a8a85f97d@mail.gmail.com \
    --to=nicolas.pouillard@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=jon@ffconsultancy.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).