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* (char-code character)))))
82 (*default-format-error-offset*
83 (1- (format-directive-end directive))))
86 :complaint "unknown format directive ~@[(character: ~A)~]"
87 :args (list (char-name character))))
88 (multiple-value-bind (new-directives new-args)
89 (funcall function stream directive
90 (cdr directives) orig-args args)
91 (values new-directives new-args)))
92 (interpret-directive-list stream new-directives
93 orig-args new-args)))))
96 ;;;; FORMAT directive definition macros and runtime support
98 (eval-when (:compile-toplevel :execute)
100 ;;; This macro is used to extract the next argument from the current arg list.
101 ;;; This is the version used by format directive interpreters.
102 (sb!xc:defmacro next-arg (&optional offset)
106 :complaint "no more arguments"
108 `(:offset ,offset))))
109 (when *logical-block-popper*
110 (funcall *logical-block-popper*))
113 (sb!xc:defmacro def-complex-format-interpreter (char lambda-list &body body)
116 "~:@(~:C~)-FORMAT-DIRECTIVE-INTERPRETER"
118 (directive (sb!xc:gensym "DIRECTIVE"))
119 (directives (if lambda-list (car (last lambda-list)) (sb!xc:gensym "DIRECTIVES"))))
121 (defun ,defun-name (stream ,directive ,directives orig-args args)
122 (declare (ignorable stream orig-args args))
124 `((let ,(mapcar (lambda (var)
126 (,(symbolicate "FORMAT-DIRECTIVE-" var)
128 (butlast lambda-list))
129 (values (progn ,@body) args)))
130 `((declare (ignore ,directive ,directives))
132 (%set-format-directive-interpreter ,char #',defun-name))))
134 (sb!xc:defmacro def-format-interpreter (char lambda-list &body body)
135 (let ((directives (sb!xc:gensym "DIRECTIVES")))
136 `(def-complex-format-interpreter ,char (,@lambda-list ,directives)
140 (sb!xc:defmacro interpret-bind-defaults (specs params &body body)
141 (once-only ((params params))
142 (collect ((bindings))
144 (destructuring-bind (var default) spec
145 (bindings `(,var (let* ((param-and-offset (pop ,params))
146 (offset (car param-and-offset))
147 (param (cdr param-and-offset)))
149 (:arg (or (next-arg offset) ,default))
150 (:remaining (length args))
157 "too many parameters, expected no more than ~W"
158 :args (list ,(length specs))
159 :offset (caar ,params)))
164 ;;;; format interpreters and support functions for simple output
166 (defun format-write-field (stream string mincol colinc minpad padchar padleft)
168 (write-string string stream))
170 (write-char padchar stream))
171 ;; As of sbcl-0.6.12.34, we could end up here when someone tries to
172 ;; print e.g. (FORMAT T "~F" "NOTFLOAT"), in which case ANSI says
173 ;; we're supposed to soldier on bravely, and so we have to deal with
174 ;; the unsupplied-MINCOL-and-COLINC case without blowing up.
175 (when (and mincol colinc)
176 (do ((chars (+ (length string) (max minpad 0)) (+ chars colinc)))
179 (write-char padchar stream))))
181 (write-string string stream)))
183 (defun format-princ (stream arg colonp atsignp mincol colinc minpad padchar)
184 (format-write-field stream
185 (if (or arg (not colonp))
186 (princ-to-string arg)
188 mincol colinc minpad padchar atsignp))
190 (def-format-interpreter #\A (colonp atsignp params)
192 (interpret-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
195 (format-princ stream (next-arg) colonp atsignp
196 mincol colinc minpad padchar))
197 (princ (if colonp (or (next-arg) "()") (next-arg)) stream)))
199 (defun format-prin1 (stream arg colonp atsignp mincol colinc minpad padchar)
200 (format-write-field stream
201 (if (or arg (not colonp))
202 (prin1-to-string arg)
204 mincol colinc minpad padchar atsignp))
206 (def-format-interpreter #\S (colonp atsignp params)
208 (interpret-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
211 (format-prin1 stream (next-arg) colonp atsignp
212 mincol colinc minpad padchar)))
214 (let ((arg (next-arg)))
217 (princ "()" stream))))
219 (prin1 (next-arg) stream))))
221 (def-format-interpreter #\C (colonp atsignp params)
222 (interpret-bind-defaults () params
224 (format-print-named-character (next-arg) stream)
226 (prin1 (next-arg) stream)
227 (write-char (next-arg) stream)))))
229 ;;; "printing" as defined in the ANSI CL glossary, which is normative.
230 (defun char-printing-p (char)
231 (and (not (eql char #\Space))
232 (graphic-char-p char)))
234 (defun format-print-named-character (char stream)
235 (cond ((not (char-printing-p char))
236 (write-string (string-capitalize (char-name char)) stream))
238 (write-char char stream))))
240 (def-format-interpreter #\W (colonp atsignp params)
241 (interpret-bind-defaults () params
242 (let ((*print-pretty* (or colonp *print-pretty*))
243 (*print-level* (unless atsignp *print-level*))
244 (*print-length* (unless atsignp *print-length*)))
245 (output-object (next-arg) stream))))
247 ;;;; format interpreters and support functions for integer output
249 ;;; FORMAT-PRINT-NUMBER does most of the work for the numeric printing
250 ;;; directives. The parameters are interpreted as defined for ~D.
251 (defun format-print-integer (stream number print-commas-p print-sign-p
252 radix mincol padchar commachar commainterval)
253 (let ((*print-base* radix)
255 (if (integerp number)
256 (let* ((text (princ-to-string (abs number)))
257 (commaed (if print-commas-p
258 (format-add-commas text commachar commainterval)
260 (signed (cond ((minusp number)
261 (concatenate 'string "-" commaed))
263 (concatenate 'string "+" commaed))
265 ;; colinc = 1, minpad = 0, padleft = t
266 (format-write-field stream signed mincol 1 0 padchar t))
267 (princ number stream))))
269 (defun format-add-commas (string commachar commainterval)
270 (let ((length (length string)))
271 (multiple-value-bind (commas extra) (truncate (1- length) commainterval)
272 (let ((new-string (make-string (+ length commas)))
273 (first-comma (1+ extra)))
274 (replace new-string string :end1 first-comma :end2 first-comma)
275 (do ((src first-comma (+ src commainterval))
276 (dst first-comma (+ dst commainterval 1)))
278 (setf (schar new-string dst) commachar)
279 (replace new-string string :start1 (1+ dst)
280 :start2 src :end2 (+ src commainterval)))
283 (eval-when (:compile-toplevel :execute)
284 (sb!xc:defmacro interpret-format-integer (base)
285 `(if (or colonp atsignp params)
286 (interpret-bind-defaults
287 ((mincol 0) (padchar #\space) (commachar #\,) (commainterval 3))
289 (format-print-integer stream (next-arg) colonp atsignp ,base mincol
290 padchar commachar commainterval))
291 (let ((*print-base* ,base)
293 (*print-escape* nil))
294 (output-object (next-arg) stream))))
297 (def-format-interpreter #\D (colonp atsignp params)
298 (interpret-format-integer 10))
300 (def-format-interpreter #\B (colonp atsignp params)
301 (interpret-format-integer 2))
303 (def-format-interpreter #\O (colonp atsignp params)
304 (interpret-format-integer 8))
306 (def-format-interpreter #\X (colonp atsignp params)
307 (interpret-format-integer 16))
309 (def-format-interpreter #\R (colonp atsignp params)
310 (interpret-bind-defaults
311 ((base nil) (mincol 0) (padchar #\space) (commachar #\,)
314 (let ((arg (next-arg)))
316 (format-print-integer stream arg colonp atsignp base mincol
317 padchar commachar commainterval)
320 (format-print-old-roman stream arg)
321 (format-print-roman stream arg))
323 (format-print-ordinal stream arg)
324 (format-print-cardinal stream arg)))))))
326 (defparameter *cardinal-ones*
327 #(nil "one" "two" "three" "four" "five" "six" "seven" "eight" "nine"))
329 (defparameter *cardinal-tens*
330 #(nil nil "twenty" "thirty" "forty"
331 "fifty" "sixty" "seventy" "eighty" "ninety"))
333 (defparameter *cardinal-teens*
334 #("ten" "eleven" "twelve" "thirteen" "fourteen" ;;; RAD
335 "fifteen" "sixteen" "seventeen" "eighteen" "nineteen"))
337 (defparameter *cardinal-periods*
338 #("" " thousand" " million" " billion" " trillion" " quadrillion"
339 " quintillion" " sextillion" " septillion" " octillion" " nonillion"
340 " decillion" " undecillion" " duodecillion" " tredecillion"
341 " quattuordecillion" " quindecillion" " sexdecillion" " septendecillion"
342 " octodecillion" " novemdecillion" " vigintillion"))
344 (defparameter *ordinal-ones*
345 #(nil "first" "second" "third" "fourth"
346 "fifth" "sixth" "seventh" "eighth" "ninth"))
348 (defparameter *ordinal-tens*
349 #(nil "tenth" "twentieth" "thirtieth" "fortieth"
350 "fiftieth" "sixtieth" "seventieth" "eightieth" "ninetieth"))
352 (defun format-print-small-cardinal (stream n)
353 (multiple-value-bind (hundreds rem) (truncate n 100)
354 (when (plusp hundreds)
355 (write-string (svref *cardinal-ones* hundreds) stream)
356 (write-string " hundred" stream)
358 (write-char #\space stream)))
360 (multiple-value-bind (tens ones) (truncate rem 10)
362 (write-string (svref *cardinal-tens* tens) stream)
364 (write-char #\- stream)
365 (write-string (svref *cardinal-ones* ones) stream)))
367 (write-string (svref *cardinal-teens* ones) stream))
369 (write-string (svref *cardinal-ones* ones) stream)))))))
371 (defun format-print-cardinal (stream n)
373 (write-string "negative " stream)
374 (format-print-cardinal-aux stream (- n) 0 n))
376 (write-string "zero" stream))
378 (format-print-cardinal-aux stream n 0 n))))
380 (defun format-print-cardinal-aux (stream n period err)
381 (multiple-value-bind (beyond here) (truncate n 1000)
382 (unless (<= period 21)
383 (error "number too large to print in English: ~:D" err))
384 (unless (zerop beyond)
385 (format-print-cardinal-aux stream beyond (1+ period) err))
387 (unless (zerop beyond)
388 (write-char #\space stream))
389 (format-print-small-cardinal stream here)
390 (write-string (svref *cardinal-periods* period) stream))))
392 (defun format-print-ordinal (stream n)
394 (write-string "negative " stream))
395 (let ((number (abs n)))
396 (multiple-value-bind (top bot) (truncate number 100)
398 (format-print-cardinal stream (- number bot)))
399 (when (and (plusp top) (plusp bot))
400 (write-char #\space stream))
401 (multiple-value-bind (tens ones) (truncate bot 10)
402 (cond ((= bot 12) (write-string "twelfth" stream))
404 (write-string (svref *cardinal-teens* ones) stream);;;RAD
405 (write-string "th" stream))
406 ((and (zerop tens) (plusp ones))
407 (write-string (svref *ordinal-ones* ones) stream))
408 ((and (zerop ones)(plusp tens))
409 (write-string (svref *ordinal-tens* tens) stream))
411 (write-string (svref *cardinal-tens* tens) stream)
412 (write-char #\- stream)
413 (write-string (svref *ordinal-ones* ones) stream))
415 (write-string "th" stream))
417 (write-string "zeroth" stream)))))))
419 ;;; Print Roman numerals
421 (defun format-print-old-roman (stream n)
423 (error "Number too large to print in old Roman numerals: ~:D" n))
424 (do ((char-list '(#\D #\C #\L #\X #\V #\I) (cdr char-list))
425 (val-list '(500 100 50 10 5 1) (cdr val-list))
426 (cur-char #\M (car char-list))
427 (cur-val 1000 (car val-list))
428 (start n (do ((i start (progn
429 (write-char cur-char stream)
434 (defun format-print-roman (stream n)
436 (error "Number too large to print in Roman numerals: ~:D" n))
437 (do ((char-list '(#\D #\C #\L #\X #\V #\I) (cdr char-list))
438 (val-list '(500 100 50 10 5 1) (cdr val-list))
439 (sub-chars '(#\C #\X #\X #\I #\I) (cdr sub-chars))
440 (sub-val '(100 10 10 1 1 0) (cdr sub-val))
441 (cur-char #\M (car char-list))
442 (cur-val 1000 (car val-list))
443 (cur-sub-char #\C (car sub-chars))
444 (cur-sub-val 100 (car sub-val))
445 (start n (do ((i start (progn
446 (write-char cur-char stream)
449 (cond ((<= (- cur-val cur-sub-val) i)
450 (write-char cur-sub-char stream)
451 (write-char cur-char stream)
452 (- i (- cur-val cur-sub-val)))
458 (def-format-interpreter #\P (colonp atsignp params)
459 (interpret-bind-defaults () params
460 (let ((arg (if colonp
461 (if (eq orig-args args)
463 :complaint "no previous argument")
464 (do ((arg-ptr orig-args (cdr arg-ptr)))
465 ((eq (cdr arg-ptr) args)
469 (write-string (if (eql arg 1) "y" "ies") stream)
470 (unless (eql arg 1) (write-char #\s stream))))))
472 ;;;; format interpreters and support functions for floating point output
474 (defun decimal-string (n)
475 (write-to-string n :base 10 :radix nil :escape nil))
477 (def-format-interpreter #\F (colonp atsignp params)
481 "cannot specify the colon modifier with this directive"))
482 (interpret-bind-defaults ((w nil) (d nil) (k nil) (ovf nil) (pad #\space))
484 (format-fixed stream (next-arg) w d k ovf pad atsignp)))
486 (defun format-fixed (stream number w d k ovf pad atsign)
489 (format-fixed-aux stream number w d k ovf pad atsign))
491 (format-fixed-aux stream (coerce number 'single-float)
492 w d k ovf pad atsign))
494 (format-write-field stream (decimal-string number) w 1 0 #\space t))
496 (format-princ stream number nil nil w 1 0 pad))))
498 ;;; We return true if we overflowed, so that ~G can output the overflow char
499 ;;; instead of spaces.
500 (defun format-fixed-aux (stream number w d k ovf pad atsign)
501 (declare (type float number))
503 ((or (float-infinity-p number)
504 (float-nan-p number))
505 (prin1 number stream)
508 (sb!impl::string-dispatch (single-float double-float)
511 (when (and w (or atsign (minusp (float-sign number))))
513 (multiple-value-bind (str len lpoint tpoint)
514 (sb!impl::flonum-to-string (abs number) spaceleft d k)
515 ;; if caller specifically requested no fraction digits, suppress the
516 ;; optional trailing zero
517 (when (and d (zerop d))
521 ;; optional leading zero
523 (if (or (> spaceleft 0) tpoint) ;force at least one digit
526 ;; optional trailing zero
531 (cond ((and w (< spaceleft 0) ovf)
532 ;; field width overflow
534 (write-char ovf stream))
538 (dotimes (i spaceleft)
539 (write-char pad stream)))
540 (if (minusp (float-sign number))
541 (write-char #\- stream)
543 (write-char #\+ stream)))
545 (write-char #\0 stream))
546 (write-string str stream)
548 (write-char #\0 stream))
551 (def-format-interpreter #\E (colonp atsignp params)
555 "cannot specify the colon modifier with this directive"))
556 (interpret-bind-defaults
557 ((w nil) (d nil) (e nil) (k 1) (ovf nil) (pad #\space) (mark nil))
559 (format-exponential stream (next-arg) w d e k ovf pad mark atsignp)))
561 (defun format-exponential (stream number w d e k ovf pad marker atsign)
564 (format-exp-aux stream number w d e k ovf pad marker atsign)
565 (if (rationalp number)
566 (format-exp-aux stream
567 (coerce number 'single-float)
568 w d e k ovf pad marker atsign)
569 (format-write-field stream
570 (decimal-string number)
572 (format-princ stream number nil nil w 1 0 pad)))
574 (defun format-exponent-marker (number)
575 (if (typep number *read-default-float-format*)
583 ;;; Here we prevent the scale factor from shifting all significance out of
584 ;;; a number to the right. We allow insignificant zeroes to be shifted in
585 ;;; to the left right, athough it is an error to specify k and d such that this
586 ;;; occurs. Perhaps we should detect both these condtions and flag them as
587 ;;; errors. As for now, we let the user get away with it, and merely guarantee
588 ;;; that at least one significant digit will appear.
590 ;;; Raymond Toy writes: The Hyperspec seems to say that the exponent
591 ;;; marker is always printed. Make it so. Also, the original version
592 ;;; causes errors when printing infinities or NaN's. The Hyperspec is
593 ;;; silent here, so let's just print out infinities and NaN's instead
594 ;;; of causing an error.
595 (defun format-exp-aux (stream number w d e k ovf pad marker atsign)
596 (declare (type float number))
597 (if (or (float-infinity-p number)
598 (float-nan-p number))
599 (prin1 number stream)
600 (multiple-value-bind (num expt) (sb!impl::scale-exponent (abs number))
601 (let* ((expt (- expt k))
602 (estr (decimal-string (abs expt)))
603 (elen (if e (max (length estr) e) (length estr)))
606 (setf spaceleft (- w 2 elen))
607 (when (or atsign (minusp (float-sign number)))
609 (if (and w ovf e (> elen e)) ;exponent overflow
610 (dotimes (i w) (write-char ovf stream))
611 (let* ((fdig (if d (if (plusp k) (1+ (- d k)) d) nil))
612 (fmin (if (minusp k) 1 fdig)))
613 (multiple-value-bind (fstr flen lpoint tpoint)
614 (sb!impl::flonum-to-string num spaceleft fdig k fmin)
615 (when (and d (zerop d)) (setq tpoint nil))
617 (decf spaceleft flen)
618 ;; See CLHS 22.3.3.2. "If the parameter d is
619 ;; omitted, ... [and] if the fraction to be
620 ;; printed is zero then a single zero digit should
621 ;; appear after the decimal point." So we need to
622 ;; subtract one from here because we're going to
623 ;; add an extra 0 digit later. [rtoy]
624 (when (and (zerop number) (null d))
627 (if (or (> spaceleft 0) tpoint)
630 (when (and tpoint (<= spaceleft 0))
632 (cond ((and w (< spaceleft 0) ovf)
633 ;;significand overflow
634 (dotimes (i w) (write-char ovf stream)))
636 (dotimes (i spaceleft) (write-char pad stream)))
637 (if (minusp (float-sign number))
638 (write-char #\- stream)
639 (if atsign (write-char #\+ stream)))
640 (when lpoint (write-char #\0 stream))
641 (write-string fstr stream)
642 (when (and (zerop number) (null d))
643 ;; It's later and we're adding the zero
645 (write-char #\0 stream))
646 (write-char (if marker
648 (format-exponent-marker number))
650 (write-char (if (minusp expt) #\- #\+) stream)
652 ;;zero-fill before exponent if necessary
653 (dotimes (i (- e (length estr)))
654 (write-char #\0 stream)))
655 (write-string estr stream))))))))))
657 (def-format-interpreter #\G (colonp atsignp params)
661 "cannot specify the colon modifier with this directive"))
662 (interpret-bind-defaults
663 ((w nil) (d nil) (e nil) (k nil) (ovf nil) (pad #\space) (mark nil))
665 (format-general stream (next-arg) w d e k ovf pad mark atsignp)))
667 (defun format-general (stream number w d e k ovf pad marker atsign)
670 (format-general-aux stream number w d e k ovf pad marker atsign)
671 (if (rationalp number)
672 (format-general-aux stream
673 (coerce number 'single-float)
674 w d e k ovf pad marker atsign)
675 (format-write-field stream
676 (decimal-string number)
678 (format-princ stream number nil nil w 1 0 pad)))
680 ;;; Raymond Toy writes: same change as for format-exp-aux
681 (defun format-general-aux (stream number w d e k ovf pad marker atsign)
682 (declare (type float number))
683 (if (or (float-infinity-p number)
684 (float-nan-p number))
685 (prin1 number stream)
686 (multiple-value-bind (ignore n) (sb!impl::scale-exponent (abs number))
687 (declare (ignore ignore))
688 ;; KLUDGE: Default d if omitted. The procedure is taken directly from
689 ;; the definition given in the manual, and is not very efficient, since
690 ;; we generate the digits twice. Future maintainers are encouraged to
691 ;; improve on this. -- rtoy?? 1998??
693 (multiple-value-bind (str len)
694 (sb!impl::flonum-to-string (abs number))
695 (declare (ignore str))
696 (let ((q (if (= len 1) 1 (1- len))))
697 (setq d (max q (min n 7))))))
698 (let* ((ee (if e (+ e 2) 4))
699 (ww (if w (- w ee) nil))
702 (let ((char (if (format-fixed-aux stream number ww dd nil
706 (dotimes (i ee) (write-char char stream))))
708 (format-exp-aux stream number w d e (or k 1)
709 ovf pad marker atsign)))))))
711 (def-format-interpreter #\$ (colonp atsignp params)
712 (interpret-bind-defaults ((d 2) (n 1) (w 0) (pad #\space)) params
713 (format-dollars stream (next-arg) d n w pad colonp atsignp)))
715 (defun format-dollars (stream number d n w pad colon atsign)
716 (when (rationalp number)
717 ;; This coercion to SINGLE-FLOAT seems as though it gratuitously
718 ;; loses precision (why not LONG-FLOAT?) but it's the default
719 ;; behavior in the ANSI spec, so in some sense it's the right
720 ;; thing, and at least the user shouldn't be surprised.
721 (setq number (coerce number 'single-float)))
723 (let* ((signstr (if (minusp (float-sign number))
726 (signlen (length signstr)))
727 (multiple-value-bind (str strlen ig2 ig3 pointplace)
728 (sb!impl::flonum-to-string number nil d nil)
729 (declare (ignore ig2 ig3 strlen))
731 (write-string signstr stream))
732 (dotimes (i (- w signlen (max n pointplace) 1 d))
733 (write-char pad stream))
735 (write-string signstr stream))
736 (dotimes (i (- n pointplace))
737 (write-char #\0 stream))
738 (write-string str stream)))
739 (format-write-field stream
740 (decimal-string number)
743 ;;;; FORMAT interpreters and support functions for line/page breaks etc.
745 (def-format-interpreter #\% (colonp atsignp params)
746 (when (or colonp atsignp)
749 "cannot specify either colon or atsign for this directive"))
750 (interpret-bind-defaults ((count 1)) params
754 (def-format-interpreter #\& (colonp atsignp params)
755 (when (or colonp atsignp)
758 "cannot specify either colon or atsign for this directive"))
759 (interpret-bind-defaults ((count 1)) params
761 (dotimes (i (1- count))
764 (def-format-interpreter #\| (colonp atsignp params)
765 (when (or colonp atsignp)
768 "cannot specify either colon or atsign for this directive"))
769 (interpret-bind-defaults ((count 1)) params
771 (write-char (code-char form-feed-char-code) stream))))
773 (def-format-interpreter #\~ (colonp atsignp params)
774 (when (or colonp atsignp)
777 "cannot specify either colon or atsign for this directive"))
778 (interpret-bind-defaults ((count 1)) params
780 (write-char #\~ stream))))
782 (def-complex-format-interpreter #\newline (colonp atsignp params directives)
783 (when (and colonp atsignp)
786 "cannot specify both colon and atsign for this directive"))
787 (interpret-bind-defaults () params
789 (write-char #\newline stream)))
790 (if (and (not colonp)
792 (simple-string-p (car directives)))
793 (cons (string-left-trim *format-whitespace-chars*
798 ;;;; format interpreters and support functions for tabs and simple pretty
801 (def-format-interpreter #\T (colonp atsignp params)
803 (interpret-bind-defaults ((n 1) (m 1)) params
804 (pprint-tab (if atsignp :section-relative :section) n m stream))
806 (interpret-bind-defaults ((colrel 1) (colinc 1)) params
807 (format-relative-tab stream colrel colinc))
808 (interpret-bind-defaults ((colnum 1) (colinc 1)) params
809 (format-absolute-tab stream colnum colinc)))))
811 (defun output-spaces (stream n)
812 (let ((spaces #.(make-string 100 :initial-element #\space)))
814 (when (< n (length spaces))
816 (write-string spaces stream)
817 (decf n (length spaces)))
818 (write-string spaces stream :end n)))
820 (defun format-relative-tab (stream colrel colinc)
821 (if (sb!pretty:pretty-stream-p stream)
822 (pprint-tab :line-relative colrel colinc stream)
823 (let* ((cur (sb!impl::charpos stream))
824 (spaces (if (and cur (plusp colinc))
825 (- (* (ceiling (+ cur colrel) colinc) colinc) cur)
827 (output-spaces stream spaces))))
829 (defun format-absolute-tab (stream colnum colinc)
830 (if (sb!pretty:pretty-stream-p stream)
831 (pprint-tab :line colnum colinc stream)
832 (let ((cur (sb!impl::charpos stream)))
834 (write-string " " stream))
836 (output-spaces stream (- colnum cur)))
838 (unless (zerop colinc)
839 (output-spaces stream
840 (- colinc (rem (- cur colnum) colinc)))))))))
842 (def-format-interpreter #\_ (colonp atsignp params)
843 (interpret-bind-defaults () params
844 (pprint-newline (if colonp
853 (def-format-interpreter #\I (colonp atsignp params)
856 :complaint "cannot specify the at-sign modifier"))
857 (interpret-bind-defaults ((n 0)) params
858 (pprint-indent (if colonp :current :block) n stream)))
860 ;;;; format interpreter for ~*
862 (def-format-interpreter #\* (colonp atsignp params)
866 :complaint "cannot specify both colon and at-sign")
867 (interpret-bind-defaults ((posn 0)) params
868 (if (<= 0 posn (length orig-args))
869 (setf args (nthcdr posn orig-args))
871 :complaint "Index ~W is out of bounds. (It should ~
872 have been between 0 and ~W.)"
873 :args (list posn (length orig-args))))))
875 (interpret-bind-defaults ((n 1)) params
876 (do ((cur-posn 0 (1+ cur-posn))
877 (arg-ptr orig-args (cdr arg-ptr)))
879 (let ((new-posn (- cur-posn n)))
880 (if (<= 0 new-posn (length orig-args))
881 (setf args (nthcdr new-posn orig-args))
884 "Index ~W is out of bounds. (It should
885 have been between 0 and ~W.)"
887 (list new-posn (length orig-args))))))))
888 (interpret-bind-defaults ((n 1)) params
892 ;;;; format interpreter for indirection
894 (def-format-interpreter #\? (colonp atsignp params string end)
897 :complaint "cannot specify the colon modifier"))
898 (interpret-bind-defaults () params
904 "~A~%while processing indirect format string:"
905 :args (list condition)
907 :control-string string
910 (setf args (%format stream (next-arg) orig-args args))
911 (%format stream (next-arg) (next-arg))))))
913 ;;;; format interpreters for capitalization
915 (def-complex-format-interpreter #\( (colonp atsignp params directives)
916 (let ((close (find-directive directives #\) nil)))
919 :complaint "no corresponding close paren"))
920 (interpret-bind-defaults () params
921 (let* ((posn (position close directives))
922 (before (subseq directives 0 posn))
923 (after (nthcdr (1+ posn) directives))
924 (stream (make-case-frob-stream stream
932 (setf args (interpret-directive-list stream before orig-args args))
935 (def-complex-format-interpreter #\) ()
937 :complaint "no corresponding open paren"))
939 ;;;; format interpreters and support functions for conditionalization
941 (def-complex-format-interpreter #\[ (colonp atsignp params directives)
942 (multiple-value-bind (sublists last-semi-with-colon-p remaining)
943 (parse-conditional-directive directives)
949 "cannot specify both the colon and at-sign modifiers")
953 "can only specify one section")
954 (interpret-bind-defaults () params
955 (let ((prev-args args)
958 (interpret-directive-list stream
964 (if (= (length sublists) 2)
965 (interpret-bind-defaults () params
967 (interpret-directive-list stream (car sublists)
969 (interpret-directive-list stream (cadr sublists)
973 "must specify exactly two sections"))
974 (interpret-bind-defaults ((index (next-arg))) params
975 (let* ((default (and last-semi-with-colon-p
977 (last (1- (length sublists)))
979 (if (<= 0 index last)
980 (nth (- last index) sublists)
982 (interpret-directive-list stream sublist orig-args
986 (def-complex-format-interpreter #\; ()
989 "~~; not contained within either ~~[...~~] or ~~<...~~>"))
991 (def-complex-format-interpreter #\] ()
994 "no corresponding open bracket"))
996 ;;;; format interpreter for up-and-out
998 (defvar *outside-args*)
1000 (def-format-interpreter #\^ (colonp atsignp params)
1002 (error 'format-error
1003 :complaint "cannot specify the at-sign modifier"))
1004 (when (and colonp (not *up-up-and-out-allowed*))
1005 (error 'format-error
1006 :complaint "attempt to use ~~:^ outside a ~~:{...~~} construct"))
1007 (when (interpret-bind-defaults ((arg1 nil) (arg2 nil) (arg3 nil)) params
1008 (cond (arg3 (<= arg1 arg2 arg3))
1009 (arg2 (eql arg1 arg2))
1012 (null *outside-args*)
1014 (throw (if colonp 'up-up-and-out 'up-and-out)
1017 ;;;; format interpreters for iteration
1019 (def-complex-format-interpreter #\{
1020 (colonp atsignp params string end directives)
1021 (let ((close (find-directive directives #\} nil)))
1023 (error 'format-error
1025 "no corresponding close brace"))
1026 (interpret-bind-defaults ((max-count nil)) params
1027 (let* ((closed-with-colon (format-directive-colonp close))
1028 (posn (position close directives))
1029 (insides (if (zerop posn)
1031 (subseq directives 0 posn)))
1032 (*up-up-and-out-allowed* colonp))
1034 ((do-guts (orig-args args)
1042 "~A~%while processing indirect format string:"
1043 :args (list condition)
1045 :control-string string
1046 :offset (1- end)))))
1047 (%format stream insides orig-args args))
1048 (interpret-directive-list stream insides
1050 (bind-args (orig-args args)
1052 (let* ((arg (next-arg))
1053 (*logical-block-popper* nil)
1054 (*outside-args* args))
1058 (do-guts orig-args args)))
1059 (do-loop (orig-args args)
1060 (catch (if colonp 'up-up-and-out 'up-and-out)
1062 (when (and (not closed-with-colon) (null args))
1064 (when (and max-count (minusp (decf max-count)))
1066 (setf args (bind-args orig-args args))
1067 (when (and closed-with-colon (null args))
1071 (setf args (do-loop orig-args args))
1072 (let ((arg (next-arg))
1073 (*logical-block-popper* nil))
1075 (nthcdr (1+ posn) directives))))))
1077 (def-complex-format-interpreter #\} ()
1078 (error 'format-error
1079 :complaint "no corresponding open brace"))
1081 ;;;; format interpreters and support functions for justification
1083 (def-complex-format-interpreter #\<
1084 (colonp atsignp params string end directives)
1085 (multiple-value-bind (segments first-semi close remaining)
1086 (parse-format-justification directives)
1088 (if (format-directive-colonp close)
1089 (multiple-value-bind (prefix per-line-p insides suffix)
1090 (parse-format-logical-block segments colonp first-semi
1091 close params string end)
1092 (interpret-format-logical-block stream orig-args args
1093 prefix per-line-p insides
1095 (let ((count (reduce #'+ (mapcar (lambda (x) (count-if #'illegal-inside-justification-p x)) segments))))
1097 ;; ANSI specifies that "an error is signalled" in this
1099 (error 'format-error
1100 :complaint "~D illegal directive~:P found inside justification block"
1102 :references (list '(:ansi-cl :section (22 3 5 2)))))
1103 (interpret-format-justification stream orig-args args
1104 segments colonp atsignp
1105 first-semi params))))
1108 (defun interpret-format-justification
1109 (stream orig-args args segments colonp atsignp first-semi params)
1110 (interpret-bind-defaults
1111 ((mincol 0) (colinc 1) (minpad 0) (padchar #\space))
1113 (let ((newline-string nil)
1119 (when (and first-semi (format-directive-colonp first-semi))
1120 (interpret-bind-defaults
1122 (len (or (sb!impl::line-length stream) 72)))
1123 (format-directive-params first-semi)
1124 (setf newline-string
1125 (with-output-to-string (stream)
1127 (interpret-directive-list stream
1131 (setf extra-space extra)
1132 (setf line-len len)))
1133 (dolist (segment segments)
1134 (push (with-output-to-string (stream)
1136 (interpret-directive-list stream segment
1140 (format-justification stream newline-string extra-space line-len strings
1141 colonp atsignp mincol colinc minpad padchar)))
1144 (defun format-justification (stream newline-prefix extra-space line-len strings
1145 pad-left pad-right mincol colinc minpad padchar)
1146 (setf strings (reverse strings))
1147 (let* ((num-gaps (+ (1- (length strings))
1149 (if pad-right 1 0)))
1150 (chars (+ (* num-gaps minpad)
1152 for string in strings
1153 summing (length string))))
1154 (length (if (> chars mincol)
1155 (+ mincol (* (ceiling (- chars mincol) colinc) colinc))
1157 (padding (+ (- length chars) (* num-gaps minpad))))
1158 (when (and newline-prefix
1159 (> (+ (or (sb!impl::charpos stream) 0)
1162 (write-string newline-prefix stream))
1163 (flet ((do-padding ()
1165 (if (zerop num-gaps) padding (truncate padding num-gaps))))
1166 (decf padding pad-len)
1168 (dotimes (i pad-len) (write-char padchar stream)))))
1169 (when (or pad-left (and (not pad-right) (null (cdr strings))))
1172 (write-string (car strings) stream)
1173 (dolist (string (cdr strings))
1175 (write-string string stream)))
1179 (defun interpret-format-logical-block
1180 (stream orig-args args prefix per-line-p insides suffix atsignp)
1181 (let ((arg (if atsignp args (next-arg))))
1183 (pprint-logical-block
1184 (stream arg :per-line-prefix prefix :suffix suffix)
1185 (let ((*logical-block-popper* (lambda () (pprint-pop))))
1187 (interpret-directive-list stream insides
1188 (if atsignp orig-args arg)
1190 (pprint-logical-block (stream arg :prefix prefix :suffix suffix)
1191 (let ((*logical-block-popper* (lambda () (pprint-pop))))
1193 (interpret-directive-list stream insides
1194 (if atsignp orig-args arg)
1196 (if atsignp nil args))
1198 ;;;; format interpreter and support functions for user-defined method
1200 (def-format-interpreter #\/ (string start end colonp atsignp params)
1201 (let ((symbol (extract-user-fun-name string start end)))
1203 (dolist (param-and-offset params)
1204 (let ((param (cdr param-and-offset)))
1206 (:arg (args (next-arg)))
1207 (:remaining (args (length args)))
1209 (apply (fdefinition symbol) stream (next-arg) colonp atsignp (args)))))