1 ! Copyright (C) 2003, 2006 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 IN: prettyprint-internals
4 USING: arrays generic hashtables io kernel math
5 namespaces parser sequences strings styles vectors words
8 GENERIC: pprint* ( obj -- )
11 M: byte-array pprint* drop "( byte array )" text ;
13 : word-style ( word -- style )
16 parsing? [ bold font-style set ] when
19 : pprint-word ( word -- )
20 dup word-name [ "( no name )" ] unless*
21 swap word-style styled-text ;
25 H{ } <flow \ POSTPONE: pprint-word pprint-word block>
30 M: real pprint* number>string text ;
32 M: f pprint* drop \ f pprint-word ;
35 : ch>ascii-escape ( ch -- str )
46 : ch>unicode-escape ( ch -- str )
47 >hex 4 CHAR: 0 pad-left "\\u" swap append ;
49 : unparse-ch ( ch -- )
53 dup ch>ascii-escape [ ] [ ch>unicode-escape ] ?if %
56 : do-string-limit ( str -- trimmed )
58 dup length margin get > [
59 margin get 3 - head "..." append
63 : pprint-string ( str prefix -- )
64 [ % [ unparse-ch ] each CHAR: " , ] "" make
65 do-string-limit text ;
67 M: string pprint* "\"" pprint-string ;
69 M: sbuf pprint* "SBUF\" " pprint-string ;
72 : nesting-limit? ( -- ? )
73 nesting-limit get dup [ pprinter-stack get length < ] when ;
75 : truncated-nesting ( obj str -- )
76 swap presented associate styled-text ;
78 : check-recursion ( obj quot -- )
80 drop "#" truncated-nesting
82 over recursion-check get memq? [
83 drop "&" truncated-nesting
85 over recursion-check get push
87 recursion-check get pop*
91 : length-limit? ( seq -- trimmed ? )
93 [ over length over > [ head t ] [ drop f ] if ]
96 : hilite-style ( -- hash )
98 { background { 0.9 0.9 0.9 1 } }
102 : pprint-hilite ( object n -- )
104 hilite-style <flow pprint* block>
109 : pprint-elements ( seq -- )
110 length-limit? >r dup hilite-quotation get eq? [
111 dup length [ pprint-hilite ] 2each
114 ] if r> [ "..." text ] when ;
116 GENERIC: >pprint-sequence ( obj -- seq start end narrow? )
118 M: complex >pprint-sequence >rect 2array \ C{ \ } f ;
120 M: quotation >pprint-sequence \ [ \ ] f ;
122 M: array >pprint-sequence \ { \ } t ;
124 M: vector >pprint-sequence \ V{ \ } t ;
126 M: hashtable >pprint-sequence hash>alist \ H{ \ } t ;
128 M: tuple >pprint-sequence tuple>array \ T{ \ } t ;
130 M: wrapper >pprint-sequence wrapped 1array \ W{ \ } f ;
132 : pprint-object ( obj -- )
134 >pprint-sequence H{ } <flow
135 rot [ pprint-word ] when*
136 [ H{ } <narrow ] [ H{ } <inset ] if
138 block> [ pprint-word ] when* block>
141 M: object pprint* pprint-object ;
145 \ \ pprint-word wrapped pprint-word