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 (define-compiler-macro format (&whole form destination control &rest args)
55 (declare (ignore control args))
56 (when (stringp destination)
57 (warn "Literal string as destination in FORMAT:~% ~S" form))
60 (defun %format (stream string-or-fun orig-args &optional (args orig-args))
61 (if (functionp string-or-fun)
62 (apply string-or-fun stream args)
64 (let* ((string (etypecase string-or-fun
68 (coerce string-or-fun 'simple-string))))
69 (*default-format-error-control-string* string)
70 (*logical-block-popper* nil))
71 (interpret-directive-list stream (tokenize-control-string string)
74 (defun interpret-directive-list (stream directives orig-args args)
76 (let ((directive (car directives)))
79 (write-string directive stream)
80 (interpret-directive-list stream (cdr directives) orig-args args))
82 (multiple-value-bind (new-directives new-args)
83 (let* ((character (format-directive-character directive))
87 (svref *format-directive-interpreters* (char-code character)))))
88 (*default-format-error-offset*
89 (1- (format-directive-end directive))))
92 :complaint "unknown format directive ~@[(character: ~A)~]"
93 :args (list (char-name character))))
94 (multiple-value-bind (new-directives new-args)
95 (funcall function stream directive
96 (cdr directives) orig-args args)
97 (values new-directives new-args)))
98 (interpret-directive-list stream new-directives
99 orig-args new-args)))))
102 ;;;; FORMAT directive definition macros and runtime support
104 (eval-when (:compile-toplevel :execute)
106 ;;; This macro is used to extract the next argument from the current arg list.
107 ;;; This is the version used by format directive interpreters.
108 (sb!xc:defmacro next-arg (&optional offset)
112 :complaint "no more arguments"
114 `(:offset ,offset))))
115 (when *logical-block-popper*
116 (funcall *logical-block-popper*))
119 (sb!xc:defmacro def-complex-format-interpreter (char lambda-list &body body)
122 "~:@(~:C~)-FORMAT-DIRECTIVE-INTERPRETER"
124 (directive (sb!xc:gensym "DIRECTIVE"))
125 (directives (if lambda-list (car (last lambda-list)) (sb!xc:gensym "DIRECTIVES"))))
127 (defun ,defun-name (stream ,directive ,directives orig-args args)
128 (declare (ignorable stream orig-args args))
130 `((let ,(mapcar (lambda (var)
132 (,(symbolicate "FORMAT-DIRECTIVE-" var)
134 (butlast lambda-list))
135 (values (progn ,@body) args)))
136 `((declare (ignore ,directive ,directives))
138 (%set-format-directive-interpreter ,char #',defun-name))))
140 (sb!xc:defmacro def-format-interpreter (char lambda-list &body body)
141 (let ((directives (sb!xc:gensym "DIRECTIVES")))
142 `(def-complex-format-interpreter ,char (,@lambda-list ,directives)
146 (sb!xc:defmacro interpret-bind-defaults (specs params &body body)
147 (once-only ((params params))
148 (collect ((bindings))
150 (destructuring-bind (var default) spec
151 (bindings `(,var (let* ((param-and-offset (pop ,params))
152 (offset (car param-and-offset))
153 (param (cdr param-and-offset)))
155 (:arg (or (next-arg offset) ,default))
156 (:remaining (length args))
163 "too many parameters, expected no more than ~W"
164 :args (list ,(length specs))
165 :offset (caar ,params)))
170 ;;;; format interpreters and support functions for simple output
172 (defun format-write-field (stream string mincol colinc minpad padchar padleft)
173 (when (and colinc (<= colinc 0))
175 :complaint "The value of colinc is ~a, should be a positive integer"
176 :args (list colinc)))
177 (when (and mincol (< mincol 0))
179 :complaint "The value of mincol is ~a, should be a non-negative integer"
180 :args (list mincol)))
182 (write-string string stream))
184 (write-char padchar stream))
185 ;; As of sbcl-0.6.12.34, we could end up here when someone tries to
186 ;; print e.g. (FORMAT T "~F" "NOTFLOAT"), in which case ANSI says
187 ;; we're supposed to soldier on bravely, and so we have to deal with
188 ;; the unsupplied-MINCOL-and-COLINC case without blowing up.
189 (when (and mincol colinc)
190 (do ((chars (+ (length string) (max minpad 0)) (+ chars colinc)))
193 (write-char padchar stream))))
195 (write-string string stream)))
197 (defun format-princ (stream arg colonp atsignp mincol colinc minpad padchar)
198 (format-write-field stream
199 (if (or arg (not colonp))
200 (princ-to-string arg)
202 mincol colinc minpad padchar atsignp))
204 (def-format-interpreter #\A (colonp atsignp params)
206 (interpret-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
209 (format-princ stream (next-arg) colonp atsignp
210 mincol colinc minpad padchar))
211 (princ (if colonp (or (next-arg) "()") (next-arg)) stream)))
213 (defun format-prin1 (stream arg colonp atsignp mincol colinc minpad padchar)
214 (format-write-field stream
215 (if (or arg (not colonp))
216 (prin1-to-string arg)
218 mincol colinc minpad padchar atsignp))
220 (def-format-interpreter #\S (colonp atsignp params)
222 (interpret-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
225 (format-prin1 stream (next-arg) colonp atsignp
226 mincol colinc minpad padchar)))
228 (let ((arg (next-arg)))
231 (princ "()" stream))))
233 (prin1 (next-arg) stream))))
235 (def-format-interpreter #\C (colonp atsignp params)
236 (interpret-bind-defaults () params
237 (let ((arg (next-arg)))
238 (unless (typep arg 'character)
240 :complaint "~s is not of type CHARACTER."
243 (format-print-named-character arg stream))
247 (write-char arg stream))))))
249 ;;; "printing" as defined in the ANSI CL glossary, which is normative.
250 (defun char-printing-p (char)
251 (and (not (eql char #\Space))
252 (graphic-char-p char)))
254 (defun format-print-named-character (char stream)
255 (cond ((not (char-printing-p char))
256 (write-string (string-capitalize (char-name char)) stream))
258 (write-char char stream))))
260 (def-format-interpreter #\W (colonp atsignp params)
261 (interpret-bind-defaults () params
262 (let ((*print-pretty* (or colonp *print-pretty*))
263 (*print-level* (unless atsignp *print-level*))
264 (*print-length* (unless atsignp *print-length*)))
265 (output-object (next-arg) stream))))
267 ;;;; format interpreters and support functions for integer output
269 ;;; FORMAT-PRINT-NUMBER does most of the work for the numeric printing
270 ;;; directives. The parameters are interpreted as defined for ~D.
271 (defun format-print-integer (stream number print-commas-p print-sign-p
272 radix mincol padchar commachar commainterval)
273 (let ((*print-base* radix)
275 (if (integerp number)
276 (let* ((text (princ-to-string (abs number)))
277 (commaed (if print-commas-p
278 (format-add-commas text commachar commainterval)
280 (signed (cond ((minusp number)
281 (concatenate 'string "-" commaed))
283 (concatenate 'string "+" commaed))
285 ;; colinc = 1, minpad = 0, padleft = t
286 (format-write-field stream signed mincol 1 0 padchar t))
287 (princ number stream))))
289 (defun format-add-commas (string commachar commainterval)
290 (let ((length (length string)))
291 (multiple-value-bind (commas extra) (truncate (1- length) commainterval)
292 (let ((new-string (make-string (+ length commas)))
293 (first-comma (1+ extra)))
294 (replace new-string string :end1 first-comma :end2 first-comma)
295 (do ((src first-comma (+ src commainterval))
296 (dst first-comma (+ dst commainterval 1)))
298 (setf (schar new-string dst) commachar)
299 (replace new-string string :start1 (1+ dst)
300 :start2 src :end2 (+ src commainterval)))
303 (eval-when (:compile-toplevel :execute)
304 (sb!xc:defmacro interpret-format-integer (base)
305 `(if (or colonp atsignp params)
306 (interpret-bind-defaults
307 ((mincol 0) (padchar #\space) (commachar #\,) (commainterval 3))
309 (format-print-integer stream (next-arg) colonp atsignp ,base mincol
310 padchar commachar commainterval))
311 (let ((*print-base* ,base)
313 (*print-escape* nil))
314 (output-object (next-arg) stream))))
317 (def-format-interpreter #\D (colonp atsignp params)
318 (interpret-format-integer 10))
320 (def-format-interpreter #\B (colonp atsignp params)
321 (interpret-format-integer 2))
323 (def-format-interpreter #\O (colonp atsignp params)
324 (interpret-format-integer 8))
326 (def-format-interpreter #\X (colonp atsignp params)
327 (interpret-format-integer 16))
329 (def-format-interpreter #\R (colonp atsignp params)
330 (interpret-bind-defaults
331 ((base nil) (mincol 0) (padchar #\space) (commachar #\,)
334 (let ((arg (next-arg)))
338 :complaint "~s is not of type INTEGER."
341 (format-print-integer stream arg colonp atsignp base mincol
342 padchar commachar commainterval)
345 (format-print-old-roman stream arg)
346 (format-print-roman stream arg))
348 (format-print-ordinal stream arg)
349 (format-print-cardinal stream arg)))))))
351 (defparameter *cardinal-ones*
352 #(nil "one" "two" "three" "four" "five" "six" "seven" "eight" "nine"))
354 (defparameter *cardinal-tens*
355 #(nil nil "twenty" "thirty" "forty"
356 "fifty" "sixty" "seventy" "eighty" "ninety"))
358 (defparameter *cardinal-teens*
359 #("ten" "eleven" "twelve" "thirteen" "fourteen" ;;; RAD
360 "fifteen" "sixteen" "seventeen" "eighteen" "nineteen"))
362 (defparameter *cardinal-periods*
363 #("" " thousand" " million" " billion" " trillion" " quadrillion"
364 " quintillion" " sextillion" " septillion" " octillion" " nonillion"
365 " decillion" " undecillion" " duodecillion" " tredecillion"
366 " quattuordecillion" " quindecillion" " sexdecillion" " septendecillion"
367 " octodecillion" " novemdecillion" " vigintillion"))
369 (defparameter *ordinal-ones*
370 #(nil "first" "second" "third" "fourth"
371 "fifth" "sixth" "seventh" "eighth" "ninth"))
373 (defparameter *ordinal-tens*
374 #(nil "tenth" "twentieth" "thirtieth" "fortieth"
375 "fiftieth" "sixtieth" "seventieth" "eightieth" "ninetieth"))
377 (defun format-print-small-cardinal (stream n)
378 (multiple-value-bind (hundreds rem) (truncate n 100)
379 (when (plusp hundreds)
380 (write-string (svref *cardinal-ones* hundreds) stream)
381 (write-string " hundred" stream)
383 (write-char #\space stream)))
385 (multiple-value-bind (tens ones) (truncate rem 10)
387 (write-string (svref *cardinal-tens* tens) stream)
389 (write-char #\- stream)
390 (write-string (svref *cardinal-ones* ones) stream)))
392 (write-string (svref *cardinal-teens* ones) stream))
394 (write-string (svref *cardinal-ones* ones) stream)))))))
396 (defun format-print-cardinal (stream n)
398 (write-string "negative " stream)
399 (format-print-cardinal-aux stream (- n) 0 n))
401 (write-string "zero" stream))
403 (format-print-cardinal-aux stream n 0 n))))
405 (defun format-print-cardinal-aux (stream n period err)
406 (multiple-value-bind (beyond here) (truncate n 1000)
407 (unless (<= period 21)
408 (error "number too large to print in English: ~:D" err))
409 (unless (zerop beyond)
410 (format-print-cardinal-aux stream beyond (1+ period) err))
412 (unless (zerop beyond)
413 (write-char #\space stream))
414 (format-print-small-cardinal stream here)
415 (write-string (svref *cardinal-periods* period) stream))))
417 (defun format-print-ordinal (stream n)
419 (write-string "negative " stream))
420 (let ((number (abs n)))
421 (multiple-value-bind (top bot) (truncate number 100)
423 (format-print-cardinal stream (- number bot)))
424 (when (and (plusp top) (plusp bot))
425 (write-char #\space stream))
426 (multiple-value-bind (tens ones) (truncate bot 10)
427 (cond ((= bot 12) (write-string "twelfth" stream))
429 (write-string (svref *cardinal-teens* ones) stream);;;RAD
430 (write-string "th" stream))
431 ((and (zerop tens) (plusp ones))
432 (write-string (svref *ordinal-ones* ones) stream))
433 ((and (zerop ones)(plusp tens))
434 (write-string (svref *ordinal-tens* tens) stream))
436 (write-string (svref *cardinal-tens* tens) stream)
437 (write-char #\- stream)
438 (write-string (svref *ordinal-ones* ones) stream))
440 (write-string "th" stream))
442 (write-string "zeroth" stream)))))))
444 ;;; Print Roman numerals
446 (defun format-print-old-roman (stream n)
448 (error "Number too large to print in old Roman numerals: ~:D" n))
449 (do ((char-list '(#\D #\C #\L #\X #\V #\I) (cdr char-list))
450 (val-list '(500 100 50 10 5 1) (cdr val-list))
451 (cur-char #\M (car char-list))
452 (cur-val 1000 (car val-list))
453 (start n (do ((i start (progn
454 (write-char cur-char stream)
459 (defun format-print-roman (stream n)
461 (error "Number too large to print in Roman numerals: ~:D" n))
462 (do ((char-list '(#\D #\C #\L #\X #\V #\I) (cdr char-list))
463 (val-list '(500 100 50 10 5 1) (cdr val-list))
464 (sub-chars '(#\C #\X #\X #\I #\I) (cdr sub-chars))
465 (sub-val '(100 10 10 1 1 0) (cdr sub-val))
466 (cur-char #\M (car char-list))
467 (cur-val 1000 (car val-list))
468 (cur-sub-char #\C (car sub-chars))
469 (cur-sub-val 100 (car sub-val))
470 (start n (do ((i start (progn
471 (write-char cur-char stream)
474 (cond ((<= (- cur-val cur-sub-val) i)
475 (write-char cur-sub-char stream)
476 (write-char cur-char stream)
477 (- i (- cur-val cur-sub-val)))
483 (def-format-interpreter #\P (colonp atsignp params)
484 (interpret-bind-defaults () params
485 (let ((arg (if colonp
486 (if (eq orig-args args)
488 :complaint "no previous argument")
489 (do ((arg-ptr orig-args (cdr arg-ptr)))
490 ((eq (cdr arg-ptr) args)
494 (write-string (if (eql arg 1) "y" "ies") stream)
495 (unless (eql arg 1) (write-char #\s stream))))))
497 ;;;; format interpreters and support functions for floating point output
499 (defun decimal-string (n)
500 (write-to-string n :base 10 :radix nil :escape nil))
502 (def-format-interpreter #\F (colonp atsignp params)
506 "cannot specify the colon modifier with this directive"))
507 (interpret-bind-defaults ((w nil) (d nil) (k nil) (ovf nil) (pad #\space))
509 (format-fixed stream (next-arg) w d k ovf pad atsignp)))
511 (defun format-fixed (stream number w d k ovf pad atsign)
514 (format-fixed-aux stream number w d k ovf pad atsign))
516 (format-fixed-aux stream (coerce number 'single-float)
517 w d k ovf pad atsign))
519 (format-write-field stream (decimal-string number) w 1 0 #\space t))
521 (format-princ stream number nil nil w 1 0 pad))))
523 ;;; We return true if we overflowed, so that ~G can output the overflow char
524 ;;; instead of spaces.
525 (defun format-fixed-aux (stream number w d k ovf pad atsign)
526 (declare (type float number))
528 ((or (float-infinity-p number)
529 (float-nan-p number))
530 (prin1 number stream)
533 (sb!impl::string-dispatch (single-float double-float)
536 (when (and w (or atsign (minusp (float-sign number))))
538 (multiple-value-bind (str len lpoint tpoint)
539 (sb!impl::flonum-to-string (abs number) spaceleft d k)
540 ;; if caller specifically requested no fraction digits, suppress the
541 ;; optional trailing zero
542 (when (and d (zerop d))
546 ;; optional leading zero
548 (if (or (> spaceleft 0) tpoint) ;force at least one digit
551 ;; optional trailing zero
556 (cond ((and w (< spaceleft 0) ovf)
557 ;; field width overflow
559 (write-char ovf stream))
563 (dotimes (i spaceleft)
564 (write-char pad stream)))
565 (if (minusp (float-sign number))
566 (write-char #\- stream)
568 (write-char #\+ stream)))
570 (write-char #\0 stream))
571 (write-string str stream)
573 (write-char #\0 stream))
576 (def-format-interpreter #\E (colonp atsignp params)
580 "cannot specify the colon modifier with this directive"))
581 (interpret-bind-defaults
582 ((w nil) (d nil) (e nil) (k 1) (ovf nil) (pad #\space) (mark nil))
584 (format-exponential stream (next-arg) w d e k ovf pad mark atsignp)))
586 (defun format-exponential (stream number w d e k ovf pad marker atsign)
589 (format-exp-aux stream number w d e k ovf pad marker atsign)
590 (if (rationalp number)
591 (format-exp-aux stream
592 (coerce number 'single-float)
593 w d e k ovf pad marker atsign)
594 (format-write-field stream
595 (decimal-string number)
597 (format-princ stream number nil nil w 1 0 pad)))
599 (defun format-exponent-marker (number)
600 (if (typep number *read-default-float-format*)
608 ;;; Here we prevent the scale factor from shifting all significance out of
609 ;;; a number to the right. We allow insignificant zeroes to be shifted in
610 ;;; to the left right, athough it is an error to specify k and d such that this
611 ;;; occurs. Perhaps we should detect both these condtions and flag them as
612 ;;; errors. As for now, we let the user get away with it, and merely guarantee
613 ;;; that at least one significant digit will appear.
615 ;;; Raymond Toy writes: The Hyperspec seems to say that the exponent
616 ;;; marker is always printed. Make it so. Also, the original version
617 ;;; causes errors when printing infinities or NaN's. The Hyperspec is
618 ;;; silent here, so let's just print out infinities and NaN's instead
619 ;;; of causing an error.
620 (defun format-exp-aux (stream number w d e k ovf pad marker atsign)
621 (declare (type float number))
622 (if (or (float-infinity-p number)
623 (float-nan-p number))
624 (prin1 number stream)
625 (multiple-value-bind (num expt) (sb!impl::scale-exponent (abs number))
626 (let* ((k (if (= num 1.0) (1- k) k))
628 (estr (decimal-string (abs expt)))
629 (elen (if e (max (length estr) e) (length estr)))
632 (setf spaceleft (- w 2 elen))
633 (when (or atsign (minusp (float-sign number)))
635 (if (and w ovf e (> elen e)) ;exponent overflow
636 (dotimes (i w) (write-char ovf stream))
637 (let* ((fdig (if d (if (plusp k) (1+ (- d k)) d) nil))
638 (fmin (if (minusp k) 1 fdig)))
639 (multiple-value-bind (fstr flen lpoint tpoint)
640 (sb!impl::flonum-to-string num spaceleft fdig k fmin)
641 (when (and d (zerop d)) (setq tpoint nil))
643 (decf spaceleft flen)
645 (if (or (> spaceleft 0) tpoint)
648 (when (and tpoint (<= spaceleft 0))
650 (cond ((and w (< spaceleft 0) ovf)
651 ;;significand overflow
652 (dotimes (i w) (write-char ovf stream)))
654 (dotimes (i spaceleft) (write-char pad stream)))
655 (if (minusp (float-sign number))
656 (write-char #\- stream)
657 (if atsign (write-char #\+ stream)))
658 (when lpoint (write-char #\0 stream))
659 (write-string fstr stream)
660 (write-char (if marker
662 (format-exponent-marker number))
664 (write-char (if (minusp expt) #\- #\+) stream)
666 ;;zero-fill before exponent if necessary
667 (dotimes (i (- e (length estr)))
668 (write-char #\0 stream)))
669 (write-string estr stream))))))))))
671 (def-format-interpreter #\G (colonp atsignp params)
675 "cannot specify the colon modifier with this directive"))
676 (interpret-bind-defaults
677 ((w nil) (d nil) (e nil) (k nil) (ovf nil) (pad #\space) (mark nil))
679 (format-general stream (next-arg) w d e k ovf pad mark atsignp)))
681 (defun format-general (stream number w d e k ovf pad marker atsign)
684 (format-general-aux stream number w d e k ovf pad marker atsign)
685 (if (rationalp number)
686 (format-general-aux stream
687 (coerce number 'single-float)
688 w d e k ovf pad marker atsign)
689 (format-write-field stream
690 (decimal-string number)
692 (format-princ stream number nil nil w 1 0 pad)))
694 ;;; Raymond Toy writes: same change as for format-exp-aux
695 (defun format-general-aux (stream number w d e k ovf pad marker atsign)
696 (declare (type float number))
697 (if (or (float-infinity-p number)
698 (float-nan-p number))
699 (prin1 number stream)
700 (multiple-value-bind (ignore n) (sb!impl::scale-exponent (abs number))
701 (declare (ignore ignore))
702 ;; KLUDGE: Default d if omitted. The procedure is taken directly from
703 ;; the definition given in the manual, and is not very efficient, since
704 ;; we generate the digits twice. Future maintainers are encouraged to
705 ;; improve on this. -- rtoy?? 1998??
707 (multiple-value-bind (str len)
708 (sb!impl::flonum-to-string (abs number))
709 (declare (ignore str))
710 (let ((q (if (= len 1) 1 (1- len))))
711 (setq d (max q (min n 7))))))
712 (let* ((ee (if e (+ e 2) 4))
713 (ww (if w (- w ee) nil))
716 (let ((char (if (format-fixed-aux stream number ww dd nil
720 (dotimes (i ee) (write-char char stream))))
722 (format-exp-aux stream number w d e (or k 1)
723 ovf pad marker atsign)))))))
725 (def-format-interpreter #\$ (colonp atsignp params)
726 (interpret-bind-defaults ((d 2) (n 1) (w 0) (pad #\space)) params
727 (format-dollars stream (next-arg) d n w pad colonp atsignp)))
729 (defun format-dollars (stream number d n w pad colon atsign)
730 (when (rationalp number)
731 ;; This coercion to SINGLE-FLOAT seems as though it gratuitously
732 ;; loses precision (why not LONG-FLOAT?) but it's the default
733 ;; behavior in the ANSI spec, so in some sense it's the right
734 ;; thing, and at least the user shouldn't be surprised.
735 (setq number (coerce number 'single-float)))
737 (let* ((signstr (if (minusp (float-sign number))
740 (signlen (length signstr)))
741 (multiple-value-bind (str strlen ig2 ig3 pointplace)
742 (sb!impl::flonum-to-string number nil d nil)
743 (declare (ignore ig2 ig3 strlen))
745 (write-string signstr stream))
746 (dotimes (i (- w signlen (max n pointplace) 1 d))
747 (write-char pad stream))
749 (write-string signstr stream))
750 (dotimes (i (- n pointplace))
751 (write-char #\0 stream))
752 (write-string str stream)))
753 (format-write-field stream
754 (decimal-string number)
757 ;;;; FORMAT interpreters and support functions for line/page breaks etc.
759 (def-format-interpreter #\% (colonp atsignp params)
760 (when (or colonp atsignp)
763 "cannot specify either colon or atsign for this directive"))
764 (interpret-bind-defaults ((count 1)) params
768 (def-format-interpreter #\& (colonp atsignp params)
769 (when (or colonp atsignp)
772 "cannot specify either colon or atsign for this directive"))
773 (interpret-bind-defaults ((count 1)) params
776 (dotimes (i (1- count))
779 (def-format-interpreter #\| (colonp atsignp params)
780 (when (or colonp atsignp)
783 "cannot specify either colon or atsign for this directive"))
784 (interpret-bind-defaults ((count 1)) params
786 (write-char (code-char form-feed-char-code) stream))))
788 (def-format-interpreter #\~ (colonp atsignp params)
789 (when (or colonp atsignp)
792 "cannot specify either colon or atsign for this directive"))
793 (interpret-bind-defaults ((count 1)) params
795 (write-char #\~ stream))))
797 (def-complex-format-interpreter #\newline (colonp atsignp params directives)
798 (when (and colonp atsignp)
801 "cannot specify both colon and atsign for this directive"))
802 (interpret-bind-defaults () params
804 (write-char #\newline stream)))
805 (if (and (not colonp)
807 (simple-string-p (car directives)))
808 (cons (string-left-trim *format-whitespace-chars*
813 ;;;; format interpreters and support functions for tabs and simple pretty
816 (def-format-interpreter #\T (colonp atsignp params)
818 (interpret-bind-defaults ((n 1) (m 1)) params
819 (pprint-tab (if atsignp :section-relative :section) n m stream))
821 (interpret-bind-defaults ((colrel 1) (colinc 1)) params
822 (format-relative-tab stream colrel colinc))
823 (interpret-bind-defaults ((colnum 1) (colinc 1)) params
824 (format-absolute-tab stream colnum colinc)))))
826 (defun output-spaces (stream n)
827 (let ((spaces #.(make-string 100 :initial-element #\space)))
829 (when (< n (length spaces))
831 (write-string spaces stream)
832 (decf n (length spaces)))
833 (write-string spaces stream :end n)))
835 (defun format-relative-tab (stream colrel colinc)
836 (if (sb!pretty:pretty-stream-p stream)
837 (pprint-tab :line-relative colrel colinc stream)
838 (let* ((cur (sb!impl::charpos stream))
839 (spaces (if (and cur (plusp colinc))
840 (- (* (ceiling (+ cur colrel) colinc) colinc) cur)
842 (output-spaces stream spaces))))
844 (defun format-absolute-tab (stream colnum colinc)
845 (if (sb!pretty:pretty-stream-p stream)
846 (pprint-tab :line colnum colinc stream)
847 (let ((cur (sb!impl::charpos stream)))
849 (write-string " " stream))
851 (output-spaces stream (- colnum cur)))
853 (unless (zerop colinc)
854 (output-spaces stream
855 (- colinc (rem (- cur colnum) colinc)))))))))
857 (def-format-interpreter #\_ (colonp atsignp params)
858 (interpret-bind-defaults () params
859 (pprint-newline (if colonp
868 (def-format-interpreter #\I (colonp atsignp params)
871 :complaint "cannot specify the at-sign modifier"))
872 (interpret-bind-defaults ((n 0)) params
873 (pprint-indent (if colonp :current :block) n stream)))
875 ;;;; format interpreter for ~*
877 (def-format-interpreter #\* (colonp atsignp params)
881 :complaint "cannot specify both colon and at-sign")
882 (interpret-bind-defaults ((posn 0)) params
883 (if (<= 0 posn (length orig-args))
884 (setf args (nthcdr posn orig-args))
886 :complaint "Index ~W is out of bounds. (It should ~
887 have been between 0 and ~W.)"
888 :args (list posn (length orig-args))))))
890 (interpret-bind-defaults ((n 1)) params
891 (do ((cur-posn 0 (1+ cur-posn))
892 (arg-ptr orig-args (cdr arg-ptr)))
894 (let ((new-posn (- cur-posn n)))
895 (if (<= 0 new-posn (length orig-args))
896 (setf args (nthcdr new-posn orig-args))
899 "Index ~W is out of bounds. (It should
900 have been between 0 and ~W.)"
902 (list new-posn (length orig-args))))))))
903 (interpret-bind-defaults ((n 1)) params
907 ;;;; format interpreter for indirection
909 (def-format-interpreter #\? (colonp atsignp params string end)
912 :complaint "cannot specify the colon modifier"))
913 (interpret-bind-defaults () params
919 "~A~%while processing indirect format string:"
920 :args (list condition)
922 :control-string string
925 (setf args (%format stream (next-arg) orig-args args))
926 (%format stream (next-arg) (next-arg))))))
928 ;;;; format interpreters for capitalization
930 (def-complex-format-interpreter #\( (colonp atsignp params directives)
931 (let ((close (find-directive directives #\) nil)))
934 :complaint "no corresponding close paren"))
935 (interpret-bind-defaults () params
936 (let* ((posn (position close directives))
937 (before (subseq directives 0 posn))
938 (after (nthcdr (1+ posn) directives))
939 (stream (make-case-frob-stream stream
947 (setf args (interpret-directive-list stream before orig-args args))
950 (def-complex-format-interpreter #\) ()
952 :complaint "no corresponding open paren"))
954 ;;;; format interpreters and support functions for conditionalization
956 (def-complex-format-interpreter #\[ (colonp atsignp params directives)
957 (multiple-value-bind (sublists last-semi-with-colon-p remaining)
958 (parse-conditional-directive directives)
964 "cannot specify both the colon and at-sign modifiers")
968 "can only specify one section")
969 (interpret-bind-defaults () params
970 (let ((prev-args args)
973 (interpret-directive-list stream
979 (if (= (length sublists) 2)
980 (interpret-bind-defaults () params
982 (interpret-directive-list stream (car sublists)
984 (interpret-directive-list stream (cadr sublists)
988 "must specify exactly two sections"))
989 (interpret-bind-defaults ((index (next-arg))) params
990 (let* ((default (and last-semi-with-colon-p
992 (last (1- (length sublists)))
994 (if (<= 0 index last)
995 (nth (- last index) sublists)
997 (interpret-directive-list stream sublist orig-args
1001 (def-complex-format-interpreter #\; ()
1002 (error 'format-error
1004 "~~; not contained within either ~~[...~~] or ~~<...~~>"))
1006 (def-complex-format-interpreter #\] ()
1007 (error 'format-error
1009 "no corresponding open bracket"))
1011 ;;;; format interpreter for up-and-out
1013 (defvar *outside-args*)
1015 (def-format-interpreter #\^ (colonp atsignp params)
1017 (error 'format-error
1018 :complaint "cannot specify the at-sign modifier"))
1019 (when (and colonp (not *up-up-and-out-allowed*))
1020 (error 'format-error
1021 :complaint "attempt to use ~~:^ outside a ~~:{...~~} construct"))
1022 (when (interpret-bind-defaults ((arg1 nil) (arg2 nil) (arg3 nil)) params
1023 (cond (arg3 (<= arg1 arg2 arg3))
1024 (arg2 (eql arg1 arg2))
1027 (null *outside-args*)
1029 (throw (if colonp 'up-up-and-out 'up-and-out)
1032 ;;;; format interpreters for iteration
1034 (def-complex-format-interpreter #\{
1035 (colonp atsignp params string end directives)
1036 (let ((close (find-directive directives #\} nil)))
1038 (error 'format-error
1040 "no corresponding close brace"))
1041 (interpret-bind-defaults ((max-count nil)) params
1042 (let* ((closed-with-colon (format-directive-colonp close))
1043 (posn (position close directives))
1044 (insides (if (zerop posn)
1046 (subseq directives 0 posn)))
1047 (*up-up-and-out-allowed* colonp))
1049 ((do-guts (orig-args args)
1057 "~A~%while processing indirect format string:"
1058 :args (list condition)
1060 :control-string string
1061 :offset (1- end)))))
1062 (%format stream insides orig-args args))
1063 (interpret-directive-list stream insides
1065 (bind-args (orig-args args)
1067 (let* ((arg (next-arg))
1068 (*logical-block-popper* nil)
1069 (*outside-args* args))
1073 (do-guts orig-args args)))
1074 (do-loop (orig-args args)
1075 (catch (if colonp 'up-up-and-out 'up-and-out)
1077 (when (and (not closed-with-colon) (null args))
1079 (when (and max-count (minusp (decf max-count)))
1081 (setf args (bind-args orig-args args))
1082 (when (and closed-with-colon (null args))
1086 (setf args (do-loop orig-args args))
1087 (let ((arg (next-arg))
1088 (*logical-block-popper* nil))
1090 (nthcdr (1+ posn) directives))))))
1092 (def-complex-format-interpreter #\} ()
1093 (error 'format-error
1094 :complaint "no corresponding open brace"))
1096 ;;;; format interpreters and support functions for justification
1098 (def-complex-format-interpreter #\<
1099 (colonp atsignp params string end directives)
1100 (multiple-value-bind (segments first-semi close remaining)
1101 (parse-format-justification directives)
1103 (if (format-directive-colonp close) ; logical block vs. justification
1104 (multiple-value-bind (prefix per-line-p insides suffix)
1105 (parse-format-logical-block segments colonp first-semi
1106 close params string end)
1107 (interpret-format-logical-block stream orig-args args
1108 prefix per-line-p insides
1110 (let ((count (reduce #'+ (mapcar (lambda (x) (count-if #'illegal-inside-justification-p x)) segments))))
1112 ;; ANSI specifies that "an error is signalled" in this
1114 (error 'format-error
1115 :complaint "~D illegal directive~:P found inside justification block"
1117 :references (list '(:ansi-cl :section (22 3 5 2)))))
1118 ;; ANSI does not explicitly say that an error should
1119 ;; be signalled, but the @ modifier is not explicitly
1120 ;; allowed for ~> either.
1121 (when (format-directive-atsignp close)
1122 (error 'format-error
1123 :complaint "@ modifier not allowed in close ~
1124 directive of justification ~
1125 block (i.e. ~~<...~~@>."
1126 :offset (1- (format-directive-end close))
1127 :references (list '(:ansi-cl :section (22 3 6 2)))))
1128 (interpret-format-justification stream orig-args args
1129 segments colonp atsignp
1130 first-semi params))))
1133 (defun interpret-format-justification
1134 (stream orig-args args segments colonp atsignp first-semi params)
1135 (interpret-bind-defaults
1136 ((mincol 0) (colinc 1) (minpad 0) (padchar #\space))
1138 (let ((newline-string nil)
1144 (when (and first-semi (format-directive-colonp first-semi))
1145 (interpret-bind-defaults
1147 (len (or (sb!impl::line-length stream) 72)))
1148 (format-directive-params first-semi)
1149 (setf newline-string
1150 (with-output-to-string (stream)
1152 (interpret-directive-list stream
1156 (setf extra-space extra)
1157 (setf line-len len)))
1158 (dolist (segment segments)
1159 (push (with-output-to-string (stream)
1161 (interpret-directive-list stream segment
1165 (format-justification stream newline-string extra-space line-len strings
1166 colonp atsignp mincol colinc minpad padchar)))
1169 (defun format-justification (stream newline-prefix extra-space line-len strings
1170 pad-left pad-right mincol colinc minpad padchar)
1171 (setf strings (reverse strings))
1172 (let* ((num-gaps (+ (1- (length strings))
1174 (if pad-right 1 0)))
1175 (chars (+ (* num-gaps minpad)
1177 for string in strings
1178 summing (length string))))
1179 (length (if (> chars mincol)
1180 (+ mincol (* (ceiling (- chars mincol) colinc) colinc))
1182 (padding (+ (- length chars) (* num-gaps minpad))))
1183 (when (and newline-prefix
1184 (> (+ (or (sb!impl::charpos stream) 0)
1187 (write-string newline-prefix stream))
1188 (flet ((do-padding ()
1190 (if (zerop num-gaps) padding (truncate padding num-gaps))))
1191 (decf padding pad-len)
1193 (dotimes (i pad-len) (write-char padchar stream)))))
1194 (when (or pad-left (and (not pad-right) (null (cdr strings))))
1197 (write-string (car strings) stream)
1198 (dolist (string (cdr strings))
1200 (write-string string stream)))
1204 (defun interpret-format-logical-block
1205 (stream orig-args args prefix per-line-p insides suffix atsignp)
1206 (let ((arg (if atsignp args (next-arg))))
1208 (pprint-logical-block
1209 (stream arg :per-line-prefix prefix :suffix suffix)
1210 (let ((*logical-block-popper* (lambda () (pprint-pop))))
1212 (interpret-directive-list stream insides
1213 (if atsignp orig-args arg)
1215 (pprint-logical-block (stream arg :prefix prefix :suffix suffix)
1216 (let ((*logical-block-popper* (lambda () (pprint-pop))))
1218 (interpret-directive-list stream insides
1219 (if atsignp orig-args arg)
1221 (if atsignp nil args))
1223 ;;;; format interpreter and support functions for user-defined method
1225 (def-format-interpreter #\/ (string start end colonp atsignp params)
1226 (let ((symbol (extract-user-fun-name string start end)))
1228 (dolist (param-and-offset params)
1229 (let ((param (cdr param-and-offset)))
1231 (:arg (args (next-arg)))
1232 (:remaining (args (length args)))
1234 (apply (fdefinition symbol) stream (next-arg) colonp atsignp (args)))))