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 base-char)
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 (aref *format-directive-expanders*
274 (char-code (format-directive-character directive))))
275 (*default-format-error-offset*
276 (1- (format-directive-end directive))))
277 (declare (type (or null function) expander))
279 (funcall expander directive more-directives)
281 :complaint "unknown directive ~@[(character: ~A)~]"
282 :args (list (char-name (format-directive-character directive)))))))
284 (values `(write-string ,directive stream)
287 (defmacro-mundanely expander-next-arg (string offset)
291 :complaint "no more arguments"
292 :control-string ,string
295 (defun expand-next-arg (&optional offset)
296 (if (or *orig-args-available* (not *only-simple-args*))
297 `(,*expander-next-arg-macro*
298 ,*default-format-error-control-string*
299 ,(or offset *default-format-error-offset*))
300 (let ((symbol (gensym "FORMAT-ARG-")))
301 (push (cons symbol (or offset *default-format-error-offset*))
305 (defmacro expand-bind-defaults (specs params &body body)
306 (once-only ((params params))
308 (collect ((expander-bindings) (runtime-bindings))
310 (destructuring-bind (var default) spec
311 (let ((symbol (gensym)))
316 (let* ((param-and-offset (pop ,params))
317 (offset (car param-and-offset))
318 (param (cdr param-and-offset)))
320 (:arg `(or ,(expand-next-arg offset)
323 (setf *only-simple-args* nil)
327 `(let ,(expander-bindings)
328 `(let ,(list ,@(runtime-bindings))
333 "too many parameters, expected no more than ~W"
334 :args (list ,(length specs))
335 :offset (caar ,params)))
340 :complaint "too many parameters, expected none"
341 :offset (caar ,params)))
344 ;;;; format directive machinery
346 ;;; FIXME: only used in this file, could be SB!XC:DEFMACRO in EVAL-WHEN
347 (defmacro def-complex-format-directive (char lambda-list &body body)
348 (let ((defun-name (intern (format nil
349 "~:@(~:C~)-FORMAT-DIRECTIVE-EXPANDER"
352 (directives (if lambda-list (car (last lambda-list)) (gensym))))
354 (defun ,defun-name (,directive ,directives)
356 `((let ,(mapcar (lambda (var)
358 (,(symbolicate "FORMAT-DIRECTIVE-" var)
360 (butlast lambda-list))
362 `((declare (ignore ,directive ,directives))
364 (%set-format-directive-expander ,char #',defun-name))))
366 ;;; FIXME: only used in this file, could be SB!XC:DEFMACRO in EVAL-WHEN
367 (defmacro def-format-directive (char lambda-list &body body)
368 (let ((directives (gensym))
370 (body-without-decls body))
372 (let ((form (car body-without-decls)))
373 (unless (and (consp form) (eq (car form) 'declare))
375 (push (pop body-without-decls) declarations)))
376 (setf declarations (reverse declarations))
377 `(def-complex-format-directive ,char (,@lambda-list ,directives)
379 (values (progn ,@body-without-decls)
382 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
384 (defun %set-format-directive-expander (char fn)
385 (setf (aref *format-directive-expanders* (char-code (char-upcase char))) fn)
388 (defun %set-format-directive-interpreter (char fn)
389 (setf (aref *format-directive-interpreters*
390 (char-code (char-upcase char)))
394 (defun find-directive (directives kind stop-at-semi)
396 (let ((next (car directives)))
397 (if (format-directive-p next)
398 (let ((char (format-directive-character next)))
399 (if (or (char= kind char)
400 (and stop-at-semi (char= char #\;)))
403 (cdr (flet ((after (char)
404 (member (find-directive (cdr directives)
415 (find-directive (cdr directives) kind stop-at-semi)))))
419 ;;;; format directives for simple output
421 (def-format-directive #\A (colonp atsignp params)
423 (expand-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
426 `(format-princ stream ,(expand-next-arg) ',colonp ',atsignp
427 ,mincol ,colinc ,minpad ,padchar))
429 `(or ,(expand-next-arg) "()")
433 (def-format-directive #\S (colonp atsignp params)
435 (expand-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
438 `(format-prin1 stream ,(expand-next-arg) ,colonp ,atsignp
439 ,mincol ,colinc ,minpad ,padchar)))
441 `(let ((arg ,(expand-next-arg)))
444 (princ "()" stream))))
446 `(prin1 ,(expand-next-arg) stream))))
448 (def-format-directive #\C (colonp atsignp params)
449 (expand-bind-defaults () params
451 `(format-print-named-character ,(expand-next-arg) stream)
453 `(prin1 ,(expand-next-arg) stream)
454 `(write-char ,(expand-next-arg) stream)))))
456 (def-format-directive #\W (colonp atsignp params)
457 (expand-bind-defaults () params
458 (if (or colonp atsignp)
459 `(let (,@(when colonp
460 '((*print-pretty* t)))
462 '((*print-level* nil)
463 (*print-length* nil))))
464 (output-object ,(expand-next-arg) stream))
465 `(output-object ,(expand-next-arg) stream))))
467 ;;;; format directives for integer output
469 (defun expand-format-integer (base colonp atsignp params)
470 (if (or colonp atsignp params)
471 (expand-bind-defaults
472 ((mincol 0) (padchar #\space) (commachar #\,) (commainterval 3))
474 `(format-print-integer stream ,(expand-next-arg) ,colonp ,atsignp
475 ,base ,mincol ,padchar ,commachar
477 `(write ,(expand-next-arg) :stream stream :base ,base :radix nil
480 (def-format-directive #\D (colonp atsignp params)
481 (expand-format-integer 10 colonp atsignp params))
483 (def-format-directive #\B (colonp atsignp params)
484 (expand-format-integer 2 colonp atsignp params))
486 (def-format-directive #\O (colonp atsignp params)
487 (expand-format-integer 8 colonp atsignp params))
489 (def-format-directive #\X (colonp atsignp params)
490 (expand-format-integer 16 colonp atsignp params))
492 (def-format-directive #\R (colonp atsignp params)
494 (expand-bind-defaults
495 ((base 10) (mincol 0) (padchar #\space) (commachar #\,)
498 `(format-print-integer stream ,(expand-next-arg) ,colonp ,atsignp
500 ,padchar ,commachar ,commainterval))
503 `(format-print-old-roman stream ,(expand-next-arg))
504 `(format-print-roman stream ,(expand-next-arg)))
506 `(format-print-ordinal stream ,(expand-next-arg))
507 `(format-print-cardinal stream ,(expand-next-arg))))))
509 ;;;; format directive for pluralization
511 (def-format-directive #\P (colonp atsignp params end)
512 (expand-bind-defaults () params
516 (*orig-args-available*
517 `(if (eq orig-args args)
519 :complaint "no previous argument"
521 (do ((arg-ptr orig-args (cdr arg-ptr)))
522 ((eq (cdr arg-ptr) args)
525 (unless *simple-args*
527 :complaint "no previous argument"))
528 (caar *simple-args*))
530 (/show0 "THROWing NEED-ORIG-ARGS from tilde-P")
531 (throw 'need-orig-args nil)))))
533 `(write-string (if (eql ,arg 1) "y" "ies") stream)
534 `(unless (eql ,arg 1) (write-char #\s stream))))))
536 ;;;; format directives for floating point output
538 (def-format-directive #\F (colonp atsignp params)
542 "The colon modifier cannot be used with this directive."))
543 (expand-bind-defaults ((w nil) (d nil) (k nil) (ovf nil) (pad #\space)) params
544 `(format-fixed stream ,(expand-next-arg) ,w ,d ,k ,ovf ,pad ,atsignp)))
546 (def-format-directive #\E (colonp atsignp params)
550 "The colon modifier cannot be used with this directive."))
551 (expand-bind-defaults
552 ((w nil) (d nil) (e nil) (k 1) (ovf nil) (pad #\space) (mark nil))
554 `(format-exponential stream ,(expand-next-arg) ,w ,d ,e ,k ,ovf ,pad ,mark
557 (def-format-directive #\G (colonp atsignp params)
561 "The colon modifier cannot be used with this directive."))
562 (expand-bind-defaults
563 ((w nil) (d nil) (e nil) (k nil) (ovf nil) (pad #\space) (mark nil))
565 `(format-general stream ,(expand-next-arg) ,w ,d ,e ,k ,ovf ,pad ,mark ,atsignp)))
567 (def-format-directive #\$ (colonp atsignp params)
568 (expand-bind-defaults ((d 2) (n 1) (w 0) (pad #\space)) params
569 `(format-dollars stream ,(expand-next-arg) ,d ,n ,w ,pad ,colonp
572 ;;;; format directives for line/page breaks etc.
574 (def-format-directive #\% (colonp atsignp params)
575 (when (or colonp atsignp)
578 "The colon and atsign modifiers cannot be used with this directive."
581 (expand-bind-defaults ((count 1)) params
586 (def-format-directive #\& (colonp atsignp params)
587 (when (or colonp atsignp)
590 "The colon and atsign modifiers cannot be used with this directive."
593 (expand-bind-defaults ((count 1)) params
596 (dotimes (i (1- ,count))
598 '(fresh-line stream)))
600 (def-format-directive #\| (colonp atsignp params)
601 (when (or colonp atsignp)
604 "The colon and atsign modifiers cannot be used with this directive."
607 (expand-bind-defaults ((count 1)) params
609 (write-char (code-char form-feed-char-code) stream)))
610 '(write-char (code-char form-feed-char-code) stream)))
612 (def-format-directive #\~ (colonp atsignp params)
613 (when (or colonp atsignp)
616 "The colon and atsign modifiers cannot be used with this directive."
619 (expand-bind-defaults ((count 1)) params
621 (write-char #\~ stream)))
622 '(write-char #\~ stream)))
624 (def-complex-format-directive #\newline (colonp atsignp params directives)
625 (when (and colonp atsignp)
627 :complaint "both colon and atsign modifiers used simultaneously"))
628 (values (expand-bind-defaults () params
630 '(write-char #\newline stream)
632 (if (and (not colonp)
634 (simple-string-p (car directives)))
635 (cons (string-left-trim *format-whitespace-chars*
640 ;;;; format directives for tabs and simple pretty printing
642 (def-format-directive #\T (colonp atsignp params)
644 (expand-bind-defaults ((n 1) (m 1)) params
645 `(pprint-tab ,(if atsignp :section-relative :section)
648 (expand-bind-defaults ((colrel 1) (colinc 1)) params
649 `(format-relative-tab stream ,colrel ,colinc))
650 (expand-bind-defaults ((colnum 1) (colinc 1)) params
651 `(format-absolute-tab stream ,colnum ,colinc)))))
653 (def-format-directive #\_ (colonp atsignp params)
654 (expand-bind-defaults () params
655 `(pprint-newline ,(if colonp
664 (def-format-directive #\I (colonp atsignp params)
668 "cannot use the at-sign modifier with this directive"))
669 (expand-bind-defaults ((n 0)) params
670 `(pprint-indent ,(if colonp :current :block) ,n stream)))
672 ;;;; format directive for ~*
674 (def-format-directive #\* (colonp atsignp params end)
679 "both colon and atsign modifiers used simultaneously")
680 (expand-bind-defaults ((posn 0)) params
681 (unless *orig-args-available*
682 (/show0 "THROWing NEED-ORIG-ARGS from tilde-@*")
683 (throw 'need-orig-args nil))
684 `(if (<= 0 ,posn (length orig-args))
685 (setf args (nthcdr ,posn orig-args))
687 :complaint "Index ~W out of bounds. Should have been ~
689 :args (list ,posn (length orig-args))
690 :offset ,(1- end)))))
692 (expand-bind-defaults ((n 1)) params
693 (unless *orig-args-available*
694 (/show0 "THROWing NEED-ORIG-ARGS from tilde-:*")
695 (throw 'need-orig-args nil))
696 `(do ((cur-posn 0 (1+ cur-posn))
697 (arg-ptr orig-args (cdr arg-ptr)))
699 (let ((new-posn (- cur-posn ,n)))
700 (if (<= 0 new-posn (length orig-args))
701 (setf args (nthcdr new-posn orig-args))
704 "Index ~W is out of bounds; should have been ~
706 :args (list new-posn (length orig-args))
707 :offset ,(1- end)))))))
709 (expand-bind-defaults ((n 1)) params
710 (setf *only-simple-args* nil)
713 (expand-next-arg)))))
715 ;;;; format directive for indirection
717 (def-format-directive #\? (colonp atsignp params string end)
720 :complaint "cannot use the colon modifier with this directive"))
721 (expand-bind-defaults () params
727 "~A~%while processing indirect format string:"
728 :args (list condition)
730 :control-string ,string
731 :offset ,(1- end)))))
733 (if *orig-args-available*
734 `(setf args (%format stream ,(expand-next-arg) orig-args args))
735 (throw 'need-orig-args nil))
736 `(%format stream ,(expand-next-arg) ,(expand-next-arg))))))
738 ;;;; format directives for capitalization
740 (def-complex-format-directive #\( (colonp atsignp params directives)
741 (let ((close (find-directive directives #\) nil)))
744 :complaint "no corresponding close parenthesis"))
745 (let* ((posn (position close directives))
746 (before (subseq directives 0 posn))
747 (after (nthcdr (1+ posn) directives)))
749 (expand-bind-defaults () params
750 `(let ((stream (make-case-frob-stream stream
758 ,@(expand-directive-list before)))
761 (def-complex-format-directive #\) ()
763 :complaint "no corresponding open parenthesis"))
765 ;;;; format directives and support functions for conditionalization
767 (def-complex-format-directive #\[ (colonp atsignp params directives)
768 (multiple-value-bind (sublists last-semi-with-colon-p remaining)
769 (parse-conditional-directive directives)
775 "both colon and atsign modifiers used simultaneously")
779 "Can only specify one section")
780 (expand-bind-defaults () params
781 (expand-maybe-conditional (car sublists)))))
783 (if (= (length sublists) 2)
784 (expand-bind-defaults () params
785 (expand-true-false-conditional (car sublists)
789 "must specify exactly two sections"))
790 (expand-bind-defaults ((index (expand-next-arg))) params
791 (setf *only-simple-args* nil)
793 (when last-semi-with-colon-p
794 (push `(t ,@(expand-directive-list (pop sublists)))
796 (let ((count (length sublists)))
797 (dolist (sublist sublists)
798 (push `(,(decf count)
799 ,@(expand-directive-list sublist))
801 `(case ,index ,@clauses)))))
804 (defun parse-conditional-directive (directives)
806 (last-semi-with-colon-p nil)
807 (remaining directives))
809 (let ((close-or-semi (find-directive remaining #\] t)))
810 (unless close-or-semi
812 :complaint "no corresponding close bracket"))
813 (let ((posn (position close-or-semi remaining)))
814 (push (subseq remaining 0 posn) sublists)
815 (setf remaining (nthcdr (1+ posn) remaining))
816 (when (char= (format-directive-character close-or-semi) #\])
818 (setf last-semi-with-colon-p
819 (format-directive-colonp close-or-semi)))))
820 (values sublists last-semi-with-colon-p remaining)))
822 (defun expand-maybe-conditional (sublist)
824 `(let ((prev-args args)
825 (arg ,(expand-next-arg)))
827 (setf args prev-args)
828 ,@(expand-directive-list sublist)))))
829 (if *only-simple-args*
830 (multiple-value-bind (guts new-args)
831 (let ((*simple-args* *simple-args*))
832 (values (expand-directive-list sublist)
834 (cond ((eq *simple-args* (cdr new-args))
835 (setf *simple-args* new-args)
836 `(when ,(caar new-args)
839 (setf *only-simple-args* nil)
843 (defun expand-true-false-conditional (true false)
844 (let ((arg (expand-next-arg)))
848 ,@(expand-directive-list true))
850 ,@(expand-directive-list false)))))
851 (if *only-simple-args*
852 (multiple-value-bind (true-guts true-args true-simple)
853 (let ((*simple-args* *simple-args*)
854 (*only-simple-args* t))
855 (values (expand-directive-list true)
858 (multiple-value-bind (false-guts false-args false-simple)
859 (let ((*simple-args* *simple-args*)
860 (*only-simple-args* t))
861 (values (expand-directive-list false)
864 (if (= (length true-args) (length false-args))
868 ,(do ((false false-args (cdr false))
869 (true true-args (cdr true))
870 (bindings nil (cons `(,(caar false) ,(caar true))
872 ((eq true *simple-args*)
873 (setf *simple-args* true-args)
874 (setf *only-simple-args*
875 (and true-simple false-simple))
882 (setf *only-simple-args* nil)
886 (def-complex-format-directive #\; ()
889 "~~; directive not contained within either ~~[...~~] or ~~<...~~>"))
891 (def-complex-format-directive #\] ()
894 "no corresponding open bracket"))
896 ;;;; format directive for up-and-out
898 (def-format-directive #\^ (colonp atsignp params)
901 :complaint "cannot use the at-sign modifier with this directive"))
902 (when (and colonp (not *up-up-and-out-allowed*))
904 :complaint "attempt to use ~~:^ outside a ~~:{...~~} construct"))
905 `(when ,(case (length params)
909 (setf *only-simple-args* nil)
911 (1 (expand-bind-defaults ((count 0)) params
913 (2 (expand-bind-defaults ((arg1 0) (arg2 0)) params
915 (t (expand-bind-defaults ((arg1 0) (arg2 0) (arg3 0)) params
916 `(<= ,arg1 ,arg2 ,arg3))))
918 '(return-from outside-loop nil)
921 ;;;; format directives for iteration
923 (def-complex-format-directive #\{ (colonp atsignp params string end directives)
924 (let ((close (find-directive directives #\} nil)))
927 :complaint "no corresponding close brace"))
928 (let* ((closed-with-colon (format-directive-colonp close))
929 (posn (position close directives)))
933 (if *orig-args-available*
939 "~A~%while processing indirect format string:"
940 :args (list condition)
942 :control-string ,string
943 :offset ,(1- end)))))
945 (%format stream inside-string orig-args args))))
946 (throw 'need-orig-args nil))
947 (let ((*up-up-and-out-allowed* colonp))
948 (expand-directive-list (subseq directives 0 posn)))))
949 (compute-loop-aux (count)
951 (setf *only-simple-args* nil))
953 ,@(unless closed-with-colon
957 `((when (and ,count (minusp (decf ,count)))
960 (let ((*expander-next-arg-macro* 'expander-next-arg)
961 (*only-simple-args* nil)
962 (*orig-args-available* t))
963 `((let* ((orig-args ,(expand-next-arg))
966 (declare (ignorable orig-args outside-args args))
968 ,@(compute-insides)))))
970 ,@(when closed-with-colon
975 (expand-bind-defaults ((count nil)) params
976 (compute-loop-aux count))
977 (compute-loop-aux nil)))
986 `(let* ((orig-args ,(expand-next-arg))
988 (declare (ignorable orig-args args))
989 ,(let ((*expander-next-arg-macro* 'expander-next-arg)
990 (*only-simple-args* nil)
991 (*orig-args-available* t))
993 (values (if (zerop posn)
994 `(let ((inside-string ,(expand-next-arg)))
997 (nthcdr (1+ posn) directives))))))
999 (def-complex-format-directive #\} ()
1000 (error 'format-error
1001 :complaint "no corresponding open brace"))
1003 ;;;; format directives and support functions for justification
1005 (defparameter *illegal-inside-justification*
1006 (mapcar (lambda (x) (parse-directive x 0))
1007 '("~W" "~:W" "~@W" "~:@W"
1008 "~_" "~:_" "~@_" "~:@_"
1010 "~I" "~:I" "~@I" "~:@I"
1013 (defun illegal-inside-justification-p (directive)
1014 (member directive *illegal-inside-justification*
1016 (and (format-directive-p x)
1017 (format-directive-p y)
1018 (eql (format-directive-character x) (format-directive-character y))
1019 (eql (format-directive-colonp x) (format-directive-colonp y))
1020 (eql (format-directive-atsignp x) (format-directive-atsignp y))))))
1022 (def-complex-format-directive #\< (colonp atsignp params string end directives)
1023 (multiple-value-bind (segments first-semi close remaining)
1024 (parse-format-justification directives)
1026 (if (format-directive-colonp close)
1027 (multiple-value-bind (prefix per-line-p insides suffix)
1028 (parse-format-logical-block segments colonp first-semi
1029 close params string end)
1030 (expand-format-logical-block prefix per-line-p insides
1032 (let ((count (reduce #'+ (mapcar (lambda (x) (count-if #'illegal-inside-justification-p x)) segments))))
1034 ;; ANSI specifies that "an error is signalled" in this
1036 (error 'format-error
1037 :complaint "~D illegal directive~:P found inside justification block"
1039 :references (list '(:ansi-cl :section (22 3 5 2)))))
1040 (expand-format-justification segments colonp atsignp
1041 first-semi params)))
1044 (def-complex-format-directive #\> ()
1045 (error 'format-error
1046 :complaint "no corresponding open bracket"))
1048 (defun parse-format-logical-block
1049 (segments colonp first-semi close params string end)
1051 (error 'format-error
1052 :complaint "No parameters can be supplied with ~~<...~~:>."
1053 :offset (caar params)))
1054 (multiple-value-bind (prefix insides suffix)
1055 (multiple-value-bind (prefix-default suffix-default)
1056 (if colonp (values "(" ")") (values "" ""))
1057 (flet ((extract-string (list prefix-p)
1058 (let ((directive (find-if #'format-directive-p list)))
1060 (error 'format-error
1062 "cannot include format directives inside the ~
1063 ~:[suffix~;prefix~] segment of ~~<...~~:>"
1064 :args (list prefix-p)
1065 :offset (1- (format-directive-end directive))
1067 (list '(:ansi-cl :section (22 3 5 2))))
1068 (apply #'concatenate 'string list)))))
1069 (case (length segments)
1070 (0 (values prefix-default nil suffix-default))
1071 (1 (values prefix-default (car segments) suffix-default))
1072 (2 (values (extract-string (car segments) t)
1073 (cadr segments) suffix-default))
1074 (3 (values (extract-string (car segments) t)
1076 (extract-string (caddr segments) nil)))
1078 (error 'format-error
1079 :complaint "too many segments for ~~<...~~:>")))))
1080 (when (format-directive-atsignp close)
1082 (add-fill-style-newlines insides
1085 (format-directive-end first-semi)
1088 (and first-semi (format-directive-atsignp first-semi))
1092 (defun add-fill-style-newlines (list string offset &optional last-directive)
1095 (let ((directive (car list)))
1097 ((simple-string-p directive)
1098 (let* ((non-space (position #\Space directive :test #'char/=))
1099 (newlinep (and last-directive
1101 (format-directive-character last-directive)
1104 ((and newlinep non-space)
1106 (list (subseq directive 0 non-space))
1107 (add-fill-style-newlines-aux
1108 (subseq directive non-space) string (+ offset non-space))
1109 (add-fill-style-newlines
1110 (cdr list) string (+ offset (length directive)))))
1113 (add-fill-style-newlines
1114 (cdr list) string (+ offset (length directive)))))
1116 (nconc (add-fill-style-newlines-aux directive string offset)
1117 (add-fill-style-newlines
1118 (cdr list) string (+ offset (length directive))))))))
1121 (add-fill-style-newlines
1123 (format-directive-end directive) directive))))))
1126 (defun add-fill-style-newlines-aux (literal string offset)
1127 (let ((end (length literal))
1129 (collect ((results))
1131 (let ((blank (position #\space literal :start posn)))
1133 (results (subseq literal posn))
1135 (let ((non-blank (or (position #\space literal :start blank
1138 (results (subseq literal posn non-blank))
1139 (results (make-format-directive
1140 :string string :character #\_
1141 :start (+ offset non-blank) :end (+ offset non-blank)
1142 :colonp t :atsignp nil :params nil))
1143 (setf posn non-blank))
1148 (defun parse-format-justification (directives)
1149 (let ((first-semi nil)
1151 (remaining directives))
1152 (collect ((segments))
1154 (let ((close-or-semi (find-directive remaining #\> t)))
1155 (unless close-or-semi
1156 (error 'format-error
1157 :complaint "no corresponding close bracket"))
1158 (let ((posn (position close-or-semi remaining)))
1159 (segments (subseq remaining 0 posn))
1160 (setf remaining (nthcdr (1+ posn) remaining)))
1161 (when (char= (format-directive-character close-or-semi)
1163 (setf close close-or-semi)
1166 (setf first-semi close-or-semi))))
1167 (values (segments) first-semi close remaining))))
1169 (sb!xc:defmacro expander-pprint-next-arg (string offset)
1172 (error 'format-error
1173 :complaint "no more arguments"
1174 :control-string ,string
1179 (defun expand-format-logical-block (prefix per-line-p insides suffix atsignp)
1180 `(let ((arg ,(if atsignp 'args (expand-next-arg))))
1182 (setf *only-simple-args* nil)
1184 (pprint-logical-block
1186 ,(if per-line-p :per-line-prefix :prefix) ,prefix
1190 `((orig-args arg))))
1191 (declare (ignorable args ,@(unless atsignp '(orig-args))))
1193 ,@(let ((*expander-next-arg-macro* 'expander-pprint-next-arg)
1194 (*only-simple-args* nil)
1195 (*orig-args-available*
1196 (if atsignp *orig-args-available* t)))
1197 (expand-directive-list insides)))))))
1199 (defun expand-format-justification (segments colonp atsignp first-semi params)
1200 (let ((newline-segment-p
1202 (format-directive-colonp first-semi))))
1203 (expand-bind-defaults
1204 ((mincol 0) (colinc 1) (minpad 0) (padchar #\space))
1206 `(let ((segments nil)
1207 ,@(when newline-segment-p
1208 '((newline-segment nil)
1212 ,@(when newline-segment-p
1213 `((setf newline-segment
1214 (with-output-to-string (stream)
1215 ,@(expand-directive-list (pop segments))))
1216 ,(expand-bind-defaults
1218 (line-len '(or (sb!impl::line-length stream) 72)))
1219 (format-directive-params first-semi)
1220 `(setf extra-space ,extra line-len ,line-len))))
1221 ,@(mapcar (lambda (segment)
1222 `(push (with-output-to-string (stream)
1223 ,@(expand-directive-list segment))
1226 (format-justification stream
1227 ,@(if newline-segment-p
1228 '(newline-segment extra-space line-len)
1230 segments ,colonp ,atsignp
1231 ,mincol ,colinc ,minpad ,padchar)))))
1233 ;;;; format directive and support function for user-defined method
1235 (def-format-directive #\/ (string start end colonp atsignp params)
1236 (let ((symbol (extract-user-fun-name string start end)))
1237 (collect ((param-names) (bindings))
1238 (dolist (param-and-offset params)
1239 (let ((param (cdr param-and-offset)))
1240 (let ((param-name (gensym)))
1241 (param-names param-name)
1242 (bindings `(,param-name
1244 (:arg (expand-next-arg))
1245 (:remaining '(length args))
1248 (,symbol stream ,(expand-next-arg) ,colonp ,atsignp
1249 ,@(param-names))))))
1251 (defun extract-user-fun-name (string start end)
1252 (let ((slash (position #\/ string :start start :end (1- end)
1255 (error 'format-error
1256 :complaint "malformed ~~/ directive"))
1257 (let* ((name (string-upcase (let ((foo string))
1258 ;; Hack alert: This is to keep the compiler
1259 ;; quiet about deleting code inside the
1260 ;; subseq expansion.
1261 (subseq foo (1+ slash) (1- end)))))
1262 (first-colon (position #\: name))
1263 (second-colon (if first-colon (position #\: name :start (1+ first-colon))))
1264 (package-name (if first-colon
1265 (subseq name 0 first-colon)
1266 "COMMON-LISP-USER"))
1267 (package (find-package package-name)))
1269 ;; FIXME: should be PACKAGE-ERROR? Could we just use
1270 ;; FIND-UNDELETED-PACKAGE-OR-LOSE?
1271 (error 'format-error
1272 :complaint "no package named ~S"
1273 :args (list package-name)))
1275 ((and second-colon (= second-colon (1+ first-colon)))
1276 (subseq name (1+ second-colon)))
1278 (subseq name (1+ first-colon)))
1282 ;;; compile-time checking for argument mismatch. This code is
1283 ;;; inspired by that of Gerd Moellmann, and comes decorated with
1285 (defun %compiler-walk-format-string (string args)
1286 (declare (type simple-string string))
1287 (let ((*default-format-error-control-string* string))
1288 (macrolet ((incf-both (&optional (increment 1))
1290 (incf min ,increment)
1291 (incf max ,increment)))
1292 (walk-complex-directive (function)
1293 `(multiple-value-bind (min-inc max-inc remaining)
1294 (,function directive directives args)
1297 (setq directives remaining))))
1298 ;; FIXME: these functions take a list of arguments as well as
1299 ;; the directive stream. This is to enable possibly some
1300 ;; limited type checking on FORMAT's arguments, as well as
1301 ;; simple argument count mismatch checking: when the minimum and
1302 ;; maximum argument counts are the same at a given point, we
1303 ;; know which argument is going to be used for a given
1304 ;; directive, and some (annotated below) require arguments of
1305 ;; particular types.
1307 ((walk-justification (justification directives args)
1308 (declare (ignore args))
1309 (let ((*default-format-error-offset*
1310 (1- (format-directive-end justification))))
1311 (multiple-value-bind (segments first-semi close remaining)
1312 (parse-format-justification directives)
1313 (declare (ignore segments first-semi))
1315 ((not (format-directive-colonp close))
1316 (values 0 0 directives))
1317 ((format-directive-atsignp justification)
1318 (values 0 sb!xc:call-arguments-limit directives))
1319 ;; FIXME: here we could assert that the
1320 ;; corresponding argument was a list.
1321 (t (values 1 1 remaining))))))
1322 (walk-conditional (conditional directives args)
1323 (let ((*default-format-error-offset*
1324 (1- (format-directive-end conditional))))
1325 (multiple-value-bind (sublists last-semi-with-colon-p remaining)
1326 (parse-conditional-directive directives)
1327 (declare (ignore last-semi-with-colon-p))
1329 (loop for s in sublists
1331 1 (walk-directive-list s args)))))
1333 ((format-directive-atsignp conditional)
1334 (values 1 (max 1 sub-max) remaining))
1335 ((loop for p in (format-directive-params conditional)
1336 thereis (or (integerp (cdr p))
1337 (memq (cdr p) '(:remaining :arg))))
1338 (values 0 sub-max remaining))
1339 ;; FIXME: if not COLONP, then the next argument
1340 ;; must be a number.
1341 (t (values 1 (1+ sub-max) remaining)))))))
1342 (walk-iteration (iteration directives args)
1343 (declare (ignore args))
1344 (let ((*default-format-error-offset*
1345 (1- (format-directive-end iteration))))
1346 (let* ((close (find-directive directives #\} nil))
1347 (posn (or (position close directives)
1348 (error 'format-error
1349 :complaint "no corresponding close brace")))
1350 (remaining (nthcdr (1+ posn) directives)))
1351 ;; FIXME: if POSN is zero, the next argument must be
1352 ;; a format control (either a function or a string).
1353 (if (format-directive-atsignp iteration)
1354 (values (if (zerop posn) 1 0)
1355 sb!xc:call-arguments-limit
1357 ;; FIXME: the argument corresponding to this
1358 ;; directive must be a list.
1359 (let ((nreq (if (zerop posn) 2 1)))
1360 (values nreq nreq remaining))))))
1361 (walk-directive-list (directives args)
1362 (let ((min 0) (max 0))
1364 (let ((directive (pop directives)))
1365 (when (null directive)
1366 (return (values min (min max sb!xc:call-arguments-limit))))
1367 (when (format-directive-p directive)
1368 (incf-both (count :arg (format-directive-params directive)
1370 (let ((c (format-directive-character directive)))
1372 ((find c "ABCDEFGORSWX$/")
1375 (unless (format-directive-colonp directive)
1377 ((or (find c "IT%&|_();>") (char= c #\Newline)))
1378 ;; FIXME: check correspondence of ~( and ~)
1380 (walk-complex-directive walk-justification))
1382 (walk-complex-directive walk-conditional))
1384 (walk-complex-directive walk-iteration))
1386 ;; FIXME: the argument corresponding to this
1387 ;; directive must be a format control.
1389 ((format-directive-atsignp directive)
1391 (setq max sb!xc:call-arguments-limit))
1393 (t (throw 'give-up-format-string-walk nil))))))))))
1394 (catch 'give-up-format-string-walk
1395 (let ((directives (tokenize-control-string string)))
1396 (walk-directive-list directives args)))))))