From: Bruno De Fraine <Bruno.De.Fraine@vub.ac.be>
To: caml-list@inria.fr
Subject: Format module for C-style syntax
Date: Tue, 6 Mar 2007 19:07:40 +0100 [thread overview]
Message-ID: <70DC8A37-C742-405E-83E4-2367664717F5@vub.ac.be> (raw)
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
next reply other threads:[~2007-03-06 18:08 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-03-06 18:07 Bruno De Fraine [this message]
2007-03-06 18:40 ` [Caml-list] " Xavier Leroy
2007-03-07 11:41 ` Christian Lindig
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=70DC8A37-C742-405E-83E4-2367664717F5@vub.ac.be \
--to=bruno.de.fraine@vub.ac.be \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox