factor/library/prettyprint.factor

207 lines
5.3 KiB
Factor
Raw Normal View History

! :folding=indent:collapseFolds=1:
2004-07-16 02:26:21 -04:00
! $Id$
!
! Copyright (C) 2003, 2004 Slava Pestov.
!
! Redistribution and use in source and binary forms, with or without
! modification, are permitted provided that the following conditions are met:
!
! 1. Redistributions of source code must retain the above copyright notice,
! this list of conditions and the following disclaimer.
!
! 2. Redistributions in binary form must reproduce the above copyright notice,
! this list of conditions and the following disclaimer in the documentation
! and/or other materials provided with the distribution.
!
! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
IN: prettyprint
USE: arithmetic
USE: combinators
2004-07-19 16:10:18 -04:00
USE: errors
2004-07-16 02:26:21 -04:00
USE: format
USE: kernel
USE: logic
USE: lists
USE: namespaces
USE: prettyprint
USE: stack
USE: stdio
USE: strings
USE: styles
USE: unparser
2004-07-19 16:10:18 -04:00
USE: vectors
2004-07-16 02:26:21 -04:00
USE: vocabularies
USE: words
: tab-size
#! Change this to suit your tastes.
4 ;
: prettyprint-limit ( -- limit )
#! Avoid infinite loops -- maximum indent, 10 levels.
"prettyprint-limit" get [ 40 ] unless* ;
2004-07-16 02:26:21 -04:00
: prettyprint-indent ( indent -- )
#! Print the given number of spaces.
" " fill write ;
: prettyprint-newline ( indent -- )
"\n" write prettyprint-indent ;
: prettyprint-space ( -- )
" " write ;
: newline-after? ( obj -- ? )
comment? ;
2004-07-16 02:26:21 -04:00
! Real definition follows
DEFER: prettyprint*
: prettyprint-element ( indent obj -- indent )
dup >r prettyprint* r> newline-after? [
dup prettyprint-newline
] [
prettyprint-space
] ifte ;
: <prettyprint ( indent -- indent )
tab-size +
"prettyprint-single-line" get [
prettyprint-space
] [
dup prettyprint-newline
] ifte ;
: prettyprint> ( indent -- indent )
tab-size -
"prettyprint-single-line" get [
dup prettyprint-newline
] unless ;
: check-recursion ( indent obj quot -- )
>r over prettyprint-limit >= [
r> drop drop "#< ... >" write
] [
r> call
] ifte ;
: prettyprint-[ ( indent -- indent )
"[" write <prettyprint ;
: prettyprint-] ( indent -- indent )
prettyprint> "]" write ;
: (prettyprint-list) ( indent list -- indent )
[
uncons >r prettyprint-element r>
dup cons? [
(prettyprint-list)
] [
[
"|" write prettyprint-space prettyprint-element
] when*
] ifte
] when* ;
2004-07-16 02:26:21 -04:00
: prettyprint-list ( indent list -- indent )
#! Pretty-print a list, without [ and ].
[ (prettyprint-list) ] check-recursion ;
2004-07-16 02:26:21 -04:00
: prettyprint-[] ( indent list -- indent )
swap prettyprint-[ swap prettyprint-list prettyprint-] ;
2004-07-19 16:10:18 -04:00
: prettyprint-{ ( indent -- indent )
"{" write <prettyprint ;
2004-07-19 16:10:18 -04:00
: prettyprint-} ( indent -- indent )
prettyprint> "}" write ;
2004-07-19 16:10:18 -04:00
: prettyprint-vector ( indent list -- indent )
#! Pretty-print a vector, without { and }.
[ [ prettyprint-element ] vector-each ] check-recursion ;
2004-07-19 16:10:18 -04:00
: prettyprint-{} ( indent list -- indent )
swap prettyprint-{ swap prettyprint-vector prettyprint-} ;
: trim-newline ( str -- str )
dup ends-with-newline? dup [ nip ] [ drop ] ifte ;
2004-07-16 02:26:21 -04:00
: prettyprint-comment ( comment -- )
[ "comments" ] get-style [ trim-newline write-attr ] bind ;
2004-07-16 02:26:21 -04:00
: word-link ( word -- link )
<%
"vocabularies'" %
dup word-vocabulary %
"'" %
word-name %
%> ;
: word-attrs ( word -- attrs )
dup word-style clone swap
dup defined? [
swap [ word-link "link" set ] extend
] [
drop
] ifte ;
: prettyprint-word ( word -- )
dup word-attrs [ word-name write-attr ] bind ;
2004-07-16 02:26:21 -04:00
: prettyprint-object ( indent obj -- indent )
unparse write ;
2004-07-16 02:26:21 -04:00
: prettyprint* ( indent obj -- indent )
[
[ f = ] [ prettyprint-object ]
[ cons? ] [ prettyprint-[] ]
2004-07-19 16:10:18 -04:00
[ vector? ] [ prettyprint-{} ]
2004-07-16 02:26:21 -04:00
[ comment? ] [ prettyprint-comment ]
[ word? ] [ prettyprint-word ]
[ drop t ] [ prettyprint-object ]
] cond ;
: prettyprint ( obj -- )
0 swap prettyprint* drop terpri ;
2004-07-16 02:26:21 -04:00
: prettyprint-: ( indent -- indent )
":" write prettyprint-space
tab-size + ;
: prettyprint-; ( indent -- indent )
";" write
tab-size - ;
: prettyprint-:; ( indent word list -- indent )
>r
>r prettyprint-: r>
prettyprint-word prettyprint-space r>
2004-07-16 02:26:21 -04:00
prettyprint-list prettyprint-; ;
2004-07-19 16:10:18 -04:00
: . ( obj -- )
<namespace> [
"prettyprint-single-line" on
2004-07-23 01:27:54 -04:00
tab-size 4 * "prettyprint-limit" set
prettyprint
] bind ;
: [.] ( list -- )
#! Unparse each element on its own line.
[ . ] each ;
: .n namestack . ;
: .s datastack . ;
: .r callstack . ;
: .c catchstack . ;