caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jeremy Yallop <jeremy.yallop@ed.ac.uk>
To: Richard Jones <rich@annexia.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list]  [camlp4] expr_of_string, string_of_expr functions exist?
Date: Wed, 06 Aug 2008 20:58:57 +0100	[thread overview]
Message-ID: <20080806205857.z2fq5n7i0w4gww0s@www.sms.ed.ac.uk> (raw)
In-Reply-To: <20080805160426.GA698@annexia.org>

Quoting Richard Jones <rich@annexia.org>:
> Maybe a simple question, but does camlp4 have functions to turn
> expr and patt AST structures to and from strings?

One way to do it is to use the Camlp4.Printers module.  To illustrate  
the idea,
here's a complete working program that prints out all expressions found in a
file.

open Camlp4.PreCast
open Syntax

(* Instantiate the module that allows us to print AST values.
    This gives us an object with methods `expr', `patt', etc.,
    each with type `Format.formatter -> t -> unit'. *)
let printer = let module P = Camlp4.Printers.OCaml.Make(Syntax)
               in new P.printer ()

(* Transform a formatter function into a to_string function. *)
let format_to_string (f : Format.formatter -> 'a -> unit) (v : 'a) : string =
   let buf = Buffer.create 128 in
   let fmt = Format.formatter_of_buffer buf in
   let () = f fmt v in
   let () = Format.pp_print_flush fmt () in
      Buffer.contents buf

(* Some to_string functions for particular AST types. *)
let expr_of_string : Ast.expr -> string = format_to_string printer#expr
let patt_of_string : Ast.patt -> string = format_to_string printer#patt

(* A "filter" that prints out expressions encountered in the AST  
(top-down). *)
let print_stuff = object
   inherit Ast.map as super
   method expr e =
      let () = print_endline ("expr: "^ expr_of_string e) in
         super#expr e
end

(* Register the filter with camlp4 *)
let () = AstFilters.register_str_item_filter print_stuff#str_item


Compile:
    ocamlc -c -I +camlp4 print.ml
Run:
    camlp4of print.cmo file.ml

Hope this helps,

Jeremy.

-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.



      parent reply	other threads:[~2008-08-06 19:59 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-05 16:04 Richard Jones
2008-08-05 17:43 ` [Caml-list] " Yitzhak Mandelbaum
2008-08-05 17:53 ` Christophe TROESTLER
2008-08-06 13:40   ` Richard Jones
2008-08-06 19:58 ` Jeremy Yallop [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=20080806205857.z2fq5n7i0w4gww0s@www.sms.ed.ac.uk \
    --to=jeremy.yallop@ed.ac.uk \
    --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;
as well as URLs for NNTP newsgroup(s).