caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* try .. finally using new camlp4
@ 2007-07-08  0:14 Jon Harrop
  2007-07-08  3:28 ` [Caml-list] " Julien Moutinho
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Jon Harrop @ 2007-07-08  0:14 UTC (permalink / raw)
  To: caml-list


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:


open Camlp4.PreCast;;

let expr : Camlp4.PreCast.Ast.expr Camlp4.PreCast.Gram.Entry.t =
  Gram.Entry.mk "expression"

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

  let printer =
    let module P = Camlp4.Printers.OCaml.Make(Syntax) in
    new P.printer ()

  EXTEND Gram
    expr: LEVEL "top"
    [[ "try"; f=expr; "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

Drop into a top-level with the extended syntax and the extension works but it 
seems to have replaced the original try .. with syntax rather than added a 
new one:

$ ocaml camlp4of.cma pa_tryfinally.cmo
        Objective Caml version 3.10.0

        Camlp4 Parsing version 3.10.0

# try raise Exit finally print_endline "Foo!";;
Foo!
Exception: Pervasives.Exit.
# try raise Exit with _ -> print_endline "Foo!";;
Parse error: "finally" expected after [expr] (in [expr])

I've tried various things to correct this but I can't get it to work. What am 
I doing wrong?

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The OCaml Journal
http://www.ffconsultancy.com/products/ocaml_journal/?e


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2007-07-10 12:44 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-07-08  0:14 try .. finally using new camlp4 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
2007-07-10  8:28   ` Bruno De Fraine
2007-07-10 12:44     ` Nicolas Pouillard

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