Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] Format module for C-style syntax

5 views
Skip to first unread message

Bruno De Fraine

unread,
Mar 6, 2007, 1:10:56 PM3/6/07
to caml...@inria.fr
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.D...@vub.ac.be


_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Xavier Leroy

unread,
Mar 6, 2007, 1:42:56 PM3/6/07
to Bruno De Fraine
> 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

Christian Lindig

unread,
Mar 7, 2007, 6:45:52 AM3/7/07
to Bruno De Fraine
> 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

0 new messages