caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Format module for C-style syntax
@ 2007-03-06 18:07 Bruno De Fraine
  2007-03-06 18:40 ` [Caml-list] " Xavier Leroy
  2007-03-07 11:41 ` Christian Lindig
  0 siblings, 2 replies; 3+ messages in thread
From: Bruno De Fraine @ 2007-03-06 18:07 UTC (permalink / raw)
  To: caml-list

Hello,

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.

To show this more concretely, imagine a language with the following  
very simple abstract syntax:

# type stmt = Call of string ;;
# type proc_decl = Procedure of string * stmt list ;;

And consider the following procedure declaration in that language:

# let foo =
   Procedure ("elaborate_foo",
    [Call "print_hello"; Call "print_world"; Call "print_newline";
     Call "cleanup_all_context"; Call "exit_main_program"]) ;;

When the body is too long for a single line (as is the case here), I  
would like to pretty-print this with the braces lined-up in a  
conventional manner, for example:

procedure elaborate_foo {
     print_hello;
     print_world;
     print_newline;
     cleanup_all_context;
     exit_main_program;
}

When the body is empty, it would be nice if this could still be  
printed as one line, for example: procedure bar { }

To start, I do this:

# open Format ;;
# let stmt ppf (Call pn) = fprintf ppf "%s;" pn ;;
val stmt : Format.formatter -> stmt -> unit = <fun>
# let rec stmts ppf = function
   | [] -> ()
   | [s] -> stmt ppf s
   | s::ss -> (fprintf ppf "%a@ " stmt s; stmts ppf ss)
   ;;
val stmts : Format.formatter -> stmt list -> unit = <fun>

Then my attempts are:

# let proc ppf (Procedure (pn,ss)) =
   fprintf ppf "@[procedure@ %s@ {@ @[<hv 4>%a@]@ }@]@." pn stmts ss
;;
   val proc : Format.formatter -> proc_decl -> unit = <fun>
# proc std_formatter foo ;;
procedure elaborate_foo {
print_hello;
     print_world;
     print_newline;
     cleanup_all_context;
     exit_main_program;
}
- : unit = ()

And:

# let proc ppf (Procedure (pn,ss)) =
   fprintf ppf "@[<4>procedure@ %s@ {@ @[<hv>%a@]@ }@]@." pn stmts ss
;;
   val proc : Format.formatter -> proc_decl -> unit = <fun>
# proc std_formatter foo ;;
procedure elaborate_foo {
     print_hello;
     print_world;
     print_newline;
     cleanup_all_context;
     exit_main_program; }
- : unit = ()

So not quite correct. Is there a better way to pretty-print this kind  
of syntax?

Best regards,
Bruno De Fraine

-- 
Bruno De Fraine
Vrije Universiteit Brussel
Faculty of Applied Sciences, INFO - SSEL
Room 4K208, Pleinlaan 2, B-1050 Brussels
tel: +32 (0)2 629 29 75
fax: +32 (0)2 629 28 70
e-mail: Bruno.De.Fraine@vub.ac.be



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Format module for C-style syntax
  2007-03-06 18:07 Format module for C-style syntax Bruno De Fraine
@ 2007-03-06 18:40 ` Xavier Leroy
  2007-03-07 11:41 ` Christian Lindig
  1 sibling, 0 replies; 3+ messages in thread
From: Xavier Leroy @ 2007-03-06 18:40 UTC (permalink / raw)
  To: Bruno De Fraine; +Cc: caml-list

> 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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Format module for C-style syntax
  2007-03-06 18:07 Format module for C-style syntax Bruno De Fraine
  2007-03-06 18:40 ` [Caml-list] " Xavier Leroy
@ 2007-03-07 11:41 ` Christian Lindig
  1 sibling, 0 replies; 3+ messages in thread
From: Christian Lindig @ 2007-03-07 11:41 UTC (permalink / raw)
  To: Bruno De Fraine; +Cc: Caml List

> 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 an alternative: I am using the purely functional pretty  
printer suggested by Wadler for emitting a subset of C. You can find  
the pretty printer itself, as well as the pretty printer for C here:

http://quest-tester.googlecode.com/svn/trunk/src/pretty.nw
http://quest-tester.googlecode.com/svn/trunk/src/ppcee.nw

-- Christian



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-03-07 11:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-06 18:07 Format module for C-style syntax Bruno De Fraine
2007-03-06 18:40 ` [Caml-list] " Xavier Leroy
2007-03-07 11:41 ` Christian Lindig

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).