1 ! Copyright (C) 2003, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays byte-arrays generic hashtables io assocs
4 kernel math namespaces make sequences strings sbufs vectors
5 words prettyprint.config prettyprint.custom prettyprint.sections
6 quotations io io.pathnames io.styles math.parser effects
7 classes.tuple math.order classes.tuple.private classes
9 IN: prettyprint.backend
11 M: effect pprint* effect>string "(" ")" surround text ;
13 : ?effect-height ( word -- n )
14 stack-effect [ effect-height ] [ 0 ] if* ;
16 : ?start-group ( word -- )
17 ?effect-height 0 > [ start-group ] when ;
19 : ?end-group ( word -- )
20 ?effect-height 0 < [ end-group ] when ;
23 : word-style ( word -- style )
24 dup "word-style" word-prop >hashtable [
28 [ parsing-word? ] [ delimiter? ] [ t eq? ] tri or or
29 [ bold font-style set ] when
34 : word-name* ( word -- str )
35 name>> "( no name )" or ;
37 : pprint-word ( word -- )
39 dup word-name* swap word-style styled-text ;
41 : pprint-prefix ( word quot -- )
42 <block swap pprint-word call block> ; inline
46 \ POSTPONE: [ pprint-word ] pprint-prefix
49 [ "break-before" word-prop line-break ]
53 [ "break-after" word-prop line-break ]
57 M: real pprint* number>string text ;
59 M: f pprint* drop \ f pprint-word ;
62 : ch>ascii-escape ( ch -- str )
74 : unparse-ch ( ch -- )
75 dup ch>ascii-escape [ "\\" % ] [ ] ?if , ;
77 : do-string-limit ( str -- trimmed )
79 dup length margin get > [
80 margin get 3 - head "..." append
84 : string-style ( obj -- hash )
87 T{ rgba f 0.3 0.3 0.3 1.0 } foreground set
90 : unparse-string ( str prefix suffix -- str )
91 [ [ % do-string-limit [ unparse-ch ] each ] dip % ] "" make ;
93 : pprint-string ( obj str prefix suffix -- )
94 unparse-string swap string-style styled-text ;
97 dup "\"" "\"" pprint-string ;
100 dup "SBUF\" " "\"" pprint-string ;
103 dup string>> "P\" " "\"" pprint-string ;
106 : nesting-limit? ( -- ? )
107 nesting-limit get dup [ pprinter-stack get length < ] when ;
109 : present-text ( str obj -- )
110 presented associate styled-text ;
112 : check-recursion ( obj quot -- )
115 "~" over class name>> "~" 3append
118 over recursion-check get memq? [
119 drop "~circularity~" swap present-text
121 over recursion-check get push
123 recursion-check get pop*
127 : tuple>assoc ( tuple -- assoc )
128 [ class all-slots ] [ tuple-slots ] bi zip
129 [ [ initial>> ] dip = not ] assoc-filter
130 [ [ name>> ] dip ] assoc-map ;
132 : pprint-slot-value ( name value -- )
133 <flow \ { pprint-word
134 [ text ] [ f <inset pprint* block> ] bi*
135 \ } pprint-word block> ;
138 boa-tuples? get [ call-next-method ] [
142 dup class pprint-word
144 tuple>assoc [ pprint-slot-value ] assoc-each
151 : do-length-limit ( seq -- trimmed n/f )
152 length-limit get dup [
154 dup zero? [ 2drop f ] [ [ head ] dip ] if
157 : pprint-elements ( seq -- )
159 [ [ pprint* ] each ] dip
160 [ "~" swap number>string " more~" 3append text ] when* ;
162 M: quotation pprint-delims drop \ [ \ ] ;
163 M: curry pprint-delims drop \ [ \ ] ;
164 M: compose pprint-delims drop \ [ \ ] ;
165 M: array pprint-delims drop \ { \ } ;
166 M: byte-array pprint-delims drop \ B{ \ } ;
167 M: vector pprint-delims drop \ V{ \ } ;
168 M: hashtable pprint-delims drop \ H{ \ } ;
169 M: tuple pprint-delims drop \ T{ \ } ;
170 M: wrapper pprint-delims drop \ W{ \ } ;
171 M: callstack pprint-delims drop \ CS{ \ } ;
173 M: object >pprint-sequence ;
174 M: vector >pprint-sequence ;
175 M: curry >pprint-sequence ;
176 M: compose >pprint-sequence ;
177 M: hashtable >pprint-sequence >alist ;
178 M: wrapper >pprint-sequence wrapped>> 1array ;
179 M: callstack >pprint-sequence callstack>array ;
181 M: tuple >pprint-sequence
182 [ class ] [ tuple-slots ] bi
183 [ 1array ] [ [ f 2array ] dip append ] if-empty ;
185 M: object pprint-narrow? drop f ;
186 M: array pprint-narrow? drop t ;
187 M: vector pprint-narrow? drop t ;
188 M: hashtable pprint-narrow? drop t ;
189 M: tuple pprint-narrow? drop t ;
191 M: object pprint-object ( obj -- )
196 dup pprint-narrow? <inset
197 >pprint-sequence pprint-elements
199 ] dip pprint-word block>
202 M: object pprint* pprint-object ;
203 M: vector pprint* pprint-object ;
204 M: hashtable pprint* pprint-object ;
205 M: curry pprint* pprint-object ;
206 M: compose pprint* pprint-object ;
209 dup wrapped>> word? [
210 <block \ \ pprint-word wrapped>> pprint-word block>