1 ! Copyright (C) 2003, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays byte-arrays byte-vectors generic
4 hashtables io assocs kernel math namespaces sequences strings
5 sbufs io.styles vectors words prettyprint.config
6 prettyprint.sections quotations io io.files math.parser effects
7 classes.tuple math.order classes.tuple.private classes
9 IN: prettyprint.backend
11 GENERIC: pprint* ( obj -- )
13 M: effect pprint* effect>string "(" swap ")" 3append text ;
15 : ?effect-height ( word -- n )
16 stack-effect [ effect-height ] [ 0 ] if* ;
18 : ?start-group ( word -- )
19 ?effect-height 0 > [ start-group ] when ;
21 : ?end-group ( word -- )
22 ?effect-height 0 < [ end-group ] when ;
24 \ >r hard "break-before" set-word-prop
25 \ r> hard "break-after" set-word-prop
28 : word-style ( word -- style )
29 dup "word-style" word-prop >hashtable [
33 [ parsing-word? ] [ delimiter? ] [ t eq? ] tri or or
34 [ bold font-style set ] when
39 : word-name* ( word -- str )
40 name>> "( no name )" or ;
42 : pprint-word ( word -- )
44 dup word-name* swap word-style styled-text ;
46 : pprint-prefix ( word quot -- )
47 <block swap pprint-word call block> ; inline
51 \ POSTPONE: [ pprint-word ] pprint-prefix
54 [ "break-before" word-prop line-break ]
58 [ "break-after" word-prop line-break ]
62 M: real pprint* number>string text ;
64 M: f pprint* drop \ f pprint-word ;
67 : ch>ascii-escape ( ch -- str )
79 : unparse-ch ( ch -- )
80 dup ch>ascii-escape [ "\\" % ] [ ] ?if , ;
82 : do-string-limit ( str -- trimmed )
84 dup length margin get > [
85 margin get 3 - head "..." append
89 : string-style ( obj -- hash )
92 { 0.3 0.3 0.3 1.0 } foreground set
95 : unparse-string ( str prefix suffix -- str )
96 [ >r % do-string-limit [ unparse-ch ] each r> % ] "" make ;
98 : pprint-string ( obj str prefix suffix -- )
99 unparse-string swap string-style styled-text ;
102 dup "\"" "\"" pprint-string ;
105 dup "SBUF\" " "\"" pprint-string ;
108 dup pathname-string "P\" " "\"" pprint-string ;
111 : nesting-limit? ( -- ? )
112 nesting-limit get dup [ pprinter-stack get length < ] when ;
114 : present-text ( str obj -- )
115 presented associate styled-text ;
117 : check-recursion ( obj quot -- )
120 "~" over class name>> "~" 3append
123 over recursion-check get memq? [
124 drop "~circularity~" swap present-text
126 over recursion-check get push
128 recursion-check get pop*
132 : do-length-limit ( seq -- trimmed n/f )
133 length-limit get dup [
135 dup zero? [ 2drop f ] [ >r head r> ] if
138 : pprint-elements ( seq -- )
141 r> [ "~" swap number>string " more~" 3append text ] when* ;
143 GENERIC: pprint-delims ( obj -- start end )
145 M: quotation pprint-delims drop \ [ \ ] ;
146 M: curry pprint-delims drop \ [ \ ] ;
147 M: compose pprint-delims drop \ [ \ ] ;
148 M: array pprint-delims drop \ { \ } ;
149 M: byte-array pprint-delims drop \ B{ \ } ;
150 M: byte-vector pprint-delims drop \ BV{ \ } ;
151 M: vector pprint-delims drop \ V{ \ } ;
152 M: hashtable pprint-delims drop \ H{ \ } ;
153 M: tuple pprint-delims drop \ T{ \ } ;
154 M: wrapper pprint-delims drop \ W{ \ } ;
155 M: callstack pprint-delims drop \ CS{ \ } ;
157 GENERIC: >pprint-sequence ( obj -- seq )
159 M: object >pprint-sequence ;
161 M: vector >pprint-sequence ;
162 M: byte-vector >pprint-sequence ;
163 M: curry >pprint-sequence ;
164 M: compose >pprint-sequence ;
165 M: hashtable >pprint-sequence >alist ;
166 M: tuple >pprint-sequence tuple>array ;
167 M: wrapper >pprint-sequence wrapped>> 1array ;
168 M: callstack >pprint-sequence callstack>array ;
170 GENERIC: pprint-narrow? ( obj -- ? )
172 M: object pprint-narrow? drop f ;
174 M: array pprint-narrow? drop t ;
175 M: vector pprint-narrow? drop t ;
176 M: hashtable pprint-narrow? drop t ;
177 M: tuple pprint-narrow? drop t ;
179 : pprint-object ( obj -- )
182 dup pprint-delims >r pprint-word
183 dup pprint-narrow? <inset
184 >pprint-sequence pprint-elements
185 block> r> pprint-word block>
188 M: object pprint* pprint-object ;
191 dup quot>> callable? [ pprint-object ] [
192 "( invalid curry )" swap present-text
196 dup [ first>> callable? ] [ second>> callable? ] bi and
198 "( invalid compose )" swap present-text
202 dup wrapped>> word? [
203 <block \ \ pprint-word wrapped>> pprint-word block>
208 M: tuple-layout pprint*
209 "( tuple layout )" swap present-text ;