* Format: Fortran (&c.) style continuation lines?
@ 2010-04-08 10:58 Thorsten Ohl (TP2)
2010-04-08 17:53 ` [Caml-list] " Martin Jambon
0 siblings, 1 reply; 2+ messages in thread
From: Thorsten Ohl (TP2) @ 2010-04-08 10:58 UTC (permalink / raw)
To: caml-list
Hi,
for ages, I've been using the following (somewhat hackish) approach to
pretty printing source code that requires special lexical markers to
allow statements that continue over more than one line. (e.g. in
Fortran
foo = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 &
+ 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 &
+ 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 &
+ 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 &
+ 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 &
+ 1 + 1 + 1 + 1 + 1 + 1 + 1
and in /bin/sh we have the same with "\\" instead of "&"):
open Format;;
let continuing = ref true;;
let wrap_newline () =
let out, flush, newline, space = get_all_formatter_output_functions
() in
let newline' () =
if !continuing then
out " &" 0 2;
newline () in
set_all_formatter_output_functions out flush newline' space;;
let nl () =
continuing := false;
print_newline ();
continuing := true;;
let _ =
wrap_newline ();;
(* Nonsensical example: *)
for statement = 1 to 3 do
printf " @[<2>foo = 1";
for i = 1 to 100 do
printf "@, + 1"
done;
nl ()
done;;
The requirement to end each statement with "nl ()" is tedious in real
world applications and the use of the global variable "continuing"
violates my sense of aesthetics...
Is there a more idiomatic approach that I'm missing?
Thanks,
-Thorsten
--
Thorsten Ohl, Wuerzburg Univ., Physics -- http://physik.uni-wuerzburg.de/ohl
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] Format: Fortran (&c.) style continuation lines?
2010-04-08 10:58 Format: Fortran (&c.) style continuation lines? Thorsten Ohl (TP2)
@ 2010-04-08 17:53 ` Martin Jambon
0 siblings, 0 replies; 2+ messages in thread
From: Martin Jambon @ 2010-04-08 17:53 UTC (permalink / raw)
To: ohl; +Cc: caml-list
Thorsten Ohl (TP2) wrote:
> Hi,
>
> for ages, I've been using the following (somewhat hackish) approach to
> pretty printing source code that requires special lexical markers to
> allow statements that continue over more than one line. (e.g. in
> Fortran
>
> foo = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 &
> + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 &
> + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 &
> + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 &
> + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 &
> + 1 + 1 + 1 + 1 + 1 + 1 + 1
>
> and in /bin/sh we have the same with "\\" instead of "&"):
>
> open Format;;
>
> let continuing = ref true;;
>
> let wrap_newline () =
> let out, flush, newline, space = get_all_formatter_output_functions
> () in
> let newline' () =
> if !continuing then
> out " &" 0 2;
> newline () in
> set_all_formatter_output_functions out flush newline' space;;
>
> let nl () =
> continuing := false;
> print_newline ();
> continuing := true;;
>
> let _ =
> wrap_newline ();;
>
> (* Nonsensical example: *)
> for statement = 1 to 3 do
> printf " @[<2>foo = 1";
> for i = 1 to 100 do
> printf "@, + 1"
> done;
> nl ()
> done;;
>
> The requirement to end each statement with "nl ()" is tedious in real
> world applications and the use of the global variable "continuing"
> violates my sense of aesthetics...
>
> Is there a more idiomatic approach that I'm missing?
Given the imperative nature of the Format module interface, your solution
seems right to me. Maybe you'll output up to 2 bytes beyond the margin but I
guess that's ok.
You may also consider using easy-format, which offers a functional interface
on top of Format. The programmer's job is to create a tree containing strings
and parameters while the actual printing is done by a single function call.
That said, easy-format does not support custom newline strings although I
could add the feature. Link: http://martin.jambon.free.fr/easy-format.html
Martin
--
http://mjambon.com/
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2010-04-08 17:53 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-04-08 10:58 Format: Fortran (&c.) style continuation lines? Thorsten Ohl (TP2)
2010-04-08 17:53 ` [Caml-list] " Martin Jambon
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox