pp.stk 9.79 KB
Newer Older
eg's avatar
eg committed
1 2 3
;;;;
;;;; pp.stk	-- Pretty Printer
;;;; 
Erick Gallesio's avatar
.  
Erick Gallesio committed
4
;;;; Copyright  2002-2007 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
eg's avatar
eg committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
;;;; 
;;;; 
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2 of the License, or
;;;; (at your option) any later version.
;;;; 
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;; 
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program; if not, write to the Free Software
;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
;;;; USA.
;;;;
;;;; This code is an adaptation of the Marc Feeley pretty printer.
;;;;		Copyright (c) 1991, Marc Feeley
;;;; 
;;;;    Creation date: 10-May-2002 16:22 (eg)
Erick Gallesio's avatar
Erick Gallesio committed
26
;;;; Last file update: 30-Apr-2007 00:28 (eg)
eg's avatar
eg committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
;;;;

#|
<doc EXT pretty-print pp
 * (pretty-print sexpr :key port width)
 * (pp sexpr :key port width)
 *
 * This function tries to obtain a pretty-printed representation of |sexpr|.
 * The pretty-printed form is written on |port| with lines which are no
 * more long than |width| characters. If |port| is omitted if defaults to
 * the current error port. As a special convention, if |port| is |t|,
 * output goes to the current output port and if |port| is |f|, the output
 * is returned as a string by |pretty-print|.
 * Note that |pp| is another name for |pretty-print|.
 * 
doc>
|#

(define-module pretty-print

  (define (reverse-string-append l)
    (define (rev-string-append l i)
      (if (pair? l)
	  (let* ((str (car l))
		 (len (string-length str))
		 (result (rev-string-append (cdr l) (+ i len))))
	    (let loop ((j 0) (k (- (- (string-length result) i) len)))
	      (if (< j len)
		  (begin
		    (string-set! result k (string-ref str j))
		    (loop (+ j 1) (+ k 1)))
		  result)))
	  (make-string i)))
    (rev-string-append l 0))

  (define genwrite:newline-str (make-string 1 #\newline))

  (define (generic-write obj display? width output)

    (define (read-macro? l)
      (define (length1? l) (and (pair? l) (null? (cdr l))))
      (let ((head (car l)) (tail (cdr l)))
	(case head
	  ((quote quasiquote unquote unquote-splicing) (length1? tail))
	  (else                                        #f))))

    (define (read-macro-body l)
      (cadr l))

    (define (read-macro-prefix l)
      (let ((head (car l)) (tail (cdr l)))
	(case head
	  ((quote)            "'")
	  ((quasiquote)       "`")
	  ((unquote)          ",")
	  ((unquote-splicing) ",@"))))

    (define (out str col)
      (and col (output str) (+ col (string-length str))))

    (define (wr obj col)

      (define (wr-expr expr col)
	(if (read-macro? expr)
	  (wr (read-macro-body expr) (out (read-macro-prefix expr) col))
	  (wr-lst expr col)))

      (define (wr-lst l col)
	(if (pair? l)
	    (let loop ((l (cdr l))
		       (col (and col (wr (car l) (out "(" col)))))
	      (cond ((not col) col)
		    ((pair? l)
		     (loop (cdr l) (wr (car l) (out " " col))))
		    ((null? l) (out ")" col))
		    (else      (out ")" (wr l (out " . " col))))))
	    (out "()" col)))

      (cond ((pair? obj)        (wr-expr obj col))
	    ((null? obj)        (wr-lst obj col))
	    ((vector? obj)      (wr-lst (vector->list obj) (out "#" col)))
	    ((boolean? obj)     (out (if obj "#t" "#f") col))
	    ((number? obj)      (out (number->string obj) col))
	    ((symbol? obj)      (out (format "~S" obj) col))
	    ((procedure? obj)   (out "#[procedure]" col))
	    ((string? obj)      (if display?
				  (out obj col)
				  (let loop ((i 0) (j 0) (col (out "\"" col)))
				    (if (and col (< j (string-length obj)))
				      (let ((c (string-ref obj j)))
					(if (or (char=? c #\\)
						(char=? c #\"))
					  (loop j
						(+ j 1)
						(out "\\"
						     (out (substring obj i j)
							  col)))
					  (loop i (+ j 1) col)))
				      (out "\""
					   (out (substring obj i j) col))))))
	    ((char? obj)        (if display?
				  (out (make-string 1 obj) col)
				  (out (case obj
					 ((#\space)   "space")
					 ((#\newline) "newline")
					 (else        (make-string 1 obj)))
				       (out "#\\" col))))
	    ((input-port? obj)  (out "#[input-port]" col))
	    ((output-port? obj) (out "#[output-port]" col))
Erick Gallesio's avatar
Erick Gallesio committed
136
	    ((eof-object? obj)  (out "#eof" col))
eg's avatar
eg committed
137 138
	    ;; STk types
	    ((keyword? obj)     (out (string-append ":" (keyword->string obj)) col))
Erick Gallesio's avatar
Erick Gallesio committed
139 140
	    ((eq? obj (void))   (out "#void" col))
	    ((hash-table? obj)  (out "#[hash-table]" col))
eg's avatar
eg committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
	    (else               (out "#[unknown]" col))))

    (define (pp obj col)

      (define (spaces n col)
	(if (> n 0)
	  (if (> n 7)
	    (spaces (- n 8) (out "        " col))
	    (out (substring "        " 0 n) col))
	  col))

      (define (indent to col)
	(and col
	     (if (< to col)
	       (and (out genwrite:newline-str col) (spaces to 0))
	       (spaces (- to col) col))))

      (define (pr obj col extra pp-pair)
	(if (or (pair? obj) (vector? obj)) ; may have to split on multiple lines
	  (let ((result '())
		(left (min (+ (- (- width col) extra) 1) max-expr-width)))
	    (generic-write obj display? #f
	      (lambda (str)
		(set! result (cons str result))
		(set! left (- left (string-length str)))
		(> left 0)))
	    (if (> left 0) ; all can be printed on one line
	      (out (reverse-string-append result) col)
	      (if (pair? obj)
		(pp-pair obj col extra)
		(pp-list (vector->list obj) (out "#" col) extra pp-expr))))
	  (wr obj col)))

      (define (pp-expr expr col extra)
	(if (read-macro? expr)
	  (pr (read-macro-body expr)
	      (out (read-macro-prefix expr) col)
	      extra
	      pp-expr)
	  (let ((head (car expr)))
	    (if (symbol? head)
	      (let ((proc (style head)))
		(if proc
		  (proc expr col extra)
		  (if (> (string-length (format "~S" head))
			 max-call-head-width)
		    (pp-general expr col extra #f #f #f pp-expr)
		    (pp-call expr col extra pp-expr))))
	      (pp-list expr col extra pp-expr)))))

      ; (head item1
      ;       item2
      ;       item3)
      (define (pp-call expr col extra pp-item)
	(let ((col* (wr (car expr) (out "(" col))))
	  (and col
	       (pp-down (cdr expr) col* (+ col* 1) extra pp-item))))

      ; (item1
      ;  item2
      ;  item3)
      (define (pp-list l col extra pp-item)
	(let ((col (out "(" col)))
	  (pp-down l col col extra pp-item)))

      (define (pp-down l col1 col2 extra pp-item)
	(let loop ((l l) (col col1))
	  (and col
	       (cond ((pair? l)
		      (let ((rest (cdr l)))
			(let ((extra (if (null? rest) (+ extra 1) 0)))
			  (loop rest
				(pr (car l) (indent col2 col) extra pp-item)))))
		     ((null? l)
		      (out ")" col))
		     (else
		      (out ")"
			   (pr l
			       (indent col2 (out "." (indent col2 col)))
			       (+ extra 1)
			       pp-item)))))))

      (define (pp-general expr col extra named? pp-1 pp-2 pp-3)

	(define (tail1 rest col1 col2 col3)
	  (if (and pp-1 (pair? rest))
	    (let* ((val1 (car rest))
		   (rest (cdr rest))
		   (extra (if (null? rest) (+ extra 1) 0)))
	      (tail2 rest col1 (pr val1 (indent col3 col2) extra pp-1) col3))
	    (tail2 rest col1 col2 col3)))

	(define (tail2 rest col1 col2 col3)
	  (if (and pp-2 (pair? rest))
	    (let* ((val1 (car rest))
		   (rest (cdr rest))
		   (extra (if (null? rest) (+ extra 1) 0)))
	      (tail3 rest col1 (pr val1 (indent col3 col2) extra pp-2)))
	    (tail3 rest col1 col2)))

	(define (tail3 rest col1 col2)
	  (pp-down rest col2 col1 extra pp-3))

	(let* ((head (car expr))
	       (rest (cdr expr))
	       (col* (wr head (out "(" col))))
	  (if (and named? (pair? rest))
	    (let* ((name (car rest))
		   (rest (cdr rest))
		   (col** (wr name (out " " col*))))
	      (tail1 rest (+ col indent-general) col** (+ col** 1)))
	    (tail1 rest (+ col indent-general) col* (+ col* 1)))))

      (define (pp-expr-list l col extra)
	(pp-list l col extra pp-expr))

      (define (pp-LAMBDA expr col extra)
	(pp-general expr col extra #f pp-expr-list #f pp-expr))

      (define (pp-IF expr col extra)
	(pp-general expr col extra #f pp-expr #f pp-expr))

      (define (pp-COND expr col extra)
	(pp-call expr col extra pp-expr-list))

      (define (pp-CASE expr col extra)
	(pp-general expr col extra #f pp-expr #f pp-expr-list))

      (define (pp-AND expr col extra)
	(pp-call expr col extra pp-expr))

      (define (pp-LET expr col extra)
	(let* ((rest (cdr expr))
	       (named? (and (pair? rest) (symbol? (car rest)))))
	  (pp-general expr col extra named? pp-expr-list #f pp-expr)))

      (define (pp-BEGIN expr col extra)
	(pp-general expr col extra #f #f #f pp-expr))

      (define (pp-DO expr col extra)
	(pp-general expr col extra #f pp-expr-list pp-expr-list pp-expr))

      ; define formatting style (change these to suit your style)

      (define indent-general 2)

      (define max-call-head-width 5)

      (define max-expr-width 50)

      (define (style head)
	(case head
	  ((lambda let* letrec define) pp-LAMBDA)
	  ((if set! when unless)       pp-IF)
	  ((cond)                      pp-COND)
	  ((case)                      pp-CASE)
	  ((and or)                    pp-AND)
	  ((let)                       pp-LET)
	  ((begin)                     pp-BEGIN)
	  ((do)                        pp-DO)
	  (else                        #f)))

      (pr obj col 0 pp-expr))

    (if width
      (out genwrite:newline-str (pp obj 0))
      (wr obj 0))))


;;;
;;; In module STklos
;;; 
(define (pp obj :key (port (current-error-port)) (width 79))
  (let ((p  (case port
Erick Gallesio's avatar
.  
Erick Gallesio committed
315
	      ((#t) (current-output-port))
eg's avatar
eg committed
316 317 318 319 320 321 322 323 324 325 326 327
	      ((#f) (open-output-string))
	      (else port)))
	(gw (in-module pretty-print generic-write)))
    (gw obj #f width (lambda (s) (display s p) #t))
    
    (if port
	(void)
	(get-output-string p))))

(define pretty-print pp)

(provide "pp")