caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Julien Moutinho <julien.moutinho@gmail.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Camlp4 3.10: printing OCaml expression within extension: how?
Date: Wed, 17 Oct 2007 00:57:47 +0200	[thread overview]
Message-ID: <20071016225747.GA31813@localhost> (raw)
In-Reply-To: <471562FB.3000808@tepkom.ru>

On Wed, Oct 17, 2007 at 01:18:51AM +0000, Dmitri Boulytchev wrote:
>     is it possible to convert OCaml expression into string within syntax
> extension? In past version
> it could be done by string_of function.

Maybe you can do like this:

% cat pa_soe.ml4

module Id =
  struct
	let name = "Pa_soe"
	let version = "$Id: Pa_soe mer 17 oct 2007 00:19:12 CEST $"
  end

module Make (Syntax : Camlp4.Sig.Camlp4Syntax) =
  struct
	include Syntax
	
	let print_interf ?input_file ?output_file ast = ()
	let print_implem ?input_file ?output_file ast = ()
	
	module PP = Camlp4.Printers.OCaml.Make(Syntax)
	
	let pp = new PP.printer ~comments: false ()
	
	let string_of_expr expr =
	  let buf = Buffer.create 42 in
	  let () = Format.bprintf buf "%a@?" pp#expr expr in
	  let str = Buffer.contents buf
	  in if str = "" then assert false else str
	
	EXTEND Gram
	  GLOBAL: expr;
	  expr: LEVEL "."
		(* A macro building a 2-tuple string * expr.
		 * Example: `(name) gives ("name", name) *)
		[ [ "`"; "("; e = SELF; ")" -> <:expr< $str:string_of_expr e$,$exp:e$ >> ] ];
	END
  end

module M = Camlp4.Register.OCamlSyntaxExtension(Id)(Make)

% camlp4of -o pa_soe.ml -loc _loc -printer o -impl pa_soe.ml4
% ocamlc -c -I +camlp4 pa_soe.ml
% cat test.ml4
prerr_endline (fst `((=) (6 * 9) 42))

% camlp4of -o test.ml -loc _loc pa_soe.cmo -printer o -impl test.ml4
% ocamlc -c test.ml
% ocamlc -o test.tpo test.cmo
% ./test.tpo
(6 * 9) = 42

Bonus, how I have found this:

% cd /usr/local/src/ocaml/_cvs/ocaml/camlp4
% grep -R string_of * > /tmp/out
% vim /tmp/out
There is a line:
boot/camlp4boot.ml:        let string_of_patt patt =
% vim boot/camlp4boot.ml
There is a line:
           let () = Format.bprintf buf "%a@?" pp#patt patt in
Match against "method patt"
2nd match: just above (with vim's line folding) there is a "method expr"
% grep -R "method expr" * > /tmp/out2
% vim /tmp/out2
Encouraging lines:
Camlp4/Printers/OCaml.ml:    method expr_list f =
Camlp4/Printers/OCaml.ml:    method expr_list_cons simple f e = 
Camlp4/Printers/OCaml.ml:    method expr f e =
% vim Camlp4/Printers/OCaml.ml
Seems like the holy grail.
pa_soe.ml4 is derivated from the module Make holding string_of_patt (boot/camlp4boot.ml)


      reply	other threads:[~2007-10-16 22:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-17  1:18 Dmitri Boulytchev
2007-10-16 22:57 ` Julien Moutinho [this message]

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=20071016225747.GA31813@localhost \
    --to=julien.moutinho@gmail.com \
    --cc=caml-list@inria.fr \
    /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).