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 :complaint "~~> without a matching ~~<"
84 :control-string string
85 :offset next-directive))
87 ((format-directive-colonp directive)
89 (setf pprint (car block)))
92 (unless justification-semicolon
93 (setf justification-semicolon semicolon))))
95 ;; block cases are handled by the #\< expander/interpreter
98 ((#\W #\I #\_) (unless pprint (setf pprint directive)))
99 (#\T (when (and (format-directive-colonp directive)
101 (setf pprint directive))))))
102 (push directive result)
103 (setf index (format-directive-end directive)))))
104 (when (and pprint justification-semicolon)
105 (let ((pprint-offset (1- (format-directive-end pprint)))
106 (justification-offset
107 (1- (format-directive-end justification-semicolon))))
109 :complaint "misuse of justification and pprint directives"
110 :control-string string
111 :offset (min pprint-offset justification-offset)
112 :second-relative (- (max pprint-offset justification-offset)
113 (min pprint-offset justification-offset)
115 :references (list '(:ansi-cl :section (22 3 5 2))))))
118 (defun parse-directive (string start)
119 (let ((posn (1+ start)) (params nil) (colonp nil) (atsignp nil)
120 (end (length string)))
124 :complaint "string ended before directive was found"
125 :control-string string
127 (schar string posn)))
129 (when (or colonp atsignp)
131 :complaint "parameters found after #\\: or #\\@ modifier"
132 :control-string string
134 :references (list '(:ansi-cl :section (22 3)))))))
136 (let ((char (get-char)))
137 (cond ((or (char<= #\0 char #\9) (char= char #\+) (char= char #\-))
139 (multiple-value-bind (param new-posn)
140 (parse-integer string :start posn :junk-allowed t)
141 (push (cons posn param) params)
149 ((or (char= char #\v)
152 (push (cons posn :arg) params)
162 (push (cons posn :remaining) params)
173 (push (cons posn (get-char)) params)
175 (unless (char= (get-char) #\,)
179 (push (cons posn nil) params))
183 :complaint "too many colons supplied"
184 :control-string string
186 :references (list '(:ansi-cl :section (22 3))))
191 :complaint "too many #\\@ characters supplied"
192 :control-string string
194 :references (list '(:ansi-cl :section (22 3))))
197 (when (and (char= (schar string (1- posn)) #\,)
199 (char/= (schar string (- posn 2)) #\')))
201 (push (cons (1- posn) nil) params))
204 (let ((char (get-char)))
205 (when (char= char #\/)
206 (let ((closing-slash (position #\/ string :start (1+ posn))))
208 (setf posn closing-slash)
210 :complaint "no matching closing slash"
211 :control-string string
213 (make-format-directive
214 :string string :start start :end (1+ posn)
215 :character (char-upcase char)
216 :colonp colonp :atsignp atsignp
217 :params (nreverse params))))))
221 (sb!xc:defmacro formatter (control-string)
222 `#',(%formatter control-string))
224 (defun %formatter (control-string)
226 (catch 'need-orig-args
227 (let* ((*simple-args* nil)
228 (*only-simple-args* t)
229 (guts (expand-control-string control-string))
231 (dolist (arg *simple-args*)
235 :complaint "required argument missing"
236 :control-string ,control-string
239 (return `(lambda (stream &optional ,@args &rest args)
240 (declare (ignorable stream))
243 (let ((*orig-args-available* t)
244 (*only-simple-args* nil))
245 `(lambda (stream &rest orig-args)
246 (declare (ignorable stream))
247 (let ((args orig-args))
248 ,(expand-control-string control-string)
251 (defun expand-control-string (string)
252 (let* ((string (etypecase string
256 (coerce string 'simple-string))))
257 (*default-format-error-control-string* string)
258 (directives (tokenize-control-string string)))
260 ,@(expand-directive-list directives))))
262 (defun expand-directive-list (directives)
264 (remaining-directives directives))
266 (unless remaining-directives
268 (multiple-value-bind (form new-directives)
269 (expand-directive (car remaining-directives)
270 (cdr remaining-directives))
272 (setf remaining-directives new-directives)))
275 (defun expand-directive (directive more-directives)
279 (let ((char (format-directive-character directive)))
282 (aref *format-directive-expanders* (sb!xc:char-code char))))))
283 (*default-format-error-offset*
284 (1- (format-directive-end directive))))
285 (declare (type (or null function) expander))
287 (funcall expander directive more-directives)
289 :complaint "unknown directive ~@[(character: ~A)~]"
290 :args (list (char-name (format-directive-character directive)))))))
292 (values `(write-string ,directive stream)
295 (defmacro-mundanely expander-next-arg (string offset)
299 :complaint "no more arguments"
300 :control-string ,string
303 (defun expand-next-arg (&optional offset)
304 (if (or *orig-args-available* (not *only-simple-args*))
305 `(,*expander-next-arg-macro*
306 ,*default-format-error-control-string*
307 ,(or offset *default-format-error-offset*))
308 (let ((symbol (sb!xc:gensym "FORMAT-ARG")))
309 (push (cons symbol (or offset *default-format-error-offset*))
313 (defmacro expand-bind-defaults (specs params &body body)
314 (once-only ((params params))
316 (collect ((expander-bindings) (runtime-bindings))
318 (destructuring-bind (var default) spec
319 (let ((symbol (sb!xc:gensym "FVAR")))
324 (let* ((param-and-offset (pop ,params))
325 (offset (car param-and-offset))
326 (param (cdr param-and-offset)))
328 (:arg `(or ,(expand-next-arg offset) ,,default))
330 (setf *only-simple-args* nil)
334 `(let ,(expander-bindings)
335 `(let ,(list ,@(runtime-bindings))
339 :complaint "too many parameters, expected no more than ~W"
340 :args (list ,(length specs))
341 :offset (caar ,params)))
346 :complaint "too many parameters, expected none"
347 :offset (caar ,params)))
350 ;;;; format directive machinery
352 (eval-when (:compile-toplevel :execute)
353 (#+sb-xc-host defmacro #-sb-xc-host sb!xc:defmacro def-complex-format-directive (char lambda-list &body body)
354 (let ((defun-name (intern (format nil
355 "~:@(~:C~)-FORMAT-DIRECTIVE-EXPANDER"
357 (directive (sb!xc:gensym "DIRECTIVE"))
358 (directives (if lambda-list (car (last lambda-list)) (sb!xc:gensym "DIRECTIVES"))))
360 (defun ,defun-name (,directive ,directives)
362 `((let ,(mapcar (lambda (var)
364 (,(symbolicate "FORMAT-DIRECTIVE-" var)
366 (butlast lambda-list))
368 `((declare (ignore ,directive ,directives))
370 (%set-format-directive-expander ,char #',defun-name))))
372 (#+sb-xc-host defmacro #-sb-xc-host sb!xc:defmacro def-format-directive (char lambda-list &body body)
373 (let ((directives (sb!xc:gensym "DIRECTIVES"))
375 (body-without-decls body))
377 (let ((form (car body-without-decls)))
378 (unless (and (consp form) (eq (car form) 'declare))
380 (push (pop body-without-decls) declarations)))
381 (setf declarations (reverse declarations))
382 `(def-complex-format-directive ,char (,@lambda-list ,directives)
384 (values (progn ,@body-without-decls)
388 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
390 (defun %set-format-directive-expander (char fn)
391 (let ((code (sb!xc:char-code (char-upcase char))))
392 (setf (aref *format-directive-expanders* code) fn))
395 (defun %set-format-directive-interpreter (char fn)
396 (let ((code (sb!xc:char-code (char-upcase char))))
397 (setf (aref *format-directive-interpreters* code) fn))
400 (defun find-directive (directives kind stop-at-semi)
402 (let ((next (car directives)))
403 (if (format-directive-p next)
404 (let ((char (format-directive-character next)))
405 (if (or (char= kind char)
406 (and stop-at-semi (char= char #\;)))
409 (cdr (flet ((after (char)
410 (member (find-directive (cdr directives)
421 (find-directive (cdr directives) kind stop-at-semi)))))
425 ;;;; format directives for simple output
427 (def-format-directive #\A (colonp atsignp params)
429 (expand-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
432 `(format-princ stream ,(expand-next-arg) ',colonp ',atsignp
433 ,mincol ,colinc ,minpad ,padchar))
435 `(or ,(expand-next-arg) "()")
439 (def-format-directive #\S (colonp atsignp params)
441 (expand-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
444 `(format-prin1 stream ,(expand-next-arg) ,colonp ,atsignp
445 ,mincol ,colinc ,minpad ,padchar)))
447 `(let ((arg ,(expand-next-arg)))
450 (princ "()" stream))))
452 `(prin1 ,(expand-next-arg) stream))))
454 (def-format-directive #\C (colonp atsignp params string end)
455 (expand-bind-defaults () params
456 (let ((n-arg (sb!xc:gensym "ARG")))
457 `(let ((,n-arg ,(expand-next-arg)))
458 (unless (typep ,n-arg 'character)
460 :complaint "~s is not of type CHARACTER."
462 :control-string ,string
465 `(format-print-named-character ,n-arg stream))
467 `(prin1 ,n-arg stream))
469 `(write-char ,n-arg stream)))))))
471 (def-format-directive #\W (colonp atsignp params)
472 (expand-bind-defaults () params
473 (if (or colonp atsignp)
474 `(let (,@(when colonp
475 '((*print-pretty* t)))
477 '((*print-level* nil)
478 (*print-length* nil))))
479 (output-object ,(expand-next-arg) stream))
480 `(output-object ,(expand-next-arg) stream))))
482 ;;;; format directives for integer output
484 (defun expand-format-integer (base colonp atsignp params)
485 (if (or colonp atsignp params)
486 (expand-bind-defaults
487 ((mincol 0) (padchar #\space) (commachar #\,) (commainterval 3))
489 `(format-print-integer stream ,(expand-next-arg) ,colonp ,atsignp
490 ,base ,mincol ,padchar ,commachar
492 `(let ((*print-base* ,base)
494 (*print-escape* nil))
495 (output-object ,(expand-next-arg) stream))))
497 (def-format-directive #\D (colonp atsignp params)
498 (expand-format-integer 10 colonp atsignp params))
500 (def-format-directive #\B (colonp atsignp params)
501 (expand-format-integer 2 colonp atsignp params))
503 (def-format-directive #\O (colonp atsignp params)
504 (expand-format-integer 8 colonp atsignp params))
506 (def-format-directive #\X (colonp atsignp params)
507 (expand-format-integer 16 colonp atsignp params))
509 (def-format-directive #\R (colonp atsignp params string end)
510 (expand-bind-defaults
511 ((base nil) (mincol 0) (padchar #\space) (commachar #\,)
514 (let ((n-arg (sb!xc:gensym "ARG")))
515 `(let ((,n-arg ,(expand-next-arg)))
519 :complaint "~s is not of type INTEGER."
521 :control-string ,string
524 (format-print-integer stream ,n-arg ,colonp ,atsignp
526 ,padchar ,commachar ,commainterval)
529 `(format-print-old-roman stream ,n-arg)
530 `(format-print-roman stream ,n-arg))
532 `(format-print-ordinal stream ,n-arg)
533 `(format-print-cardinal stream ,n-arg))))))))
535 ;;;; format directive for pluralization
537 (def-format-directive #\P (colonp atsignp params end)
538 (expand-bind-defaults () params
542 (*orig-args-available*
543 `(if (eq orig-args args)
545 :complaint "no previous argument"
547 (do ((arg-ptr orig-args (cdr arg-ptr)))
548 ((eq (cdr arg-ptr) args)
551 (unless *simple-args*
553 :complaint "no previous argument"))
554 (caar *simple-args*))
556 (/show0 "THROWing NEED-ORIG-ARGS from tilde-P")
557 (throw 'need-orig-args nil)))))
559 `(write-string (if (eql ,arg 1) "y" "ies") stream)
560 `(unless (eql ,arg 1) (write-char #\s stream))))))
562 ;;;; format directives for floating point output
564 (def-format-directive #\F (colonp atsignp params)
568 "The colon modifier cannot be used with this directive."))
569 (expand-bind-defaults ((w nil) (d nil) (k nil) (ovf nil) (pad #\space)) params
570 `(format-fixed stream ,(expand-next-arg) ,w ,d ,k ,ovf ,pad ,atsignp)))
572 (def-format-directive #\E (colonp atsignp params)
576 "The colon modifier cannot be used with this directive."))
577 (expand-bind-defaults
578 ((w nil) (d nil) (e nil) (k 1) (ovf nil) (pad #\space) (mark nil))
580 `(format-exponential stream ,(expand-next-arg) ,w ,d ,e ,k ,ovf ,pad ,mark
583 (def-format-directive #\G (colonp atsignp params)
587 "The colon modifier cannot be used with this directive."))
588 (expand-bind-defaults
589 ((w nil) (d nil) (e nil) (k nil) (ovf nil) (pad #\space) (mark nil))
591 `(format-general stream ,(expand-next-arg) ,w ,d ,e ,k ,ovf ,pad ,mark ,atsignp)))
593 (def-format-directive #\$ (colonp atsignp params)
594 (expand-bind-defaults ((d 2) (n 1) (w 0) (pad #\space)) params
595 `(format-dollars stream ,(expand-next-arg) ,d ,n ,w ,pad ,colonp
598 ;;;; format directives for line/page breaks etc.
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
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
623 (dotimes (i (1- ,count))
625 '(fresh-line stream)))
627 (def-format-directive #\| (colonp atsignp params)
628 (when (or colonp atsignp)
631 "The colon and atsign modifiers cannot be used with this directive."
634 (expand-bind-defaults ((count 1)) params
636 (write-char (code-char form-feed-char-code) stream)))
637 '(write-char (code-char form-feed-char-code) stream)))
639 (def-format-directive #\~ (colonp atsignp params)
640 (when (or colonp atsignp)
643 "The colon and atsign modifiers cannot be used with this directive."
646 (expand-bind-defaults ((count 1)) params
648 (write-char #\~ stream)))
649 '(write-char #\~ stream)))
651 (def-complex-format-directive #\newline (colonp atsignp params directives)
652 (when (and colonp atsignp)
654 :complaint "both colon and atsign modifiers used simultaneously"))
655 (values (expand-bind-defaults () params
657 '(write-char #\newline stream)
659 (if (and (not colonp)
661 (simple-string-p (car directives)))
662 (cons (string-left-trim *format-whitespace-chars*
667 ;;;; format directives for tabs and simple pretty printing
669 (def-format-directive #\T (colonp atsignp params)
671 (expand-bind-defaults ((n 1) (m 1)) params
672 `(pprint-tab ,(if atsignp :section-relative :section)
675 (expand-bind-defaults ((colrel 1) (colinc 1)) params
676 `(format-relative-tab stream ,colrel ,colinc))
677 (expand-bind-defaults ((colnum 1) (colinc 1)) params
678 `(format-absolute-tab stream ,colnum ,colinc)))))
680 (def-format-directive #\_ (colonp atsignp params)
681 (expand-bind-defaults () params
682 `(pprint-newline ,(if colonp
691 (def-format-directive #\I (colonp atsignp params)
695 "cannot use the at-sign modifier with this directive"))
696 (expand-bind-defaults ((n 0)) params
697 `(pprint-indent ,(if colonp :current :block) ,n stream)))
699 ;;;; format directive for ~*
701 (def-format-directive #\* (colonp atsignp params end)
706 "both colon and atsign modifiers used simultaneously")
707 (expand-bind-defaults ((posn 0)) params
708 (unless *orig-args-available*
709 (/show0 "THROWing NEED-ORIG-ARGS from tilde-@*")
710 (throw 'need-orig-args nil))
711 `(if (<= 0 ,posn (length orig-args))
712 (setf args (nthcdr ,posn orig-args))
714 :complaint "Index ~W out of bounds. Should have been ~
716 :args (list ,posn (length orig-args))
717 :offset ,(1- end)))))
719 (expand-bind-defaults ((n 1)) params
720 (unless *orig-args-available*
721 (/show0 "THROWing NEED-ORIG-ARGS from tilde-:*")
722 (throw 'need-orig-args nil))
723 `(do ((cur-posn 0 (1+ cur-posn))
724 (arg-ptr orig-args (cdr arg-ptr)))
726 (let ((new-posn (- cur-posn ,n)))
727 (if (<= 0 new-posn (length orig-args))
728 (setf args (nthcdr new-posn orig-args))
731 "Index ~W is out of bounds; should have been ~
733 :args (list new-posn (length orig-args))
734 :offset ,(1- end)))))))
736 (expand-bind-defaults ((n 1)) params
737 (setf *only-simple-args* nil)
740 (expand-next-arg)))))
742 ;;;; format directive for indirection
744 (def-format-directive #\? (colonp atsignp params string end)
747 :complaint "cannot use the colon modifier with this directive"))
748 (expand-bind-defaults () params
754 "~A~%while processing indirect format string:"
755 :args (list condition)
757 :control-string ,string
758 :offset ,(1- end)))))
760 (if *orig-args-available*
761 `(setf args (%format stream ,(expand-next-arg) orig-args args))
762 (throw 'need-orig-args nil))
763 `(%format stream ,(expand-next-arg) ,(expand-next-arg))))))
765 ;;;; format directives for capitalization
767 (def-complex-format-directive #\( (colonp atsignp params directives)
768 (let ((close (find-directive directives #\) nil)))
771 :complaint "no corresponding close parenthesis"))
772 (let* ((posn (position close directives))
773 (before (subseq directives 0 posn))
774 (after (nthcdr (1+ posn) directives)))
776 (expand-bind-defaults () params
777 `(let ((stream (make-case-frob-stream stream
785 ,@(expand-directive-list before)))
788 (def-complex-format-directive #\) ()
790 :complaint "no corresponding open parenthesis"))
792 ;;;; format directives and support functions for conditionalization
794 (def-complex-format-directive #\[ (colonp atsignp params directives)
795 (multiple-value-bind (sublists last-semi-with-colon-p remaining)
796 (parse-conditional-directive directives)
802 "both colon and atsign modifiers used simultaneously")
806 "Can only specify one section")
807 (expand-bind-defaults () params
808 (expand-maybe-conditional (car sublists)))))
810 (if (= (length sublists) 2)
811 (expand-bind-defaults () params
812 (expand-true-false-conditional (car sublists)
816 "must specify exactly two sections"))
817 (expand-bind-defaults ((index nil)) params
818 (setf *only-simple-args* nil)
820 (case `(or ,index ,(expand-next-arg))))
821 (when last-semi-with-colon-p
822 (push `(t ,@(expand-directive-list (pop sublists)))
824 (let ((count (length sublists)))
825 (dolist (sublist sublists)
826 (push `(,(decf count)
827 ,@(expand-directive-list sublist))
829 `(case ,case ,@clauses)))))
832 (defun parse-conditional-directive (directives)
834 (last-semi-with-colon-p nil)
835 (remaining directives))
837 (let ((close-or-semi (find-directive remaining #\] t)))
838 (unless close-or-semi
840 :complaint "no corresponding close bracket"))
841 (let ((posn (position close-or-semi remaining)))
842 (push (subseq remaining 0 posn) sublists)
843 (setf remaining (nthcdr (1+ posn) remaining))
844 (when (char= (format-directive-character close-or-semi) #\])
846 (setf last-semi-with-colon-p
847 (format-directive-colonp close-or-semi)))))
848 (values sublists last-semi-with-colon-p remaining)))
850 (defun expand-maybe-conditional (sublist)
852 `(let ((prev-args args)
853 (arg ,(expand-next-arg)))
855 (setf args prev-args)
856 ,@(expand-directive-list sublist)))))
857 (if *only-simple-args*
858 (multiple-value-bind (guts new-args)
859 (let ((*simple-args* *simple-args*))
860 (values (expand-directive-list sublist)
862 (cond ((and new-args (eq *simple-args* (cdr new-args)))
863 (setf *simple-args* new-args)
864 `(when ,(caar new-args)
867 (setf *only-simple-args* nil)
871 (defun expand-true-false-conditional (true false)
872 (let ((arg (expand-next-arg)))
876 ,@(expand-directive-list true))
878 ,@(expand-directive-list false)))))
879 (if *only-simple-args*
880 (multiple-value-bind (true-guts true-args true-simple)
881 (let ((*simple-args* *simple-args*)
882 (*only-simple-args* t))
883 (values (expand-directive-list true)
886 (multiple-value-bind (false-guts false-args false-simple)
887 (let ((*simple-args* *simple-args*)
888 (*only-simple-args* t))
889 (values (expand-directive-list false)
892 (if (= (length true-args) (length false-args))
896 ,(do ((false false-args (cdr false))
897 (true true-args (cdr true))
898 (bindings nil (cons `(,(caar false) ,(caar true))
900 ((eq true *simple-args*)
901 (setf *simple-args* true-args)
902 (setf *only-simple-args*
903 (and true-simple false-simple))
910 (setf *only-simple-args* nil)
914 (def-complex-format-directive #\; ()
917 "~~; directive not contained within either ~~[...~~] or ~~<...~~>"))
919 (def-complex-format-directive #\] ()
922 "no corresponding open bracket"))
924 ;;;; format directive for up-and-out
926 (def-format-directive #\^ (colonp atsignp params)
929 :complaint "cannot use the at-sign modifier with this directive"))
930 (when (and colonp (not *up-up-and-out-allowed*))
932 :complaint "attempt to use ~~:^ outside a ~~:{...~~} construct"))
933 `(when ,(expand-bind-defaults ((arg1 nil) (arg2 nil) (arg3 nil)) params
934 `(cond (,arg3 (<= ,arg1 ,arg2 ,arg3))
935 (,arg2 (eql ,arg1 ,arg2))
936 (,arg1 (eql ,arg1 0))
940 (setf *only-simple-args* nil)
943 '(return-from outside-loop nil)
946 ;;;; format directives for iteration
948 (def-complex-format-directive #\{ (colonp atsignp params string end directives)
949 (let ((close (find-directive directives #\} nil)))
952 :complaint "no corresponding close brace"))
953 (let* ((closed-with-colon (format-directive-colonp close))
954 (posn (position close directives)))
958 (if *orig-args-available*
964 "~A~%while processing indirect format string:"
965 :args (list condition)
967 :control-string ,string
968 :offset ,(1- end)))))
970 (%format stream inside-string orig-args args))))
971 (throw 'need-orig-args nil))
972 (let ((*up-up-and-out-allowed* colonp))
973 (expand-directive-list (subseq directives 0 posn)))))
974 (compute-loop (count)
976 (setf *only-simple-args* nil))
978 ,@(unless closed-with-colon
982 `((when (and ,count (minusp (decf ,count)))
985 (let ((*expander-next-arg-macro* 'expander-next-arg)
986 (*only-simple-args* nil)
987 (*orig-args-available* t))
988 `((let* ((orig-args ,(expand-next-arg))
991 (declare (ignorable orig-args outside-args args))
993 ,@(compute-insides)))))
995 ,@(when closed-with-colon
998 (compute-block (count)
1000 `(block outside-loop
1001 ,(compute-loop count))
1002 (compute-loop count)))
1003 (compute-bindings (count)
1005 (compute-block count)
1006 `(let* ((orig-args ,(expand-next-arg))
1008 (declare (ignorable orig-args args))
1009 ,(let ((*expander-next-arg-macro* 'expander-next-arg)
1010 (*only-simple-args* nil)
1011 (*orig-args-available* t))
1012 (compute-block count))))))
1014 (expand-bind-defaults ((count nil)) params
1016 `(let ((inside-string ,(expand-next-arg)))
1017 ,(compute-bindings count))
1018 (compute-bindings count)))
1020 `(let ((inside-string ,(expand-next-arg)))
1021 ,(compute-bindings nil))
1022 (compute-bindings nil)))
1023 (nthcdr (1+ posn) directives))))))
1025 (def-complex-format-directive #\} ()
1026 (error 'format-error
1027 :complaint "no corresponding open brace"))
1029 ;;;; format directives and support functions for justification
1031 (defparameter *illegal-inside-justification*
1032 (mapcar (lambda (x) (parse-directive x 0))
1033 '("~W" "~:W" "~@W" "~:@W"
1034 "~_" "~:_" "~@_" "~:@_"
1036 "~I" "~:I" "~@I" "~:@I"
1039 (defun illegal-inside-justification-p (directive)
1040 (member directive *illegal-inside-justification*
1042 (and (format-directive-p x)
1043 (format-directive-p y)
1044 (eql (format-directive-character x) (format-directive-character y))
1045 (eql (format-directive-colonp x) (format-directive-colonp y))
1046 (eql (format-directive-atsignp x) (format-directive-atsignp y))))))
1048 (def-complex-format-directive #\< (colonp atsignp params string end directives)
1049 (multiple-value-bind (segments first-semi close remaining)
1050 (parse-format-justification directives)
1052 (if (format-directive-colonp close)
1053 (multiple-value-bind (prefix per-line-p insides suffix)
1054 (parse-format-logical-block segments colonp first-semi
1055 close params string end)
1056 (expand-format-logical-block prefix per-line-p insides
1058 (let ((count (reduce #'+ (mapcar (lambda (x) (count-if #'illegal-inside-justification-p x)) segments))))
1060 ;; ANSI specifies that "an error is signalled" in this
1062 (error 'format-error
1063 :complaint "~D illegal directive~:P found inside justification block"
1065 :references (list '(:ansi-cl :section (22 3 5 2)))))
1066 (expand-format-justification segments colonp atsignp
1067 first-semi params)))
1070 (def-complex-format-directive #\> ()
1071 (error 'format-error
1072 :complaint "no corresponding open bracket"))
1074 (defun parse-format-logical-block
1075 (segments colonp first-semi close params string end)
1077 (error 'format-error
1078 :complaint "No parameters can be supplied with ~~<...~~:>."
1079 :offset (caar params)))
1080 (multiple-value-bind (prefix insides suffix)
1081 (multiple-value-bind (prefix-default suffix-default)
1082 (if colonp (values "(" ")") (values "" ""))
1083 (flet ((extract-string (list prefix-p)
1084 (let ((directive (find-if #'format-directive-p list)))
1086 (error 'format-error
1088 "cannot include format directives inside the ~
1089 ~:[suffix~;prefix~] segment of ~~<...~~:>"
1090 :args (list prefix-p)
1091 :offset (1- (format-directive-end directive))
1093 (list '(:ansi-cl :section (22 3 5 2))))
1094 (apply #'concatenate 'string list)))))
1095 (case (length segments)
1096 (0 (values prefix-default nil suffix-default))
1097 (1 (values prefix-default (car segments) suffix-default))
1098 (2 (values (extract-string (car segments) t)
1099 (cadr segments) suffix-default))
1100 (3 (values (extract-string (car segments) t)
1102 (extract-string (caddr segments) nil)))
1104 (error 'format-error
1105 :complaint "too many segments for ~~<...~~:>")))))
1106 (when (format-directive-atsignp close)
1108 (add-fill-style-newlines insides
1111 (format-directive-end first-semi)
1114 (and first-semi (format-directive-atsignp first-semi))
1118 (defun add-fill-style-newlines (list string offset &optional last-directive)
1121 (let ((directive (car list)))
1123 ((simple-string-p directive)
1124 (let* ((non-space (position #\Space directive :test #'char/=))
1125 (newlinep (and last-directive
1127 (format-directive-character last-directive)
1130 ((and newlinep non-space)
1132 (list (subseq directive 0 non-space))
1133 (add-fill-style-newlines-aux
1134 (subseq directive non-space) string (+ offset non-space))
1135 (add-fill-style-newlines
1136 (cdr list) string (+ offset (length directive)))))
1139 (add-fill-style-newlines
1140 (cdr list) string (+ offset (length directive)))))
1142 (nconc (add-fill-style-newlines-aux directive string offset)
1143 (add-fill-style-newlines
1144 (cdr list) string (+ offset (length directive))))))))
1147 (add-fill-style-newlines
1149 (format-directive-end directive) directive))))))
1152 (defun add-fill-style-newlines-aux (literal string offset)
1153 (let ((end (length literal))
1155 (collect ((results))
1157 (let ((blank (position #\space literal :start posn)))
1159 (results (subseq literal posn))
1161 (let ((non-blank (or (position #\space literal :start blank
1164 (results (subseq literal posn non-blank))
1165 (results (make-format-directive
1166 :string string :character #\_
1167 :start (+ offset non-blank) :end (+ offset non-blank)
1168 :colonp t :atsignp nil :params nil))
1169 (setf posn non-blank))
1174 (defun parse-format-justification (directives)
1175 (let ((first-semi nil)
1177 (remaining directives))
1178 (collect ((segments))
1180 (let ((close-or-semi (find-directive remaining #\> t)))
1181 (unless close-or-semi
1182 (error 'format-error
1183 :complaint "no corresponding close bracket"))
1184 (let ((posn (position close-or-semi remaining)))
1185 (segments (subseq remaining 0 posn))
1186 (setf remaining (nthcdr (1+ posn) remaining)))
1187 (when (char= (format-directive-character close-or-semi)
1189 (setf close close-or-semi)
1192 (setf first-semi close-or-semi))))
1193 (values (segments) first-semi close remaining))))
1195 (sb!xc:defmacro expander-pprint-next-arg (string offset)
1198 (error 'format-error
1199 :complaint "no more arguments"
1200 :control-string ,string
1205 (defun expand-format-logical-block (prefix per-line-p insides suffix atsignp)
1206 `(let ((arg ,(if atsignp 'args (expand-next-arg))))
1208 (setf *only-simple-args* nil)
1210 (pprint-logical-block
1212 ,(if per-line-p :per-line-prefix :prefix) ,prefix
1216 `((orig-args arg))))
1217 (declare (ignorable args ,@(unless atsignp '(orig-args))))
1219 ,@(let ((*expander-next-arg-macro* 'expander-pprint-next-arg)
1220 (*only-simple-args* nil)
1221 (*orig-args-available*
1222 (if atsignp *orig-args-available* t)))
1223 (expand-directive-list insides)))))))
1225 (defun expand-format-justification (segments colonp atsignp first-semi params)
1226 (let ((newline-segment-p
1228 (format-directive-colonp first-semi))))
1229 (expand-bind-defaults
1230 ((mincol 0) (colinc 1) (minpad 0) (padchar #\space))
1232 `(let ((segments nil)
1233 ,@(when newline-segment-p
1234 '((newline-segment nil)
1238 ,@(when newline-segment-p
1239 `((setf newline-segment
1240 (with-output-to-string (stream)
1241 ,@(expand-directive-list (pop segments))))
1242 ,(expand-bind-defaults
1244 (line-len '(or (sb!impl::line-length stream) 72)))
1245 (format-directive-params first-semi)
1246 `(setf extra-space ,extra line-len ,line-len))))
1247 ,@(mapcar (lambda (segment)
1248 `(push (with-output-to-string (stream)
1249 ,@(expand-directive-list segment))
1252 (format-justification stream
1253 ,@(if newline-segment-p
1254 '(newline-segment extra-space line-len)
1256 segments ,colonp ,atsignp
1257 ,mincol ,colinc ,minpad ,padchar)))))
1259 ;;;; format directive and support function for user-defined method
1261 (def-format-directive #\/ (string start end colonp atsignp params)
1262 (let ((symbol (extract-user-fun-name string start end)))
1263 (collect ((param-names) (bindings))
1264 (dolist (param-and-offset params)
1265 (let ((param (cdr param-and-offset)))
1266 (let ((param-name (sb!xc:gensym "PARAM")))
1267 (param-names param-name)
1268 (bindings `(,param-name
1270 (:arg (expand-next-arg))
1271 (:remaining '(length args))
1274 (,symbol stream ,(expand-next-arg) ,colonp ,atsignp
1275 ,@(param-names))))))
1277 (defun extract-user-fun-name (string start end)
1278 (let ((slash (position #\/ string :start start :end (1- end)
1281 (error 'format-error
1282 :complaint "malformed ~~/ directive"))
1283 (let* ((name (string-upcase (let ((foo string))
1284 ;; Hack alert: This is to keep the compiler
1285 ;; quiet about deleting code inside the
1286 ;; subseq expansion.
1287 (subseq foo (1+ slash) (1- end)))))
1288 (first-colon (position #\: name))
1289 (second-colon (if first-colon (position #\: name :start (1+ first-colon))))
1290 (package-name (if first-colon
1291 (subseq name 0 first-colon)
1292 "COMMON-LISP-USER"))
1293 (package (find-package package-name)))
1295 ;; FIXME: should be PACKAGE-ERROR? Could we just use
1296 ;; FIND-UNDELETED-PACKAGE-OR-LOSE?
1297 (error 'format-error
1298 :complaint "no package named ~S"
1299 :args (list package-name)))
1301 ((and second-colon (= second-colon (1+ first-colon)))
1302 (subseq name (1+ second-colon)))
1304 (subseq name (1+ first-colon)))
1308 ;;; compile-time checking for argument mismatch. This code is
1309 ;;; inspired by that of Gerd Moellmann, and comes decorated with
1311 (defun %compiler-walk-format-string (string args)
1312 (declare (type simple-string string))
1313 (let ((*default-format-error-control-string* string))
1314 (macrolet ((incf-both (&optional (increment 1))
1316 (incf min ,increment)
1317 (incf max ,increment)))
1318 (walk-complex-directive (function)
1319 `(multiple-value-bind (min-inc max-inc remaining)
1320 (,function directive directives args)
1323 (setq directives remaining))))
1324 ;; FIXME: these functions take a list of arguments as well as
1325 ;; the directive stream. This is to enable possibly some
1326 ;; limited type checking on FORMAT's arguments, as well as
1327 ;; simple argument count mismatch checking: when the minimum and
1328 ;; maximum argument counts are the same at a given point, we
1329 ;; know which argument is going to be used for a given
1330 ;; directive, and some (annotated below) require arguments of
1331 ;; particular types.
1333 ((walk-justification (justification directives args)
1334 (declare (ignore args))
1335 (let ((*default-format-error-offset*
1336 (1- (format-directive-end justification))))
1337 (multiple-value-bind (segments first-semi close remaining)
1338 (parse-format-justification directives)
1339 (declare (ignore segments first-semi))
1341 ((not (format-directive-colonp close))
1342 (values 0 0 directives))
1343 ((format-directive-atsignp justification)
1344 (values 0 sb!xc:call-arguments-limit directives))
1345 ;; FIXME: here we could assert that the
1346 ;; corresponding argument was a list.
1347 (t (values 1 1 remaining))))))
1348 (walk-conditional (conditional directives args)
1349 (let ((*default-format-error-offset*
1350 (1- (format-directive-end conditional))))
1351 (multiple-value-bind (sublists last-semi-with-colon-p remaining)
1352 (parse-conditional-directive directives)
1353 (declare (ignore last-semi-with-colon-p))
1355 (loop for s in sublists
1357 1 (walk-directive-list s args)))))
1359 ((format-directive-atsignp conditional)
1360 (values 1 (max 1 sub-max) remaining))
1361 ((loop for p in (format-directive-params conditional)
1362 thereis (or (integerp (cdr p))
1363 (memq (cdr p) '(:remaining :arg))))
1364 (values 0 sub-max remaining))
1365 ;; FIXME: if not COLONP, then the next argument
1366 ;; must be a number.
1367 (t (values 1 (1+ sub-max) remaining)))))))
1368 (walk-iteration (iteration directives args)
1369 (declare (ignore args))
1370 (let ((*default-format-error-offset*
1371 (1- (format-directive-end iteration))))
1372 (let* ((close (find-directive directives #\} nil))
1373 (posn (or (position close directives)
1374 (error 'format-error
1375 :complaint "no corresponding close brace")))
1376 (remaining (nthcdr (1+ posn) directives)))
1377 ;; FIXME: if POSN is zero, the next argument must be
1378 ;; a format control (either a function or a string).
1379 (if (format-directive-atsignp iteration)
1380 (values (if (zerop posn) 1 0)
1381 sb!xc:call-arguments-limit
1383 ;; FIXME: the argument corresponding to this
1384 ;; directive must be a list.
1385 (let ((nreq (if (zerop posn) 2 1)))
1386 (values nreq nreq remaining))))))
1387 (walk-directive-list (directives args)
1388 (let ((min 0) (max 0))
1390 (let ((directive (pop directives)))
1391 (when (null directive)
1392 (return (values min (min max sb!xc:call-arguments-limit))))
1393 (when (format-directive-p directive)
1394 (incf-both (count :arg (format-directive-params directive)
1396 (let ((c (format-directive-character directive)))
1398 ((find c "ABCDEFGORSWX$/")
1401 (unless (format-directive-colonp directive)
1403 ((or (find c "IT%&|_();>~") (char= c #\Newline)))
1404 ;; FIXME: check correspondence of ~( and ~)
1406 (walk-complex-directive walk-justification))
1408 (walk-complex-directive walk-conditional))
1410 (walk-complex-directive walk-iteration))
1412 ;; FIXME: the argument corresponding to this
1413 ;; directive must be a format control.
1415 ((format-directive-atsignp directive)
1417 (setq max sb!xc:call-arguments-limit))
1419 (t (throw 'give-up-format-string-walk nil))))))))))
1420 (catch 'give-up-format-string-walk
1421 (let ((directives (tokenize-control-string string)))
1422 (walk-directive-list directives args)))))))