]> gitweb.factorcode.org Git - factor.git/blob - basis/prettyprint/backend/backend.factor
Merge branch 'master' into experimental (untested!)
[factor.git] / basis / prettyprint / backend / backend.factor
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 io.styles
5 vectors words prettyprint.config prettyprint.custom
6 prettyprint.sections quotations io io.files math.parser effects
7 classes.tuple math.order classes.tuple.private classes
8 combinators colors ;
9 IN: prettyprint.backend
10
11 M: effect pprint* effect>string "(" ")" surround text ;
12
13 : ?effect-height ( word -- n )
14     stack-effect [ effect-height ] [ 0 ] if* ;
15
16 : ?start-group ( word -- )
17     ?effect-height 0 > [ start-group ] when ;
18
19 : ?end-group ( word -- )
20     ?effect-height 0 < [ end-group ] when ;
21
22 ! Atoms
23 : word-style ( word -- style )
24     dup "word-style" word-prop >hashtable [
25         [
26             [ presented set ]
27             [
28                 [ parsing-word? ] [ delimiter? ] [ t eq? ] tri or or
29                 [ bold font-style set ] when
30             ] bi
31         ] bind
32     ] keep ;
33
34 : word-name* ( word -- str )
35     name>> "( no name )" or ;
36
37 : pprint-word ( word -- )
38     dup record-vocab
39     dup word-name* swap word-style styled-text ;
40
41 : pprint-prefix ( word quot -- )
42     <block swap pprint-word call block> ; inline
43
44 M: word pprint*
45     dup parsing-word? [
46         \ POSTPONE: [ pprint-word ] pprint-prefix
47     ] [
48         {
49             [ "break-before" word-prop line-break ]
50             [ pprint-word ]
51             [ ?start-group ]
52             [ ?end-group ]
53             [ "break-after" word-prop line-break ]
54         } cleave
55     ] if ;
56
57 M: real pprint* number>string text ;
58
59 M: f pprint* drop \ f pprint-word ;
60
61 ! Strings
62 : ch>ascii-escape ( ch -- str )
63     H{
64         { CHAR: \a CHAR: a  }
65         { CHAR: \e CHAR: e  }
66         { CHAR: \n CHAR: n  }
67         { CHAR: \r CHAR: r  }
68         { CHAR: \t CHAR: t  }
69         { CHAR: \0 CHAR: 0  }
70         { CHAR: \\ CHAR: \\ }
71         { CHAR: \" CHAR: \" }
72     } at ;
73
74 : unparse-ch ( ch -- )
75     dup ch>ascii-escape [ "\\" % ] [ ] ?if , ;
76
77 : do-string-limit ( str -- trimmed )
78     string-limit? get [
79         dup length margin get > [
80             margin get 3 - head "..." append
81         ] when
82     ] when ;
83
84 : string-style ( obj -- hash )
85     [
86         presented set
87         T{ rgba f 0.3 0.3 0.3 1.0 } foreground set
88     ] H{ } make-assoc ;
89
90 : unparse-string ( str prefix suffix -- str )
91     [ [ % do-string-limit [ unparse-ch ] each ] dip % ] "" make ;
92
93 : pprint-string ( obj str prefix suffix -- )
94     unparse-string swap string-style styled-text ;
95
96 M: string pprint*
97     dup "\"" "\"" pprint-string ;
98
99 M: sbuf pprint*
100     dup "SBUF\" " "\"" pprint-string ;
101
102 M: pathname pprint*
103     dup string>> "P\" " "\"" pprint-string ;
104
105 ! Sequences
106 : nesting-limit? ( -- ? )
107     nesting-limit get dup [ pprinter-stack get length < ] when ;
108
109 : present-text ( str obj -- )
110     presented associate styled-text ;
111
112 : check-recursion ( obj quot -- )
113     nesting-limit? [
114         drop
115         "~" over class name>> "~" 3append
116         swap present-text
117     ] [
118         over recursion-check get memq? [
119             drop "~circularity~" swap present-text
120         ] [
121             over recursion-check get push
122             call
123             recursion-check get pop*
124         ] if
125     ] if ; inline
126
127 : tuple>assoc ( tuple -- assoc )
128     [ class all-slots ] [ tuple-slots ] bi zip
129     [ [ initial>> ] dip = not ] assoc-filter
130     [ [ name>> ] dip ] assoc-map ;
131
132 : pprint-slot-value ( name value -- )
133     <flow \ { pprint-word
134     [ text ] [ f <inset pprint* block> ] bi*
135     \ } pprint-word block> ;
136
137 M: tuple pprint*
138     boa-tuples? get [ call-next-method ] [
139         [
140             <flow
141             \ T{ pprint-word
142             dup class pprint-word
143             t <inset
144             tuple>assoc [ pprint-slot-value ] assoc-each
145             block>
146             \ } pprint-word
147             block>
148         ] check-recursion
149     ] if ;
150
151 : do-length-limit ( seq -- trimmed n/f )
152     length-limit get dup [
153         over length over [-]
154         dup zero? [ 2drop f ] [ [ head ] dip ] if
155     ] when ;
156
157 : pprint-elements ( seq -- )
158     do-length-limit
159     [ [ pprint* ] each ] dip
160     [ "~" swap number>string " more~" 3append text ] when* ;
161
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{ \ } ;
172
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 ;
180
181 M: tuple >pprint-sequence
182     [ class ] [ tuple-slots ] bi
183     [ 1array ] [ [ f 2array ] dip append ] if-empty ;
184
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 ;
190
191 M: object pprint-object ( obj -- )
192     [
193         <flow
194         dup pprint-delims [
195             pprint-word
196             dup pprint-narrow? <inset
197             >pprint-sequence pprint-elements
198             block>
199         ] dip pprint-word block>
200     ] check-recursion ;
201
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 ;
207
208 M: wrapper pprint*
209     dup wrapped>> word? [
210         <block \ \ pprint-word wrapped>> pprint-word block>
211     ] [
212         pprint-object
213     ] if ;