|
1 (* ========================================================================= *) |
|
2 (* PRETTY-PRINTING *) |
|
3 (* Copyright (c) 2001-2008 Joe Hurd, distributed under the GNU GPL version 2 *) |
|
4 (* ========================================================================= *) |
|
5 |
|
6 signature Print = |
|
7 sig |
|
8 |
|
9 (* ------------------------------------------------------------------------- *) |
|
10 (* A type of pretty-printers. *) |
|
11 (* ------------------------------------------------------------------------- *) |
|
12 |
|
13 datatype breakStyle = Consistent | Inconsistent |
|
14 |
|
15 datatype ppStep = |
|
16 BeginBlock of breakStyle * int |
|
17 | EndBlock |
|
18 | AddString of string |
|
19 | AddBreak of int |
|
20 | AddNewline |
|
21 |
|
22 type ppstream = ppStep Stream.stream |
|
23 |
|
24 type 'a pp = 'a -> ppstream |
|
25 |
|
26 (* ------------------------------------------------------------------------- *) |
|
27 (* Pretty-printer primitives. *) |
|
28 (* ------------------------------------------------------------------------- *) |
|
29 |
|
30 val beginBlock : breakStyle -> int -> ppstream |
|
31 |
|
32 val endBlock : ppstream |
|
33 |
|
34 val addString : string -> ppstream |
|
35 |
|
36 val addBreak : int -> ppstream |
|
37 |
|
38 val addNewline : ppstream |
|
39 |
|
40 val skip : ppstream |
|
41 |
|
42 val sequence : ppstream -> ppstream -> ppstream |
|
43 |
|
44 val duplicate : int -> ppstream -> ppstream |
|
45 |
|
46 val program : ppstream list -> ppstream |
|
47 |
|
48 val stream : ppstream Stream.stream -> ppstream |
|
49 |
|
50 val block : breakStyle -> int -> ppstream -> ppstream |
|
51 |
|
52 val blockProgram : breakStyle -> int -> ppstream list -> ppstream |
|
53 |
|
54 val bracket : string -> string -> ppstream -> ppstream |
|
55 |
|
56 val field : string -> ppstream -> ppstream |
|
57 |
|
58 val record : (string * ppstream) list -> ppstream |
|
59 |
|
60 (* ------------------------------------------------------------------------- *) |
|
61 (* Pretty-printer combinators. *) |
|
62 (* ------------------------------------------------------------------------- *) |
|
63 |
|
64 val ppMap : ('a -> 'b) -> 'b pp -> 'a pp |
|
65 |
|
66 val ppBracket : string -> string -> 'a pp -> 'a pp |
|
67 |
|
68 val ppOp : string -> ppstream |
|
69 |
|
70 val ppOp2 : string -> 'a pp -> 'b pp -> ('a * 'b) pp |
|
71 |
|
72 val ppOp3 : string -> string -> 'a pp -> 'b pp -> 'c pp -> ('a * 'b * 'c) pp |
|
73 |
|
74 val ppOpList : string -> 'a pp -> 'a list pp |
|
75 |
|
76 val ppOpStream : string -> 'a pp -> 'a Stream.stream pp |
|
77 |
|
78 (* ------------------------------------------------------------------------- *) |
|
79 (* Pretty-printers for common types. *) |
|
80 (* ------------------------------------------------------------------------- *) |
|
81 |
|
82 val ppChar : char pp |
|
83 |
|
84 val ppString : string pp |
|
85 |
|
86 val ppEscapeString : {escape : char list} -> string pp |
|
87 |
|
88 val ppUnit : unit pp |
|
89 |
|
90 val ppBool : bool pp |
|
91 |
|
92 val ppInt : int pp |
|
93 |
|
94 val ppPrettyInt : int pp |
|
95 |
|
96 val ppReal : real pp |
|
97 |
|
98 val ppPercent : real pp |
|
99 |
|
100 val ppOrder : order pp |
|
101 |
|
102 val ppList : 'a pp -> 'a list pp |
|
103 |
|
104 val ppStream : 'a pp -> 'a Stream.stream pp |
|
105 |
|
106 val ppOption : 'a pp -> 'a option pp |
|
107 |
|
108 val ppPair : 'a pp -> 'b pp -> ('a * 'b) pp |
|
109 |
|
110 val ppTriple : 'a pp -> 'b pp -> 'c pp -> ('a * 'b * 'c) pp |
|
111 |
|
112 val ppBreakStyle : breakStyle pp |
|
113 |
|
114 val ppPpStep : ppStep pp |
|
115 |
|
116 val ppPpstream : ppstream pp |
|
117 |
|
118 (* ------------------------------------------------------------------------- *) |
|
119 (* Pretty-printing infix operators. *) |
|
120 (* ------------------------------------------------------------------------- *) |
|
121 |
|
122 datatype infixes = |
|
123 Infixes of |
|
124 {token : string, |
|
125 precedence : int, |
|
126 leftAssoc : bool} list |
|
127 |
|
128 val tokensInfixes : infixes -> StringSet.set |
|
129 |
|
130 val layerInfixes : |
|
131 infixes -> |
|
132 {tokens : {leftSpaces : int, token : string, rightSpaces : int} list, |
|
133 leftAssoc : bool} list |
|
134 |
|
135 val ppInfixes : |
|
136 infixes -> ('a -> (string * 'a * 'a) option) -> ('a * bool) pp -> |
|
137 ('a * bool) pp |
|
138 |
|
139 (* ------------------------------------------------------------------------- *) |
|
140 (* Executing pretty-printers to generate lines. *) |
|
141 (* ------------------------------------------------------------------------- *) |
|
142 |
|
143 val execute : {lineLength : int} -> ppstream -> string Stream.stream |
|
144 |
|
145 (* ------------------------------------------------------------------------- *) |
|
146 (* Executing pretty-printers with a global line length. *) |
|
147 (* ------------------------------------------------------------------------- *) |
|
148 |
|
149 val lineLength : int ref |
|
150 |
|
151 val toString : 'a pp -> 'a -> string |
|
152 |
|
153 val toStream : 'a pp -> 'a -> string Stream.stream |
|
154 |
|
155 val trace : 'a pp -> string -> 'a -> unit |
|
156 |
|
157 end |