From: "Nicolas Pouillard" <nicolas.pouillard@gmail.com>
To: "Richard W.M. Jones" <rich@annexia.org>
Cc: OCaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] camlp4 question: Mixing a printer and Ast.fold
Date: Fri, 21 Mar 2008 16:46:56 +0100 [thread overview]
Message-ID: <1206113824-sup-4922@port-ext16.ensta.fr> (raw)
In-Reply-To: <20080321150639.GA29482@annexia.org>
[-- 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 --]
next prev parent reply other threads:[~2008-03-21 15:48 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-03-21 15:06 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 [this message]
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
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=1206113824-sup-4922@port-ext16.ensta.fr \
--to=nicolas.pouillard@gmail.com \
--cc=caml-list@inria.fr \
--cc=rich@annexia.org \
/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