1 (* Copyright (C
) 1999-2006 Henry Cejtin
, Matthew Fluet
, Suresh
2 * Jagannathan
, and Stephen Weeks
.
4 * MLton is released under a BSD
-style license
.
5 * See the file MLton
-LICENSE for details
.
10 * Functional Unparsing
11 * BRICS Technical Report RS
98-12
12 * Olivier Danvy
, May
1998
15 structure Format
:> FORMAT
=
18 type ('a
, 'b
) t
= (string list
-> 'a
) * string list
-> 'b
20 val new
: ('b
-> string) -> ('a
, 'b
-> 'a
) t
=
21 fn toString
=> fn (k
, ss
) => fn b
=> k (toString b
:: ss
)
23 val lit
: string -> ('a
, 'a
) t
= fn s
=> fn (k
, ss
) => k (s
:: ss
)
25 val eol
: ('a
, 'a
) t
= fn z
=> lit
"\n" z
28 * Trace
.trace ("Format.concat", List.layout
String.layout
, String.layout
) concat
31 val format
: (string, 'a
) t
-> 'a
= fn f
=> f (concat
o rev
, [])
33 val int: ('a
, int -> 'a
) t
= fn z
=> new
Int.toString z
35 val list
: ('a
, 'b
-> 'a
) t
-> ('a
, 'b list
-> 'a
) t
=
37 fn [] => k ("[]" :: ss
)
43 | x
:: xs
=> f (loop xs
, ", " :: ss
) x
44 in f (loop xs
, "[" :: ss
) x
47 val op o: ('a
, 'b
) t
* ('c
, 'a
) t
-> ('c
, 'b
) t
=
48 fn (f
, g
) => fn (k
, ss
) => f (fn ss
=> g (k
, ss
), ss
)
50 val string: ('a
, string -> 'a
) t
= fn z
=> new (fn s
=> s
) z