]> gitweb.factorcode.org Git - factor.git/blob - basis/prettyprint/sections/sections.factor
basis: removing unnecessary method stack effects.
[factor.git] / basis / prettyprint / sections / sections.factor
1 ! Copyright (C) 2003, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors classes.maybe combinators
4 combinators.short-circuit continuations hashtables io io.styles
5 kernel make math namespaces prettyprint.config sequences sets
6 splitting strings vocabs vocabs.parser words ;
7 IN: prettyprint.sections
8
9 ! State
10 SYMBOL: position
11 SYMBOL: recursion-check
12 SYMBOL: pprinter-stack
13
14 ! We record vocabs of all words
15 SYMBOL: pprinter-in
16 SYMBOL: pprinter-use
17
18 TUPLE: pprinter last-newline line-count indent ;
19
20 : <pprinter> ( -- pprinter ) 0 1 0 pprinter boa ;
21
22 : (record-vocab) ( vocab -- )
23     dup pprinter-in get dup [ vocab-name ] when =
24     [ drop ] [ pprinter-use get adjoin ] if ;
25
26 GENERIC: vocabulary-name ( obj -- string )
27
28 M: word vocabulary-name
29     vocabulary>> ;
30
31 M: maybe vocabulary-name
32     class>> vocabulary>> ;
33
34 : record-vocab ( word -- )
35     vocabulary-name {
36         { f [ ] }
37         { "syntax" [ ] }
38         [ (record-vocab) ]
39     } case ;
40
41 ! Utility words
42 : line-limit? ( -- ? )
43     line-limit get dup [ pprinter get line-count>> <= ] when ;
44
45 : do-indent ( -- )
46     pprinter get indent>> [ CHAR: \s <string> write ] unless-zero ;
47
48 : fresh-line ( n -- )
49     pprinter get 2dup last-newline>> = [
50         2drop
51     ] [
52         swap >>last-newline
53         line-limit? [
54             "..." write return
55         ] when
56         [ 1 + ] change-line-count drop
57         nl do-indent
58     ] if ;
59
60 : text-fits? ( len -- ? )
61     margin get [
62         drop t
63     ] [
64         [ pprinter get indent>> + ] dip <=
65     ] if-zero ;
66
67 ! break only if position margin 2 / >
68 SYMBOL: soft
69
70 ! always breaks
71 SYMBOL: hard
72
73 ! Section protocol
74 GENERIC: section-fits? ( section -- ? )
75
76 GENERIC: short-section ( section -- )
77
78 GENERIC: long-section ( section -- )
79
80 GENERIC: indent-section? ( section -- ? )
81
82 GENERIC: unindent-first-line? ( section -- ? )
83
84 GENERIC: newline-after? ( section -- ? )
85
86 GENERIC: short-section? ( section -- ? )
87
88 ! Sections
89 TUPLE: section
90 start end
91 start-group? end-group?
92 style overhang ;
93
94 : new-section ( length class -- section )
95     new
96         position [
97             [ >>start ] keep
98             swapd +
99             [ >>end ] keep
100         ] change
101         0 >>overhang ; inline
102
103 M: section section-fits?
104     [ end>> 1 - pprinter get last-newline>> - ]
105     [ overhang>> ] bi + text-fits? ;
106
107 M: section indent-section? drop f ;
108
109 M: section unindent-first-line? drop f ;
110
111 M: section newline-after? drop f ;
112
113 M: object short-section? section-fits? ;
114
115 : indent+ ( section n -- )
116     swap indent-section? [
117         pprinter get [ + ] change-indent drop
118     ] [ drop ] if ;
119
120 : <indent ( section -- ) tab-size get indent+ ;
121
122 : indent> ( section -- ) tab-size get neg indent+ ;
123
124 : <fresh-line ( section -- )
125     start>> fresh-line ;
126
127 : fresh-line> ( section -- )
128     dup newline-after? [ end>> fresh-line ] [ drop ] if ;
129
130 : <long-section ( section -- )
131     dup unindent-first-line?
132     [ dup <fresh-line <indent ] [ dup <indent <fresh-line ] if ;
133
134 : long-section> ( section -- )
135     dup indent> fresh-line> ;
136
137 : pprint-section ( section -- )
138     dup short-section? [
139         dup style>> [ short-section ] with-style
140     ] [
141         [ <long-section ]
142         [ dup style>> [ long-section ] with-style ]
143         [ long-section> ]
144         tri
145     ] if ;
146
147 ! Break section
148 TUPLE: line-break < section type ;
149
150 : <line-break> ( type -- section )
151     0 line-break new-section
152         swap >>type ;
153
154 M: line-break short-section drop ;
155
156 M: line-break long-section drop ;
157
158 ! Block sections
159 TUPLE: block < section sections ;
160
161 : new-block ( class -- block )
162     0 swap new-section
163         V{ } clone >>sections ; inline
164
165 : <block> ( style -- block )
166     block new-block
167         swap >>style ;
168
169 : pprinter-block ( -- block ) pprinter-stack get last ;
170
171 : add-section ( section -- )
172     pprinter-block sections>> push ;
173
174 : last-section ( -- section )
175     pprinter-block sections>>
176     [ line-break? not ] find-last nip ;
177
178 : start-group ( -- )
179     last-section t >>start-group? drop ;
180
181 : end-group ( -- )
182     last-section t >>end-group? drop ;
183
184 : advance ( section -- )
185     {
186         [ start>> pprinter get last-newline>> = not ]
187         [ short-section? ]
188     } 1&& [ bl ] when ;
189
190 : add-line-break ( type -- ) [ <line-break> add-section ] when* ;
191
192 M: block section-fits?
193     line-limit? [ drop t ] [ call-next-method ] if ;
194
195 : pprint-sections ( block advancer -- )
196     [
197         sections>> [ line-break? ] reject
198         unclip-slice pprint-section
199     ] dip
200     [ [ pprint-section ] bi ] curry each ; inline
201
202 M: block short-section
203     [ advance ] pprint-sections ;
204
205 : do-break ( break -- )
206     [ ]
207     [ type>> hard eq? ]
208     [ end>> pprinter get last-newline>> - margin get 2/ > ] tri
209     or [ <fresh-line ] [ drop ] if ;
210
211 : empty-block? ( block -- ? ) sections>> empty? ;
212
213 : unless-empty-block ( block quot: ( block -- ) -- )
214     [ dup empty-block? [ drop ] ] dip if ; inline
215
216 : (<block) ( block -- ) pprinter-stack get push ;
217
218 : <block ( -- ) f <block> (<block) ;
219
220 : <object ( obj -- ) presented associate <block> (<block) ;
221
222 ! Text section
223 TUPLE: text-section < section string ;
224
225 : <text> ( string style -- text )
226     over length 1 + text-section new-section
227         swap >>style
228         swap >>string ;
229
230 M: text-section short-section string>> write ;
231
232 M: text-section long-section short-section ;
233
234 : styled-text ( string style -- ) <text> add-section ;
235
236 : text ( string -- ) f styled-text ;
237
238 ! Inset section
239 TUPLE: inset < block narrow? ;
240
241 : <inset> ( narrow? -- block )
242     inset new-block
243         2 >>overhang
244         swap >>narrow? ;
245
246 M: inset long-section
247     dup narrow?>> [
248         [ <fresh-line ] pprint-sections
249     ] [
250         call-next-method
251     ] if ;
252
253 M: inset indent-section? drop t ;
254
255 M: inset newline-after? drop t ;
256
257 : <inset ( narrow? -- ) <inset> (<block) ;
258
259 ! Flow section
260 TUPLE: flow < block ;
261
262 : <flow> ( -- block )
263     flow new-block ;
264
265 M: flow short-section?
266     ! If we can make room for this entire block by inserting
267     ! a newline, do it; otherwise, don't bother, print it as
268     ! a short section
269     {
270         [ section-fits? ]
271         [ [ end>> 1 - ] [ start>> ] bi - text-fits? not ]
272     } 1|| ;
273
274 : <flow ( -- ) <flow> (<block) ;
275
276 ! Colon definition section
277 TUPLE: colon < block ;
278
279 : <colon> ( -- block )
280     colon new-block ;
281
282 M: colon long-section short-section ;
283
284 M: colon indent-section? drop t ;
285
286 M: colon unindent-first-line? drop t ;
287
288 : <colon ( -- ) <colon> (<block) ;
289
290 : save-end-position ( block -- )
291     position get >>end drop ;
292
293 : block> ( -- )
294     pprinter-stack get pop [
295         [ save-end-position ] [ add-section ] bi
296     ] unless-empty-block ;
297
298 : do-pprint ( block -- )
299     <pprinter> pprinter [
300         [
301             dup style>> [
302                 [
303                     short-section
304                 ] curry with-return
305             ] with-nesting
306         ] unless-empty-block
307     ] with-variable ;
308
309 ! Long section layout algorithm
310 : chop-break ( seq -- seq )
311     [ dup last line-break? ] [ but-last-slice ] while ;
312
313 SYMBOL: prev
314 SYMBOL: next
315
316 : split-groups ( ? -- ) [ t , ] when ;
317
318 : split-before ( section -- )
319     {
320         [ start-group?>> prev get [ end-group?>> and ] when* ]
321         [ flow? prev get flow? not and ]
322     } 1|| split-groups ;
323
324 : split-after ( section -- )
325     [ end-group?>> ] [ f ] if* split-groups ;
326
327 : group-flow ( seq -- newseq )
328     [
329         dup length <iota> [
330             2dup 1 - swap ?nth prev namespaces:set
331             2dup 1 + swap ?nth next namespaces:set
332             swap nth dup split-before dup , split-after
333         ] with each
334     ] { } make { t } split harvest ;
335
336 : break-group? ( seq -- ? )
337     { [ first section-fits? ] [ last section-fits? not ] } 1&& ;
338
339 : ?break-group ( seq -- )
340     dup break-group? [ first <fresh-line ] [ drop ] if ;
341
342 M: block long-section
343     [
344         sections>> chop-break group-flow [
345             dup ?break-group [
346                 dup line-break? [
347                     do-break
348                 ] [
349                     [ advance ] [ pprint-section ] bi
350                 ] if
351             ] each
352         ] each
353     ] unless-empty-block ;
354
355 : pprinter-manifest ( -- manifest )
356     <manifest>
357         pprinter-use get members V{ } like >>search-vocabs
358         pprinter-in get >>current-vocab ;
359
360 : make-pprint ( obj quot manifest? -- block manifest/f )
361     [
362         0 position namespaces:set
363         HS{ } clone pprinter-use namespaces:set
364         V{ } clone recursion-check namespaces:set
365         V{ } clone pprinter-stack namespaces:set
366
367         [ over <object call pprinter-block ] dip
368         [ pprinter-manifest ] [ f ] if
369     ] with-scope ; inline
370
371 : with-pprint ( obj quot -- )
372     f make-pprint drop do-pprint ; inline