English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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: 2007-03-06 (18:40)
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