Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] camlp4: pretty printing not to a file
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Yann_Régis-Gianas <yann@l...>
Subject: Re: [Caml-list] camlp4: pretty printing not to a file
On Thu, Aug 29, 2002 at 02:51:41PM -0400, jehenrik wrote:
> Hi, I want a camlp4 macro which takes a caml expression for a list and 
> replaces it by a list of (source code as string, value) tuples.  I have 
> two problems: printing not to a file, and pattern matching the list.  It 
> seems that the good camlp4 pretty printing facilities are only able to 
> go to files, 

	Yes. The first time I tried to print AST with camlp4, I
thought that camlp4 should be called camlp2 ... In fact, it is due to
the poor documentation about printers. It is possible to print AST in
something different from file using global variables. Look:

(** global buffer. *)
let b = Buffer.create 1024

(** our printer *)
let printer pr x =
  Buffer.clear b;
  Spretty.print_pretty Format.print_char Format.print_string
    Format.print_flush "" "" 78
    (fun _ _ -> ("", 0, 0, 0)) 0 (pr.pr_fun "top" x "" [<>]);
  Format.print_flush ();
  Buffer.contents b

(** initialize the pretty printer output. *)		   
let init_printer () =
  let _ = Buffer.clear b in
  let null ()               = () 
  and bufferize s start len = 
      Buffer.add_substring b s start len;
  in
    Format.set_formatter_output_functions bufferize null

(** printer for ocaml types *)
let print_ctyp = printer pr_ctyp

(** printer for ocaml expr *)
let print_expr = printer pr_expr

(** printer for ocaml pattern *)
let print_patt = printer pr_patt




-- 
Yann Régis-Gianas.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners