From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.2 required=5.0 tests=RCVD_IN_SORBS_WEB autolearn=disabled version=3.1.3 Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 2D080BC6C for ; Tue, 6 Mar 2007 19:08:28 +0100 (CET) Received: from dorado.vub.ac.be (smtp.vub.ac.be [134.184.129.10]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l26I8Rc2009840 for ; Tue, 6 Mar 2007 19:08:27 +0100 Received: from exchange.etro.vub.ac.be (etro10.vub.ac.be [134.184.2.10]) by dorado.vub.ac.be (Postfix) with ESMTP id 793152FC for ; Tue, 6 Mar 2007 19:08:25 +0100 (CET) Received: from ssel.vub.ac.be ([10.0.4.37]) by exchange.etro.vub.ac.be with Microsoft SMTPSVC(6.0.3790.1830); Tue, 6 Mar 2007 19:08:25 +0100 Received: from localhost (localhost.localdomain [127.0.0.1]) by ssel.vub.ac.be (Postfix) with ESMTP id 4567C2468929 for ; Tue, 6 Mar 2007 19:08:25 +0100 (CET) X-Virus-Scanned: amavisd-new at ssel.vub.ac.be Received: from ssel.vub.ac.be ([127.0.0.1]) by localhost (ssel.vub.ac.be [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id y7p-kJ9EZaTt for ; Tue, 6 Mar 2007 19:08:15 +0100 (CET) Received: from [192.168.2.112] (kotnet-150.kulnet.kuleuven.be [193.190.253.150]) by ssel.vub.ac.be (Postfix) with ESMTP id 8AD082468926 for ; Tue, 6 Mar 2007 19:08:15 +0100 (CET) Mime-Version: 1.0 (Apple Message framework v752.3) Content-Transfer-Encoding: 7bit Message-Id: <70DC8A37-C742-405E-83E4-2367664717F5@vub.ac.be> Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed To: caml-list@inria.fr From: Bruno De Fraine Subject: Format module for C-style syntax Date: Tue, 6 Mar 2007 19:07:40 +0100 X-Mailer: Apple Mail (2.752.3) X-OriginalArrivalTime: 06 Mar 2007 18:08:25.0383 (UTC) FILETIME=[6F2AA370:01C7601A] X-Miltered: at discorde with ID 45EDAE1B.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; c-style:01 syntax:01 c-style:01 syntax:01 concretely:01 decl:01 foo:01 foo:01 ppf:01 fprintf:01 ppf:01 val:01 fprintf:01 val:01 decl:01 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 = # 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 = Then my attempts are: # let proc ppf (Procedure (pn,ss)) = fprintf ppf "@[procedure@ %s@ {@ @[%a@]@ }@]@." pn stmts ss ;; val proc : Format.formatter -> proc_decl -> unit = # 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@ {@ @[%a@]@ }@]@." pn stmts ss ;; val proc : Format.formatter -> proc_decl -> unit = # 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