factor/basis/prettyprint/sections/sections.factor

372 lines
8.5 KiB
Factor
Raw Normal View History

2010-01-14 10:10:13 -05:00
! Copyright (C) 2003, 2010 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
2015-07-17 19:42:20 -04:00
USING: accessors classes.maybe combinators
combinators.short-circuit continuations hashtables io io.styles
kernel make math namespaces prettyprint.config sequences sets
splitting strings vocabs vocabs.parser words ;
2007-09-20 18:09:08 -04:00
IN: prettyprint.sections
! State
SYMBOL: position
SYMBOL: recursion-check
SYMBOL: pprinter-stack
! We record vocabs of all words
SYMBOL: pprinter-in
SYMBOL: pprinter-use
2008-05-07 08:49:29 -04:00
TUPLE: pprinter last-newline line-count indent ;
2008-05-07 08:49:29 -04:00
: <pprinter> ( -- pprinter ) 0 1 0 pprinter boa ;
: (record-vocab) ( vocab -- )
dup pprinter-in get dup [ vocab-name ] when =
[ drop ] [ pprinter-use get adjoin ] if ;
GENERIC: vocabulary-name ( obj -- string )
M: word vocabulary-name
vocabulary>> ;
M: maybe vocabulary-name
class>> vocabulary>> ;
2007-09-20 18:09:08 -04:00
: record-vocab ( word -- )
vocabulary-name {
{ f [ ] }
{ "syntax" [ ] }
[ (record-vocab) ]
} case ;
2007-09-20 18:09:08 -04:00
! Utility words
: line-limit? ( -- ? )
line-limit get dup [ pprinter get line-count>> <= ] when ;
2007-09-20 18:09:08 -04:00
: do-indent ( -- ) pprinter get indent>> CHAR: \s <string> write ;
2007-09-20 18:09:08 -04:00
: fresh-line ( n -- )
2015-07-17 19:42:20 -04:00
pprinter get 2dup last-newline>> = [
2drop
2007-09-20 18:09:08 -04:00
] [
2015-07-17 19:42:20 -04:00
swap >>last-newline
line-limit? [
2015-07-17 19:42:20 -04:00
"..." write return
] when
2015-07-17 19:42:20 -04:00
[ 1 + ] change-line-count drop
2007-09-20 18:09:08 -04:00
nl do-indent
] if ;
: text-fits? ( len -- ? )
2015-07-17 19:42:20 -04:00
margin get [
drop t
] [
[ pprinter get indent>> + ] dip <=
] if-zero ;
2007-09-20 18:09:08 -04:00
! break only if position margin 2 / >
SYMBOL: soft
! always breaks
SYMBOL: hard
2007-09-20 18:09:08 -04:00
! Section protocol
GENERIC: section-fits? ( section -- ? )
GENERIC: short-section ( section -- )
GENERIC: long-section ( section -- )
GENERIC: indent-section? ( section -- ? )
GENERIC: unindent-first-line? ( section -- ? )
GENERIC: newline-after? ( section -- ? )
GENERIC: short-section? ( section -- ? )
! Sections
TUPLE: section
start end
start-group? end-group?
style overhang ;
: new-section ( length class -- section )
new
position [
[ >>start ] keep
swapd +
[ >>end ] keep
] change
2008-04-04 05:33:35 -04:00
0 >>overhang ; inline
2007-09-20 18:09:08 -04:00
M: section section-fits? ( section -- ? )
[ end>> 1 - pprinter get last-newline>> - ]
2014-12-11 10:52:14 -05:00
[ overhang>> ] bi + text-fits? ;
2007-09-20 18:09:08 -04:00
M: section indent-section? drop f ;
M: section unindent-first-line? drop f ;
M: section newline-after? drop f ;
M: object short-section? section-fits? ;
: indent+ ( section n -- )
swap indent-section? [
pprinter get [ + ] change-indent drop
] [ drop ] if ;
2007-09-20 18:09:08 -04:00
: <indent ( section -- ) tab-size get indent+ ;
2007-09-20 18:09:08 -04:00
: indent> ( section -- ) tab-size get neg indent+ ;
2007-09-20 18:09:08 -04:00
: <fresh-line ( section -- )
2008-04-04 05:33:35 -04:00
start>> fresh-line ;
2007-09-20 18:09:08 -04:00
: fresh-line> ( section -- )
2008-04-04 05:33:35 -04:00
dup newline-after? [ end>> fresh-line ] [ drop ] if ;
2007-09-20 18:09:08 -04:00
: <long-section ( section -- )
dup unindent-first-line?
[ dup <fresh-line <indent ] [ dup <indent <fresh-line ] if ;
: long-section> ( section -- )
dup indent> fresh-line> ;
: pprint-section ( section -- )
dup short-section? [
2008-08-30 22:55:29 -04:00
dup style>> [ short-section ] with-style
2007-09-20 18:09:08 -04:00
] [
[ <long-section ]
2008-08-30 22:55:29 -04:00
[ dup style>> [ long-section ] with-style ]
[ long-section> ]
tri
2007-09-20 18:09:08 -04:00
] if ;
! Break section
2008-04-04 05:33:35 -04:00
TUPLE: line-break < section type ;
2007-09-20 18:09:08 -04:00
: <line-break> ( type -- section )
0 line-break new-section
swap >>type ;
2007-09-20 18:09:08 -04:00
M: line-break short-section drop ;
2007-09-20 18:09:08 -04:00
M: line-break long-section drop ;
2007-09-20 18:09:08 -04:00
! Block sections
2008-04-04 05:33:35 -04:00
TUPLE: block < section sections ;
2007-09-20 18:09:08 -04:00
: new-block ( class -- block )
0 swap new-section
V{ } clone >>sections ; inline
2007-09-20 18:09:08 -04:00
2008-04-04 05:33:35 -04:00
: <block> ( style -- block )
block new-block
swap >>style ;
2007-09-20 18:09:08 -04:00
: pprinter-block ( -- block ) pprinter-stack get last ;
2007-09-20 18:09:08 -04:00
: add-section ( section -- )
2008-04-04 05:33:35 -04:00
pprinter-block sections>> push ;
2007-09-20 18:09:08 -04:00
: last-section ( -- section )
2008-04-04 05:33:35 -04:00
pprinter-block sections>>
[ line-break? not ] find-last nip ;
2007-09-20 18:09:08 -04:00
: start-group ( -- )
2008-04-04 05:33:35 -04:00
last-section t >>start-group? drop ;
2007-09-20 18:09:08 -04:00
: end-group ( -- )
2008-04-04 05:33:35 -04:00
last-section t >>end-group? drop ;
2007-09-20 18:09:08 -04:00
: advance ( section -- )
{
[ start>> pprinter get last-newline>> = not ]
[ short-section? ]
} 1&& [ bl ] when ;
2007-09-20 18:09:08 -04:00
: add-line-break ( type -- ) [ <line-break> add-section ] when* ;
2007-09-20 18:09:08 -04:00
M: block section-fits? ( section -- ? )
2008-04-04 05:33:35 -04:00
line-limit? [ drop t ] [ call-next-method ] if ;
2007-09-20 18:09:08 -04:00
: pprint-sections ( block advancer -- )
2008-08-15 05:09:34 -04:00
[
sections>> [ line-break? ] reject
2008-08-15 05:09:34 -04:00
unclip-slice pprint-section
] dip
[ [ pprint-section ] bi ] curry each ; inline
2007-09-20 18:09:08 -04:00
M: block short-section ( block -- )
[ advance ] pprint-sections ;
: do-break ( break -- )
[ ]
[ type>> hard eq? ]
[ end>> pprinter get last-newline>> - margin get 2/ > ] tri
or [ <fresh-line ] [ drop ] if ;
2007-09-20 18:09:08 -04:00
2008-04-04 05:33:35 -04:00
: empty-block? ( block -- ? ) sections>> empty? ;
2007-09-20 18:09:08 -04:00
2014-12-11 10:52:14 -05:00
: unless-empty-block ( block quot: ( block -- ) -- )
2008-12-03 09:46:16 -05:00
[ dup empty-block? [ drop ] ] dip if ; inline
2007-09-20 18:09:08 -04:00
2008-06-08 16:32:55 -04:00
: (<block) ( block -- ) pprinter-stack get push ;
2007-09-20 18:09:08 -04:00
2008-06-08 16:32:55 -04:00
: <block ( -- ) f <block> (<block) ;
2007-09-20 18:09:08 -04:00
: <object ( obj -- ) presented associate <block> (<block) ;
! Text section
TUPLE: text-section < section string ;
2007-09-20 18:09:08 -04:00
: <text> ( string style -- text )
2015-07-17 19:42:20 -04:00
over length 1 + text-section new-section
2008-04-04 05:33:35 -04:00
swap >>style
swap >>string ;
2007-09-20 18:09:08 -04:00
M: text-section short-section string>> write ;
2007-09-20 18:09:08 -04:00
M: text-section long-section short-section ;
2007-09-20 18:09:08 -04:00
: styled-text ( string style -- ) <text> add-section ;
: text ( string -- ) f styled-text ;
2007-09-20 18:09:08 -04:00
! Inset section
2008-04-04 05:33:35 -04:00
TUPLE: inset < block narrow? ;
2007-09-20 18:09:08 -04:00
: <inset> ( narrow? -- block )
inset new-block
2008-04-04 05:33:35 -04:00
2 >>overhang
swap >>narrow? ;
2007-09-20 18:09:08 -04:00
M: inset long-section
2008-04-04 05:33:35 -04:00
dup narrow?>> [
2007-09-20 18:09:08 -04:00
[ <fresh-line ] pprint-sections
] [
2008-04-04 05:33:35 -04:00
call-next-method
2007-09-20 18:09:08 -04:00
] if ;
M: inset indent-section? drop t ;
M: inset newline-after? drop t ;
: <inset ( narrow? -- ) <inset> (<block) ;
! Flow section
2008-04-04 05:33:35 -04:00
TUPLE: flow < block ;
2007-09-20 18:09:08 -04:00
: <flow> ( -- block )
flow new-block ;
2007-09-20 18:09:08 -04:00
M: flow short-section? ( section -- ? )
2015-09-08 19:15:10 -04:00
! If we can make room for this entire block by inserting
! a newline, do it; otherwise, don't bother, print it as
! a short section
{
[ section-fits? ]
[ [ end>> 1 - ] [ start>> ] bi - text-fits? not ]
} 1|| ;
2007-09-20 18:09:08 -04:00
: <flow ( -- ) <flow> (<block) ;
! Colon definition section
2008-04-04 05:33:35 -04:00
TUPLE: colon < block ;
2007-09-20 18:09:08 -04:00
: <colon> ( -- block )
colon new-block ;
2007-09-20 18:09:08 -04:00
M: colon long-section short-section ;
M: colon indent-section? drop t ;
M: colon unindent-first-line? drop t ;
: <colon ( -- ) <colon> (<block) ;
: save-end-position ( block -- )
2008-04-04 05:33:35 -04:00
position get >>end drop ;
2007-09-20 18:09:08 -04:00
: block> ( -- )
2014-12-11 10:52:14 -05:00
pprinter-stack get pop [
[ save-end-position ] [ add-section ] bi
] unless-empty-block ;
2007-09-20 18:09:08 -04:00
: do-pprint ( block -- )
<pprinter> pprinter [
2007-09-20 18:09:08 -04:00
[
2008-04-04 05:33:35 -04:00
dup style>> [
[
short-section
2008-05-07 08:49:29 -04:00
] curry with-return
] with-nesting
2014-12-11 10:52:14 -05:00
] unless-empty-block
] with-variable ;
2007-09-20 18:09:08 -04:00
! Long section layout algorithm
: chop-break ( seq -- seq )
2014-12-11 11:02:45 -05:00
[ dup last line-break? ] [ but-last-slice ] while ;
2007-09-20 18:09:08 -04:00
SYMBOL: prev
SYMBOL: next
2008-06-08 16:32:55 -04:00
: split-groups ( ? -- ) [ t , ] when ;
2007-09-20 18:09:08 -04:00
: split-before ( section -- )
{
2014-12-11 11:02:45 -05:00
[ start-group?>> prev get [ end-group?>> and ] when* ]
[ flow? prev get flow? not and ]
} 1|| split-groups ;
2007-09-20 18:09:08 -04:00
: split-after ( section -- )
[ end-group?>> ] [ f ] if* split-groups ;
2007-09-20 18:09:08 -04:00
: group-flow ( seq -- newseq )
[
2010-01-14 10:10:13 -05:00
dup length iota [
2dup 1 - swap ?nth prev namespaces:set
2dup 1 + swap ?nth next namespaces:set
2007-09-20 18:09:08 -04:00
swap nth dup split-before dup , split-after
2008-01-09 17:36:30 -05:00
] with each
2008-05-14 00:36:55 -04:00
] { } make { t } split harvest ;
2007-09-20 18:09:08 -04:00
: break-group? ( seq -- ? )
{ [ first section-fits? ] [ last section-fits? not ] } 1&& ;
2007-09-20 18:09:08 -04:00
: ?break-group ( seq -- )
dup break-group? [ first <fresh-line ] [ drop ] if ;
M: block long-section ( block -- )
[
2008-04-04 05:33:35 -04:00
sections>> chop-break group-flow [
2007-09-20 18:09:08 -04:00
dup ?break-group [
dup line-break? [
2007-09-20 18:09:08 -04:00
do-break
] [
2008-04-04 05:33:35 -04:00
[ advance ] [ pprint-section ] bi
2007-09-20 18:09:08 -04:00
] if
] each
] each
2014-12-11 10:52:14 -05:00
] unless-empty-block ;
: pprinter-manifest ( -- manifest )
<manifest>
2014-12-11 10:52:14 -05:00
pprinter-use get members V{ } like >>search-vocabs
pprinter-in get >>current-vocab ;
: make-pprint ( obj quot manifest? -- block manifest/f )
[
0 position namespaces:set
HS{ } clone pprinter-use namespaces:set
V{ } clone recursion-check namespaces:set
V{ } clone pprinter-stack namespaces:set
2014-12-11 10:52:14 -05:00
[ over <object call pprinter-block ] dip
[ pprinter-manifest ] [ f ] if
2014-12-11 10:52:14 -05:00
] with-scope ; inline
: with-pprint ( obj quot -- )
f make-pprint drop do-pprint ; inline