1 ! :folding=indent:collapseFolds=1:
5 ! Copyright (C) 2003, 2004 Slava Pestov.
7 ! Redistribution and use in source and binary forms, with or without
8 ! modification, are permitted provided that the following conditions are met:
10 ! 1. Redistributions of source code must retain the above copyright notice,
11 ! this list of conditions and the following disclaimer.
13 ! 2. Redistributions in binary form must reproduce the above copyright notice,
14 ! this list of conditions and the following disclaimer in the documentation
15 ! and/or other materials provided with the distribution.
17 ! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 ! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 ! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 ! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 ! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 ! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 ! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 ! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 ! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 ! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 #! Change this to suit your tastes.
51 : prettyprint-indent ( indent -- )
52 #! Print the given number of spaces.
55 : prettyprint-newline ( indent -- )
56 "\n" write prettyprint-indent ;
58 : prettyprint-space ( -- )
61 : newline-after? ( obj -- ? )
64 ! Real definition follows
67 : prettyprint-element ( indent obj -- indent )
68 dup >r prettyprint* r> newline-after? [
69 dup prettyprint-newline
74 : <prettyprint ( indent -- indent )
76 "prettyprint-single-line" get [
79 dup prettyprint-newline
82 : prettyprint> ( indent -- indent )
84 "prettyprint-single-line" get [
85 dup prettyprint-newline
88 : prettyprint-[ ( indent -- indent )
89 "[" write <prettyprint ;
91 : prettyprint-] ( indent -- indent )
92 prettyprint> "]" write ;
94 : prettyprint-list ( indent list -- indent )
95 #! Pretty-print a list, without [ and ].
96 [ prettyprint-element ] each ;
98 : prettyprint-[] ( indent list -- indent )
99 swap prettyprint-[ swap prettyprint-list prettyprint-] ;
101 : prettyprint-{ ( indent -- indent )
102 "{" write <prettyprint ;
104 : prettyprint-} ( indent -- indent )
105 prettyprint> "}" write ;
107 : prettyprint-vector ( indent list -- indent )
108 #! Pretty-print a vector, without { and }.
109 [ prettyprint-element ] vector-each ;
111 : prettyprint-{} ( indent list -- indent )
112 swap prettyprint-{ swap prettyprint-vector prettyprint-} ;
114 : trim-newline ( str -- str )
115 dup ends-with-newline? dup [ nip ] [ drop ] ifte ;
117 : prettyprint-comment ( comment -- )
118 [ "comments" ] get-style [ trim-newline write-attr ] bind ;
120 : word-link ( word -- link )
123 dup word-vocabulary %
128 : word-attrs ( word -- attrs )
129 dup word-style clone swap
131 swap [ word-link "link" set ] extend
136 : prettyprint-word ( word -- )
137 dup word-attrs [ word-name write-attr ] bind ;
139 : prettyprint-object ( indent obj -- indent )
142 : prettyprint* ( indent obj -- indent )
144 [ f = ] [ prettyprint-object ]
145 [ list? ] [ prettyprint-[] ]
146 [ vector? ] [ prettyprint-{} ]
147 [ comment? ] [ prettyprint-comment ]
148 [ word? ] [ prettyprint-word ]
149 [ drop t ] [ prettyprint-object ]
152 : prettyprint ( obj -- )
153 0 swap prettyprint* drop terpri ;
155 : prettyprint-: ( indent -- indent )
156 ":" write prettyprint-space
159 : prettyprint-; ( indent -- indent )
163 : prettyprint-:; ( indent word list -- indent )
166 prettyprint-word prettyprint-space r>
167 prettyprint-list prettyprint-; ;
171 "prettyprint-single-line" on prettyprint
175 #! Unparse each element on its own line.