1.0.6.30: clean up FD-SOUT a little bit
[sbcl.git] / src / code / late-format.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
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.
9
10 (in-package "SB!FORMAT")
11 \f
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
23                  :initform t))
24   (:report %print-format-error)
25   (:default-initargs :references nil))
26
27 (defun %print-format-error (condition stream)
28   (format stream
29           "~:[~*~;error in ~S: ~]~?~@[~%  ~A~%  ~V@T^~@[~V@T^~]~]"
30           (format-error-print-banner condition)
31           'format
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)))
37 \f
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)
49                   stream
50                   :start (format-directive-start x)
51                   :end (format-directive-end x))))
52 \f
53 ;;;; TOKENIZE-CONTROL-STRING
54
55 (defun tokenize-control-string (string)
56   (declare (simple-string string))
57   (let ((index 0)
58         (end (length string))
59         (result nil)
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.
63         (block)
64         (pprint)
65         (semicolon)
66         (justification-semicolon))
67     (loop
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)
72           (return))
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
76           (cond
77             ((char= char #\<) (push directive block))
78             ((and block (char= char #\;) (format-directive-colonp directive))
79              (setf semicolon directive))
80             ((char= char #\>)
81              (unless block
82                (error 'format-error
83                       :complaint "~~> without a matching ~~<"
84                       :control-string string
85                       :offset next-directive))
86              (cond
87                ((format-directive-colonp directive)
88                 (unless pprint
89                   (setf pprint (car block)))
90                 (setf semicolon nil))
91                (semicolon
92                 (unless justification-semicolon
93                   (setf justification-semicolon semicolon))))
94              (pop block))
95             ;; block cases are handled by the #\< expander/interpreter
96             ((not block)
97              (case char
98                ((#\W #\I #\_) (unless pprint (setf pprint directive)))
99                (#\T (when (and (format-directive-colonp directive)
100                                (not pprint))
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))))
108         (error 'format-error
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)
114                                    1)
115                :references (list '(:ansi-cl :section (22 3 5 2))))))
116     (nreverse result)))
117
118 (defun parse-directive (string start)
119   (let ((posn (1+ start)) (params nil) (colonp nil) (atsignp nil)
120         (end (length string)))
121     (flet ((get-char ()
122              (if (= posn end)
123                  (error 'format-error
124                         :complaint "string ended before directive was found"
125                         :control-string string
126                         :offset start)
127                  (schar string posn)))
128            (check-ordering ()
129              (when (or colonp atsignp)
130                (error 'format-error
131                       :complaint "parameters found after #\\: or #\\@ modifier"
132                       :control-string string
133                       :offset posn
134                       :references (list '(:ansi-cl :section (22 3)))))))
135       (loop
136         (let ((char (get-char)))
137           (cond ((or (char<= #\0 char #\9) (char= char #\+) (char= char #\-))
138                  (check-ordering)
139                  (multiple-value-bind (param new-posn)
140                      (parse-integer string :start posn :junk-allowed t)
141                    (push (cons posn param) params)
142                    (setf posn new-posn)
143                    (case (get-char)
144                      (#\,)
145                      ((#\: #\@)
146                       (decf posn))
147                      (t
148                       (return)))))
149                 ((or (char= char #\v)
150                      (char= char #\V))
151                  (check-ordering)
152                  (push (cons posn :arg) params)
153                  (incf posn)
154                  (case (get-char)
155                    (#\,)
156                    ((#\: #\@)
157                     (decf posn))
158                    (t
159                     (return))))
160                 ((char= char #\#)
161                  (check-ordering)
162                  (push (cons posn :remaining) params)
163                  (incf posn)
164                  (case (get-char)
165                    (#\,)
166                    ((#\: #\@)
167                     (decf posn))
168                    (t
169                     (return))))
170                 ((char= char #\')
171                  (check-ordering)
172                  (incf posn)
173                  (push (cons posn (get-char)) params)
174                  (incf posn)
175                  (unless (char= (get-char) #\,)
176                    (decf posn)))
177                 ((char= char #\,)
178                  (check-ordering)
179                  (push (cons posn nil) params))
180                 ((char= char #\:)
181                  (if colonp
182                      (error 'format-error
183                             :complaint "too many colons supplied"
184                             :control-string string
185                             :offset posn
186                             :references (list '(:ansi-cl :section (22 3))))
187                      (setf colonp t)))
188                 ((char= char #\@)
189                  (if atsignp
190                      (error 'format-error
191                             :complaint "too many #\\@ characters supplied"
192                             :control-string string
193                             :offset posn
194                             :references (list '(:ansi-cl :section (22 3))))
195                      (setf atsignp t)))
196                 (t
197                  (when (and (char= (schar string (1- posn)) #\,)
198                             (or (< posn 2)
199                                 (char/= (schar string (- posn 2)) #\')))
200                    (check-ordering)
201                    (push (cons (1- posn) nil) params))
202                  (return))))
203         (incf posn))
204       (let ((char (get-char)))
205         (when (char= char #\/)
206           (let ((closing-slash (position #\/ string :start (1+ posn))))
207             (if closing-slash
208                 (setf posn closing-slash)
209                 (error 'format-error
210                        :complaint "no matching closing slash"
211                        :control-string string
212                        :offset posn))))
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))))))
218 \f
219 ;;;; FORMATTER stuff
220
221 (sb!xc:defmacro formatter (control-string)
222   `#',(%formatter control-string))
223
224 (defun %formatter (control-string)
225   (block nil
226     (catch 'need-orig-args
227       (let* ((*simple-args* nil)
228              (*only-simple-args* t)
229              (guts (expand-control-string control-string))
230              (args nil))
231         (dolist (arg *simple-args*)
232           (push `(,(car arg)
233                   (error
234                    'format-error
235                    :complaint "required argument missing"
236                    :control-string ,control-string
237                    :offset ,(cdr arg)))
238                 args))
239         (return `(lambda (stream &optional ,@args &rest args)
240                    (declare (ignorable stream))
241                    ,guts
242                    args))))
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)
249            args)))))
250
251 (defun expand-control-string (string)
252   (let* ((string (etypecase string
253                    (simple-string
254                     string)
255                    (string
256                     (coerce string 'simple-string))))
257          (*default-format-error-control-string* string)
258          (directives (tokenize-control-string string)))
259     `(block nil
260        ,@(expand-directive-list directives))))
261
262 (defun expand-directive-list (directives)
263   (let ((results nil)
264         (remaining-directives directives))
265     (loop
266       (unless remaining-directives
267         (return))
268       (multiple-value-bind (form new-directives)
269           (expand-directive (car remaining-directives)
270                             (cdr remaining-directives))
271         (push form results)
272         (setf remaining-directives new-directives)))
273     (reverse results)))
274
275 (defun expand-directive (directive more-directives)
276   (etypecase directive
277     (format-directive
278      (let ((expander
279             (let ((char (format-directive-character directive)))
280               (typecase char
281                 (base-char
282                  (aref *format-directive-expanders* (char-code char)))
283                 (character nil))))
284            (*default-format-error-offset*
285             (1- (format-directive-end directive))))
286        (declare (type (or null function) expander))
287        (if expander
288            (funcall expander directive more-directives)
289            (error 'format-error
290                   :complaint "unknown directive ~@[(character: ~A)~]"
291                   :args (list (char-name (format-directive-character directive)))))))
292     (simple-string
293      (values `(write-string ,directive stream)
294              more-directives))))
295
296 (defmacro-mundanely expander-next-arg (string offset)
297   `(if args
298        (pop args)
299        (error 'format-error
300               :complaint "no more arguments"
301               :control-string ,string
302               :offset ,offset)))
303
304 (defun expand-next-arg (&optional offset)
305   (if (or *orig-args-available* (not *only-simple-args*))
306       `(,*expander-next-arg-macro*
307         ,*default-format-error-control-string*
308         ,(or offset *default-format-error-offset*))
309       (let ((symbol (gensym "FORMAT-ARG-")))
310         (push (cons symbol (or offset *default-format-error-offset*))
311               *simple-args*)
312         symbol)))
313
314 (defmacro expand-bind-defaults (specs params &body body)
315   (once-only ((params params))
316     (if specs
317         (collect ((expander-bindings) (runtime-bindings))
318                  (dolist (spec specs)
319                    (destructuring-bind (var default) spec
320                      (let ((symbol (gensym)))
321                        (expander-bindings
322                         `(,var ',symbol))
323                        (runtime-bindings
324                         `(list ',symbol
325                                (let* ((param-and-offset (pop ,params))
326                                       (offset (car param-and-offset))
327                                       (param (cdr param-and-offset)))
328                                  (case param
329                                    (:arg `(or ,(expand-next-arg offset)
330                                               ,,default))
331                                    (:remaining
332                                     (setf *only-simple-args* nil)
333                                     '(length args))
334                                    ((nil) ,default)
335                                    (t param))))))))
336                  `(let ,(expander-bindings)
337                     `(let ,(list ,@(runtime-bindings))
338                        ,@(if ,params
339                              (error
340                               'format-error
341                               :complaint
342                               "too many parameters, expected no more than ~W"
343                               :args (list ,(length specs))
344                               :offset (caar ,params)))
345                        ,,@body)))
346         `(progn
347            (when ,params
348              (error 'format-error
349                     :complaint "too many parameters, expected none"
350                     :offset (caar ,params)))
351            ,@body))))
352 \f
353 ;;;; format directive machinery
354
355 ;;; FIXME: only used in this file, could be SB!XC:DEFMACRO in EVAL-WHEN
356 (defmacro def-complex-format-directive (char lambda-list &body body)
357   (let ((defun-name (intern (format nil
358                                     "~:@(~:C~)-FORMAT-DIRECTIVE-EXPANDER"
359                                     char)))
360         (directive (gensym))
361         (directives (if lambda-list (car (last lambda-list)) (gensym))))
362     `(progn
363        (defun ,defun-name (,directive ,directives)
364          ,@(if lambda-list
365                `((let ,(mapcar (lambda (var)
366                                  `(,var
367                                    (,(symbolicate "FORMAT-DIRECTIVE-" var)
368                                     ,directive)))
369                                (butlast lambda-list))
370                    ,@body))
371                `((declare (ignore ,directive ,directives))
372                  ,@body)))
373        (%set-format-directive-expander ,char #',defun-name))))
374
375 ;;; FIXME: only used in this file, could be SB!XC:DEFMACRO in EVAL-WHEN
376 (defmacro def-format-directive (char lambda-list &body body)
377   (let ((directives (gensym))
378         (declarations nil)
379         (body-without-decls body))
380     (loop
381       (let ((form (car body-without-decls)))
382         (unless (and (consp form) (eq (car form) 'declare))
383           (return))
384         (push (pop body-without-decls) declarations)))
385     (setf declarations (reverse declarations))
386     `(def-complex-format-directive ,char (,@lambda-list ,directives)
387        ,@declarations
388        (values (progn ,@body-without-decls)
389                ,directives))))
390
391 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
392
393 (defun %set-format-directive-expander (char fn)
394   (setf (aref *format-directive-expanders* (char-code (char-upcase char))) fn)
395   char)
396
397 (defun %set-format-directive-interpreter (char fn)
398   (setf (aref *format-directive-interpreters*
399               (char-code (char-upcase char)))
400         fn)
401   char)
402
403 (defun find-directive (directives kind stop-at-semi)
404   (if directives
405       (let ((next (car directives)))
406         (if (format-directive-p next)
407             (let ((char (format-directive-character next)))
408               (if (or (char= kind char)
409                       (and stop-at-semi (char= char #\;)))
410                   (car directives)
411                   (find-directive
412                    (cdr (flet ((after (char)
413                                  (member (find-directive (cdr directives)
414                                                          char
415                                                          nil)
416                                          directives)))
417                           (case char
418                             (#\( (after #\)))
419                             (#\< (after #\>))
420                             (#\[ (after #\]))
421                             (#\{ (after #\}))
422                             (t directives))))
423                    kind stop-at-semi)))
424             (find-directive (cdr directives) kind stop-at-semi)))))
425
426 ) ; EVAL-WHEN
427 \f
428 ;;;; format directives for simple output
429
430 (def-format-directive #\A (colonp atsignp params)
431   (if params
432       (expand-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
433                              (padchar #\space))
434                      params
435         `(format-princ stream ,(expand-next-arg) ',colonp ',atsignp
436                        ,mincol ,colinc ,minpad ,padchar))
437       `(princ ,(if colonp
438                    `(or ,(expand-next-arg) "()")
439                    (expand-next-arg))
440               stream)))
441
442 (def-format-directive #\S (colonp atsignp params)
443   (cond (params
444          (expand-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
445                                 (padchar #\space))
446                         params
447            `(format-prin1 stream ,(expand-next-arg) ,colonp ,atsignp
448                           ,mincol ,colinc ,minpad ,padchar)))
449         (colonp
450          `(let ((arg ,(expand-next-arg)))
451             (if arg
452                 (prin1 arg stream)
453                 (princ "()" stream))))
454         (t
455          `(prin1 ,(expand-next-arg) stream))))
456
457 (def-format-directive #\C (colonp atsignp params)
458   (expand-bind-defaults () params
459     (if colonp
460         `(format-print-named-character ,(expand-next-arg) stream)
461         (if atsignp
462             `(prin1 ,(expand-next-arg) stream)
463             `(write-char ,(expand-next-arg) stream)))))
464
465 (def-format-directive #\W (colonp atsignp params)
466   (expand-bind-defaults () params
467     (if (or colonp atsignp)
468         `(let (,@(when colonp
469                    '((*print-pretty* t)))
470                ,@(when atsignp
471                    '((*print-level* nil)
472                      (*print-length* nil))))
473            (output-object ,(expand-next-arg) stream))
474         `(output-object ,(expand-next-arg) stream))))
475 \f
476 ;;;; format directives for integer output
477
478 (defun expand-format-integer (base colonp atsignp params)
479   (if (or colonp atsignp params)
480       (expand-bind-defaults
481           ((mincol 0) (padchar #\space) (commachar #\,) (commainterval 3))
482           params
483         `(format-print-integer stream ,(expand-next-arg) ,colonp ,atsignp
484                                ,base ,mincol ,padchar ,commachar
485                                ,commainterval))
486       `(write ,(expand-next-arg) :stream stream :base ,base :radix nil
487               :escape nil)))
488
489 (def-format-directive #\D (colonp atsignp params)
490   (expand-format-integer 10 colonp atsignp params))
491
492 (def-format-directive #\B (colonp atsignp params)
493   (expand-format-integer 2 colonp atsignp params))
494
495 (def-format-directive #\O (colonp atsignp params)
496   (expand-format-integer 8 colonp atsignp params))
497
498 (def-format-directive #\X (colonp atsignp params)
499   (expand-format-integer 16 colonp atsignp params))
500
501 (def-format-directive #\R (colonp atsignp params)
502   (expand-bind-defaults
503       ((base nil) (mincol 0) (padchar #\space) (commachar #\,)
504        (commainterval 3))
505       params
506     (let ((n-arg (gensym)))
507       `(let ((,n-arg ,(expand-next-arg)))
508          (if ,base
509              (format-print-integer stream ,n-arg ,colonp ,atsignp
510                                    ,base ,mincol
511                                    ,padchar ,commachar ,commainterval)
512              ,(if atsignp
513                   (if colonp
514                       `(format-print-old-roman stream ,n-arg)
515                       `(format-print-roman stream ,n-arg))
516                   (if colonp
517                       `(format-print-ordinal stream ,n-arg)
518                       `(format-print-cardinal stream ,n-arg))))))))
519 \f
520 ;;;; format directive for pluralization
521
522 (def-format-directive #\P (colonp atsignp params end)
523   (expand-bind-defaults () params
524     (let ((arg (cond
525                 ((not colonp)
526                  (expand-next-arg))
527                 (*orig-args-available*
528                  `(if (eq orig-args args)
529                       (error 'format-error
530                              :complaint "no previous argument"
531                              :offset ,(1- end))
532                       (do ((arg-ptr orig-args (cdr arg-ptr)))
533                           ((eq (cdr arg-ptr) args)
534                            (car arg-ptr)))))
535                 (*only-simple-args*
536                  (unless *simple-args*
537                    (error 'format-error
538                           :complaint "no previous argument"))
539                  (caar *simple-args*))
540                 (t
541                  (/show0 "THROWing NEED-ORIG-ARGS from tilde-P")
542                  (throw 'need-orig-args nil)))))
543       (if atsignp
544           `(write-string (if (eql ,arg 1) "y" "ies") stream)
545           `(unless (eql ,arg 1) (write-char #\s stream))))))
546 \f
547 ;;;; format directives for floating point output
548
549 (def-format-directive #\F (colonp atsignp params)
550   (when colonp
551     (error 'format-error
552            :complaint
553            "The colon modifier cannot be used with this directive."))
554   (expand-bind-defaults ((w nil) (d nil) (k nil) (ovf nil) (pad #\space)) params
555     `(format-fixed stream ,(expand-next-arg) ,w ,d ,k ,ovf ,pad ,atsignp)))
556
557 (def-format-directive #\E (colonp atsignp params)
558   (when colonp
559     (error 'format-error
560            :complaint
561            "The colon modifier cannot be used with this directive."))
562   (expand-bind-defaults
563       ((w nil) (d nil) (e nil) (k 1) (ovf nil) (pad #\space) (mark nil))
564       params
565     `(format-exponential stream ,(expand-next-arg) ,w ,d ,e ,k ,ovf ,pad ,mark
566                          ,atsignp)))
567
568 (def-format-directive #\G (colonp atsignp params)
569   (when colonp
570     (error 'format-error
571            :complaint
572            "The colon modifier cannot be used with this directive."))
573   (expand-bind-defaults
574       ((w nil) (d nil) (e nil) (k nil) (ovf nil) (pad #\space) (mark nil))
575       params
576     `(format-general stream ,(expand-next-arg) ,w ,d ,e ,k ,ovf ,pad ,mark ,atsignp)))
577
578 (def-format-directive #\$ (colonp atsignp params)
579   (expand-bind-defaults ((d 2) (n 1) (w 0) (pad #\space)) params
580     `(format-dollars stream ,(expand-next-arg) ,d ,n ,w ,pad ,colonp
581                      ,atsignp)))
582 \f
583 ;;;; format directives for line/page breaks etc.
584
585 (def-format-directive #\% (colonp atsignp params)
586   (when (or colonp atsignp)
587     (error 'format-error
588            :complaint
589            "The colon and atsign modifiers cannot be used with this directive."
590            ))
591   (if params
592       (expand-bind-defaults ((count 1)) params
593         `(dotimes (i ,count)
594            (terpri stream)))
595       '(terpri stream)))
596
597 (def-format-directive #\& (colonp atsignp params)
598   (when (or colonp atsignp)
599     (error 'format-error
600            :complaint
601            "The colon and atsign modifiers cannot be used with this directive."
602            ))
603   (if params
604       (expand-bind-defaults ((count 1)) params
605         `(progn
606            (fresh-line stream)
607            (dotimes (i (1- ,count))
608              (terpri stream))))
609       '(fresh-line stream)))
610
611 (def-format-directive #\| (colonp atsignp params)
612   (when (or colonp atsignp)
613     (error 'format-error
614            :complaint
615            "The colon and atsign modifiers cannot be used with this directive."
616            ))
617   (if params
618       (expand-bind-defaults ((count 1)) params
619         `(dotimes (i ,count)
620            (write-char (code-char form-feed-char-code) stream)))
621       '(write-char (code-char form-feed-char-code) stream)))
622
623 (def-format-directive #\~ (colonp atsignp params)
624   (when (or colonp atsignp)
625     (error 'format-error
626            :complaint
627            "The colon and atsign modifiers cannot be used with this directive."
628            ))
629   (if params
630       (expand-bind-defaults ((count 1)) params
631         `(dotimes (i ,count)
632            (write-char #\~ stream)))
633       '(write-char #\~ stream)))
634
635 (def-complex-format-directive #\newline (colonp atsignp params directives)
636   (when (and colonp atsignp)
637     (error 'format-error
638            :complaint "both colon and atsign modifiers used simultaneously"))
639   (values (expand-bind-defaults () params
640             (if atsignp
641                 '(write-char #\newline stream)
642                 nil))
643           (if (and (not colonp)
644                    directives
645                    (simple-string-p (car directives)))
646               (cons (string-left-trim *format-whitespace-chars*
647                                       (car directives))
648                     (cdr directives))
649               directives)))
650 \f
651 ;;;; format directives for tabs and simple pretty printing
652
653 (def-format-directive #\T (colonp atsignp params)
654   (if colonp
655       (expand-bind-defaults ((n 1) (m 1)) params
656         `(pprint-tab ,(if atsignp :section-relative :section)
657                      ,n ,m stream))
658       (if atsignp
659           (expand-bind-defaults ((colrel 1) (colinc 1)) params
660             `(format-relative-tab stream ,colrel ,colinc))
661           (expand-bind-defaults ((colnum 1) (colinc 1)) params
662             `(format-absolute-tab stream ,colnum ,colinc)))))
663
664 (def-format-directive #\_ (colonp atsignp params)
665   (expand-bind-defaults () params
666     `(pprint-newline ,(if colonp
667                           (if atsignp
668                               :mandatory
669                               :fill)
670                           (if atsignp
671                               :miser
672                               :linear))
673                      stream)))
674
675 (def-format-directive #\I (colonp atsignp params)
676   (when atsignp
677     (error 'format-error
678            :complaint
679            "cannot use the at-sign modifier with this directive"))
680   (expand-bind-defaults ((n 0)) params
681     `(pprint-indent ,(if colonp :current :block) ,n stream)))
682 \f
683 ;;;; format directive for ~*
684
685 (def-format-directive #\* (colonp atsignp params end)
686   (if atsignp
687       (if colonp
688           (error 'format-error
689                  :complaint
690                  "both colon and atsign modifiers used simultaneously")
691           (expand-bind-defaults ((posn 0)) params
692             (unless *orig-args-available*
693               (/show0 "THROWing NEED-ORIG-ARGS from tilde-@*")
694               (throw 'need-orig-args nil))
695             `(if (<= 0 ,posn (length orig-args))
696                  (setf args (nthcdr ,posn orig-args))
697                  (error 'format-error
698                         :complaint "Index ~W out of bounds. Should have been ~
699                                     between 0 and ~W."
700                         :args (list ,posn (length orig-args))
701                         :offset ,(1- end)))))
702       (if colonp
703           (expand-bind-defaults ((n 1)) params
704             (unless *orig-args-available*
705               (/show0 "THROWing NEED-ORIG-ARGS from tilde-:*")
706               (throw 'need-orig-args nil))
707             `(do ((cur-posn 0 (1+ cur-posn))
708                   (arg-ptr orig-args (cdr arg-ptr)))
709                  ((eq arg-ptr args)
710                   (let ((new-posn (- cur-posn ,n)))
711                     (if (<= 0 new-posn (length orig-args))
712                         (setf args (nthcdr new-posn orig-args))
713                         (error 'format-error
714                                :complaint
715                                "Index ~W is out of bounds; should have been ~
716                                 between 0 and ~W."
717                                :args (list new-posn (length orig-args))
718                                :offset ,(1- end)))))))
719           (if params
720               (expand-bind-defaults ((n 1)) params
721                 (setf *only-simple-args* nil)
722                 `(dotimes (i ,n)
723                    ,(expand-next-arg)))
724               (expand-next-arg)))))
725 \f
726 ;;;; format directive for indirection
727
728 (def-format-directive #\? (colonp atsignp params string end)
729   (when colonp
730     (error 'format-error
731            :complaint "cannot use the colon modifier with this directive"))
732   (expand-bind-defaults () params
733     `(handler-bind
734          ((format-error
735            (lambda (condition)
736              (error 'format-error
737                     :complaint
738                     "~A~%while processing indirect format string:"
739                     :args (list condition)
740                     :print-banner nil
741                     :control-string ,string
742                     :offset ,(1- end)))))
743        ,(if atsignp
744             (if *orig-args-available*
745                 `(setf args (%format stream ,(expand-next-arg) orig-args args))
746                 (throw 'need-orig-args nil))
747             `(%format stream ,(expand-next-arg) ,(expand-next-arg))))))
748 \f
749 ;;;; format directives for capitalization
750
751 (def-complex-format-directive #\( (colonp atsignp params directives)
752   (let ((close (find-directive directives #\) nil)))
753     (unless close
754       (error 'format-error
755              :complaint "no corresponding close parenthesis"))
756     (let* ((posn (position close directives))
757            (before (subseq directives 0 posn))
758            (after (nthcdr (1+ posn) directives)))
759       (values
760        (expand-bind-defaults () params
761          `(let ((stream (make-case-frob-stream stream
762                                                ,(if colonp
763                                                     (if atsignp
764                                                         :upcase
765                                                         :capitalize)
766                                                     (if atsignp
767                                                         :capitalize-first
768                                                         :downcase)))))
769             ,@(expand-directive-list before)))
770        after))))
771
772 (def-complex-format-directive #\) ()
773   (error 'format-error
774          :complaint "no corresponding open parenthesis"))
775 \f
776 ;;;; format directives and support functions for conditionalization
777
778 (def-complex-format-directive #\[ (colonp atsignp params directives)
779   (multiple-value-bind (sublists last-semi-with-colon-p remaining)
780       (parse-conditional-directive directives)
781     (values
782      (if atsignp
783          (if colonp
784              (error 'format-error
785                     :complaint
786                     "both colon and atsign modifiers used simultaneously")
787              (if (cdr sublists)
788                  (error 'format-error
789                         :complaint
790                         "Can only specify one section")
791                  (expand-bind-defaults () params
792                    (expand-maybe-conditional (car sublists)))))
793          (if colonp
794              (if (= (length sublists) 2)
795                  (expand-bind-defaults () params
796                    (expand-true-false-conditional (car sublists)
797                                                   (cadr sublists)))
798                  (error 'format-error
799                         :complaint
800                         "must specify exactly two sections"))
801              (expand-bind-defaults ((index nil)) params
802                (setf *only-simple-args* nil)
803                (let ((clauses nil)
804                      (case `(or ,index ,(expand-next-arg))))
805                  (when last-semi-with-colon-p
806                    (push `(t ,@(expand-directive-list (pop sublists)))
807                          clauses))
808                  (let ((count (length sublists)))
809                    (dolist (sublist sublists)
810                      (push `(,(decf count)
811                              ,@(expand-directive-list sublist))
812                            clauses)))
813                  `(case ,case ,@clauses)))))
814      remaining)))
815
816 (defun parse-conditional-directive (directives)
817   (let ((sublists nil)
818         (last-semi-with-colon-p nil)
819         (remaining directives))
820     (loop
821       (let ((close-or-semi (find-directive remaining #\] t)))
822         (unless close-or-semi
823           (error 'format-error
824                  :complaint "no corresponding close bracket"))
825         (let ((posn (position close-or-semi remaining)))
826           (push (subseq remaining 0 posn) sublists)
827           (setf remaining (nthcdr (1+ posn) remaining))
828           (when (char= (format-directive-character close-or-semi) #\])
829             (return))
830           (setf last-semi-with-colon-p
831                 (format-directive-colonp close-or-semi)))))
832     (values sublists last-semi-with-colon-p remaining)))
833
834 (defun expand-maybe-conditional (sublist)
835   (flet ((hairy ()
836            `(let ((prev-args args)
837                   (arg ,(expand-next-arg)))
838               (when arg
839                 (setf args prev-args)
840                 ,@(expand-directive-list sublist)))))
841     (if *only-simple-args*
842         (multiple-value-bind (guts new-args)
843             (let ((*simple-args* *simple-args*))
844               (values (expand-directive-list sublist)
845                       *simple-args*))
846           (cond ((and new-args (eq *simple-args* (cdr new-args)))
847                  (setf *simple-args* new-args)
848                  `(when ,(caar new-args)
849                     ,@guts))
850                 (t
851                  (setf *only-simple-args* nil)
852                  (hairy))))
853         (hairy))))
854
855 (defun expand-true-false-conditional (true false)
856   (let ((arg (expand-next-arg)))
857     (flet ((hairy ()
858              `(if ,arg
859                   (progn
860                     ,@(expand-directive-list true))
861                   (progn
862                     ,@(expand-directive-list false)))))
863       (if *only-simple-args*
864           (multiple-value-bind (true-guts true-args true-simple)
865               (let ((*simple-args* *simple-args*)
866                     (*only-simple-args* t))
867                 (values (expand-directive-list true)
868                         *simple-args*
869                         *only-simple-args*))
870             (multiple-value-bind (false-guts false-args false-simple)
871                 (let ((*simple-args* *simple-args*)
872                       (*only-simple-args* t))
873                   (values (expand-directive-list false)
874                           *simple-args*
875                           *only-simple-args*))
876               (if (= (length true-args) (length false-args))
877                   `(if ,arg
878                        (progn
879                          ,@true-guts)
880                        ,(do ((false false-args (cdr false))
881                              (true true-args (cdr true))
882                              (bindings nil (cons `(,(caar false) ,(caar true))
883                                                  bindings)))
884                             ((eq true *simple-args*)
885                              (setf *simple-args* true-args)
886                              (setf *only-simple-args*
887                                    (and true-simple false-simple))
888                              (if bindings
889                                  `(let ,bindings
890                                     ,@false-guts)
891                                  `(progn
892                                     ,@false-guts)))))
893                   (progn
894                     (setf *only-simple-args* nil)
895                     (hairy)))))
896           (hairy)))))
897
898 (def-complex-format-directive #\; ()
899   (error 'format-error
900          :complaint
901          "~~; directive not contained within either ~~[...~~] or ~~<...~~>"))
902
903 (def-complex-format-directive #\] ()
904   (error 'format-error
905          :complaint
906          "no corresponding open bracket"))
907 \f
908 ;;;; format directive for up-and-out
909
910 (def-format-directive #\^ (colonp atsignp params)
911   (when atsignp
912     (error 'format-error
913            :complaint "cannot use the at-sign modifier with this directive"))
914   (when (and colonp (not *up-up-and-out-allowed*))
915     (error 'format-error
916            :complaint "attempt to use ~~:^ outside a ~~:{...~~} construct"))
917   `(when ,(expand-bind-defaults ((arg1 nil) (arg2 nil) (arg3 nil)) params
918             `(cond (,arg3 (<= ,arg1 ,arg2 ,arg3))
919                    (,arg2 (eql ,arg1 ,arg2))
920                    (,arg1 (eql ,arg1 0))
921                    (t ,(if colonp
922                            '(null outside-args)
923                            (progn
924                              (setf *only-simple-args* nil)
925                              '(null args))))))
926      ,(if colonp
927           '(return-from outside-loop nil)
928           '(return))))
929 \f
930 ;;;; format directives for iteration
931
932 (def-complex-format-directive #\{ (colonp atsignp params string end directives)
933   (let ((close (find-directive directives #\} nil)))
934     (unless close
935       (error 'format-error
936              :complaint "no corresponding close brace"))
937     (let* ((closed-with-colon (format-directive-colonp close))
938            (posn (position close directives)))
939       (labels
940           ((compute-insides ()
941              (if (zerop posn)
942                  (if *orig-args-available*
943                      `((handler-bind
944                            ((format-error
945                              (lambda (condition)
946                                (error 'format-error
947                                       :complaint
948                               "~A~%while processing indirect format string:"
949                                       :args (list condition)
950                                       :print-banner nil
951                                       :control-string ,string
952                                       :offset ,(1- end)))))
953                          (setf args
954                                (%format stream inside-string orig-args args))))
955                      (throw 'need-orig-args nil))
956                  (let ((*up-up-and-out-allowed* colonp))
957                    (expand-directive-list (subseq directives 0 posn)))))
958            (compute-loop (count)
959              (when atsignp
960                (setf *only-simple-args* nil))
961              `(loop
962                 ,@(unless closed-with-colon
963                     '((when (null args)
964                         (return))))
965                 ,@(when count
966                     `((when (and ,count (minusp (decf ,count)))
967                         (return))))
968                 ,@(if colonp
969                       (let ((*expander-next-arg-macro* 'expander-next-arg)
970                             (*only-simple-args* nil)
971                             (*orig-args-available* t))
972                         `((let* ((orig-args ,(expand-next-arg))
973                                  (outside-args args)
974                                  (args orig-args))
975                             (declare (ignorable orig-args outside-args args))
976                             (block nil
977                               ,@(compute-insides)))))
978                       (compute-insides))
979                 ,@(when closed-with-colon
980                     '((when (null args)
981                         (return))))))
982            (compute-block (count)
983              (if colonp
984                  `(block outside-loop
985                     ,(compute-loop count))
986                  (compute-loop count)))
987            (compute-bindings (count)
988              (if atsignp
989                  (compute-block count)
990                  `(let* ((orig-args ,(expand-next-arg))
991                          (args orig-args))
992                    (declare (ignorable orig-args args))
993                    ,(let ((*expander-next-arg-macro* 'expander-next-arg)
994                           (*only-simple-args* nil)
995                           (*orig-args-available* t))
996                       (compute-block count))))))
997         (values (if params
998                     (expand-bind-defaults ((count nil)) params
999                       (if (zerop posn)
1000                           `(let ((inside-string ,(expand-next-arg)))
1001                             ,(compute-bindings count))
1002                           (compute-bindings count)))
1003                     (if (zerop posn)
1004                         `(let ((inside-string ,(expand-next-arg)))
1005                           ,(compute-bindings nil))
1006                         (compute-bindings nil)))
1007                 (nthcdr (1+ posn) directives))))))
1008
1009 (def-complex-format-directive #\} ()
1010   (error 'format-error
1011          :complaint "no corresponding open brace"))
1012 \f
1013 ;;;; format directives and support functions for justification
1014
1015 (defparameter *illegal-inside-justification*
1016   (mapcar (lambda (x) (parse-directive x 0))
1017           '("~W" "~:W" "~@W" "~:@W"
1018             "~_" "~:_" "~@_" "~:@_"
1019             "~:>" "~:@>"
1020             "~I" "~:I" "~@I" "~:@I"
1021             "~:T" "~:@T")))
1022
1023 (defun illegal-inside-justification-p (directive)
1024   (member directive *illegal-inside-justification*
1025           :test (lambda (x y)
1026                   (and (format-directive-p x)
1027                        (format-directive-p y)
1028                        (eql (format-directive-character x) (format-directive-character y))
1029                        (eql (format-directive-colonp x) (format-directive-colonp y))
1030                        (eql (format-directive-atsignp x) (format-directive-atsignp y))))))
1031
1032 (def-complex-format-directive #\< (colonp atsignp params string end directives)
1033   (multiple-value-bind (segments first-semi close remaining)
1034       (parse-format-justification directives)
1035     (values
1036      (if (format-directive-colonp close)
1037          (multiple-value-bind (prefix per-line-p insides suffix)
1038              (parse-format-logical-block segments colonp first-semi
1039                                          close params string end)
1040            (expand-format-logical-block prefix per-line-p insides
1041                                         suffix atsignp))
1042          (let ((count (reduce #'+ (mapcar (lambda (x) (count-if #'illegal-inside-justification-p x)) segments))))
1043            (when (> count 0)
1044              ;; ANSI specifies that "an error is signalled" in this
1045              ;; situation.
1046              (error 'format-error
1047                     :complaint "~D illegal directive~:P found inside justification block"
1048                     :args (list count)
1049                     :references (list '(:ansi-cl :section (22 3 5 2)))))
1050            (expand-format-justification segments colonp atsignp
1051                                         first-semi params)))
1052      remaining)))
1053
1054 (def-complex-format-directive #\> ()
1055   (error 'format-error
1056          :complaint "no corresponding open bracket"))
1057
1058 (defun parse-format-logical-block
1059        (segments colonp first-semi close params string end)
1060   (when params
1061     (error 'format-error
1062            :complaint "No parameters can be supplied with ~~<...~~:>."
1063            :offset (caar params)))
1064   (multiple-value-bind (prefix insides suffix)
1065       (multiple-value-bind (prefix-default suffix-default)
1066           (if colonp (values "(" ")") (values "" ""))
1067         (flet ((extract-string (list prefix-p)
1068                  (let ((directive (find-if #'format-directive-p list)))
1069                    (if directive
1070                        (error 'format-error
1071                               :complaint
1072                               "cannot include format directives inside the ~
1073                                ~:[suffix~;prefix~] segment of ~~<...~~:>"
1074                               :args (list prefix-p)
1075                               :offset (1- (format-directive-end directive))
1076                               :references
1077                               (list '(:ansi-cl :section (22 3 5 2))))
1078                        (apply #'concatenate 'string list)))))
1079         (case (length segments)
1080           (0 (values prefix-default nil suffix-default))
1081           (1 (values prefix-default (car segments) suffix-default))
1082           (2 (values (extract-string (car segments) t)
1083                      (cadr segments) suffix-default))
1084           (3 (values (extract-string (car segments) t)
1085                      (cadr segments)
1086                      (extract-string (caddr segments) nil)))
1087           (t
1088            (error 'format-error
1089                   :complaint "too many segments for ~~<...~~:>")))))
1090     (when (format-directive-atsignp close)
1091       (setf insides
1092             (add-fill-style-newlines insides
1093                                      string
1094                                      (if first-semi
1095                                          (format-directive-end first-semi)
1096                                          end))))
1097     (values prefix
1098             (and first-semi (format-directive-atsignp first-semi))
1099             insides
1100             suffix)))
1101
1102 (defun add-fill-style-newlines (list string offset &optional last-directive)
1103   (cond
1104     (list
1105      (let ((directive (car list)))
1106        (cond
1107          ((simple-string-p directive)
1108           (let* ((non-space (position #\Space directive :test #'char/=))
1109                  (newlinep (and last-directive
1110                                 (char=
1111                                  (format-directive-character last-directive)
1112                                  #\Newline))))
1113             (cond
1114               ((and newlinep non-space)
1115                (nconc
1116                 (list (subseq directive 0 non-space))
1117                 (add-fill-style-newlines-aux
1118                  (subseq directive non-space) string (+ offset non-space))
1119                 (add-fill-style-newlines
1120                  (cdr list) string (+ offset (length directive)))))
1121               (newlinep
1122                (cons directive
1123                      (add-fill-style-newlines
1124                       (cdr list) string (+ offset (length directive)))))
1125               (t
1126                (nconc (add-fill-style-newlines-aux directive string offset)
1127                       (add-fill-style-newlines
1128                        (cdr list) string (+ offset (length directive))))))))
1129          (t
1130           (cons directive
1131                 (add-fill-style-newlines
1132                  (cdr list) string
1133                  (format-directive-end directive) directive))))))
1134     (t nil)))
1135
1136 (defun add-fill-style-newlines-aux (literal string offset)
1137   (let ((end (length literal))
1138         (posn 0))
1139     (collect ((results))
1140       (loop
1141         (let ((blank (position #\space literal :start posn)))
1142           (when (null blank)
1143             (results (subseq literal posn))
1144             (return))
1145           (let ((non-blank (or (position #\space literal :start blank
1146                                          :test #'char/=)
1147                                end)))
1148             (results (subseq literal posn non-blank))
1149             (results (make-format-directive
1150                       :string string :character #\_
1151                       :start (+ offset non-blank) :end (+ offset non-blank)
1152                       :colonp t :atsignp nil :params nil))
1153             (setf posn non-blank))
1154           (when (= posn end)
1155             (return))))
1156       (results))))
1157
1158 (defun parse-format-justification (directives)
1159   (let ((first-semi nil)
1160         (close nil)
1161         (remaining directives))
1162     (collect ((segments))
1163       (loop
1164         (let ((close-or-semi (find-directive remaining #\> t)))
1165           (unless close-or-semi
1166             (error 'format-error
1167                    :complaint "no corresponding close bracket"))
1168           (let ((posn (position close-or-semi remaining)))
1169             (segments (subseq remaining 0 posn))
1170             (setf remaining (nthcdr (1+ posn) remaining)))
1171           (when (char= (format-directive-character close-or-semi)
1172                        #\>)
1173             (setf close close-or-semi)
1174             (return))
1175           (unless first-semi
1176             (setf first-semi close-or-semi))))
1177       (values (segments) first-semi close remaining))))
1178
1179 (sb!xc:defmacro expander-pprint-next-arg (string offset)
1180   `(progn
1181      (when (null args)
1182        (error 'format-error
1183               :complaint "no more arguments"
1184               :control-string ,string
1185               :offset ,offset))
1186      (pprint-pop)
1187      (pop args)))
1188
1189 (defun expand-format-logical-block (prefix per-line-p insides suffix atsignp)
1190   `(let ((arg ,(if atsignp 'args (expand-next-arg))))
1191      ,@(when atsignp
1192          (setf *only-simple-args* nil)
1193          '((setf args nil)))
1194      (pprint-logical-block
1195          (stream arg
1196                  ,(if per-line-p :per-line-prefix :prefix) ,prefix
1197                  :suffix ,suffix)
1198        (let ((args arg)
1199              ,@(unless atsignp
1200                  `((orig-args arg))))
1201          (declare (ignorable args ,@(unless atsignp '(orig-args))))
1202          (block nil
1203            ,@(let ((*expander-next-arg-macro* 'expander-pprint-next-arg)
1204                    (*only-simple-args* nil)
1205                    (*orig-args-available*
1206                     (if atsignp *orig-args-available* t)))
1207                (expand-directive-list insides)))))))
1208
1209 (defun expand-format-justification (segments colonp atsignp first-semi params)
1210   (let ((newline-segment-p
1211          (and first-semi
1212               (format-directive-colonp first-semi))))
1213     (expand-bind-defaults
1214         ((mincol 0) (colinc 1) (minpad 0) (padchar #\space))
1215         params
1216       `(let ((segments nil)
1217              ,@(when newline-segment-p
1218                  '((newline-segment nil)
1219                    (extra-space 0)
1220                    (line-len 72))))
1221          (block nil
1222            ,@(when newline-segment-p
1223                `((setf newline-segment
1224                        (with-output-to-string (stream)
1225                          ,@(expand-directive-list (pop segments))))
1226                  ,(expand-bind-defaults
1227                       ((extra 0)
1228                        (line-len '(or (sb!impl::line-length stream) 72)))
1229                       (format-directive-params first-semi)
1230                     `(setf extra-space ,extra line-len ,line-len))))
1231            ,@(mapcar (lambda (segment)
1232                        `(push (with-output-to-string (stream)
1233                                 ,@(expand-directive-list segment))
1234                               segments))
1235                      segments))
1236          (format-justification stream
1237                                ,@(if newline-segment-p
1238                                      '(newline-segment extra-space line-len)
1239                                      '(nil 0 0))
1240                                segments ,colonp ,atsignp
1241                                ,mincol ,colinc ,minpad ,padchar)))))
1242 \f
1243 ;;;; format directive and support function for user-defined method
1244
1245 (def-format-directive #\/ (string start end colonp atsignp params)
1246   (let ((symbol (extract-user-fun-name string start end)))
1247     (collect ((param-names) (bindings))
1248       (dolist (param-and-offset params)
1249         (let ((param (cdr param-and-offset)))
1250           (let ((param-name (gensym)))
1251             (param-names param-name)
1252             (bindings `(,param-name
1253                         ,(case param
1254                            (:arg (expand-next-arg))
1255                            (:remaining '(length args))
1256                            (t param)))))))
1257       `(let ,(bindings)
1258          (,symbol stream ,(expand-next-arg) ,colonp ,atsignp
1259                   ,@(param-names))))))
1260
1261 (defun extract-user-fun-name (string start end)
1262   (let ((slash (position #\/ string :start start :end (1- end)
1263                          :from-end t)))
1264     (unless slash
1265       (error 'format-error
1266              :complaint "malformed ~~/ directive"))
1267     (let* ((name (string-upcase (let ((foo string))
1268                                   ;; Hack alert: This is to keep the compiler
1269                                   ;; quiet about deleting code inside the
1270                                   ;; subseq expansion.
1271                                   (subseq foo (1+ slash) (1- end)))))
1272            (first-colon (position #\: name))
1273            (second-colon (if first-colon (position #\: name :start (1+ first-colon))))
1274            (package-name (if first-colon
1275                              (subseq name 0 first-colon)
1276                              "COMMON-LISP-USER"))
1277            (package (find-package package-name)))
1278       (unless package
1279         ;; FIXME: should be PACKAGE-ERROR? Could we just use
1280         ;; FIND-UNDELETED-PACKAGE-OR-LOSE?
1281         (error 'format-error
1282                :complaint "no package named ~S"
1283                :args (list package-name)))
1284       (intern (cond
1285                 ((and second-colon (= second-colon (1+ first-colon)))
1286                  (subseq name (1+ second-colon)))
1287                 (first-colon
1288                  (subseq name (1+ first-colon)))
1289                 (t name))
1290               package))))
1291
1292 ;;; compile-time checking for argument mismatch.  This code is
1293 ;;; inspired by that of Gerd Moellmann, and comes decorated with
1294 ;;; FIXMEs:
1295 (defun %compiler-walk-format-string (string args)
1296   (declare (type simple-string string))
1297   (let ((*default-format-error-control-string* string))
1298     (macrolet ((incf-both (&optional (increment 1))
1299                  `(progn
1300                    (incf min ,increment)
1301                    (incf max ,increment)))
1302                (walk-complex-directive (function)
1303                  `(multiple-value-bind (min-inc max-inc remaining)
1304                    (,function directive directives args)
1305                    (incf min min-inc)
1306                    (incf max max-inc)
1307                    (setq directives remaining))))
1308       ;; FIXME: these functions take a list of arguments as well as
1309       ;; the directive stream.  This is to enable possibly some
1310       ;; limited type checking on FORMAT's arguments, as well as
1311       ;; simple argument count mismatch checking: when the minimum and
1312       ;; maximum argument counts are the same at a given point, we
1313       ;; know which argument is going to be used for a given
1314       ;; directive, and some (annotated below) require arguments of
1315       ;; particular types.
1316       (labels
1317           ((walk-justification (justification directives args)
1318              (declare (ignore args))
1319              (let ((*default-format-error-offset*
1320                     (1- (format-directive-end justification))))
1321                (multiple-value-bind (segments first-semi close remaining)
1322                    (parse-format-justification directives)
1323                  (declare (ignore segments first-semi))
1324                  (cond
1325                    ((not (format-directive-colonp close))
1326                     (values 0 0 directives))
1327                    ((format-directive-atsignp justification)
1328                     (values 0 sb!xc:call-arguments-limit directives))
1329                    ;; FIXME: here we could assert that the
1330                    ;; corresponding argument was a list.
1331                    (t (values 1 1 remaining))))))
1332            (walk-conditional (conditional directives args)
1333              (let ((*default-format-error-offset*
1334                     (1- (format-directive-end conditional))))
1335                (multiple-value-bind (sublists last-semi-with-colon-p remaining)
1336                    (parse-conditional-directive directives)
1337                  (declare (ignore last-semi-with-colon-p))
1338                  (let ((sub-max
1339                         (loop for s in sublists
1340                               maximize (nth-value
1341                                         1 (walk-directive-list s args)))))
1342                    (cond
1343                      ((format-directive-atsignp conditional)
1344                       (values 1 (max 1 sub-max) remaining))
1345                      ((loop for p in (format-directive-params conditional)
1346                             thereis (or (integerp (cdr p))
1347                                         (memq (cdr p) '(:remaining :arg))))
1348                       (values 0 sub-max remaining))
1349                      ;; FIXME: if not COLONP, then the next argument
1350                      ;; must be a number.
1351                      (t (values 1 (1+ sub-max) remaining)))))))
1352            (walk-iteration (iteration directives args)
1353              (declare (ignore args))
1354              (let ((*default-format-error-offset*
1355                     (1- (format-directive-end iteration))))
1356                (let* ((close (find-directive directives #\} nil))
1357                       (posn (or (position close directives)
1358                                 (error 'format-error
1359                                        :complaint "no corresponding close brace")))
1360                       (remaining (nthcdr (1+ posn) directives)))
1361                  ;; FIXME: if POSN is zero, the next argument must be
1362                  ;; a format control (either a function or a string).
1363                  (if (format-directive-atsignp iteration)
1364                      (values (if (zerop posn) 1 0)
1365                              sb!xc:call-arguments-limit
1366                              remaining)
1367                      ;; FIXME: the argument corresponding to this
1368                      ;; directive must be a list.
1369                      (let ((nreq (if (zerop posn) 2 1)))
1370                        (values nreq nreq remaining))))))
1371            (walk-directive-list (directives args)
1372              (let ((min 0) (max 0))
1373                (loop
1374                 (let ((directive (pop directives)))
1375                   (when (null directive)
1376                     (return (values min (min max sb!xc:call-arguments-limit))))
1377                   (when (format-directive-p directive)
1378                     (incf-both (count :arg (format-directive-params directive)
1379                                       :key #'cdr))
1380                     (let ((c (format-directive-character directive)))
1381                       (cond
1382                         ((find c "ABCDEFGORSWX$/")
1383                          (incf-both))
1384                         ((char= c #\P)
1385                          (unless (format-directive-colonp directive)
1386                            (incf-both)))
1387                         ((or (find c "IT%&|_();>~") (char= c #\Newline)))
1388                         ;; FIXME: check correspondence of ~( and ~)
1389                         ((char= c #\<)
1390                          (walk-complex-directive walk-justification))
1391                         ((char= c #\[)
1392                          (walk-complex-directive walk-conditional))
1393                         ((char= c #\{)
1394                          (walk-complex-directive walk-iteration))
1395                         ((char= c #\?)
1396                          ;; FIXME: the argument corresponding to this
1397                          ;; directive must be a format control.
1398                          (cond
1399                            ((format-directive-atsignp directive)
1400                             (incf min)
1401                             (setq max sb!xc:call-arguments-limit))
1402                            (t (incf-both 2))))
1403                         (t (throw 'give-up-format-string-walk nil))))))))))
1404         (catch 'give-up-format-string-walk
1405           (let ((directives (tokenize-control-string string)))
1406             (walk-directive-list directives args)))))))