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: Bruno De Fraine <Bruno.De.Fraine@v...>
Subject: Format module for C-style syntax
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