1 ;;;; functions to implement FORMAT and FORMATTER
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!FORMAT")
16 (defun format (destination control-string &rest format-arguments)
18 "Provides various facilities for formatting output.
19 CONTROL-STRING contains a string to be output, possibly with embedded
20 directives, which are flagged with the escape character \"~\". Directives
21 generally expand into additional text to be output, usually consuming one
22 or more of the FORMAT-ARGUMENTS in the process. A few useful directives
24 ~A or ~nA Prints one argument as if by PRINC
25 ~S or ~nS Prints one argument as if by PRIN1
26 ~D or ~nD Prints one argument as a decimal integer
29 where n is the width of the field in which the object is printed.
31 DESTINATION controls where the result will go. If DESTINATION is T, then
32 the output is sent to the standard output stream. If it is NIL, then the
33 output is returned in a string as the value of the call. Otherwise,
34 DESTINATION must be a stream to which the output will be sent.
36 Example: (FORMAT NIL \"The answer is ~D.\" 10) => \"The answer is 10.\"
38 FORMAT has many additional capabilities not described here. Consult the
40 (etypecase destination
42 (with-output-to-string (stream)
43 (%format stream control-string format-arguments)))
45 (with-output-to-string (stream destination)
46 (%format stream control-string format-arguments)))
48 (%format *standard-output* control-string format-arguments)
51 (%format destination control-string format-arguments)
54 (defun %format (stream string-or-fun orig-args &optional (args orig-args))
55 (if (functionp string-or-fun)
56 (apply string-or-fun stream args)
58 (let* ((string (etypecase string-or-fun
62 (coerce string-or-fun 'simple-string))))
63 (*default-format-error-control-string* string)
64 (*logical-block-popper* nil))
65 (interpret-directive-list stream (tokenize-control-string string)
68 (defun interpret-directive-list (stream directives orig-args args)
70 (let ((directive (car directives)))
73 (write-string directive stream)
74 (interpret-directive-list stream (cdr directives) orig-args args))
76 (multiple-value-bind (new-directives new-args)
77 (let* ((character (format-directive-character directive))
81 (svref *format-directive-interpreters*
82 (char-code character)))
84 (*default-format-error-offset*
85 (1- (format-directive-end directive))))
88 :complaint "unknown format directive ~@[(character: ~A)~]"
89 :args (list (char-name character))))
90 (multiple-value-bind (new-directives new-args)
91 (funcall function stream directive
92 (cdr directives) orig-args args)
93 (values new-directives new-args)))
94 (interpret-directive-list stream new-directives
95 orig-args new-args)))))
98 ;;;; FORMAT directive definition macros and runtime support
100 (eval-when (:compile-toplevel :execute)
102 ;;; This macro is used to extract the next argument from the current arg list.
103 ;;; This is the version used by format directive interpreters.
104 (sb!xc:defmacro next-arg (&optional offset)
108 :complaint "no more arguments"
110 `(:offset ,offset))))
111 (when *logical-block-popper*
112 (funcall *logical-block-popper*))
115 (sb!xc:defmacro def-complex-format-interpreter (char lambda-list &body body)
118 "~:@(~:C~)-FORMAT-DIRECTIVE-INTERPRETER"
121 (directives (if lambda-list (car (last lambda-list)) (gensym))))
123 (defun ,defun-name (stream ,directive ,directives orig-args args)
124 (declare (ignorable stream orig-args args))
126 `((let ,(mapcar (lambda (var)
128 (,(symbolicate "FORMAT-DIRECTIVE-" var)
130 (butlast lambda-list))
131 (values (progn ,@body) args)))
132 `((declare (ignore ,directive ,directives))
134 (%set-format-directive-interpreter ,char #',defun-name))))
136 (sb!xc:defmacro def-format-interpreter (char lambda-list &body body)
137 (let ((directives (gensym)))
138 `(def-complex-format-interpreter ,char (,@lambda-list ,directives)
142 (sb!xc:defmacro interpret-bind-defaults (specs params &body body)
143 (once-only ((params params))
144 (collect ((bindings))
146 (destructuring-bind (var default) spec
147 (bindings `(,var (let* ((param-and-offset (pop ,params))
148 (offset (car param-and-offset))
149 (param (cdr param-and-offset)))
151 (:arg (or (next-arg offset) ,default))
152 (:remaining (length args))
159 "too many parameters, expected no more than ~W"
160 :args (list ,(length specs))
161 :offset (caar ,params)))
166 ;;;; format interpreters and support functions for simple output
168 (defun format-write-field (stream string mincol colinc minpad padchar padleft)
170 (write-string string stream))
172 (write-char padchar stream))
173 ;; As of sbcl-0.6.12.34, we could end up here when someone tries to
174 ;; print e.g. (FORMAT T "~F" "NOTFLOAT"), in which case ANSI says
175 ;; we're supposed to soldier on bravely, and so we have to deal with
176 ;; the unsupplied-MINCOL-and-COLINC case without blowing up.
177 (when (and mincol colinc)
178 (do ((chars (+ (length string) (max minpad 0)) (+ chars colinc)))
181 (write-char padchar stream))))
183 (write-string string stream)))
185 (defun format-princ (stream arg colonp atsignp mincol colinc minpad padchar)
186 (format-write-field stream
187 (if (or arg (not colonp))
188 (princ-to-string arg)
190 mincol colinc minpad padchar atsignp))
192 (def-format-interpreter #\A (colonp atsignp params)
194 (interpret-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
197 (format-princ stream (next-arg) colonp atsignp
198 mincol colinc minpad padchar))
199 (princ (if colonp (or (next-arg) "()") (next-arg)) stream)))
201 (defun format-prin1 (stream arg colonp atsignp mincol colinc minpad padchar)
202 (format-write-field stream
203 (if (or arg (not colonp))
204 (prin1-to-string arg)
206 mincol colinc minpad padchar atsignp))
208 (def-format-interpreter #\S (colonp atsignp params)
210 (interpret-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
213 (format-prin1 stream (next-arg) colonp atsignp
214 mincol colinc minpad padchar)))
216 (let ((arg (next-arg)))
219 (princ "()" stream))))
221 (prin1 (next-arg) stream))))
223 (def-format-interpreter #\C (colonp atsignp params)
224 (interpret-bind-defaults () params
226 (format-print-named-character (next-arg) stream)
228 (prin1 (next-arg) stream)
229 (write-char (next-arg) stream)))))
231 (defun format-print-named-character (char stream)
232 (let* ((name (char-name char)))
234 (write-string (string-capitalize name) stream))
236 (write-char char stream)))))
238 (def-format-interpreter #\W (colonp atsignp params)
239 (interpret-bind-defaults () params
240 (let ((*print-pretty* (or colonp *print-pretty*))
241 (*print-level* (unless atsignp *print-level*))
242 (*print-length* (unless atsignp *print-length*)))
243 (output-object (next-arg) stream))))
245 ;;;; format interpreters and support functions for integer output
247 ;;; FORMAT-PRINT-NUMBER does most of the work for the numeric printing
248 ;;; directives. The parameters are interpreted as defined for ~D.
249 (defun format-print-integer (stream number print-commas-p print-sign-p
250 radix mincol padchar commachar commainterval)
251 (let ((*print-base* radix)
253 (if (integerp number)
254 (let* ((text (princ-to-string (abs number)))
255 (commaed (if print-commas-p
256 (format-add-commas text commachar commainterval)
258 (signed (cond ((minusp number)
259 (concatenate 'string "-" commaed))
261 (concatenate 'string "+" commaed))
263 ;; colinc = 1, minpad = 0, padleft = t
264 (format-write-field stream signed mincol 1 0 padchar t))
265 (princ number stream))))
267 (defun format-add-commas (string commachar commainterval)
268 (let ((length (length string)))
269 (multiple-value-bind (commas extra) (truncate (1- length) commainterval)
270 (let ((new-string (make-string (+ length commas)))
271 (first-comma (1+ extra)))
272 (replace new-string string :end1 first-comma :end2 first-comma)
273 (do ((src first-comma (+ src commainterval))
274 (dst first-comma (+ dst commainterval 1)))
276 (setf (schar new-string dst) commachar)
277 (replace new-string string :start1 (1+ dst)
278 :start2 src :end2 (+ src commainterval)))
281 ;;; FIXME: This is only needed in this file, could be defined with
282 ;;; SB!XC:DEFMACRO inside EVAL-WHEN
283 (defmacro interpret-format-integer (base)
284 `(if (or colonp atsignp params)
285 (interpret-bind-defaults
286 ((mincol 0) (padchar #\space) (commachar #\,) (commainterval 3))
288 (format-print-integer stream (next-arg) colonp atsignp ,base mincol
289 padchar commachar commainterval))
290 (write (next-arg) :stream stream :base ,base :radix nil :escape nil)))
292 (def-format-interpreter #\D (colonp atsignp params)
293 (interpret-format-integer 10))
295 (def-format-interpreter #\B (colonp atsignp params)
296 (interpret-format-integer 2))
298 (def-format-interpreter #\O (colonp atsignp params)
299 (interpret-format-integer 8))
301 (def-format-interpreter #\X (colonp atsignp params)
302 (interpret-format-integer 16))
304 (def-format-interpreter #\R (colonp atsignp params)
305 (interpret-bind-defaults
306 ((base nil) (mincol 0) (padchar #\space) (commachar #\,)
309 (let ((arg (next-arg)))
311 (format-print-integer stream arg colonp atsignp base mincol
312 padchar commachar commainterval)
315 (format-print-old-roman stream arg)
316 (format-print-roman stream arg))
318 (format-print-ordinal stream arg)
319 (format-print-cardinal stream arg)))))))
321 (defparameter *cardinal-ones*
322 #(nil "one" "two" "three" "four" "five" "six" "seven" "eight" "nine"))
324 (defparameter *cardinal-tens*
325 #(nil nil "twenty" "thirty" "forty"
326 "fifty" "sixty" "seventy" "eighty" "ninety"))
328 (defparameter *cardinal-teens*
329 #("ten" "eleven" "twelve" "thirteen" "fourteen" ;;; RAD
330 "fifteen" "sixteen" "seventeen" "eighteen" "nineteen"))
332 (defparameter *cardinal-periods*
333 #("" " thousand" " million" " billion" " trillion" " quadrillion"
334 " quintillion" " sextillion" " septillion" " octillion" " nonillion"
335 " decillion" " undecillion" " duodecillion" " tredecillion"
336 " quattuordecillion" " quindecillion" " sexdecillion" " septendecillion"
337 " octodecillion" " novemdecillion" " vigintillion"))
339 (defparameter *ordinal-ones*
340 #(nil "first" "second" "third" "fourth"
341 "fifth" "sixth" "seventh" "eighth" "ninth"))
343 (defparameter *ordinal-tens*
344 #(nil "tenth" "twentieth" "thirtieth" "fortieth"
345 "fiftieth" "sixtieth" "seventieth" "eightieth" "ninetieth"))
347 (defun format-print-small-cardinal (stream n)
348 (multiple-value-bind (hundreds rem) (truncate n 100)
349 (when (plusp hundreds)
350 (write-string (svref *cardinal-ones* hundreds) stream)
351 (write-string " hundred" stream)
353 (write-char #\space stream)))
355 (multiple-value-bind (tens ones) (truncate rem 10)
357 (write-string (svref *cardinal-tens* tens) stream)
359 (write-char #\- stream)
360 (write-string (svref *cardinal-ones* ones) stream)))
362 (write-string (svref *cardinal-teens* ones) stream))
364 (write-string (svref *cardinal-ones* ones) stream)))))))
366 (defun format-print-cardinal (stream n)
368 (write-string "negative " stream)
369 (format-print-cardinal-aux stream (- n) 0 n))
371 (write-string "zero" stream))
373 (format-print-cardinal-aux stream n 0 n))))
375 (defun format-print-cardinal-aux (stream n period err)
376 (multiple-value-bind (beyond here) (truncate n 1000)
377 (unless (<= period 20)
378 (error "number too large to print in English: ~:D" err))
379 (unless (zerop beyond)
380 (format-print-cardinal-aux stream beyond (1+ period) err))
382 (unless (zerop beyond)
383 (write-char #\space stream))
384 (format-print-small-cardinal stream here)
385 (write-string (svref *cardinal-periods* period) stream))))
387 (defun format-print-ordinal (stream n)
389 (write-string "negative " stream))
390 (let ((number (abs n)))
391 (multiple-value-bind (top bot) (truncate number 100)
393 (format-print-cardinal stream (- number bot)))
394 (when (and (plusp top) (plusp bot))
395 (write-char #\space stream))
396 (multiple-value-bind (tens ones) (truncate bot 10)
397 (cond ((= bot 12) (write-string "twelfth" stream))
399 (write-string (svref *cardinal-teens* ones) stream);;;RAD
400 (write-string "th" stream))
401 ((and (zerop tens) (plusp ones))
402 (write-string (svref *ordinal-ones* ones) stream))
403 ((and (zerop ones)(plusp tens))
404 (write-string (svref *ordinal-tens* tens) stream))
406 (write-string (svref *cardinal-tens* tens) stream)
407 (write-char #\- stream)
408 (write-string (svref *ordinal-ones* ones) stream))
410 (write-string "th" stream))
412 (write-string "zeroth" stream)))))))
414 ;;; Print Roman numerals
416 (defun format-print-old-roman (stream n)
418 (error "Number too large to print in old Roman numerals: ~:D" n))
419 (do ((char-list '(#\D #\C #\L #\X #\V #\I) (cdr char-list))
420 (val-list '(500 100 50 10 5 1) (cdr val-list))
421 (cur-char #\M (car char-list))
422 (cur-val 1000 (car val-list))
423 (start n (do ((i start (progn
424 (write-char cur-char stream)
429 (defun format-print-roman (stream n)
431 (error "Number too large to print in Roman numerals: ~:D" n))
432 (do ((char-list '(#\D #\C #\L #\X #\V #\I) (cdr char-list))
433 (val-list '(500 100 50 10 5 1) (cdr val-list))
434 (sub-chars '(#\C #\X #\X #\I #\I) (cdr sub-chars))
435 (sub-val '(100 10 10 1 1 0) (cdr sub-val))
436 (cur-char #\M (car char-list))
437 (cur-val 1000 (car val-list))
438 (cur-sub-char #\C (car sub-chars))
439 (cur-sub-val 100 (car sub-val))
440 (start n (do ((i start (progn
441 (write-char cur-char stream)
444 (cond ((<= (- cur-val cur-sub-val) i)
445 (write-char cur-sub-char stream)
446 (write-char cur-char stream)
447 (- i (- cur-val cur-sub-val)))
453 (def-format-interpreter #\P (colonp atsignp params)
454 (interpret-bind-defaults () params
455 (let ((arg (if colonp
456 (if (eq orig-args args)
458 :complaint "no previous argument")
459 (do ((arg-ptr orig-args (cdr arg-ptr)))
460 ((eq (cdr arg-ptr) args)
464 (write-string (if (eql arg 1) "y" "ies") stream)
465 (unless (eql arg 1) (write-char #\s stream))))))
467 ;;;; format interpreters and support functions for floating point output
469 (defun decimal-string (n)
470 (write-to-string n :base 10 :radix nil :escape nil))
472 (def-format-interpreter #\F (colonp atsignp params)
476 "cannot specify the colon modifier with this directive"))
477 (interpret-bind-defaults ((w nil) (d nil) (k nil) (ovf nil) (pad #\space))
479 (format-fixed stream (next-arg) w d k ovf pad atsignp)))
481 (defun format-fixed (stream number w d k ovf pad atsign)
484 (format-fixed-aux stream number w d k ovf pad atsign)
485 (if (rationalp number)
486 (format-fixed-aux stream
487 (coerce number 'single-float)
488 w d k ovf pad atsign)
489 (format-write-field stream
490 (decimal-string number)
492 (format-princ stream number nil nil w 1 0 pad)))
494 ;;; We return true if we overflowed, so that ~G can output the overflow char
495 ;;; instead of spaces.
496 (defun format-fixed-aux (stream number w d k ovf pad atsign)
497 (declare (type float number))
499 ((and (floatp number)
500 (or (float-infinity-p number)
501 (float-nan-p number)))
502 (prin1 number stream)
506 (when (and w (or atsign (minusp (float-sign number))))
508 (multiple-value-bind (str len lpoint tpoint)
509 (sb!impl::flonum-to-string (abs number) spaceleft d k)
510 ;;if caller specifically requested no fraction digits, suppress the
511 ;;optional trailing zero
512 (when (and d (zerop d)) (setq tpoint nil))
515 ;;optional leading zero
517 (if (or (> spaceleft 0) tpoint) ;force at least one digit
520 ;;optional trailing zero
525 (cond ((and w (< spaceleft 0) ovf)
526 ;;field width overflow
527 (dotimes (i w) (write-char ovf stream))
530 (when w (dotimes (i spaceleft) (write-char pad stream)))
531 (if (minusp (float-sign number))
532 (write-char #\- stream)
533 (if atsign (write-char #\+ stream)))
534 (when lpoint (write-char #\0 stream))
535 (write-string str stream)
536 (when tpoint (write-char #\0 stream))
539 (def-format-interpreter #\E (colonp atsignp params)
543 "cannot specify the colon modifier with this directive"))
544 (interpret-bind-defaults
545 ((w nil) (d nil) (e nil) (k 1) (ovf nil) (pad #\space) (mark nil))
547 (format-exponential stream (next-arg) w d e k ovf pad mark atsignp)))
549 (defun format-exponential (stream number w d e k ovf pad marker atsign)
552 (format-exp-aux stream number w d e k ovf pad marker atsign)
553 (if (rationalp number)
554 (format-exp-aux stream
555 (coerce number 'single-float)
556 w d e k ovf pad marker atsign)
557 (format-write-field stream
558 (decimal-string number)
560 (format-princ stream number nil nil w 1 0 pad)))
562 (defun format-exponent-marker (number)
563 (if (typep number *read-default-float-format*)
571 ;;; Here we prevent the scale factor from shifting all significance out of
572 ;;; a number to the right. We allow insignificant zeroes to be shifted in
573 ;;; to the left right, athough it is an error to specify k and d such that this
574 ;;; occurs. Perhaps we should detect both these condtions and flag them as
575 ;;; errors. As for now, we let the user get away with it, and merely guarantee
576 ;;; that at least one significant digit will appear.
578 ;;; Raymond Toy writes: The Hyperspec seems to say that the exponent
579 ;;; marker is always printed. Make it so. Also, the original version
580 ;;; causes errors when printing infinities or NaN's. The Hyperspec is
581 ;;; silent here, so let's just print out infinities and NaN's instead
582 ;;; of causing an error.
583 (defun format-exp-aux (stream number w d e k ovf pad marker atsign)
584 (declare (type float number))
585 (if (or (float-infinity-p number)
586 (float-nan-p number))
587 (prin1 number stream)
588 (multiple-value-bind (num expt) (sb!impl::scale-exponent (abs number))
589 (let* ((expt (- expt k))
590 (estr (decimal-string (abs expt)))
591 (elen (if e (max (length estr) e) (length estr)))
594 (setf spaceleft (- w 2 elen))
595 (when (or atsign (minusp (float-sign number)))
597 (if (and w ovf e (> elen e)) ;exponent overflow
598 (dotimes (i w) (write-char ovf stream))
599 (let* ((fdig (if d (if (plusp k) (1+ (- d k)) d) nil))
600 (fmin (if (minusp k) 1 fdig)))
601 (multiple-value-bind (fstr flen lpoint tpoint)
602 (sb!impl::flonum-to-string num spaceleft fdig k fmin)
603 (when (and d (zerop d)) (setq tpoint nil))
605 (decf spaceleft flen)
606 ;; See CLHS 22.3.3.2. "If the parameter d is
607 ;; omitted, ... [and] if the fraction to be
608 ;; printed is zero then a single zero digit should
609 ;; appear after the decimal point." So we need to
610 ;; subtract one from here because we're going to
611 ;; add an extra 0 digit later. [rtoy]
612 (when (and (zerop number) (null d))
615 (if (or (> spaceleft 0) tpoint)
618 (when (and tpoint (<= spaceleft 0))
620 (cond ((and w (< spaceleft 0) ovf)
621 ;;significand overflow
622 (dotimes (i w) (write-char ovf stream)))
624 (dotimes (i spaceleft) (write-char pad stream)))
625 (if (minusp (float-sign number))
626 (write-char #\- stream)
627 (if atsign (write-char #\+ stream)))
628 (when lpoint (write-char #\0 stream))
629 (write-string fstr stream)
630 (when (and (zerop number) (null d))
631 ;; It's later and we're adding the zero
633 (write-char #\0 stream))
634 (write-char (if marker
636 (format-exponent-marker number))
638 (write-char (if (minusp expt) #\- #\+) stream)
640 ;;zero-fill before exponent if necessary
641 (dotimes (i (- e (length estr)))
642 (write-char #\0 stream)))
643 (write-string estr stream))))))))))
645 (def-format-interpreter #\G (colonp atsignp params)
649 "cannot specify the colon modifier with this directive"))
650 (interpret-bind-defaults
651 ((w nil) (d nil) (e nil) (k nil) (ovf nil) (pad #\space) (mark nil))
653 (format-general stream (next-arg) w d e k ovf pad mark atsignp)))
655 (defun format-general (stream number w d e k ovf pad marker atsign)
658 (format-general-aux stream number w d e k ovf pad marker atsign)
659 (if (rationalp number)
660 (format-general-aux stream
661 (coerce number 'single-float)
662 w d e k ovf pad marker atsign)
663 (format-write-field stream
664 (decimal-string number)
666 (format-princ stream number nil nil w 1 0 pad)))
668 ;;; Raymond Toy writes: same change as for format-exp-aux
669 (defun format-general-aux (stream number w d e k ovf pad marker atsign)
670 (declare (type float number))
671 (if (or (float-infinity-p number)
672 (float-nan-p number))
673 (prin1 number stream)
674 (multiple-value-bind (ignore n) (sb!impl::scale-exponent (abs number))
675 (declare (ignore ignore))
676 ;; KLUDGE: Default d if omitted. The procedure is taken directly from
677 ;; the definition given in the manual, and is not very efficient, since
678 ;; we generate the digits twice. Future maintainers are encouraged to
679 ;; improve on this. -- rtoy?? 1998??
681 (multiple-value-bind (str len)
682 (sb!impl::flonum-to-string (abs number))
683 (declare (ignore str))
684 (let ((q (if (= len 1) 1 (1- len))))
685 (setq d (max q (min n 7))))))
686 (let* ((ee (if e (+ e 2) 4))
687 (ww (if w (- w ee) nil))
690 (let ((char (if (format-fixed-aux stream number ww dd nil
694 (dotimes (i ee) (write-char char stream))))
696 (format-exp-aux stream number w d e (or k 1)
697 ovf pad marker atsign)))))))
699 (def-format-interpreter #\$ (colonp atsignp params)
700 (interpret-bind-defaults ((d 2) (n 1) (w 0) (pad #\space)) params
701 (format-dollars stream (next-arg) d n w pad colonp atsignp)))
703 (defun format-dollars (stream number d n w pad colon atsign)
704 (when (rationalp number)
705 ;; This coercion to SINGLE-FLOAT seems as though it gratuitously
706 ;; loses precision (why not LONG-FLOAT?) but it's the default
707 ;; behavior in the ANSI spec, so in some sense it's the right
708 ;; thing, and at least the user shouldn't be surprised.
709 (setq number (coerce number 'single-float)))
711 (let* ((signstr (if (minusp (float-sign number))
714 (signlen (length signstr)))
715 (multiple-value-bind (str strlen ig2 ig3 pointplace)
716 (sb!impl::flonum-to-string number nil d nil)
717 (declare (ignore ig2 ig3 strlen))
719 (write-string signstr stream))
720 (dotimes (i (- w signlen (max n pointplace) 1 d))
721 (write-char pad stream))
723 (write-string signstr stream))
724 (dotimes (i (- n pointplace))
725 (write-char #\0 stream))
726 (write-string str stream)))
727 (format-write-field stream
728 (decimal-string number)
731 ;;;; FORMAT interpreters and support functions for line/page breaks etc.
733 (def-format-interpreter #\% (colonp atsignp params)
734 (when (or colonp atsignp)
737 "cannot specify either colon or atsign for this directive"))
738 (interpret-bind-defaults ((count 1)) params
742 (def-format-interpreter #\& (colonp atsignp params)
743 (when (or colonp atsignp)
746 "cannot specify either colon or atsign for this directive"))
747 (interpret-bind-defaults ((count 1)) params
749 (dotimes (i (1- count))
752 (def-format-interpreter #\| (colonp atsignp params)
753 (when (or colonp atsignp)
756 "cannot specify either colon or atsign for this directive"))
757 (interpret-bind-defaults ((count 1)) params
759 (write-char (code-char form-feed-char-code) stream))))
761 (def-format-interpreter #\~ (colonp atsignp params)
762 (when (or colonp atsignp)
765 "cannot specify either colon or atsign for this directive"))
766 (interpret-bind-defaults ((count 1)) params
768 (write-char #\~ stream))))
770 (def-complex-format-interpreter #\newline (colonp atsignp params directives)
771 (when (and colonp atsignp)
774 "cannot specify both colon and atsign for this directive"))
775 (interpret-bind-defaults () params
777 (write-char #\newline stream)))
778 (if (and (not colonp)
780 (simple-string-p (car directives)))
781 (cons (string-left-trim *format-whitespace-chars*
786 ;;;; format interpreters and support functions for tabs and simple pretty
789 (def-format-interpreter #\T (colonp atsignp params)
791 (interpret-bind-defaults ((n 1) (m 1)) params
792 (pprint-tab (if atsignp :section-relative :section) n m stream))
794 (interpret-bind-defaults ((colrel 1) (colinc 1)) params
795 (format-relative-tab stream colrel colinc))
796 (interpret-bind-defaults ((colnum 1) (colinc 1)) params
797 (format-absolute-tab stream colnum colinc)))))
799 (defun output-spaces (stream n)
800 (let ((spaces #.(make-string 100 :initial-element #\space)))
802 (when (< n (length spaces))
804 (write-string spaces stream)
805 (decf n (length spaces)))
806 (write-string spaces stream :end n)))
808 (defun format-relative-tab (stream colrel colinc)
809 (if (sb!pretty:pretty-stream-p stream)
810 (pprint-tab :line-relative colrel colinc stream)
811 (let* ((cur (sb!impl::charpos stream))
812 (spaces (if (and cur (plusp colinc))
813 (- (* (ceiling (+ cur colrel) colinc) colinc) cur)
815 (output-spaces stream spaces))))
817 (defun format-absolute-tab (stream colnum colinc)
818 (if (sb!pretty:pretty-stream-p stream)
819 (pprint-tab :line colnum colinc stream)
820 (let ((cur (sb!impl::charpos stream)))
822 (write-string " " stream))
824 (output-spaces stream (- colnum cur)))
826 (unless (zerop colinc)
827 (output-spaces stream
828 (- colinc (rem (- cur colnum) colinc)))))))))
830 (def-format-interpreter #\_ (colonp atsignp params)
831 (interpret-bind-defaults () params
832 (pprint-newline (if colonp
841 (def-format-interpreter #\I (colonp atsignp params)
844 :complaint "cannot specify the at-sign modifier"))
845 (interpret-bind-defaults ((n 0)) params
846 (pprint-indent (if colonp :current :block) n stream)))
848 ;;;; format interpreter for ~*
850 (def-format-interpreter #\* (colonp atsignp params)
854 :complaint "cannot specify both colon and at-sign")
855 (interpret-bind-defaults ((posn 0)) params
856 (if (<= 0 posn (length orig-args))
857 (setf args (nthcdr posn orig-args))
859 :complaint "Index ~W is out of bounds. (It should ~
860 have been between 0 and ~W.)"
861 :args (list posn (length orig-args))))))
863 (interpret-bind-defaults ((n 1)) params
864 (do ((cur-posn 0 (1+ cur-posn))
865 (arg-ptr orig-args (cdr arg-ptr)))
867 (let ((new-posn (- cur-posn n)))
868 (if (<= 0 new-posn (length orig-args))
869 (setf args (nthcdr new-posn orig-args))
872 "Index ~W is out of bounds. (It should
873 have been between 0 and ~W.)"
875 (list new-posn (length orig-args))))))))
876 (interpret-bind-defaults ((n 1)) params
880 ;;;; format interpreter for indirection
882 (def-format-interpreter #\? (colonp atsignp params string end)
885 :complaint "cannot specify the colon modifier"))
886 (interpret-bind-defaults () params
892 "~A~%while processing indirect format string:"
893 :args (list condition)
895 :control-string string
898 (setf args (%format stream (next-arg) orig-args args))
899 (%format stream (next-arg) (next-arg))))))
901 ;;;; format interpreters for capitalization
903 (def-complex-format-interpreter #\( (colonp atsignp params directives)
904 (let ((close (find-directive directives #\) nil)))
907 :complaint "no corresponding close paren"))
908 (interpret-bind-defaults () params
909 (let* ((posn (position close directives))
910 (before (subseq directives 0 posn))
911 (after (nthcdr (1+ posn) directives))
912 (stream (make-case-frob-stream stream
920 (setf args (interpret-directive-list stream before orig-args args))
923 (def-complex-format-interpreter #\) ()
925 :complaint "no corresponding open paren"))
927 ;;;; format interpreters and support functions for conditionalization
929 (def-complex-format-interpreter #\[ (colonp atsignp params directives)
930 (multiple-value-bind (sublists last-semi-with-colon-p remaining)
931 (parse-conditional-directive directives)
937 "cannot specify both the colon and at-sign modifiers")
941 "can only specify one section")
942 (interpret-bind-defaults () params
943 (let ((prev-args args)
946 (interpret-directive-list stream
952 (if (= (length sublists) 2)
953 (interpret-bind-defaults () params
955 (interpret-directive-list stream (car sublists)
957 (interpret-directive-list stream (cadr sublists)
961 "must specify exactly two sections"))
962 (interpret-bind-defaults ((index (next-arg))) params
963 (let* ((default (and last-semi-with-colon-p
965 (last (1- (length sublists)))
967 (if (<= 0 index last)
968 (nth (- last index) sublists)
970 (interpret-directive-list stream sublist orig-args
974 (def-complex-format-interpreter #\; ()
977 "~~; not contained within either ~~[...~~] or ~~<...~~>"))
979 (def-complex-format-interpreter #\] ()
982 "no corresponding open bracket"))
984 ;;;; format interpreter for up-and-out
986 (defvar *outside-args*)
988 (def-format-interpreter #\^ (colonp atsignp params)
991 :complaint "cannot specify the at-sign modifier"))
992 (when (and colonp (not *up-up-and-out-allowed*))
994 :complaint "attempt to use ~~:^ outside a ~~:{...~~} construct"))
995 (when (interpret-bind-defaults ((arg1 nil) (arg2 nil) (arg3 nil)) params
996 (cond (arg3 (<= arg1 arg2 arg3))
997 (arg2 (eql arg1 arg2))
1000 (null *outside-args*)
1002 (throw (if colonp 'up-up-and-out 'up-and-out)
1005 ;;;; format interpreters for iteration
1007 (def-complex-format-interpreter #\{
1008 (colonp atsignp params string end directives)
1009 (let ((close (find-directive directives #\} nil)))
1011 (error 'format-error
1013 "no corresponding close brace"))
1014 (interpret-bind-defaults ((max-count nil)) params
1015 (let* ((closed-with-colon (format-directive-colonp close))
1016 (posn (position close directives))
1017 (insides (if (zerop posn)
1019 (subseq directives 0 posn)))
1020 (*up-up-and-out-allowed* colonp))
1022 ((do-guts (orig-args args)
1030 "~A~%while processing indirect format string:"
1031 :args (list condition)
1033 :control-string string
1034 :offset (1- end)))))
1035 (%format stream insides orig-args args))
1036 (interpret-directive-list stream insides
1038 (bind-args (orig-args args)
1040 (let* ((arg (next-arg))
1041 (*logical-block-popper* nil)
1042 (*outside-args* args))
1046 (do-guts orig-args args)))
1047 (do-loop (orig-args args)
1048 (catch (if colonp 'up-up-and-out 'up-and-out)
1050 (when (and (not closed-with-colon) (null args))
1052 (when (and max-count (minusp (decf max-count)))
1054 (setf args (bind-args orig-args args))
1055 (when (and closed-with-colon (null args))
1059 (setf args (do-loop orig-args args))
1060 (let ((arg (next-arg))
1061 (*logical-block-popper* nil))
1063 (nthcdr (1+ posn) directives))))))
1065 (def-complex-format-interpreter #\} ()
1066 (error 'format-error
1067 :complaint "no corresponding open brace"))
1069 ;;;; format interpreters and support functions for justification
1071 (def-complex-format-interpreter #\<
1072 (colonp atsignp params string end directives)
1073 (multiple-value-bind (segments first-semi close remaining)
1074 (parse-format-justification directives)
1076 (if (format-directive-colonp close)
1077 (multiple-value-bind (prefix per-line-p insides suffix)
1078 (parse-format-logical-block segments colonp first-semi
1079 close params string end)
1080 (interpret-format-logical-block stream orig-args args
1081 prefix per-line-p insides
1083 (let ((count (reduce #'+ (mapcar (lambda (x) (count-if #'illegal-inside-justification-p x)) segments))))
1085 ;; ANSI specifies that "an error is signalled" in this
1087 (error 'format-error
1088 :complaint "~D illegal directive~:P found inside justification block"
1090 :references (list '(:ansi-cl :section (22 3 5 2)))))
1091 (interpret-format-justification stream orig-args args
1092 segments colonp atsignp
1093 first-semi params))))
1096 (defun interpret-format-justification
1097 (stream orig-args args segments colonp atsignp first-semi params)
1098 (interpret-bind-defaults
1099 ((mincol 0) (colinc 1) (minpad 0) (padchar #\space))
1101 (let ((newline-string nil)
1107 (when (and first-semi (format-directive-colonp first-semi))
1108 (interpret-bind-defaults
1110 (len (or (sb!impl::line-length stream) 72)))
1111 (format-directive-params first-semi)
1112 (setf newline-string
1113 (with-output-to-string (stream)
1115 (interpret-directive-list stream
1119 (setf extra-space extra)
1120 (setf line-len len)))
1121 (dolist (segment segments)
1122 (push (with-output-to-string (stream)
1124 (interpret-directive-list stream segment
1128 (format-justification stream newline-string extra-space line-len strings
1129 colonp atsignp mincol colinc minpad padchar)))
1132 (defun format-justification (stream newline-prefix extra-space line-len strings
1133 pad-left pad-right mincol colinc minpad padchar)
1134 (setf strings (reverse strings))
1135 (let* ((num-gaps (+ (1- (length strings))
1137 (if pad-right 1 0)))
1138 (chars (+ (* num-gaps minpad)
1140 for string in strings
1141 summing (length string))))
1142 (length (if (> chars mincol)
1143 (+ mincol (* (ceiling (- chars mincol) colinc) colinc))
1145 (padding (+ (- length chars) (* num-gaps minpad))))
1146 (when (and newline-prefix
1147 (> (+ (or (sb!impl::charpos stream) 0)
1150 (write-string newline-prefix stream))
1151 (flet ((do-padding ()
1153 (if (zerop num-gaps) padding (truncate padding num-gaps))))
1154 (decf padding pad-len)
1156 (dotimes (i pad-len) (write-char padchar stream)))))
1157 (when (or pad-left (and (not pad-right) (null (cdr strings))))
1160 (write-string (car strings) stream)
1161 (dolist (string (cdr strings))
1163 (write-string string stream)))
1167 (defun interpret-format-logical-block
1168 (stream orig-args args prefix per-line-p insides suffix atsignp)
1169 (let ((arg (if atsignp args (next-arg))))
1171 (pprint-logical-block
1172 (stream arg :per-line-prefix prefix :suffix suffix)
1173 (let ((*logical-block-popper* (lambda () (pprint-pop))))
1175 (interpret-directive-list stream insides
1176 (if atsignp orig-args arg)
1178 (pprint-logical-block (stream arg :prefix prefix :suffix suffix)
1179 (let ((*logical-block-popper* (lambda () (pprint-pop))))
1181 (interpret-directive-list stream insides
1182 (if atsignp orig-args arg)
1184 (if atsignp nil args))
1186 ;;;; format interpreter and support functions for user-defined method
1188 (def-format-interpreter #\/ (string start end colonp atsignp params)
1189 (let ((symbol (extract-user-fun-name string start end)))
1191 (dolist (param-and-offset params)
1192 (let ((param (cdr param-and-offset)))
1194 (:arg (args (next-arg)))
1195 (:remaining (args (length args)))
1197 (apply (fdefinition symbol) stream (next-arg) colonp atsignp (args)))))