1 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
10 (in-package "SB!FORMAT")
12 (define-condition format-error (error reference-condition)
13 ((complaint :reader format-error-complaint :initarg :complaint)
14 (args :reader format-error-args :initarg :args :initform nil)
15 (control-string :reader format-error-control-string
16 :initarg :control-string
17 :initform *default-format-error-control-string*)
18 (offset :reader format-error-offset :initarg :offset
19 :initform *default-format-error-offset*)
20 (second-relative :reader format-error-second-relative
21 :initarg :second-relative :initform nil)
22 (print-banner :reader format-error-print-banner :initarg :print-banner
24 (:report %print-format-error)
25 (:default-initargs :references nil))
27 (defun %print-format-error (condition stream)
29 "~:[~*~;error in ~S: ~]~?~@[~% ~A~% ~V@T^~@[~V@T^~]~]"
30 (format-error-print-banner condition)
32 (format-error-complaint condition)
33 (format-error-args condition)
34 (format-error-control-string condition)
35 (format-error-offset condition)
36 (format-error-second-relative condition)))
38 (def!struct format-directive
39 (string (missing-arg) :type simple-string)
40 (start (missing-arg) :type (and unsigned-byte fixnum))
41 (end (missing-arg) :type (and unsigned-byte fixnum))
42 (character (missing-arg) :type character)
43 (colonp nil :type (member t nil))
44 (atsignp nil :type (member t nil))
45 (params nil :type list))
46 (def!method print-object ((x format-directive) stream)
47 (print-unreadable-object (x stream)
48 (write-string (format-directive-string x)
50 :start (format-directive-start x)
51 :end (format-directive-end x))))
53 ;;;; TOKENIZE-CONTROL-STRING
55 (defun tokenize-control-string (string)
56 (declare (simple-string string))
60 ;; FIXME: consider rewriting this 22.3.5.2-related processing
61 ;; using specials to maintain state and doing the logic inside
62 ;; the directive expanders themselves.
66 (justification-semicolon))
68 (let ((next-directive (or (position #\~ string :start index) end)))
69 (when (> next-directive index)
70 (push (subseq string index next-directive) result))
71 (when (= next-directive end)
73 (let* ((directive (parse-directive string next-directive))
74 (char (format-directive-character directive)))
75 ;; this processing is required by CLHS 22.3.5.2
77 ((char= char #\<) (push directive block))
78 ((and block (char= char #\;) (format-directive-colonp directive))
79 (setf semicolon directive))
83 ((format-directive-colonp directive)
85 (setf pprint (car block)))
88 (unless justification-semicolon
89 (setf justification-semicolon semicolon))))
91 ;; block cases are handled by the #\< expander/interpreter
94 ((#\W #\I #\_) (unless pprint (setf pprint directive)))
95 (#\T (when (and (format-directive-colonp directive)
97 (setf pprint directive))))))
98 (push directive result)
99 (setf index (format-directive-end directive)))))
100 (when (and pprint justification-semicolon)
101 (let ((pprint-offset (1- (format-directive-end pprint)))
102 (justification-offset
103 (1- (format-directive-end justification-semicolon))))
105 :complaint "misuse of justification and pprint directives"
106 :control-string string
107 :offset (min pprint-offset justification-offset)
108 :second-relative (- (max pprint-offset justification-offset)
109 (min pprint-offset justification-offset)
111 :references (list '(:ansi-cl :section (22 3 5 2))))))
114 (defun parse-directive (string start)
115 (let ((posn (1+ start)) (params nil) (colonp nil) (atsignp nil)
116 (end (length string)))
120 :complaint "string ended before directive was found"
121 :control-string string
123 (schar string posn)))
125 (when (or colonp atsignp)
127 :complaint "parameters found after #\\: or #\\@ modifier"
128 :control-string string
130 :references (list '(:ansi-cl :section (22 3)))))))
132 (let ((char (get-char)))
133 (cond ((or (char<= #\0 char #\9) (char= char #\+) (char= char #\-))
135 (multiple-value-bind (param new-posn)
136 (parse-integer string :start posn :junk-allowed t)
137 (push (cons posn param) params)
145 ((or (char= char #\v)
148 (push (cons posn :arg) params)
158 (push (cons posn :remaining) params)
169 (push (cons posn (get-char)) params)
171 (unless (char= (get-char) #\,)
175 (push (cons posn nil) params))
179 :complaint "too many colons supplied"
180 :control-string string
182 :references (list '(:ansi-cl :section (22 3))))
187 :complaint "too many #\\@ characters supplied"
188 :control-string string
190 :references (list '(:ansi-cl :section (22 3))))
193 (when (and (char= (schar string (1- posn)) #\,)
195 (char/= (schar string (- posn 2)) #\')))
197 (push (cons (1- posn) nil) params))
200 (let ((char (get-char)))
201 (when (char= char #\/)
202 (let ((closing-slash (position #\/ string :start (1+ posn))))
204 (setf posn closing-slash)
206 :complaint "no matching closing slash"
207 :control-string string
209 (make-format-directive
210 :string string :start start :end (1+ posn)
211 :character (char-upcase char)
212 :colonp colonp :atsignp atsignp
213 :params (nreverse params))))))
217 (sb!xc:defmacro formatter (control-string)
218 `#',(%formatter control-string))
220 (defun %formatter (control-string)
222 (catch 'need-orig-args
223 (let* ((*simple-args* nil)
224 (*only-simple-args* t)
225 (guts (expand-control-string control-string))
227 (dolist (arg *simple-args*)
231 :complaint "required argument missing"
232 :control-string ,control-string
235 (return `(lambda (stream &optional ,@args &rest args)
238 (let ((*orig-args-available* t)
239 (*only-simple-args* nil))
240 `(lambda (stream &rest orig-args)
241 (let ((args orig-args))
242 ,(expand-control-string control-string)
245 (defun expand-control-string (string)
246 (let* ((string (etypecase string
250 (coerce string 'simple-string))))
251 (*default-format-error-control-string* string)
252 (directives (tokenize-control-string string)))
254 ,@(expand-directive-list directives))))
256 (defun expand-directive-list (directives)
258 (remaining-directives directives))
260 (unless remaining-directives
262 (multiple-value-bind (form new-directives)
263 (expand-directive (car remaining-directives)
264 (cdr remaining-directives))
266 (setf remaining-directives new-directives)))
269 (defun expand-directive (directive more-directives)
273 (let ((char (format-directive-character directive)))
276 (aref *format-directive-expanders* (char-code char)))
278 (*default-format-error-offset*
279 (1- (format-directive-end directive))))
280 (declare (type (or null function) expander))
282 (funcall expander directive more-directives)
284 :complaint "unknown directive ~@[(character: ~A)~]"
285 :args (list (char-name (format-directive-character directive)))))))
287 (values `(write-string ,directive stream)
290 (defmacro-mundanely expander-next-arg (string offset)
294 :complaint "no more arguments"
295 :control-string ,string
298 (defun expand-next-arg (&optional offset)
299 (if (or *orig-args-available* (not *only-simple-args*))
300 `(,*expander-next-arg-macro*
301 ,*default-format-error-control-string*
302 ,(or offset *default-format-error-offset*))
303 (let ((symbol (gensym "FORMAT-ARG-")))
304 (push (cons symbol (or offset *default-format-error-offset*))
308 (defmacro expand-bind-defaults (specs params &body body)
309 (once-only ((params params))
311 (collect ((expander-bindings) (runtime-bindings))
313 (destructuring-bind (var default) spec
314 (let ((symbol (gensym)))
319 (let* ((param-and-offset (pop ,params))
320 (offset (car param-and-offset))
321 (param (cdr param-and-offset)))
323 (:arg `(or ,(expand-next-arg offset)
326 (setf *only-simple-args* nil)
330 `(let ,(expander-bindings)
331 `(let ,(list ,@(runtime-bindings))
336 "too many parameters, expected no more than ~W"
337 :args (list ,(length specs))
338 :offset (caar ,params)))
343 :complaint "too many parameters, expected none"
344 :offset (caar ,params)))
347 ;;;; format directive machinery
349 ;;; FIXME: only used in this file, could be SB!XC:DEFMACRO in EVAL-WHEN
350 (defmacro def-complex-format-directive (char lambda-list &body body)
351 (let ((defun-name (intern (format nil
352 "~:@(~:C~)-FORMAT-DIRECTIVE-EXPANDER"
355 (directives (if lambda-list (car (last lambda-list)) (gensym))))
357 (defun ,defun-name (,directive ,directives)
359 `((let ,(mapcar (lambda (var)
361 (,(symbolicate "FORMAT-DIRECTIVE-" var)
363 (butlast lambda-list))
365 `((declare (ignore ,directive ,directives))
367 (%set-format-directive-expander ,char #',defun-name))))
369 ;;; FIXME: only used in this file, could be SB!XC:DEFMACRO in EVAL-WHEN
370 (defmacro def-format-directive (char lambda-list &body body)
371 (let ((directives (gensym))
373 (body-without-decls body))
375 (let ((form (car body-without-decls)))
376 (unless (and (consp form) (eq (car form) 'declare))
378 (push (pop body-without-decls) declarations)))
379 (setf declarations (reverse declarations))
380 `(def-complex-format-directive ,char (,@lambda-list ,directives)
382 (values (progn ,@body-without-decls)
385 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
387 (defun %set-format-directive-expander (char fn)
388 (setf (aref *format-directive-expanders* (char-code (char-upcase char))) fn)
391 (defun %set-format-directive-interpreter (char fn)
392 (setf (aref *format-directive-interpreters*
393 (char-code (char-upcase char)))
397 (defun find-directive (directives kind stop-at-semi)
399 (let ((next (car directives)))
400 (if (format-directive-p next)
401 (let ((char (format-directive-character next)))
402 (if (or (char= kind char)
403 (and stop-at-semi (char= char #\;)))
406 (cdr (flet ((after (char)
407 (member (find-directive (cdr directives)
418 (find-directive (cdr directives) kind stop-at-semi)))))
422 ;;;; format directives for simple output
424 (def-format-directive #\A (colonp atsignp params)
426 (expand-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
429 `(format-princ stream ,(expand-next-arg) ',colonp ',atsignp
430 ,mincol ,colinc ,minpad ,padchar))
432 `(or ,(expand-next-arg) "()")
436 (def-format-directive #\S (colonp atsignp params)
438 (expand-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
441 `(format-prin1 stream ,(expand-next-arg) ,colonp ,atsignp
442 ,mincol ,colinc ,minpad ,padchar)))
444 `(let ((arg ,(expand-next-arg)))
447 (princ "()" stream))))
449 `(prin1 ,(expand-next-arg) stream))))
451 (def-format-directive #\C (colonp atsignp params)
452 (expand-bind-defaults () params
454 `(format-print-named-character ,(expand-next-arg) stream)
456 `(prin1 ,(expand-next-arg) stream)
457 `(write-char ,(expand-next-arg) stream)))))
459 (def-format-directive #\W (colonp atsignp params)
460 (expand-bind-defaults () params
461 (if (or colonp atsignp)
462 `(let (,@(when colonp
463 '((*print-pretty* t)))
465 '((*print-level* nil)
466 (*print-length* nil))))
467 (output-object ,(expand-next-arg) stream))
468 `(output-object ,(expand-next-arg) stream))))
470 ;;;; format directives for integer output
472 (defun expand-format-integer (base colonp atsignp params)
473 (if (or colonp atsignp params)
474 (expand-bind-defaults
475 ((mincol 0) (padchar #\space) (commachar #\,) (commainterval 3))
477 `(format-print-integer stream ,(expand-next-arg) ,colonp ,atsignp
478 ,base ,mincol ,padchar ,commachar
480 `(write ,(expand-next-arg) :stream stream :base ,base :radix nil
483 (def-format-directive #\D (colonp atsignp params)
484 (expand-format-integer 10 colonp atsignp params))
486 (def-format-directive #\B (colonp atsignp params)
487 (expand-format-integer 2 colonp atsignp params))
489 (def-format-directive #\O (colonp atsignp params)
490 (expand-format-integer 8 colonp atsignp params))
492 (def-format-directive #\X (colonp atsignp params)
493 (expand-format-integer 16 colonp atsignp params))
495 (def-format-directive #\R (colonp atsignp params)
497 (expand-bind-defaults
498 ((base 10) (mincol 0) (padchar #\space) (commachar #\,)
501 `(format-print-integer stream ,(expand-next-arg) ,colonp ,atsignp
503 ,padchar ,commachar ,commainterval))
506 `(format-print-old-roman stream ,(expand-next-arg))
507 `(format-print-roman stream ,(expand-next-arg)))
509 `(format-print-ordinal stream ,(expand-next-arg))
510 `(format-print-cardinal stream ,(expand-next-arg))))))
512 ;;;; format directive for pluralization
514 (def-format-directive #\P (colonp atsignp params end)
515 (expand-bind-defaults () params
519 (*orig-args-available*
520 `(if (eq orig-args args)
522 :complaint "no previous argument"
524 (do ((arg-ptr orig-args (cdr arg-ptr)))
525 ((eq (cdr arg-ptr) args)
528 (unless *simple-args*
530 :complaint "no previous argument"))
531 (caar *simple-args*))
533 (/show0 "THROWing NEED-ORIG-ARGS from tilde-P")
534 (throw 'need-orig-args nil)))))
536 `(write-string (if (eql ,arg 1) "y" "ies") stream)
537 `(unless (eql ,arg 1) (write-char #\s stream))))))
539 ;;;; format directives for floating point output
541 (def-format-directive #\F (colonp atsignp params)
545 "The colon modifier cannot be used with this directive."))
546 (expand-bind-defaults ((w nil) (d nil) (k nil) (ovf nil) (pad #\space)) params
547 `(format-fixed stream ,(expand-next-arg) ,w ,d ,k ,ovf ,pad ,atsignp)))
549 (def-format-directive #\E (colonp atsignp params)
553 "The colon modifier cannot be used with this directive."))
554 (expand-bind-defaults
555 ((w nil) (d nil) (e nil) (k 1) (ovf nil) (pad #\space) (mark nil))
557 `(format-exponential stream ,(expand-next-arg) ,w ,d ,e ,k ,ovf ,pad ,mark
560 (def-format-directive #\G (colonp atsignp params)
564 "The colon modifier cannot be used with this directive."))
565 (expand-bind-defaults
566 ((w nil) (d nil) (e nil) (k nil) (ovf nil) (pad #\space) (mark nil))
568 `(format-general stream ,(expand-next-arg) ,w ,d ,e ,k ,ovf ,pad ,mark ,atsignp)))
570 (def-format-directive #\$ (colonp atsignp params)
571 (expand-bind-defaults ((d 2) (n 1) (w 0) (pad #\space)) params
572 `(format-dollars stream ,(expand-next-arg) ,d ,n ,w ,pad ,colonp
575 ;;;; format directives for line/page breaks etc.
577 (def-format-directive #\% (colonp atsignp params)
578 (when (or colonp atsignp)
581 "The colon and atsign modifiers cannot be used with this directive."
584 (expand-bind-defaults ((count 1)) params
589 (def-format-directive #\& (colonp atsignp params)
590 (when (or colonp atsignp)
593 "The colon and atsign modifiers cannot be used with this directive."
596 (expand-bind-defaults ((count 1)) params
599 (dotimes (i (1- ,count))
601 '(fresh-line stream)))
603 (def-format-directive #\| (colonp atsignp params)
604 (when (or colonp atsignp)
607 "The colon and atsign modifiers cannot be used with this directive."
610 (expand-bind-defaults ((count 1)) params
612 (write-char (code-char form-feed-char-code) stream)))
613 '(write-char (code-char form-feed-char-code) stream)))
615 (def-format-directive #\~ (colonp atsignp params)
616 (when (or colonp atsignp)
619 "The colon and atsign modifiers cannot be used with this directive."
622 (expand-bind-defaults ((count 1)) params
624 (write-char #\~ stream)))
625 '(write-char #\~ stream)))
627 (def-complex-format-directive #\newline (colonp atsignp params directives)
628 (when (and colonp atsignp)
630 :complaint "both colon and atsign modifiers used simultaneously"))
631 (values (expand-bind-defaults () params
633 '(write-char #\newline stream)
635 (if (and (not colonp)
637 (simple-string-p (car directives)))
638 (cons (string-left-trim *format-whitespace-chars*
643 ;;;; format directives for tabs and simple pretty printing
645 (def-format-directive #\T (colonp atsignp params)
647 (expand-bind-defaults ((n 1) (m 1)) params
648 `(pprint-tab ,(if atsignp :section-relative :section)
651 (expand-bind-defaults ((colrel 1) (colinc 1)) params
652 `(format-relative-tab stream ,colrel ,colinc))
653 (expand-bind-defaults ((colnum 1) (colinc 1)) params
654 `(format-absolute-tab stream ,colnum ,colinc)))))
656 (def-format-directive #\_ (colonp atsignp params)
657 (expand-bind-defaults () params
658 `(pprint-newline ,(if colonp
667 (def-format-directive #\I (colonp atsignp params)
671 "cannot use the at-sign modifier with this directive"))
672 (expand-bind-defaults ((n 0)) params
673 `(pprint-indent ,(if colonp :current :block) ,n stream)))
675 ;;;; format directive for ~*
677 (def-format-directive #\* (colonp atsignp params end)
682 "both colon and atsign modifiers used simultaneously")
683 (expand-bind-defaults ((posn 0)) params
684 (unless *orig-args-available*
685 (/show0 "THROWing NEED-ORIG-ARGS from tilde-@*")
686 (throw 'need-orig-args nil))
687 `(if (<= 0 ,posn (length orig-args))
688 (setf args (nthcdr ,posn orig-args))
690 :complaint "Index ~W out of bounds. Should have been ~
692 :args (list ,posn (length orig-args))
693 :offset ,(1- end)))))
695 (expand-bind-defaults ((n 1)) params
696 (unless *orig-args-available*
697 (/show0 "THROWing NEED-ORIG-ARGS from tilde-:*")
698 (throw 'need-orig-args nil))
699 `(do ((cur-posn 0 (1+ cur-posn))
700 (arg-ptr orig-args (cdr arg-ptr)))
702 (let ((new-posn (- cur-posn ,n)))
703 (if (<= 0 new-posn (length orig-args))
704 (setf args (nthcdr new-posn orig-args))
707 "Index ~W is out of bounds; should have been ~
709 :args (list new-posn (length orig-args))
710 :offset ,(1- end)))))))
712 (expand-bind-defaults ((n 1)) params
713 (setf *only-simple-args* nil)
716 (expand-next-arg)))))
718 ;;;; format directive for indirection
720 (def-format-directive #\? (colonp atsignp params string end)
723 :complaint "cannot use the colon modifier with this directive"))
724 (expand-bind-defaults () params
730 "~A~%while processing indirect format string:"
731 :args (list condition)
733 :control-string ,string
734 :offset ,(1- end)))))
736 (if *orig-args-available*
737 `(setf args (%format stream ,(expand-next-arg) orig-args args))
738 (throw 'need-orig-args nil))
739 `(%format stream ,(expand-next-arg) ,(expand-next-arg))))))
741 ;;;; format directives for capitalization
743 (def-complex-format-directive #\( (colonp atsignp params directives)
744 (let ((close (find-directive directives #\) nil)))
747 :complaint "no corresponding close parenthesis"))
748 (let* ((posn (position close directives))
749 (before (subseq directives 0 posn))
750 (after (nthcdr (1+ posn) directives)))
752 (expand-bind-defaults () params
753 `(let ((stream (make-case-frob-stream stream
761 ,@(expand-directive-list before)))
764 (def-complex-format-directive #\) ()
766 :complaint "no corresponding open parenthesis"))
768 ;;;; format directives and support functions for conditionalization
770 (def-complex-format-directive #\[ (colonp atsignp params directives)
771 (multiple-value-bind (sublists last-semi-with-colon-p remaining)
772 (parse-conditional-directive directives)
778 "both colon and atsign modifiers used simultaneously")
782 "Can only specify one section")
783 (expand-bind-defaults () params
784 (expand-maybe-conditional (car sublists)))))
786 (if (= (length sublists) 2)
787 (expand-bind-defaults () params
788 (expand-true-false-conditional (car sublists)
792 "must specify exactly two sections"))
793 (expand-bind-defaults ((index (expand-next-arg))) params
794 (setf *only-simple-args* nil)
796 (when last-semi-with-colon-p
797 (push `(t ,@(expand-directive-list (pop sublists)))
799 (let ((count (length sublists)))
800 (dolist (sublist sublists)
801 (push `(,(decf count)
802 ,@(expand-directive-list sublist))
804 `(case ,index ,@clauses)))))
807 (defun parse-conditional-directive (directives)
809 (last-semi-with-colon-p nil)
810 (remaining directives))
812 (let ((close-or-semi (find-directive remaining #\] t)))
813 (unless close-or-semi
815 :complaint "no corresponding close bracket"))
816 (let ((posn (position close-or-semi remaining)))
817 (push (subseq remaining 0 posn) sublists)
818 (setf remaining (nthcdr (1+ posn) remaining))
819 (when (char= (format-directive-character close-or-semi) #\])
821 (setf last-semi-with-colon-p
822 (format-directive-colonp close-or-semi)))))
823 (values sublists last-semi-with-colon-p remaining)))
825 (defun expand-maybe-conditional (sublist)
827 `(let ((prev-args args)
828 (arg ,(expand-next-arg)))
830 (setf args prev-args)
831 ,@(expand-directive-list sublist)))))
832 (if *only-simple-args*
833 (multiple-value-bind (guts new-args)
834 (let ((*simple-args* *simple-args*))
835 (values (expand-directive-list sublist)
837 (cond ((eq *simple-args* (cdr new-args))
838 (setf *simple-args* new-args)
839 `(when ,(caar new-args)
842 (setf *only-simple-args* nil)
846 (defun expand-true-false-conditional (true false)
847 (let ((arg (expand-next-arg)))
851 ,@(expand-directive-list true))
853 ,@(expand-directive-list false)))))
854 (if *only-simple-args*
855 (multiple-value-bind (true-guts true-args true-simple)
856 (let ((*simple-args* *simple-args*)
857 (*only-simple-args* t))
858 (values (expand-directive-list true)
861 (multiple-value-bind (false-guts false-args false-simple)
862 (let ((*simple-args* *simple-args*)
863 (*only-simple-args* t))
864 (values (expand-directive-list false)
867 (if (= (length true-args) (length false-args))
871 ,(do ((false false-args (cdr false))
872 (true true-args (cdr true))
873 (bindings nil (cons `(,(caar false) ,(caar true))
875 ((eq true *simple-args*)
876 (setf *simple-args* true-args)
877 (setf *only-simple-args*
878 (and true-simple false-simple))
885 (setf *only-simple-args* nil)
889 (def-complex-format-directive #\; ()
892 "~~; directive not contained within either ~~[...~~] or ~~<...~~>"))
894 (def-complex-format-directive #\] ()
897 "no corresponding open bracket"))
899 ;;;; format directive for up-and-out
901 (def-format-directive #\^ (colonp atsignp params)
904 :complaint "cannot use the at-sign modifier with this directive"))
905 (when (and colonp (not *up-up-and-out-allowed*))
907 :complaint "attempt to use ~~:^ outside a ~~:{...~~} construct"))
908 `(when ,(case (length params)
912 (setf *only-simple-args* nil)
914 (1 (expand-bind-defaults ((count 0)) params
916 (2 (expand-bind-defaults ((arg1 0) (arg2 0)) params
918 (t (expand-bind-defaults ((arg1 0) (arg2 0) (arg3 0)) params
919 `(<= ,arg1 ,arg2 ,arg3))))
921 '(return-from outside-loop nil)
924 ;;;; format directives for iteration
926 (def-complex-format-directive #\{ (colonp atsignp params string end directives)
927 (let ((close (find-directive directives #\} nil)))
930 :complaint "no corresponding close brace"))
931 (let* ((closed-with-colon (format-directive-colonp close))
932 (posn (position close directives)))
936 (if *orig-args-available*
942 "~A~%while processing indirect format string:"
943 :args (list condition)
945 :control-string ,string
946 :offset ,(1- end)))))
948 (%format stream inside-string orig-args args))))
949 (throw 'need-orig-args nil))
950 (let ((*up-up-and-out-allowed* colonp))
951 (expand-directive-list (subseq directives 0 posn)))))
952 (compute-loop-aux (count)
954 (setf *only-simple-args* nil))
956 ,@(unless closed-with-colon
960 `((when (and ,count (minusp (decf ,count)))
963 (let ((*expander-next-arg-macro* 'expander-next-arg)
964 (*only-simple-args* nil)
965 (*orig-args-available* t))
966 `((let* ((orig-args ,(expand-next-arg))
969 (declare (ignorable orig-args outside-args args))
971 ,@(compute-insides)))))
973 ,@(when closed-with-colon
978 (expand-bind-defaults ((count nil)) params
979 (compute-loop-aux count))
980 (compute-loop-aux nil)))
989 `(let* ((orig-args ,(expand-next-arg))
991 (declare (ignorable orig-args args))
992 ,(let ((*expander-next-arg-macro* 'expander-next-arg)
993 (*only-simple-args* nil)
994 (*orig-args-available* t))
996 (values (if (zerop posn)
997 `(let ((inside-string ,(expand-next-arg)))
1000 (nthcdr (1+ posn) directives))))))
1002 (def-complex-format-directive #\} ()
1003 (error 'format-error
1004 :complaint "no corresponding open brace"))
1006 ;;;; format directives and support functions for justification
1008 (defparameter *illegal-inside-justification*
1009 (mapcar (lambda (x) (parse-directive x 0))
1010 '("~W" "~:W" "~@W" "~:@W"
1011 "~_" "~:_" "~@_" "~:@_"
1013 "~I" "~:I" "~@I" "~:@I"
1016 (defun illegal-inside-justification-p (directive)
1017 (member directive *illegal-inside-justification*
1019 (and (format-directive-p x)
1020 (format-directive-p y)
1021 (eql (format-directive-character x) (format-directive-character y))
1022 (eql (format-directive-colonp x) (format-directive-colonp y))
1023 (eql (format-directive-atsignp x) (format-directive-atsignp y))))))
1025 (def-complex-format-directive #\< (colonp atsignp params string end directives)
1026 (multiple-value-bind (segments first-semi close remaining)
1027 (parse-format-justification directives)
1029 (if (format-directive-colonp close)
1030 (multiple-value-bind (prefix per-line-p insides suffix)
1031 (parse-format-logical-block segments colonp first-semi
1032 close params string end)
1033 (expand-format-logical-block prefix per-line-p insides
1035 (let ((count (reduce #'+ (mapcar (lambda (x) (count-if #'illegal-inside-justification-p x)) segments))))
1037 ;; ANSI specifies that "an error is signalled" in this
1039 (error 'format-error
1040 :complaint "~D illegal directive~:P found inside justification block"
1042 :references (list '(:ansi-cl :section (22 3 5 2)))))
1043 (expand-format-justification segments colonp atsignp
1044 first-semi params)))
1047 (def-complex-format-directive #\> ()
1048 (error 'format-error
1049 :complaint "no corresponding open bracket"))
1051 (defun parse-format-logical-block
1052 (segments colonp first-semi close params string end)
1054 (error 'format-error
1055 :complaint "No parameters can be supplied with ~~<...~~:>."
1056 :offset (caar params)))
1057 (multiple-value-bind (prefix insides suffix)
1058 (multiple-value-bind (prefix-default suffix-default)
1059 (if colonp (values "(" ")") (values "" ""))
1060 (flet ((extract-string (list prefix-p)
1061 (let ((directive (find-if #'format-directive-p list)))
1063 (error 'format-error
1065 "cannot include format directives inside the ~
1066 ~:[suffix~;prefix~] segment of ~~<...~~:>"
1067 :args (list prefix-p)
1068 :offset (1- (format-directive-end directive))
1070 (list '(:ansi-cl :section (22 3 5 2))))
1071 (apply #'concatenate 'string list)))))
1072 (case (length segments)
1073 (0 (values prefix-default nil suffix-default))
1074 (1 (values prefix-default (car segments) suffix-default))
1075 (2 (values (extract-string (car segments) t)
1076 (cadr segments) suffix-default))
1077 (3 (values (extract-string (car segments) t)
1079 (extract-string (caddr segments) nil)))
1081 (error 'format-error
1082 :complaint "too many segments for ~~<...~~:>")))))
1083 (when (format-directive-atsignp close)
1085 (add-fill-style-newlines insides
1088 (format-directive-end first-semi)
1091 (and first-semi (format-directive-atsignp first-semi))
1095 (defun add-fill-style-newlines (list string offset &optional last-directive)
1098 (let ((directive (car list)))
1100 ((simple-string-p directive)
1101 (let* ((non-space (position #\Space directive :test #'char/=))
1102 (newlinep (and last-directive
1104 (format-directive-character last-directive)
1107 ((and newlinep non-space)
1109 (list (subseq directive 0 non-space))
1110 (add-fill-style-newlines-aux
1111 (subseq directive non-space) string (+ offset non-space))
1112 (add-fill-style-newlines
1113 (cdr list) string (+ offset (length directive)))))
1116 (add-fill-style-newlines
1117 (cdr list) string (+ offset (length directive)))))
1119 (nconc (add-fill-style-newlines-aux directive string offset)
1120 (add-fill-style-newlines
1121 (cdr list) string (+ offset (length directive))))))))
1124 (add-fill-style-newlines
1126 (format-directive-end directive) directive))))))
1129 (defun add-fill-style-newlines-aux (literal string offset)
1130 (let ((end (length literal))
1132 (collect ((results))
1134 (let ((blank (position #\space literal :start posn)))
1136 (results (subseq literal posn))
1138 (let ((non-blank (or (position #\space literal :start blank
1141 (results (subseq literal posn non-blank))
1142 (results (make-format-directive
1143 :string string :character #\_
1144 :start (+ offset non-blank) :end (+ offset non-blank)
1145 :colonp t :atsignp nil :params nil))
1146 (setf posn non-blank))
1151 (defun parse-format-justification (directives)
1152 (let ((first-semi nil)
1154 (remaining directives))
1155 (collect ((segments))
1157 (let ((close-or-semi (find-directive remaining #\> t)))
1158 (unless close-or-semi
1159 (error 'format-error
1160 :complaint "no corresponding close bracket"))
1161 (let ((posn (position close-or-semi remaining)))
1162 (segments (subseq remaining 0 posn))
1163 (setf remaining (nthcdr (1+ posn) remaining)))
1164 (when (char= (format-directive-character close-or-semi)
1166 (setf close close-or-semi)
1169 (setf first-semi close-or-semi))))
1170 (values (segments) first-semi close remaining))))
1172 (sb!xc:defmacro expander-pprint-next-arg (string offset)
1175 (error 'format-error
1176 :complaint "no more arguments"
1177 :control-string ,string
1182 (defun expand-format-logical-block (prefix per-line-p insides suffix atsignp)
1183 `(let ((arg ,(if atsignp 'args (expand-next-arg))))
1185 (setf *only-simple-args* nil)
1187 (pprint-logical-block
1189 ,(if per-line-p :per-line-prefix :prefix) ,prefix
1193 `((orig-args arg))))
1194 (declare (ignorable args ,@(unless atsignp '(orig-args))))
1196 ,@(let ((*expander-next-arg-macro* 'expander-pprint-next-arg)
1197 (*only-simple-args* nil)
1198 (*orig-args-available*
1199 (if atsignp *orig-args-available* t)))
1200 (expand-directive-list insides)))))))
1202 (defun expand-format-justification (segments colonp atsignp first-semi params)
1203 (let ((newline-segment-p
1205 (format-directive-colonp first-semi))))
1206 (expand-bind-defaults
1207 ((mincol 0) (colinc 1) (minpad 0) (padchar #\space))
1209 `(let ((segments nil)
1210 ,@(when newline-segment-p
1211 '((newline-segment nil)
1215 ,@(when newline-segment-p
1216 `((setf newline-segment
1217 (with-output-to-string (stream)
1218 ,@(expand-directive-list (pop segments))))
1219 ,(expand-bind-defaults
1221 (line-len '(or (sb!impl::line-length stream) 72)))
1222 (format-directive-params first-semi)
1223 `(setf extra-space ,extra line-len ,line-len))))
1224 ,@(mapcar (lambda (segment)
1225 `(push (with-output-to-string (stream)
1226 ,@(expand-directive-list segment))
1229 (format-justification stream
1230 ,@(if newline-segment-p
1231 '(newline-segment extra-space line-len)
1233 segments ,colonp ,atsignp
1234 ,mincol ,colinc ,minpad ,padchar)))))
1236 ;;;; format directive and support function for user-defined method
1238 (def-format-directive #\/ (string start end colonp atsignp params)
1239 (let ((symbol (extract-user-fun-name string start end)))
1240 (collect ((param-names) (bindings))
1241 (dolist (param-and-offset params)
1242 (let ((param (cdr param-and-offset)))
1243 (let ((param-name (gensym)))
1244 (param-names param-name)
1245 (bindings `(,param-name
1247 (:arg (expand-next-arg))
1248 (:remaining '(length args))
1251 (,symbol stream ,(expand-next-arg) ,colonp ,atsignp
1252 ,@(param-names))))))
1254 (defun extract-user-fun-name (string start end)
1255 (let ((slash (position #\/ string :start start :end (1- end)
1258 (error 'format-error
1259 :complaint "malformed ~~/ directive"))
1260 (let* ((name (string-upcase (let ((foo string))
1261 ;; Hack alert: This is to keep the compiler
1262 ;; quiet about deleting code inside the
1263 ;; subseq expansion.
1264 (subseq foo (1+ slash) (1- end)))))
1265 (first-colon (position #\: name))
1266 (second-colon (if first-colon (position #\: name :start (1+ first-colon))))
1267 (package-name (if first-colon
1268 (subseq name 0 first-colon)
1269 "COMMON-LISP-USER"))
1270 (package (find-package package-name)))
1272 ;; FIXME: should be PACKAGE-ERROR? Could we just use
1273 ;; FIND-UNDELETED-PACKAGE-OR-LOSE?
1274 (error 'format-error
1275 :complaint "no package named ~S"
1276 :args (list package-name)))
1278 ((and second-colon (= second-colon (1+ first-colon)))
1279 (subseq name (1+ second-colon)))
1281 (subseq name (1+ first-colon)))
1285 ;;; compile-time checking for argument mismatch. This code is
1286 ;;; inspired by that of Gerd Moellmann, and comes decorated with
1288 (defun %compiler-walk-format-string (string args)
1289 (declare (type simple-string string))
1290 (let ((*default-format-error-control-string* string))
1291 (macrolet ((incf-both (&optional (increment 1))
1293 (incf min ,increment)
1294 (incf max ,increment)))
1295 (walk-complex-directive (function)
1296 `(multiple-value-bind (min-inc max-inc remaining)
1297 (,function directive directives args)
1300 (setq directives remaining))))
1301 ;; FIXME: these functions take a list of arguments as well as
1302 ;; the directive stream. This is to enable possibly some
1303 ;; limited type checking on FORMAT's arguments, as well as
1304 ;; simple argument count mismatch checking: when the minimum and
1305 ;; maximum argument counts are the same at a given point, we
1306 ;; know which argument is going to be used for a given
1307 ;; directive, and some (annotated below) require arguments of
1308 ;; particular types.
1310 ((walk-justification (justification directives args)
1311 (declare (ignore args))
1312 (let ((*default-format-error-offset*
1313 (1- (format-directive-end justification))))
1314 (multiple-value-bind (segments first-semi close remaining)
1315 (parse-format-justification directives)
1316 (declare (ignore segments first-semi))
1318 ((not (format-directive-colonp close))
1319 (values 0 0 directives))
1320 ((format-directive-atsignp justification)
1321 (values 0 sb!xc:call-arguments-limit directives))
1322 ;; FIXME: here we could assert that the
1323 ;; corresponding argument was a list.
1324 (t (values 1 1 remaining))))))
1325 (walk-conditional (conditional directives args)
1326 (let ((*default-format-error-offset*
1327 (1- (format-directive-end conditional))))
1328 (multiple-value-bind (sublists last-semi-with-colon-p remaining)
1329 (parse-conditional-directive directives)
1330 (declare (ignore last-semi-with-colon-p))
1332 (loop for s in sublists
1334 1 (walk-directive-list s args)))))
1336 ((format-directive-atsignp conditional)
1337 (values 1 (max 1 sub-max) remaining))
1338 ((loop for p in (format-directive-params conditional)
1339 thereis (or (integerp (cdr p))
1340 (memq (cdr p) '(:remaining :arg))))
1341 (values 0 sub-max remaining))
1342 ;; FIXME: if not COLONP, then the next argument
1343 ;; must be a number.
1344 (t (values 1 (1+ sub-max) remaining)))))))
1345 (walk-iteration (iteration directives args)
1346 (declare (ignore args))
1347 (let ((*default-format-error-offset*
1348 (1- (format-directive-end iteration))))
1349 (let* ((close (find-directive directives #\} nil))
1350 (posn (or (position close directives)
1351 (error 'format-error
1352 :complaint "no corresponding close brace")))
1353 (remaining (nthcdr (1+ posn) directives)))
1354 ;; FIXME: if POSN is zero, the next argument must be
1355 ;; a format control (either a function or a string).
1356 (if (format-directive-atsignp iteration)
1357 (values (if (zerop posn) 1 0)
1358 sb!xc:call-arguments-limit
1360 ;; FIXME: the argument corresponding to this
1361 ;; directive must be a list.
1362 (let ((nreq (if (zerop posn) 2 1)))
1363 (values nreq nreq remaining))))))
1364 (walk-directive-list (directives args)
1365 (let ((min 0) (max 0))
1367 (let ((directive (pop directives)))
1368 (when (null directive)
1369 (return (values min (min max sb!xc:call-arguments-limit))))
1370 (when (format-directive-p directive)
1371 (incf-both (count :arg (format-directive-params directive)
1373 (let ((c (format-directive-character directive)))
1375 ((find c "ABCDEFGORSWX$/")
1378 (unless (format-directive-colonp directive)
1380 ((or (find c "IT%&|_();>~") (char= c #\Newline)))
1381 ;; FIXME: check correspondence of ~( and ~)
1383 (walk-complex-directive walk-justification))
1385 (walk-complex-directive walk-conditional))
1387 (walk-complex-directive walk-iteration))
1389 ;; FIXME: the argument corresponding to this
1390 ;; directive must be a format control.
1392 ((format-directive-atsignp directive)
1394 (setq max sb!xc:call-arguments-limit))
1396 (t (throw 'give-up-format-string-walk nil))))))))))
1397 (catch 'give-up-format-string-walk
1398 (let ((directives (tokenize-control-string string)))
1399 (walk-directive-list directives args)))))))