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
7 classes.maybe combinators.short-circuit ;
8 FROM: sets => members ;
9 FROM: namespaces => set ;
10 IN: prettyprint.sections
14 SYMBOL: recursion-check
15 SYMBOL: pprinter-stack
17 ! We record vocabs of all words
21 TUPLE: pprinter last-newline line-count indent ;
23 : <pprinter> ( -- pprinter ) 0 1 0 pprinter boa ;
25 : (record-vocab) ( vocab -- )
26 dup pprinter-in get dup [ vocab-name ] when =
27 [ drop ] [ pprinter-use get adjoin ] if ;
29 GENERIC: vocabulary-name ( obj -- string )
31 M: word vocabulary-name
34 M: maybe vocabulary-name
35 class>> vocabulary>> ;
37 : record-vocab ( word -- )
45 : line-limit? ( -- ? )
46 line-limit get dup [ pprinter get line-count>> <= ] when ;
48 : do-indent ( -- ) pprinter get indent>> CHAR: \s <string> write ;
51 dup pprinter get last-newline>> = [
54 pprinter get last-newline<<
56 "..." write pprinter get return
58 pprinter get [ 1 + ] change-line-count drop
62 : text-fits? ( len -- ? )
64 [ drop t ] [ [ pprinter get indent>> + ] dip <= ] if-zero ;
66 ! break only if position margin 2 / >
73 GENERIC: section-fits? ( section -- ? )
75 GENERIC: short-section ( section -- )
77 GENERIC: long-section ( section -- )
79 GENERIC: indent-section? ( section -- ? )
81 GENERIC: unindent-first-line? ( section -- ? )
83 GENERIC: newline-after? ( section -- ? )
85 GENERIC: short-section? ( section -- ? )
90 start-group? end-group?
93 : new-section ( length class -- section )
96 swap position [ + ] change
100 M: section section-fits? ( section -- ? )
101 [ end>> 1 - pprinter get last-newline>> - ]
102 [ overhang>> ] bi + text-fits? ;
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 -- )
183 [ start>> pprinter get last-newline>> = not ]
187 : add-line-break ( type -- ) [ <line-break> add-section ] when* ;
189 M: block section-fits? ( section -- ? )
190 line-limit? [ drop t ] [ call-next-method ] if ;
192 : pprint-sections ( block advancer -- )
194 sections>> [ line-break? not ] filter
195 unclip-slice pprint-section
197 [ [ pprint-section ] bi ] curry each ; inline
199 M: block short-section ( block -- )
200 [ advance ] pprint-sections ;
202 : do-break ( break -- )
205 [ end>> pprinter get last-newline>> - margin get 2/ > ] tri
206 or [ <fresh-line ] [ drop ] if ;
208 : empty-block? ( block -- ? ) sections>> empty? ;
210 : unless-empty-block ( block quot: ( block -- ) -- )
211 [ dup empty-block? [ drop ] ] dip if ; inline
213 : (<block) ( block -- ) pprinter-stack get push ;
215 : <block ( -- ) f <block> (<block) ;
217 : <object ( obj -- ) presented associate <block> (<block) ;
220 TUPLE: text-section < section string ;
222 : <text> ( string style -- text )
223 over length 1 + \ text-section new-section
227 M: text-section short-section string>> write ;
229 M: text-section long-section short-section ;
231 : styled-text ( string style -- ) <text> add-section ;
233 : text ( string -- ) H{ } styled-text ;
236 TUPLE: inset < block narrow? ;
238 : <inset> ( narrow? -- block )
243 M: inset long-section
245 [ <fresh-line ] pprint-sections
250 M: inset indent-section? drop t ;
252 M: inset newline-after? drop t ;
254 : <inset ( narrow? -- ) <inset> (<block) ;
257 TUPLE: flow < block ;
259 : <flow> ( -- block )
260 H{ } flow new-block ;
262 M: flow short-section? ( section -- ? )
263 #! If we can make room for this entire block by inserting
264 #! a newline, do it; otherwise, don't bother, print it as
268 [ [ end>> 1 - ] [ start>> ] bi - text-fits? not ]
271 : <flow ( -- ) <flow> (<block) ;
273 ! Colon definition section
274 TUPLE: colon < block ;
276 : <colon> ( -- block )
277 H{ } colon new-block ;
279 M: colon long-section short-section ;
281 M: colon indent-section? drop t ;
283 M: colon unindent-first-line? drop t ;
285 : <colon ( -- ) <colon> (<block) ;
287 : save-end-position ( block -- )
288 position get >>end drop ;
291 pprinter-stack get pop [
292 [ save-end-position ] [ add-section ] bi
293 ] unless-empty-block ;
295 : do-pprint ( block -- )
296 <pprinter> pprinter [
306 ! Long section layout algorithm
307 : chop-break ( seq -- seq )
308 [ dup last line-break? ] [ but-last-slice ] while ;
313 : split-groups ( ? -- ) [ t , ] when ;
315 : split-before ( section -- )
317 [ start-group?>> prev get [ end-group?>> and ] when* ]
318 [ flow? prev get flow? not and ]
321 : split-after ( section -- )
322 [ end-group?>> ] [ f ] if* split-groups ;
324 : group-flow ( seq -- newseq )
327 2dup 1 - swap ?nth prev set
328 2dup 1 + swap ?nth next set
329 swap nth dup split-before dup , split-after
331 ] { } make { t } split harvest ;
333 : break-group? ( seq -- ? )
334 { [ first section-fits? ] [ last section-fits? not ] } 1&& ;
336 : ?break-group ( seq -- )
337 dup break-group? [ first <fresh-line ] [ drop ] if ;
339 M: block long-section ( block -- )
341 sections>> chop-break group-flow [
346 [ advance ] [ pprint-section ] bi
350 ] unless-empty-block ;
352 : pprinter-manifest ( -- manifest )
354 pprinter-use get members V{ } like >>search-vocabs
355 pprinter-in get >>current-vocab ;
357 : make-pprint ( obj quot manifest? -- block manifest/f )
360 HS{ } clone pprinter-use set
361 V{ } clone recursion-check set
362 V{ } clone pprinter-stack set
364 [ over <object call pprinter-block ] dip
365 [ pprinter-manifest ] [ f ] if
366 ] with-scope ; inline
368 : with-pprint ( obj quot -- )
369 f make-pprint drop do-pprint ; inline