Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2002-09-06 (16:33)
From: jehenrik <jehenrik@y...>
Subject: Re: [Caml-list] camlp4: pretty printing not to a file
> 	Something like:
> "PCaml.string_of_X : X -> string" where X is in { expr, patt, ctyp ... }
> 	could be simple and useful. It can be the application of this
> function with a default format configuration structure:
> "PCaml.string_of_X_formatted : format_config -> X -> string"

It seems like the Format module provides for whatever "format_config" 
would be, no?  For example, I changed your code to the following, which 
does not require global variables:

(** our printer *)
let printer pr x =
   let b = Buffer.create 1024 in
   let f = Format.formatter_of_buffer b in
   Spretty.print_pretty (Format.pp_print_char f) (Format.pp_print_string 
     (Format.pp_print_flush f) "" "" 78
     (fun (_,_) -> ())  (pr.pr_fun "top" x "" [<>]);
   Format.pp_print_flush f ();
   Buffer.contents b;;

No worrying about redirecting other formatters necessary.  And the 
Format.pp_set_all_formatter_output_functions function seems to provide 
what you would need in terms of "formatting options," at least what I 
can think of.  So maybe camlp4 just needs a function:

PCaml.make_formatted_printer : (unit -> Format.formatter) -> 'a 
Grammar.Entry.e -> 'a -> string

The thunk is so that optionally a fresh buffer can be allocated each 
time.  And then a convenience function with a normal "make buffer, 
print, dump string" thing plugged in:

PCaml.print_to_string : 'a Grammar.Entry.e -> 'a -> string


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: