* 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
* Re: [Caml-list] try .. finally using new camlp4
2007-07-08 0:14 try .. finally using new camlp4 Jon Harrop
@ 2007-07-08 3:28 ` Julien Moutinho
2007-07-09 9:01 ` Vincent Hanquez
2007-07-09 13:06 ` Nicolas Pouillard
2 siblings, 0 replies; 8+ messages in thread
From: Julien Moutinho @ 2007-07-08 3:28 UTC (permalink / raw)
To: Jon Harrop; +Cc: caml-list
> 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:
Interestingly, here is what your code does
with the current CVS version <http://camlcvs.inria.fr/>:
$ ocaml camlp4of.cma pa_try_finally.cmo
Objective Caml version 3.11+dev2 (2007-05-08)
Camlp4 Parsing version 3.11+dev2 (2007-05-08)
# try raise Exit finally print_endline "Foo!";;
Foo!
Exception: Pervasives.Exit.
# try raise Exit with _ -> print_endline "Foo!";;
Foo!
- : unit = ()
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] try .. finally using new camlp4
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 13:06 ` Nicolas Pouillard
2 siblings, 1 reply; 8+ messages in thread
From: Vincent Hanquez @ 2007-07-09 9:01 UTC (permalink / raw)
To: Jon Harrop; +Cc: caml-list
On Sun, Jul 08, 2007 at 01:14:41AM +0100, Jon Harrop 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:
A bit OT, but I found it quite easy to actually hack ocaml source
directly to do the right thing for try finally. My modification seems to
works correctly, without having any deep knowledge of ocaml internal
compiler code.
> 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
I don't know camlp4, but why don't you use a more straightforward
(let r = try f() with e -> g(); raise e in g(); r) construct
instead of wrapping/unwrapping the thing into polymorphic variant ?
Cheers,
--
Vincent Hanquez
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] try .. finally using new camlp4
2007-07-09 9:01 ` Vincent Hanquez
@ 2007-07-09 9:43 ` Jeremy Yallop
2007-07-09 9:33 ` Vincent Hanquez
0 siblings, 1 reply; 8+ messages in thread
From: Jeremy Yallop @ 2007-07-09 9:43 UTC (permalink / raw)
To: Vincent Hanquez; +Cc: Jon Harrop, caml-list
Vincent Hanquez wrote:
>> 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
>
> I don't know camlp4, but why don't you use a more straightforward
> (let r = try f() with e -> g(); raise e in g(); r) construct
> instead of wrapping/unwrapping the thing into polymorphic variant ?
Jon's version avoids name capture. If the user writes
let r = 1 in
try
2
finally
print_endline (string_of_int r)
then your version (modulo the unit arguments) expands into
let r = 1 in
let r = try 2
with e ->
print_endline (string_of_int r);
raise e in
print_endline (string_of_int r);
r
which prints "2" instead of "1".
Jeremy.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] try .. finally using new camlp4
2007-07-09 9:43 ` Jeremy Yallop
@ 2007-07-09 9:33 ` Vincent Hanquez
0 siblings, 0 replies; 8+ messages in thread
From: Vincent Hanquez @ 2007-07-09 9:33 UTC (permalink / raw)
To: Jeremy Yallop; +Cc: Jon Harrop, caml-list
On Mon, Jul 09, 2007 at 10:43:04AM +0100, Jeremy Yallop wrote:
> Jon's version avoids name capture. If the user writes
>
> let r = 1 in
> try
> 2
> finally
> print_endline (string_of_int r)
>
> then your version (modulo the unit arguments) expands into
>
> let r = 1 in
> let r = try 2
> with e ->
> print_endline (string_of_int r);
> raise e in
> print_endline (string_of_int r);
> r
>
> which prints "2" instead of "1".
indeed,
Thanks,
--
Vincent Hanquez
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] try .. finally using new camlp4
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 13:06 ` Nicolas Pouillard
2007-07-10 8:28 ` Bruno De Fraine
2 siblings, 1 reply; 8+ messages in thread
From: Nicolas Pouillard @ 2007-07-09 13:06 UTC (permalink / raw)
To: Jon Harrop; +Cc: caml-list
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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] try .. finally using new camlp4
2007-07-09 13:06 ` Nicolas Pouillard
@ 2007-07-10 8:28 ` Bruno De Fraine
2007-07-10 12:44 ` Nicolas Pouillard
0 siblings, 1 reply; 8+ messages in thread
From: Bruno De Fraine @ 2007-07-10 8:28 UTC (permalink / raw)
To: caml-list
On 09 Jul 2007, at 15:06, Nicolas Pouillard wrote:
> Let's go
Of course, if you wrote the thing, it's easy... :-)
>> 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 ->
Just a remark, you don't need the source code to check the existing
definition.
$ ocaml camlp4orf.cma
# open Camlp4.PreCast ;;
# Gram.Entry.print Format.std_formatter Syntax.expr ;;
Will show:
expr: [
...
| "top" RIGHTA [
...
| "try"; sequence; "with"; match_case
Regards,
Bruno
--
Bruno De Fraine
Vrije Universiteit Brussel
Faculty of Applied Sciences, DINF - SSEL
Room 4K208, Pleinlaan 2, B-1050 Brussels
tel: +32 (0)2 629 29 75
fax: +32 (0)2 629 28 70
e-mail: Bruno.De.Fraine@vub.ac.be
^ 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