Version française
Home     About     Download     Resources     Contact us    
Browse thread
Format module for C-style syntax
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: [Caml-list] Format module for C-style syntax
> Has anybody used the Format module for pretty-printing C-style  syntax?
> After reading the documentation ("Using the Format module"),  I cannot
> get it to line up the braces in a manner that is convential  for such a
> syntax.

Here is one possible solution:

let pretty_decl ppf (Procedure(name, body)) =
  fprintf ppf "@[<hv 4>procedure %s {" name;
  List.iter (fun (Call s) -> fprintf ppf "@ %s;" s) body;
  fprintf ppf "@;<1 -4>}@]";;

The two tricks are:
1- using only one box that starts at "procedure", no need to start a new
   box at the opening brace;
2- the "@;", a.k.a. print_break, with a negative offset that cancels
   the indentation of the current block; it's a bit of a hack but
   surprisingly useful.

Hope this helps,

- Xavier Leroy