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)
496 (expand-bind-defaults
497 ((base nil) (mincol 0) (padchar #\space) (commachar #\,)
500 (let ((n-arg (gensym)))
501 `(let ((,n-arg ,(expand-next-arg)))
503 (format-print-integer stream ,n-arg ,colonp ,atsignp
505 ,padchar ,commachar ,commainterval)
508 `(format-print-old-roman stream ,n-arg)
509 `(format-print-roman stream ,n-arg))
511 `(format-print-ordinal stream ,n-arg)
512 `(format-print-cardinal stream ,n-arg))))))))
514 ;;;; format directive for pluralization
516 (def-format-directive #\P (colonp atsignp params end)
517 (expand-bind-defaults () params
521 (*orig-args-available*
522 `(if (eq orig-args args)
524 :complaint "no previous argument"
526 (do ((arg-ptr orig-args (cdr arg-ptr)))
527 ((eq (cdr arg-ptr) args)
530 (unless *simple-args*
532 :complaint "no previous argument"))
533 (caar *simple-args*))
535 (/show0 "THROWing NEED-ORIG-ARGS from tilde-P")
536 (throw 'need-orig-args nil)))))
538 `(write-string (if (eql ,arg 1) "y" "ies") stream)
539 `(unless (eql ,arg 1) (write-char #\s stream))))))
541 ;;;; format directives for floating point output
543 (def-format-directive #\F (colonp atsignp params)
547 "The colon modifier cannot be used with this directive."))
548 (expand-bind-defaults ((w nil) (d nil) (k nil) (ovf nil) (pad #\space)) params
549 `(format-fixed stream ,(expand-next-arg) ,w ,d ,k ,ovf ,pad ,atsignp)))
551 (def-format-directive #\E (colonp atsignp params)
555 "The colon modifier cannot be used with this directive."))
556 (expand-bind-defaults
557 ((w nil) (d nil) (e nil) (k 1) (ovf nil) (pad #\space) (mark nil))
559 `(format-exponential stream ,(expand-next-arg) ,w ,d ,e ,k ,ovf ,pad ,mark
562 (def-format-directive #\G (colonp atsignp params)
566 "The colon modifier cannot be used with this directive."))
567 (expand-bind-defaults
568 ((w nil) (d nil) (e nil) (k nil) (ovf nil) (pad #\space) (mark nil))
570 `(format-general stream ,(expand-next-arg) ,w ,d ,e ,k ,ovf ,pad ,mark ,atsignp)))
572 (def-format-directive #\$ (colonp atsignp params)
573 (expand-bind-defaults ((d 2) (n 1) (w 0) (pad #\space)) params
574 `(format-dollars stream ,(expand-next-arg) ,d ,n ,w ,pad ,colonp
577 ;;;; format directives for line/page breaks etc.
579 (def-format-directive #\% (colonp atsignp params)
580 (when (or colonp atsignp)
583 "The colon and atsign modifiers cannot be used with this directive."
586 (expand-bind-defaults ((count 1)) params
591 (def-format-directive #\& (colonp atsignp params)
592 (when (or colonp atsignp)
595 "The colon and atsign modifiers cannot be used with this directive."
598 (expand-bind-defaults ((count 1)) params
601 (dotimes (i (1- ,count))
603 '(fresh-line stream)))
605 (def-format-directive #\| (colonp atsignp params)
606 (when (or colonp atsignp)
609 "The colon and atsign modifiers cannot be used with this directive."
612 (expand-bind-defaults ((count 1)) params
614 (write-char (code-char form-feed-char-code) stream)))
615 '(write-char (code-char form-feed-char-code) stream)))
617 (def-format-directive #\~ (colonp atsignp params)
618 (when (or colonp atsignp)
621 "The colon and atsign modifiers cannot be used with this directive."
624 (expand-bind-defaults ((count 1)) params
626 (write-char #\~ stream)))
627 '(write-char #\~ stream)))
629 (def-complex-format-directive #\newline (colonp atsignp params directives)
630 (when (and colonp atsignp)
632 :complaint "both colon and atsign modifiers used simultaneously"))
633 (values (expand-bind-defaults () params
635 '(write-char #\newline stream)
637 (if (and (not colonp)
639 (simple-string-p (car directives)))
640 (cons (string-left-trim *format-whitespace-chars*
645 ;;;; format directives for tabs and simple pretty printing
647 (def-format-directive #\T (colonp atsignp params)
649 (expand-bind-defaults ((n 1) (m 1)) params
650 `(pprint-tab ,(if atsignp :section-relative :section)
653 (expand-bind-defaults ((colrel 1) (colinc 1)) params
654 `(format-relative-tab stream ,colrel ,colinc))
655 (expand-bind-defaults ((colnum 1) (colinc 1)) params
656 `(format-absolute-tab stream ,colnum ,colinc)))))
658 (def-format-directive #\_ (colonp atsignp params)
659 (expand-bind-defaults () params
660 `(pprint-newline ,(if colonp
669 (def-format-directive #\I (colonp atsignp params)
673 "cannot use the at-sign modifier with this directive"))
674 (expand-bind-defaults ((n 0)) params
675 `(pprint-indent ,(if colonp :current :block) ,n stream)))
677 ;;;; format directive for ~*
679 (def-format-directive #\* (colonp atsignp params end)
684 "both colon and atsign modifiers used simultaneously")
685 (expand-bind-defaults ((posn 0)) params
686 (unless *orig-args-available*
687 (/show0 "THROWing NEED-ORIG-ARGS from tilde-@*")
688 (throw 'need-orig-args nil))
689 `(if (<= 0 ,posn (length orig-args))
690 (setf args (nthcdr ,posn orig-args))
692 :complaint "Index ~W out of bounds. Should have been ~
694 :args (list ,posn (length orig-args))
695 :offset ,(1- end)))))
697 (expand-bind-defaults ((n 1)) params
698 (unless *orig-args-available*
699 (/show0 "THROWing NEED-ORIG-ARGS from tilde-:*")
700 (throw 'need-orig-args nil))
701 `(do ((cur-posn 0 (1+ cur-posn))
702 (arg-ptr orig-args (cdr arg-ptr)))
704 (let ((new-posn (- cur-posn ,n)))
705 (if (<= 0 new-posn (length orig-args))
706 (setf args (nthcdr new-posn orig-args))
709 "Index ~W is out of bounds; should have been ~
711 :args (list new-posn (length orig-args))
712 :offset ,(1- end)))))))
714 (expand-bind-defaults ((n 1)) params
715 (setf *only-simple-args* nil)
718 (expand-next-arg)))))
720 ;;;; format directive for indirection
722 (def-format-directive #\? (colonp atsignp params string end)
725 :complaint "cannot use the colon modifier with this directive"))
726 (expand-bind-defaults () params
732 "~A~%while processing indirect format string:"
733 :args (list condition)
735 :control-string ,string
736 :offset ,(1- end)))))
738 (if *orig-args-available*
739 `(setf args (%format stream ,(expand-next-arg) orig-args args))
740 (throw 'need-orig-args nil))
741 `(%format stream ,(expand-next-arg) ,(expand-next-arg))))))
743 ;;;; format directives for capitalization
745 (def-complex-format-directive #\( (colonp atsignp params directives)
746 (let ((close (find-directive directives #\) nil)))
749 :complaint "no corresponding close parenthesis"))
750 (let* ((posn (position close directives))
751 (before (subseq directives 0 posn))
752 (after (nthcdr (1+ posn) directives)))
754 (expand-bind-defaults () params
755 `(let ((stream (make-case-frob-stream stream
763 ,@(expand-directive-list before)))
766 (def-complex-format-directive #\) ()
768 :complaint "no corresponding open parenthesis"))
770 ;;;; format directives and support functions for conditionalization
772 (def-complex-format-directive #\[ (colonp atsignp params directives)
773 (multiple-value-bind (sublists last-semi-with-colon-p remaining)
774 (parse-conditional-directive directives)
780 "both colon and atsign modifiers used simultaneously")
784 "Can only specify one section")
785 (expand-bind-defaults () params
786 (expand-maybe-conditional (car sublists)))))
788 (if (= (length sublists) 2)
789 (expand-bind-defaults () params
790 (expand-true-false-conditional (car sublists)
794 "must specify exactly two sections"))
795 (expand-bind-defaults ((index (expand-next-arg))) params
796 (setf *only-simple-args* nil)
798 (when last-semi-with-colon-p
799 (push `(t ,@(expand-directive-list (pop sublists)))
801 (let ((count (length sublists)))
802 (dolist (sublist sublists)
803 (push `(,(decf count)
804 ,@(expand-directive-list sublist))
806 `(case ,index ,@clauses)))))
809 (defun parse-conditional-directive (directives)
811 (last-semi-with-colon-p nil)
812 (remaining directives))
814 (let ((close-or-semi (find-directive remaining #\] t)))
815 (unless close-or-semi
817 :complaint "no corresponding close bracket"))
818 (let ((posn (position close-or-semi remaining)))
819 (push (subseq remaining 0 posn) sublists)
820 (setf remaining (nthcdr (1+ posn) remaining))
821 (when (char= (format-directive-character close-or-semi) #\])
823 (setf last-semi-with-colon-p
824 (format-directive-colonp close-or-semi)))))
825 (values sublists last-semi-with-colon-p remaining)))
827 (defun expand-maybe-conditional (sublist)
829 `(let ((prev-args args)
830 (arg ,(expand-next-arg)))
832 (setf args prev-args)
833 ,@(expand-directive-list sublist)))))
834 (if *only-simple-args*
835 (multiple-value-bind (guts new-args)
836 (let ((*simple-args* *simple-args*))
837 (values (expand-directive-list sublist)
839 (cond ((eq *simple-args* (cdr new-args))
840 (setf *simple-args* new-args)
841 `(when ,(caar new-args)
844 (setf *only-simple-args* nil)
848 (defun expand-true-false-conditional (true false)
849 (let ((arg (expand-next-arg)))
853 ,@(expand-directive-list true))
855 ,@(expand-directive-list false)))))
856 (if *only-simple-args*
857 (multiple-value-bind (true-guts true-args true-simple)
858 (let ((*simple-args* *simple-args*)
859 (*only-simple-args* t))
860 (values (expand-directive-list true)
863 (multiple-value-bind (false-guts false-args false-simple)
864 (let ((*simple-args* *simple-args*)
865 (*only-simple-args* t))
866 (values (expand-directive-list false)
869 (if (= (length true-args) (length false-args))
873 ,(do ((false false-args (cdr false))
874 (true true-args (cdr true))
875 (bindings nil (cons `(,(caar false) ,(caar true))
877 ((eq true *simple-args*)
878 (setf *simple-args* true-args)
879 (setf *only-simple-args*
880 (and true-simple false-simple))
887 (setf *only-simple-args* nil)
891 (def-complex-format-directive #\; ()
894 "~~; directive not contained within either ~~[...~~] or ~~<...~~>"))
896 (def-complex-format-directive #\] ()
899 "no corresponding open bracket"))
901 ;;;; format directive for up-and-out
903 (def-format-directive #\^ (colonp atsignp params)
906 :complaint "cannot use the at-sign modifier with this directive"))
907 (when (and colonp (not *up-up-and-out-allowed*))
909 :complaint "attempt to use ~~:^ outside a ~~:{...~~} construct"))
910 `(when ,(expand-bind-defaults ((arg1 nil) (arg2 nil) (arg3 nil)) params
911 `(cond (,arg3 (<= ,arg1 ,arg2 ,arg3))
912 (,arg2 (eql ,arg1 ,arg2))
913 (,arg1 (eql ,arg1 0))
917 (setf *only-simple-args* nil)
920 '(return-from outside-loop nil)
923 ;;;; format directives for iteration
925 (def-complex-format-directive #\{ (colonp atsignp params string end directives)
926 (let ((close (find-directive directives #\} nil)))
929 :complaint "no corresponding close brace"))
930 (let* ((closed-with-colon (format-directive-colonp close))
931 (posn (position close directives)))
935 (if *orig-args-available*
941 "~A~%while processing indirect format string:"
942 :args (list condition)
944 :control-string ,string
945 :offset ,(1- end)))))
947 (%format stream inside-string orig-args args))))
948 (throw 'need-orig-args nil))
949 (let ((*up-up-and-out-allowed* colonp))
950 (expand-directive-list (subseq directives 0 posn)))))
951 (compute-loop-aux (count)
953 (setf *only-simple-args* nil))
955 ,@(unless closed-with-colon
959 `((when (and ,count (minusp (decf ,count)))
962 (let ((*expander-next-arg-macro* 'expander-next-arg)
963 (*only-simple-args* nil)
964 (*orig-args-available* t))
965 `((let* ((orig-args ,(expand-next-arg))
968 (declare (ignorable orig-args outside-args args))
970 ,@(compute-insides)))))
972 ,@(when closed-with-colon
977 (expand-bind-defaults ((count nil)) params
978 (compute-loop-aux count))
979 (compute-loop-aux nil)))
988 `(let* ((orig-args ,(expand-next-arg))
990 (declare (ignorable orig-args args))
991 ,(let ((*expander-next-arg-macro* 'expander-next-arg)
992 (*only-simple-args* nil)
993 (*orig-args-available* t))
995 (values (if (zerop posn)
996 `(let ((inside-string ,(expand-next-arg)))
999 (nthcdr (1+ posn) directives))))))
1001 (def-complex-format-directive #\} ()
1002 (error 'format-error
1003 :complaint "no corresponding open brace"))
1005 ;;;; format directives and support functions for justification
1007 (defparameter *illegal-inside-justification*
1008 (mapcar (lambda (x) (parse-directive x 0))
1009 '("~W" "~:W" "~@W" "~:@W"
1010 "~_" "~:_" "~@_" "~:@_"
1012 "~I" "~:I" "~@I" "~:@I"
1015 (defun illegal-inside-justification-p (directive)
1016 (member directive *illegal-inside-justification*
1018 (and (format-directive-p x)
1019 (format-directive-p y)
1020 (eql (format-directive-character x) (format-directive-character y))
1021 (eql (format-directive-colonp x) (format-directive-colonp y))
1022 (eql (format-directive-atsignp x) (format-directive-atsignp y))))))
1024 (def-complex-format-directive #\< (colonp atsignp params string end directives)
1025 (multiple-value-bind (segments first-semi close remaining)
1026 (parse-format-justification directives)
1028 (if (format-directive-colonp close)
1029 (multiple-value-bind (prefix per-line-p insides suffix)
1030 (parse-format-logical-block segments colonp first-semi
1031 close params string end)
1032 (expand-format-logical-block prefix per-line-p insides
1034 (let ((count (reduce #'+ (mapcar (lambda (x) (count-if #'illegal-inside-justification-p x)) segments))))
1036 ;; ANSI specifies that "an error is signalled" in this
1038 (error 'format-error
1039 :complaint "~D illegal directive~:P found inside justification block"
1041 :references (list '(:ansi-cl :section (22 3 5 2)))))
1042 (expand-format-justification segments colonp atsignp
1043 first-semi params)))
1046 (def-complex-format-directive #\> ()
1047 (error 'format-error
1048 :complaint "no corresponding open bracket"))
1050 (defun parse-format-logical-block
1051 (segments colonp first-semi close params string end)
1053 (error 'format-error
1054 :complaint "No parameters can be supplied with ~~<...~~:>."
1055 :offset (caar params)))
1056 (multiple-value-bind (prefix insides suffix)
1057 (multiple-value-bind (prefix-default suffix-default)
1058 (if colonp (values "(" ")") (values "" ""))
1059 (flet ((extract-string (list prefix-p)
1060 (let ((directive (find-if #'format-directive-p list)))
1062 (error 'format-error
1064 "cannot include format directives inside the ~
1065 ~:[suffix~;prefix~] segment of ~~<...~~:>"
1066 :args (list prefix-p)
1067 :offset (1- (format-directive-end directive))
1069 (list '(:ansi-cl :section (22 3 5 2))))
1070 (apply #'concatenate 'string list)))))
1071 (case (length segments)
1072 (0 (values prefix-default nil suffix-default))
1073 (1 (values prefix-default (car segments) suffix-default))
1074 (2 (values (extract-string (car segments) t)
1075 (cadr segments) suffix-default))
1076 (3 (values (extract-string (car segments) t)
1078 (extract-string (caddr segments) nil)))
1080 (error 'format-error
1081 :complaint "too many segments for ~~<...~~:>")))))
1082 (when (format-directive-atsignp close)
1084 (add-fill-style-newlines insides
1087 (format-directive-end first-semi)
1090 (and first-semi (format-directive-atsignp first-semi))
1094 (defun add-fill-style-newlines (list string offset &optional last-directive)
1097 (let ((directive (car list)))
1099 ((simple-string-p directive)
1100 (let* ((non-space (position #\Space directive :test #'char/=))
1101 (newlinep (and last-directive
1103 (format-directive-character last-directive)
1106 ((and newlinep non-space)
1108 (list (subseq directive 0 non-space))
1109 (add-fill-style-newlines-aux
1110 (subseq directive non-space) string (+ offset non-space))
1111 (add-fill-style-newlines
1112 (cdr list) string (+ offset (length directive)))))
1115 (add-fill-style-newlines
1116 (cdr list) string (+ offset (length directive)))))
1118 (nconc (add-fill-style-newlines-aux directive string offset)
1119 (add-fill-style-newlines
1120 (cdr list) string (+ offset (length directive))))))))
1123 (add-fill-style-newlines
1125 (format-directive-end directive) directive))))))
1128 (defun add-fill-style-newlines-aux (literal string offset)
1129 (let ((end (length literal))
1131 (collect ((results))
1133 (let ((blank (position #\space literal :start posn)))
1135 (results (subseq literal posn))
1137 (let ((non-blank (or (position #\space literal :start blank
1140 (results (subseq literal posn non-blank))
1141 (results (make-format-directive
1142 :string string :character #\_
1143 :start (+ offset non-blank) :end (+ offset non-blank)
1144 :colonp t :atsignp nil :params nil))
1145 (setf posn non-blank))
1150 (defun parse-format-justification (directives)
1151 (let ((first-semi nil)
1153 (remaining directives))
1154 (collect ((segments))
1156 (let ((close-or-semi (find-directive remaining #\> t)))
1157 (unless close-or-semi
1158 (error 'format-error
1159 :complaint "no corresponding close bracket"))
1160 (let ((posn (position close-or-semi remaining)))
1161 (segments (subseq remaining 0 posn))
1162 (setf remaining (nthcdr (1+ posn) remaining)))
1163 (when (char= (format-directive-character close-or-semi)
1165 (setf close close-or-semi)
1168 (setf first-semi close-or-semi))))
1169 (values (segments) first-semi close remaining))))
1171 (sb!xc:defmacro expander-pprint-next-arg (string offset)
1174 (error 'format-error
1175 :complaint "no more arguments"
1176 :control-string ,string
1181 (defun expand-format-logical-block (prefix per-line-p insides suffix atsignp)
1182 `(let ((arg ,(if atsignp 'args (expand-next-arg))))
1184 (setf *only-simple-args* nil)
1186 (pprint-logical-block
1188 ,(if per-line-p :per-line-prefix :prefix) ,prefix
1192 `((orig-args arg))))
1193 (declare (ignorable args ,@(unless atsignp '(orig-args))))
1195 ,@(let ((*expander-next-arg-macro* 'expander-pprint-next-arg)
1196 (*only-simple-args* nil)
1197 (*orig-args-available*
1198 (if atsignp *orig-args-available* t)))
1199 (expand-directive-list insides)))))))
1201 (defun expand-format-justification (segments colonp atsignp first-semi params)
1202 (let ((newline-segment-p
1204 (format-directive-colonp first-semi))))
1205 (expand-bind-defaults
1206 ((mincol 0) (colinc 1) (minpad 0) (padchar #\space))
1208 `(let ((segments nil)
1209 ,@(when newline-segment-p
1210 '((newline-segment nil)
1214 ,@(when newline-segment-p
1215 `((setf newline-segment
1216 (with-output-to-string (stream)
1217 ,@(expand-directive-list (pop segments))))
1218 ,(expand-bind-defaults
1220 (line-len '(or (sb!impl::line-length stream) 72)))
1221 (format-directive-params first-semi)
1222 `(setf extra-space ,extra line-len ,line-len))))
1223 ,@(mapcar (lambda (segment)
1224 `(push (with-output-to-string (stream)
1225 ,@(expand-directive-list segment))
1228 (format-justification stream
1229 ,@(if newline-segment-p
1230 '(newline-segment extra-space line-len)
1232 segments ,colonp ,atsignp
1233 ,mincol ,colinc ,minpad ,padchar)))))
1235 ;;;; format directive and support function for user-defined method
1237 (def-format-directive #\/ (string start end colonp atsignp params)
1238 (let ((symbol (extract-user-fun-name string start end)))
1239 (collect ((param-names) (bindings))
1240 (dolist (param-and-offset params)
1241 (let ((param (cdr param-and-offset)))
1242 (let ((param-name (gensym)))
1243 (param-names param-name)
1244 (bindings `(,param-name
1246 (:arg (expand-next-arg))
1247 (:remaining '(length args))
1250 (,symbol stream ,(expand-next-arg) ,colonp ,atsignp
1251 ,@(param-names))))))
1253 (defun extract-user-fun-name (string start end)
1254 (let ((slash (position #\/ string :start start :end (1- end)
1257 (error 'format-error
1258 :complaint "malformed ~~/ directive"))
1259 (let* ((name (string-upcase (let ((foo string))
1260 ;; Hack alert: This is to keep the compiler
1261 ;; quiet about deleting code inside the
1262 ;; subseq expansion.
1263 (subseq foo (1+ slash) (1- end)))))
1264 (first-colon (position #\: name))
1265 (second-colon (if first-colon (position #\: name :start (1+ first-colon))))
1266 (package-name (if first-colon
1267 (subseq name 0 first-colon)
1268 "COMMON-LISP-USER"))
1269 (package (find-package package-name)))
1271 ;; FIXME: should be PACKAGE-ERROR? Could we just use
1272 ;; FIND-UNDELETED-PACKAGE-OR-LOSE?
1273 (error 'format-error
1274 :complaint "no package named ~S"
1275 :args (list package-name)))
1277 ((and second-colon (= second-colon (1+ first-colon)))
1278 (subseq name (1+ second-colon)))
1280 (subseq name (1+ first-colon)))
1284 ;;; compile-time checking for argument mismatch. This code is
1285 ;;; inspired by that of Gerd Moellmann, and comes decorated with
1287 (defun %compiler-walk-format-string (string args)
1288 (declare (type simple-string string))
1289 (let ((*default-format-error-control-string* string))
1290 (macrolet ((incf-both (&optional (increment 1))
1292 (incf min ,increment)
1293 (incf max ,increment)))
1294 (walk-complex-directive (function)
1295 `(multiple-value-bind (min-inc max-inc remaining)
1296 (,function directive directives args)
1299 (setq directives remaining))))
1300 ;; FIXME: these functions take a list of arguments as well as
1301 ;; the directive stream. This is to enable possibly some
1302 ;; limited type checking on FORMAT's arguments, as well as
1303 ;; simple argument count mismatch checking: when the minimum and
1304 ;; maximum argument counts are the same at a given point, we
1305 ;; know which argument is going to be used for a given
1306 ;; directive, and some (annotated below) require arguments of
1307 ;; particular types.
1309 ((walk-justification (justification directives args)
1310 (declare (ignore args))
1311 (let ((*default-format-error-offset*
1312 (1- (format-directive-end justification))))
1313 (multiple-value-bind (segments first-semi close remaining)
1314 (parse-format-justification directives)
1315 (declare (ignore segments first-semi))
1317 ((not (format-directive-colonp close))
1318 (values 0 0 directives))
1319 ((format-directive-atsignp justification)
1320 (values 0 sb!xc:call-arguments-limit directives))
1321 ;; FIXME: here we could assert that the
1322 ;; corresponding argument was a list.
1323 (t (values 1 1 remaining))))))
1324 (walk-conditional (conditional directives args)
1325 (let ((*default-format-error-offset*
1326 (1- (format-directive-end conditional))))
1327 (multiple-value-bind (sublists last-semi-with-colon-p remaining)
1328 (parse-conditional-directive directives)
1329 (declare (ignore last-semi-with-colon-p))
1331 (loop for s in sublists
1333 1 (walk-directive-list s args)))))
1335 ((format-directive-atsignp conditional)
1336 (values 1 (max 1 sub-max) remaining))
1337 ((loop for p in (format-directive-params conditional)
1338 thereis (or (integerp (cdr p))
1339 (memq (cdr p) '(:remaining :arg))))
1340 (values 0 sub-max remaining))
1341 ;; FIXME: if not COLONP, then the next argument
1342 ;; must be a number.
1343 (t (values 1 (1+ sub-max) remaining)))))))
1344 (walk-iteration (iteration directives args)
1345 (declare (ignore args))
1346 (let ((*default-format-error-offset*
1347 (1- (format-directive-end iteration))))
1348 (let* ((close (find-directive directives #\} nil))
1349 (posn (or (position close directives)
1350 (error 'format-error
1351 :complaint "no corresponding close brace")))
1352 (remaining (nthcdr (1+ posn) directives)))
1353 ;; FIXME: if POSN is zero, the next argument must be
1354 ;; a format control (either a function or a string).
1355 (if (format-directive-atsignp iteration)
1356 (values (if (zerop posn) 1 0)
1357 sb!xc:call-arguments-limit
1359 ;; FIXME: the argument corresponding to this
1360 ;; directive must be a list.
1361 (let ((nreq (if (zerop posn) 2 1)))
1362 (values nreq nreq remaining))))))
1363 (walk-directive-list (directives args)
1364 (let ((min 0) (max 0))
1366 (let ((directive (pop directives)))
1367 (when (null directive)
1368 (return (values min (min max sb!xc:call-arguments-limit))))
1369 (when (format-directive-p directive)
1370 (incf-both (count :arg (format-directive-params directive)
1372 (let ((c (format-directive-character directive)))
1374 ((find c "ABCDEFGORSWX$/")
1377 (unless (format-directive-colonp directive)
1379 ((or (find c "IT%&|_();>~") (char= c #\Newline)))
1380 ;; FIXME: check correspondence of ~( and ~)
1382 (walk-complex-directive walk-justification))
1384 (walk-complex-directive walk-conditional))
1386 (walk-complex-directive walk-iteration))
1388 ;; FIXME: the argument corresponding to this
1389 ;; directive must be a format control.
1391 ((format-directive-atsignp directive)
1393 (setq max sb!xc:call-arguments-limit))
1395 (t (throw 'give-up-format-string-walk nil))))))))))
1396 (catch 'give-up-format-string-walk
1397 (let ((directives (tokenize-control-string string)))
1398 (walk-directive-list directives args)))))))