1 ! Copyright (C) 2003, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays generic hashtables io kernel math assocs
4 namespaces make sequences strings io.styles vectors words
5 prettyprint.config splitting classes continuations
6 accessors sets vocabs.parser combinators vocabs
8 FROM: namespaces => set ;
9 IN: prettyprint.sections
13 SYMBOL: recursion-check
14 SYMBOL: pprinter-stack
16 ! We record vocabs of all words
20 TUPLE: pprinter last-newline line-count indent ;
22 : <pprinter> ( -- pprinter ) 0 1 0 pprinter boa ;
24 : (record-vocab) ( vocab -- )
25 dup pprinter-in get dup [ vocab-name ] when =
26 [ drop ] [ pprinter-use get conjoin ] if ;
28 GENERIC: vocabulary-name ( obj -- string )
30 M: word vocabulary-name
33 M: maybe vocabulary-name
34 class>> vocabulary>> ;
36 : record-vocab ( word -- )
44 : line-limit? ( -- ? )
45 line-limit get dup [ pprinter get line-count>> <= ] when ;
47 : do-indent ( -- ) pprinter get indent>> CHAR: \s <string> write ;
50 dup pprinter get last-newline>> = [
53 pprinter get last-newline<<
55 "..." write pprinter get return
57 pprinter get [ 1 + ] change-line-count drop
61 : text-fits? ( len -- ? )
63 [ drop t ] [ [ pprinter get indent>> + ] dip <= ] if-zero ;
65 ! break only if position margin 2 / >
72 GENERIC: section-fits? ( section -- ? )
74 GENERIC: short-section ( section -- )
76 GENERIC: long-section ( section -- )
78 GENERIC: indent-section? ( section -- ? )
80 GENERIC: unindent-first-line? ( section -- ? )
82 GENERIC: newline-after? ( section -- ? )
84 GENERIC: short-section? ( section -- ? )
89 start-group? end-group?
92 : new-section ( length class -- section )
95 swap position [ + ] change
99 M: section section-fits? ( section -- ? )
100 [ end>> pprinter get last-newline>> - ]
104 M: section indent-section? drop f ;
106 M: section unindent-first-line? drop f ;
108 M: section newline-after? drop f ;
110 M: object short-section? section-fits? ;
112 : indent+ ( section n -- )
113 swap indent-section? [
114 pprinter get [ + ] change-indent drop
117 : <indent ( section -- ) tab-size get indent+ ;
119 : indent> ( section -- ) tab-size get neg indent+ ;
121 : <fresh-line ( section -- )
124 : fresh-line> ( section -- )
125 dup newline-after? [ end>> fresh-line ] [ drop ] if ;
127 : <long-section ( section -- )
128 dup unindent-first-line?
129 [ dup <fresh-line <indent ] [ dup <indent <fresh-line ] if ;
131 : long-section> ( section -- )
132 dup indent> fresh-line> ;
134 : pprint-section ( section -- )
136 dup style>> [ short-section ] with-style
139 [ dup style>> [ long-section ] with-style ]
145 TUPLE: line-break < section type ;
147 : <line-break> ( type -- section )
148 0 \ line-break new-section
151 M: line-break short-section drop ;
153 M: line-break long-section drop ;
156 TUPLE: block < section sections ;
158 : new-block ( style class -- block )
160 V{ } clone >>sections
161 swap >>style ; inline
163 : <block> ( style -- block )
166 : pprinter-block ( -- block ) pprinter-stack get last ;
168 : add-section ( section -- )
169 pprinter-block sections>> push ;
171 : last-section ( -- section )
172 pprinter-block sections>>
173 [ line-break? not ] find-last nip ;
176 last-section t >>start-group? drop ;
179 last-section t >>end-group? drop ;
181 : advance ( section -- )
182 [ start>> pprinter get last-newline>> = not ]
183 [ short-section? ] bi
186 : add-line-break ( type -- ) [ <line-break> add-section ] when* ;
188 M: block section-fits? ( section -- ? )
189 line-limit? [ drop t ] [ call-next-method ] if ;
191 : pprint-sections ( block advancer -- )
193 sections>> [ line-break? not ] filter
194 unclip-slice pprint-section
196 [ [ pprint-section ] bi ] curry each ; inline
198 M: block short-section ( block -- )
199 [ advance ] pprint-sections ;
201 : do-break ( break -- )
204 [ end>> pprinter get last-newline>> - margin get 2/ > ] tri
205 or [ <fresh-line ] [ drop ] if ;
207 : empty-block? ( block -- ? ) sections>> empty? ;
209 : if-nonempty ( block quot -- )
210 [ dup empty-block? [ drop ] ] dip if ; inline
212 : (<block) ( block -- ) pprinter-stack get push ;
214 : <block ( -- ) f <block> (<block) ;
216 : <object ( obj -- ) presented associate <block> (<block) ;
219 TUPLE: text < section string ;
221 : <text> ( string style -- text )
222 over length 1 + \ text new-section
226 M: text short-section string>> write ;
228 M: text long-section short-section ;
230 : styled-text ( string style -- ) <text> add-section ;
232 : text ( string -- ) H{ } styled-text ;
235 TUPLE: inset < block narrow? ;
237 : <inset> ( narrow? -- block )
242 M: inset long-section
244 [ <fresh-line ] pprint-sections
249 M: inset indent-section? drop t ;
251 M: inset newline-after? drop t ;
253 : <inset ( narrow? -- ) <inset> (<block) ;
256 TUPLE: flow < block ;
258 : <flow> ( -- block )
259 H{ } flow new-block ;
261 M: flow short-section? ( section -- ? )
262 #! If we can make room for this entire block by inserting
263 #! a newline, do it; otherwise, don't bother, print it as
266 [ [ end>> ] [ start>> ] bi - text-fits? not ] bi
269 : <flow ( -- ) <flow> (<block) ;
271 ! Colon definition section
272 TUPLE: colon < block ;
274 : <colon> ( -- block )
275 H{ } colon new-block ;
277 M: colon long-section short-section ;
279 M: colon indent-section? drop t ;
281 M: colon unindent-first-line? drop t ;
283 : <colon ( -- ) <colon> (<block) ;
285 : save-end-position ( block -- )
286 position get >>end drop ;
289 pprinter-stack get pop
290 [ [ save-end-position ] [ add-section ] bi ] if-nonempty ;
292 : do-pprint ( block -- )
293 <pprinter> pprinter [
303 ! Long section layout algorithm
304 : chop-break ( seq -- seq )
305 dup last line-break? [ but-last-slice chop-break ] when ;
310 : split-groups ( ? -- ) [ t , ] when ;
312 : split-before ( section -- )
313 [ start-group?>> prev get [ end-group?>> ] [ t ] if* and ]
314 [ flow? prev get flow? not and ]
317 : split-after ( section -- )
318 [ end-group?>> ] [ f ] if* split-groups ;
320 : group-flow ( seq -- newseq )
323 2dup 1 - swap ?nth prev set
324 2dup 1 + swap ?nth next set
325 swap nth dup split-before dup , split-after
327 ] { } make { t } split harvest ;
329 : break-group? ( seq -- ? )
330 [ first section-fits? ] [ last section-fits? not ] bi and ;
332 : ?break-group ( seq -- )
333 dup break-group? [ first <fresh-line ] [ drop ] if ;
335 M: block long-section ( block -- )
337 sections>> chop-break group-flow [
342 [ advance ] [ pprint-section ] bi
348 : pprinter-manifest ( -- manifest )
350 [ [ pprinter-use get keys >vector ] dip search-vocabs<< ]
351 [ [ pprinter-in get ] dip current-vocab<< ]
355 : make-pprint ( obj quot -- block manifest )
358 H{ } clone pprinter-use ,,
359 V{ } clone recursion-check ,,
360 V{ } clone pprinter-stack ,,
366 ] with-variables ; inline
368 : with-pprint ( obj quot -- )
369 make-pprint drop do-pprint ; inline