* camlp4 question: Mixing a printer and Ast.fold
@ 2008-03-21 15:06 Richard Jones
2008-03-21 15:36 ` [Caml-list] " Jeremy Yallop
2008-03-21 15:46 ` Nicolas Pouillard
0 siblings, 2 replies; 12+ messages in thread
From: Richard Jones @ 2008-03-21 15:06 UTC (permalink / raw)
To: caml-list
I'm trying to translate Sylvain Le Gall's gettext module to use camlp4
from ocaml 3.10.0. The module is a printer which folds over the AST
looking for certain types of function call by name. A simplified
version is shown below.
This program is supposed to look for all instances of a function named
f applied to a string.
I cannot for the life of me work out how to get this to compile. I've
tried about a dozen different variations of the module names, 'open',
'include' etc. and got a dozen different errors.
----------------------------------------------------------------------
module Id = struct
let name = "pr_gettext"
let version = "$Id$"
end
module Make (Syntax : Camlp4.Sig.Syntax)
: Camlp4.Sig.Printer(Syntax.Ast).S =
struct
module Loc = Syntax.Loc
module Ast = Syntax.Ast
class visitor = object
inherit Ast.fold as super
val t = []
method t = t
method expr = function
| <:expr@loc< f $str:singular$ >> ->
let t = str :: t in
{< t = t >}
| e -> super#expr e
end
let print_interf ?input_file ?output_file _ = ()
let print_implem ?input_file ?output_file ast =
let visitor = (new visitor)#str_item in
let t = (visitor ast)#t in
List.iter prerr_endline t
end
(* Register the new printer. *)
module M = Camlp4.Register.Printer(Id)(Make)
----------------------------------------------------------------------
ocamlc -I +camlp4 -I /usr/lib64/ocaml/camlp4/Camlp4Parsers -pp camlp4of.opt camlp4lib.cma test.ml -o test.cmo
File "test.ml", line 19, characters 19-19:
Unbound constructor Ast.ExApp
Please let me know how to compile this before I go mad...
Rich.
--
Richard Jones
Red Hat
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] camlp4 question: Mixing a printer and Ast.fold
2008-03-21 15:06 camlp4 question: Mixing a printer and Ast.fold Richard Jones
@ 2008-03-21 15:36 ` Jeremy Yallop
2008-03-21 15:51 ` Richard Jones
2008-03-21 15:46 ` Nicolas Pouillard
1 sibling, 1 reply; 12+ messages in thread
From: Jeremy Yallop @ 2008-03-21 15:36 UTC (permalink / raw)
To: Richard Jones; +Cc: caml-list
Richard Jones wrote:
> I cannot for the life of me work out how to get this to compile. I've
> tried about a dozen different variations of the module names, 'open',
> 'include' etc. and got a dozen different errors.
Here's another variation that compiles and seems to work. I changed the
following:
* the argument type to Make from "Syntax" to "Camlp4Syntax"
* the register functor from "Printer" to "OCamlPrinter"
* the accumulated value in "expr" from "str" to "singular".
Jeremy.
module Id = struct
let name = "pr_gettext"
let version = "$Id$"
end
module Make (Syntax : Camlp4.Sig.Camlp4Syntax)
: Camlp4.Sig.Printer(Syntax.Ast).S =
struct
open Syntax
class visitor = object
inherit Ast.fold as super
val t = []
method t = t
method expr = function
| <:expr@loc< f $str:singular$ >> ->
let t = singular :: t in
{< t = t >}
| e -> super#expr e
end
let print_interf ?input_file ?output_file _ = ()
let print_implem ?input_file ?output_file ast =
let visitor = (new visitor)#str_item in
let t = (visitor ast)#t in
List.iter prerr_endline t
end
(* Register the new printer. *)
module M = Camlp4.Register.OCamlPrinter(Id)(Make)
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] camlp4 question: Mixing a printer and Ast.fold
2008-03-21 15:36 ` [Caml-list] " Jeremy Yallop
@ 2008-03-21 15:51 ` Richard Jones
0 siblings, 0 replies; 12+ messages in thread
From: Richard Jones @ 2008-03-21 15:51 UTC (permalink / raw)
To: Jeremy Yallop; +Cc: caml-list
On Fri, Mar 21, 2008 at 03:36:11PM +0000, Jeremy Yallop wrote:
> Richard Jones wrote:
> >I cannot for the life of me work out how to get this to compile. I've
> >tried about a dozen different variations of the module names, 'open',
> >'include' etc. and got a dozen different errors.
>
> Here's another variation that compiles and seems to work. I changed the
> following:
>
> * the argument type to Make from "Syntax" to "Camlp4Syntax"
> * the register functor from "Printer" to "OCamlPrinter"
> * the accumulated value in "expr" from "str" to "singular".
Brilliant, that works, thanks!
> | <:expr@loc< f $str:singular$ >> ->
> let t = singular :: t in
The above was just a typo when I was simplifying the program ...
Rich.
--
Richard Jones
Red Hat
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] camlp4 question: Mixing a printer and Ast.fold
2008-03-21 15:06 camlp4 question: Mixing a printer and Ast.fold Richard Jones
2008-03-21 15:36 ` [Caml-list] " Jeremy Yallop
@ 2008-03-21 15:46 ` Nicolas Pouillard
2008-03-21 15:58 ` Richard Jones
2008-03-21 16:44 ` [Caml-list] " Richard Jones
1 sibling, 2 replies; 12+ messages in thread
From: Nicolas Pouillard @ 2008-03-21 15:46 UTC (permalink / raw)
To: Richard W.M. Jones; +Cc: OCaml Mailing List
[-- Attachment #1.1: Type: text/plain, Size: 2457 bytes --]
Excerpts from Richard W.M. Jones's message of Fri Mar 21 16:06:40 +0100 2008:
> I'm trying to translate Sylvain Le Gall's gettext module to use camlp4
> from ocaml 3.10.0. The module is a printer which folds over the AST
> looking for certain types of function call by name. A simplified
> version is shown below.
>
> This program is supposed to look for all instances of a function named
> f applied to a string.
>
> I cannot for the life of me work out how to get this to compile. I've
> tried about a dozen different variations of the module names, 'open',
> 'include' etc. and got a dozen different errors.
You where very close to something working...
Essentially three errors:
> ----------------------------------------------------------------------
> module Id = struct
> let name = "pr_gettext"
> let version = "$Id$"
> end
>
> module Make (Syntax : Camlp4.Sig.Syntax)
Here it's Camlp4.Sig.Camlp4Syntax otherwise types are abstract and you don't
have access to constructors.
> : Camlp4.Sig.Printer(Syntax.Ast).S =
> struct
> module Loc = Syntax.Loc
> module Ast = Syntax.Ast
>
> class visitor = object
> inherit Ast.fold as super
>
> val t = []
> method t = t
>
> method expr = function
> | <:expr@loc< f $str:singular$ >> ->
Here you bind loc but don't use it, however that's just a warning.
> let t = str :: t in
Here the variable is singular not str.
> {< t = t >}
>
> | e -> super#expr e
> end
>
> let print_interf ?input_file ?output_file _ = ()
>
> let print_implem ?input_file ?output_file ast =
> let visitor = (new visitor)#str_item in
> let t = (visitor ast)#t in
> List.iter prerr_endline t
> end
>
> (* Register the new printer. *)
> module M = Camlp4.Register.Printer(Id)(Make)
And here it's Camlp4.Register.OCamlPrinter because Camlp4.Register.Printer is
too abstract.
I attach three working versions of it.
To compile them I use
ocamlbuild -tags camlp4of,use_camlp4 pr_gettext.cmo
The first [1] is very closer to yours. However you don't really define a
proper printer here prerr_endline, so if the goal is just to iterate over the
AST [2] is a simpler version.
But why make an effect while one can easily go one step further and produce
an OCaml output that define the list of strings to be translated [3].
Cheers,
[1]: pr_gettext.ml
[2]: pr_gettext_simple.ml
[3]: pr_gettext_simple2.ml
--
Nicolas Pouillard aka Ertai
[-- Attachment #1.2: pr_gettext.ml --]
[-- Type: application/octet-stream, Size: 761 bytes --]
module Id = struct
let name = "pr_gettext"
let version = "$Id$"
end
module Make (Syntax : Camlp4.Sig.Camlp4Syntax)
: Camlp4.Sig.Printer(Syntax.Ast).S =
struct
module Loc = Syntax.Loc
module Ast = Syntax.Ast
class visitor = object
inherit Ast.fold as super
val t = []
method t = t
method expr = function
| <:expr< f $str:singular$ >> ->
let t = singular :: t in
{< t = t >}
| e -> super#expr e
end
let print_interf ?input_file:(_) ?output_file:(_) _ = ()
let print_implem ?input_file:(_) ?output_file:(_) ast =
let visitor = (new visitor)#str_item in
let t = (visitor ast)#t in
List.iter prerr_endline t
end
(* Register the new printer. *)
module M = Camlp4.Register.OCamlPrinter(Id)(Make)
[-- Attachment #1.3: pr_gettext_simple.ml --]
[-- Type: application/octet-stream, Size: 410 bytes --]
open Camlp4.PreCast;;
class visitor = object
inherit Ast.fold as super
val t = []
method t = t
method expr = function
| <:expr< f $str:singular$ >> ->
let t = singular :: t in
{< t = t >}
| e -> super#expr e
end;;
let filter ast =
let visitor = (new visitor)#str_item in
let t = (visitor ast)#t in
List.iter prerr_endline t;
ast
;;
AstFilters.register_str_item_filter filter;;
[-- Attachment #1.4: pr_gettext_simple2.ml --]
[-- Type: application/octet-stream, Size: 588 bytes --]
open Camlp4.PreCast;;
class visitor = object
inherit Ast.fold as super
val t = []
method t = t
method expr = function
| <:expr< f $str:singular$ >> ->
let t = singular :: t in
{< t = t >}
| e -> super#expr e
end;;
let filter ast =
let visitor = (new visitor)#str_item in
let t = (visitor ast)#t in
let ghost = Loc.ghost in
let expr =
List.fold_right begin fun s acc ->
<:expr@ghost< $str:s$ :: $acc$ >>
end t <:expr@ghost< [] >>
in <:str_item@ghost< let strings_to_translate = $exp:expr$ >>
;;
AstFilters.register_str_item_filter filter;;
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 194 bytes --]
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] camlp4 question: Mixing a printer and Ast.fold
2008-03-21 15:46 ` Nicolas Pouillard
@ 2008-03-21 15:58 ` Richard Jones
2008-03-26 9:29 ` Sylvain Le Gall
2008-03-21 16:44 ` [Caml-list] " Richard Jones
1 sibling, 1 reply; 12+ messages in thread
From: Richard Jones @ 2008-03-21 15:58 UTC (permalink / raw)
To: Nicolas Pouillard; +Cc: caml-list
Thanks. I've now got pr_gettext working again.
BTW, I think the ability to be able to map & fold over the AST is an
extraordinarily powerful feature, and deserves many more examples and
tutorials. I know that strictly speaking it was possible before (with
Le Gall's ast-analyze extension) but the way it is done in the new
camlp4 makes the code much shorter.
Rich.
--
Richard Jones
Red Hat
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: camlp4 question: Mixing a printer and Ast.fold
2008-03-21 15:58 ` Richard Jones
@ 2008-03-26 9:29 ` Sylvain Le Gall
0 siblings, 0 replies; 12+ messages in thread
From: Sylvain Le Gall @ 2008-03-26 9:29 UTC (permalink / raw)
To: caml-list
On 21-03-2008, Richard Jones <rich@annexia.org> wrote:
>
> Thanks. I've now got pr_gettext working again.
>
> BTW, I think the ability to be able to map & fold over the AST is an
> extraordinarily powerful feature, and deserves many more examples and
> tutorials. I know that strictly speaking it was possible before (with
> Le Gall's ast-analyze extension) but the way it is done in the new
> camlp4 makes the code much shorter.
>
And it gives me one reason to drop ast-analyze... This new camlp4
feature, seems indeed very powerful.
Regards,
Sylvain Le Gall
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] camlp4 question: Mixing a printer and Ast.fold
2008-03-21 15:46 ` Nicolas Pouillard
2008-03-21 15:58 ` Richard Jones
@ 2008-03-21 16:44 ` Richard Jones
2008-03-21 16:50 ` Nicolas Pouillard
1 sibling, 1 reply; 12+ messages in thread
From: Richard Jones @ 2008-03-21 16:44 UTC (permalink / raw)
To: Nicolas Pouillard; +Cc: OCaml Mailing List
I'm trying to link the camlp4 module into a binary, by doing:
ocamlc -I +camlp4 camlp4lib.cma Camlp4Bin.cmo pr_gettext.cmo \
-o ocaml-xgettext
but my binary always prints out:
Failure: "No implementation printer"
Apparently I need to somehow register the printer as well? I can't
work out how to do that though.
As an aside, is mkcamlp4 supposed to do anything? For me it always
prints this error:
Cannot find file Camlp4.cma
Rich.
--
Richard Jones
Red Hat
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] camlp4 question: Mixing a printer and Ast.fold
2008-03-21 16:44 ` [Caml-list] " Richard Jones
@ 2008-03-21 16:50 ` Nicolas Pouillard
2008-03-21 16:56 ` Richard Jones
0 siblings, 1 reply; 12+ messages in thread
From: Nicolas Pouillard @ 2008-03-21 16:50 UTC (permalink / raw)
To: Richard W.M. Jones; +Cc: OCaml Mailing List
[-- Attachment #1: Type: text/plain, Size: 658 bytes --]
Excerpts from Richard W.M. Jones's message of Fri Mar 21 17:44:30 +0100 2008:
> I'm trying to link the camlp4 module into a binary, by doing:
>
> ocamlc -I +camlp4 camlp4lib.cma Camlp4Bin.cmo pr_gettext.cmo \
> -o ocaml-xgettext
>
> but my binary always prints out:
>
> Failure: "No implementation printer"
>
> Apparently I need to somehow register the printer as well? I can't
> work out how to do that though.
What version of pr_gettext are you using?
> As an aside, is mkcamlp4 supposed to do anything? For me it always
> prints this error:
>
> Cannot find file Camlp4.cma
Hum mkcamlp4 seems br0ken :(
--
Nicolas Pouillard aka Ertai
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 194 bytes --]
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] camlp4 question: Mixing a printer and Ast.fold
2008-03-21 16:50 ` Nicolas Pouillard
@ 2008-03-21 16:56 ` Richard Jones
2008-03-21 17:03 ` Nicolas Pouillard
0 siblings, 1 reply; 12+ messages in thread
From: Richard Jones @ 2008-03-21 16:56 UTC (permalink / raw)
To: Nicolas Pouillard; +Cc: OCaml Mailing List
On Fri, Mar 21, 2008 at 05:50:00PM +0100, Nicolas Pouillard wrote:
> Excerpts from Richard W.M. Jones's message of Fri Mar 21 17:44:30 +0100 2008:
> > I'm trying to link the camlp4 module into a binary, by doing:
> >
> > ocamlc -I +camlp4 camlp4lib.cma Camlp4Bin.cmo pr_gettext.cmo \
> > -o ocaml-xgettext
> >
> > but my binary always prints out:
> >
> > Failure: "No implementation printer"
> >
> > Apparently I need to somehow register the printer as well? I can't
> > work out how to do that though.
>
> What version of pr_gettext are you using?
Well, no version -- I'm trying to make Sylvain Le Gall's pr_gettext
work with camlp4 3.10. It has to be linked into a standalone binary
called ocaml-xgettext, but I can't work out how to do that.
Previously Sylvain was using mkcamlp4, but that is broken.
Rich.
--
Richard Jones
Red Hat
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] camlp4 question: Mixing a printer and Ast.fold
2008-03-21 16:56 ` Richard Jones
@ 2008-03-21 17:03 ` Nicolas Pouillard
2008-03-21 17:21 ` Richard Jones
0 siblings, 1 reply; 12+ messages in thread
From: Nicolas Pouillard @ 2008-03-21 17:03 UTC (permalink / raw)
To: Richard W.M. Jones; +Cc: OCaml Mailing List
[-- Attachment #1: Type: text/plain, Size: 1045 bytes --]
Excerpts from Richard W.M. Jones's message of Fri Mar 21 17:56:25 +0100 2008:
> On Fri, Mar 21, 2008 at 05:50:00PM +0100, Nicolas Pouillard wrote:
> > Excerpts from Richard W.M. Jones's message of Fri Mar 21 17:44:30 +0100 2008:
> > > I'm trying to link the camlp4 module into a binary, by doing:
> > >
> > > ocamlc -I +camlp4 camlp4lib.cma Camlp4Bin.cmo pr_gettext.cmo \
> > > -o ocaml-xgettext
> > >
> > > but my binary always prints out:
> > >
> > > Failure: "No implementation printer"
> > >
> > > Apparently I need to somehow register the printer as well? I can't
> > > work out how to do that though.
> >
> > What version of pr_gettext are you using?
>
> Well, no version -- I'm trying to make Sylvain Le Gall's pr_gettext
> work with camlp4 3.10. It has to be linked into a standalone binary
> called ocaml-xgettext, but I can't work out how to do that.
>
> Previously Sylvain was using mkcamlp4, but that is broken.
I mean between different proposals for pr_gettext.ml, in this thread.
--
Nicolas Pouillard aka Ertai
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 194 bytes --]
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] camlp4 question: Mixing a printer and Ast.fold
2008-03-21 17:03 ` Nicolas Pouillard
@ 2008-03-21 17:21 ` Richard Jones
2008-03-21 20:19 ` Richard Jones
0 siblings, 1 reply; 12+ messages in thread
From: Richard Jones @ 2008-03-21 17:21 UTC (permalink / raw)
To: Nicolas Pouillard; +Cc: OCaml Mailing List
On Fri, Mar 21, 2008 at 06:03:15PM +0100, Nicolas Pouillard wrote:
> Excerpts from Richard W.M. Jones's message of Fri Mar 21 17:56:25 +0100 2008:
> > On Fri, Mar 21, 2008 at 05:50:00PM +0100, Nicolas Pouillard wrote:
> > > Excerpts from Richard W.M. Jones's message of Fri Mar 21 17:44:30 +0100 2008:
> > > > I'm trying to link the camlp4 module into a binary, by doing:
> > > >
> > > > ocamlc -I +camlp4 camlp4lib.cma Camlp4Bin.cmo pr_gettext.cmo \
> > > > -o ocaml-xgettext
> > > >
> > > > but my binary always prints out:
> > > >
> > > > Failure: "No implementation printer"
> > > >
> > > > Apparently I need to somehow register the printer as well? I can't
> > > > work out how to do that though.
> > >
> > > What version of pr_gettext are you using?
> >
> > Well, no version -- I'm trying to make Sylvain Le Gall's pr_gettext
> > work with camlp4 3.10. It has to be linked into a standalone binary
> > called ocaml-xgettext, but I can't work out how to do that.
> >
> > Previously Sylvain was using mkcamlp4, but that is broken.
>
> I mean between different proposals for pr_gettext.ml, in this thread.
I was using Jeremy Yallop's simple version (same as your first
version). I got it to work by linking it as follows:
$(OCAMLC) \
-I +camlp4 camlp4lib.cma \
unix.cma \
`$(OCAMLFIND) query -r -predicates byte gettext.extract -i-format` \
`$(OCAMLFIND) query -r -predicates byte gettext.extract -a-format` \
`$(OCAMLFIND) query -r -predicates byte gettext.extract -o-format` \
Camlp4Bin.cmo \
-o $@
Rich.
--
Richard Jones
Red Hat
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2008-03-26 9:30 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-21 15:06 camlp4 question: Mixing a printer and Ast.fold Richard Jones
2008-03-21 15:36 ` [Caml-list] " Jeremy Yallop
2008-03-21 15:51 ` Richard Jones
2008-03-21 15:46 ` Nicolas Pouillard
2008-03-21 15:58 ` Richard Jones
2008-03-26 9:29 ` Sylvain Le Gall
2008-03-21 16:44 ` [Caml-list] " Richard Jones
2008-03-21 16:50 ` Nicolas Pouillard
2008-03-21 16:56 ` Richard Jones
2008-03-21 17:03 ` Nicolas Pouillard
2008-03-21 17:21 ` Richard Jones
2008-03-21 20:19 ` Richard Jones
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox