factor/library/prettyprint.factor

230 lines
5.9 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: 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 >= [
2004-08-04 23:09:33 -04:00
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 vector -- indent )
dup vector-length 0 = [
drop "{ }" write
] [
swap prettyprint-{ swap prettyprint-vector prettyprint-}
] ifte ;
2004-07-19 16:10:18 -04:00
: trim-newline ( str -- str )
dup ends-with-newline? dup [ nip ] [ drop ] ifte ;
2004-07-16 02:26:21 -04:00
: prettyprint-comment ( comment -- )
2004-08-21 02:55:37 -04:00
trim-newline [ "comments" ] get-style write-attr ;
2004-07-16 02:26:21 -04:00
: word-link ( word -- link )
<%
"vocabularies'" %
dup word-vocabulary %
"'" %
word-name %
%> ;
: word-attrs ( word -- attrs )
dup defined? [
2004-08-21 02:55:37 -04:00
dup >r word-link "link" r> word-style acons
2004-07-16 02:26:21 -04:00
] [
2004-08-21 02:55:37 -04:00
word-style
2004-07-16 02:26:21 -04:00
] ifte ;
: prettyprint-word ( word -- )
2004-08-21 02:55:37 -04:00
dup word-name swap word-attrs write-attr ;
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
2004-08-17 23:09:16 -04:00
: vocab-link ( vocab -- link )
<% "vocabularies'" % % %> ;
: vocab-attrs ( word -- attrs )
2004-08-21 02:55:37 -04:00
vocab-link "link" default-style acons ;
2004-08-17 23:09:16 -04:00
: prettyprint-vocab ( vocab -- )
2004-08-21 02:55:37 -04:00
dup vocab-attrs write-attr ;
2004-08-17 23:09:16 -04:00
: prettyprint-IN: ( indent word -- indent )
"IN:" write prettyprint-space
word-vocabulary prettyprint-vocab
dup prettyprint-newline ;
2004-07-16 02:26:21 -04:00
: prettyprint-: ( indent -- indent )
":" write prettyprint-space
tab-size + ;
: prettyprint-; ( indent -- indent )
";" write
tab-size - ;
2004-08-17 23:09:16 -04:00
: prettyprint-plist ( word -- )
"parsing" over word-property [ " parsing" write ] when
"inline" over word-property [ " inline" write ] when
drop ;
2004-07-16 02:26:21 -04:00
: prettyprint-:; ( indent word list -- indent )
2004-08-17 23:09:16 -04:00
over >r >r dup
>r prettyprint-IN: prettyprint-: r>
prettyprint-word
native? [ dup prettyprint-newline ] [ prettyprint-space ] ifte
r>
prettyprint-list prettyprint-; r> prettyprint-plist ;
2004-07-19 16:10:18 -04:00
: . ( obj -- )
[
"prettyprint-single-line" on
2004-07-23 01:27:54 -04:00
tab-size 4 * "prettyprint-limit" set
prettyprint
] with-scope ;
: [.] ( list -- )
#! Unparse each element on its own line.
[ . ] each ;
: .n namestack . ;
: .s datastack . ;
: .r callstack . ;
: .c catchstack . ;