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
* (char-code char
)))
284 (*default-format-error-offset
*
285 (1- (format-directive-end directive
))))
286 (declare (type (or null function
) expander
))
288 (funcall expander directive more-directives
)
290 :complaint
"unknown directive ~@[(character: ~A)~]"
291 :args
(list (char-name (format-directive-character directive
)))))))
293 (values `(write-string ,directive stream
)
296 (defmacro-mundanely expander-next-arg
(string offset
)
300 :complaint
"no more arguments"
301 :control-string
,string
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
*))
314 (defmacro expand-bind-defaults
(specs params
&body body
)
315 (once-only ((params params
))
317 (collect ((expander-bindings) (runtime-bindings))
319 (destructuring-bind (var default
) spec
320 (let ((symbol (gensym)))
325 (let* ((param-and-offset (pop ,params
))
326 (offset (car param-and-offset
))
327 (param (cdr param-and-offset
)))
329 (:arg
`(or ,(expand-next-arg offset
)
332 (setf *only-simple-args
* nil
)
336 `(let ,(expander-bindings)
337 `(let ,(list ,@(runtime-bindings))
342 "too many parameters, expected no more than ~W"
343 :args
(list ,(length specs
))
344 :offset
(caar ,params
)))
349 :complaint
"too many parameters, expected none"
350 :offset
(caar ,params
)))
353 ;;;; format directive machinery
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"
361 (directives (if lambda-list
(car (last lambda-list
)) (gensym))))
363 (defun ,defun-name
(,directive
,directives
)
365 `((let ,(mapcar (lambda (var)
367 (,(symbolicate "FORMAT-DIRECTIVE-" var
)
369 (butlast lambda-list
))
371 `((declare (ignore ,directive
,directives
))
373 (%set-format-directive-expander
,char
#',defun-name
))))
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))
379 (body-without-decls body
))
381 (let ((form (car body-without-decls
)))
382 (unless (and (consp form
) (eq (car form
) 'declare
))
384 (push (pop body-without-decls
) declarations
)))
385 (setf declarations
(reverse declarations
))
386 `(def-complex-format-directive ,char
(,@lambda-list
,directives
)
388 (values (progn ,@body-without-decls
)
391 (eval-when (#-sb-xc
:compile-toplevel
:load-toplevel
:execute
)
393 (defun %set-format-directive-expander
(char fn
)
394 (setf (aref *format-directive-expanders
* (char-code (char-upcase char
))) fn
)
397 (defun %set-format-directive-interpreter
(char fn
)
398 (setf (aref *format-directive-interpreters
*
399 (char-code (char-upcase char
)))
403 (defun find-directive (directives kind stop-at-semi
)
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
#\
;)))
412 (cdr (flet ((after (char)
413 (member (find-directive (cdr directives
)
424 (find-directive (cdr directives
) kind stop-at-semi
)))))
428 ;;;; format directives for simple output
430 (def-format-directive #\A
(colonp atsignp params
)
432 (expand-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
435 `(format-princ stream
,(expand-next-arg) ',colonp
',atsignp
436 ,mincol
,colinc
,minpad
,padchar
))
438 `(or ,(expand-next-arg) "()")
442 (def-format-directive #\S
(colonp atsignp params
)
444 (expand-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
447 `(format-prin1 stream
,(expand-next-arg) ,colonp
,atsignp
448 ,mincol
,colinc
,minpad
,padchar
)))
450 `(let ((arg ,(expand-next-arg)))
453 (princ "()" stream
))))
455 `(prin1 ,(expand-next-arg) stream
))))
457 (def-format-directive #\C
(colonp atsignp params
)
458 (expand-bind-defaults () params
460 `(format-print-named-character ,(expand-next-arg) stream
)
462 `(prin1 ,(expand-next-arg) stream
)
463 `(write-char ,(expand-next-arg) stream
)))))
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
)))
471 '((*print-level
* nil
)
472 (*print-length
* nil
))))
473 (output-object ,(expand-next-arg) stream
))
474 `(output-object ,(expand-next-arg) stream
))))
476 ;;;; format directives for integer output
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))
483 `(format-print-integer stream
,(expand-next-arg) ,colonp
,atsignp
484 ,base
,mincol
,padchar
,commachar
486 `(write ,(expand-next-arg) :stream stream
:base
,base
:radix nil
489 (def-format-directive #\D
(colonp atsignp params
)
490 (expand-format-integer 10 colonp atsignp params
))
492 (def-format-directive #\B
(colonp atsignp params
)
493 (expand-format-integer 2 colonp atsignp params
))
495 (def-format-directive #\O
(colonp atsignp params
)
496 (expand-format-integer 8 colonp atsignp params
))
498 (def-format-directive #\X
(colonp atsignp params
)
499 (expand-format-integer 16 colonp atsignp params
))
501 (def-format-directive #\R
(colonp atsignp params
)
502 (expand-bind-defaults
503 ((base nil
) (mincol 0) (padchar #\space
) (commachar #\
,)
506 (let ((n-arg (gensym)))
507 `(let ((,n-arg
,(expand-next-arg)))
509 (format-print-integer stream
,n-arg
,colonp
,atsignp
511 ,padchar
,commachar
,commainterval
)
514 `(format-print-old-roman stream
,n-arg
)
515 `(format-print-roman stream
,n-arg
))
517 `(format-print-ordinal stream
,n-arg
)
518 `(format-print-cardinal stream
,n-arg
))))))))
520 ;;;; format directive for pluralization
522 (def-format-directive #\P
(colonp atsignp params end
)
523 (expand-bind-defaults () params
527 (*orig-args-available
*
528 `(if (eq orig-args args
)
530 :complaint
"no previous argument"
532 (do ((arg-ptr orig-args
(cdr arg-ptr
)))
533 ((eq (cdr arg-ptr
) args
)
536 (unless *simple-args
*
538 :complaint
"no previous argument"))
539 (caar *simple-args
*))
541 (/show0
"THROWing NEED-ORIG-ARGS from tilde-P")
542 (throw 'need-orig-args nil
)))))
544 `(write-string (if (eql ,arg
1) "y" "ies") stream
)
545 `(unless (eql ,arg
1) (write-char #\s stream
))))))
547 ;;;; format directives for floating point output
549 (def-format-directive #\F
(colonp atsignp params
)
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
)))
557 (def-format-directive #\E
(colonp atsignp params
)
561 "The colon modifier cannot be used with this directive."))
562 (expand-bind-defaults
563 ((w nil
) (d nil
) (e nil
) (k 1) (ovf nil
) (pad #\space
) (mark nil
))
565 `(format-exponential stream
,(expand-next-arg) ,w
,d
,e
,k
,ovf
,pad
,mark
568 (def-format-directive #\G
(colonp atsignp params
)
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
))
576 `(format-general stream
,(expand-next-arg) ,w
,d
,e
,k
,ovf
,pad
,mark
,atsignp
)))
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
583 ;;;; format directives for line/page breaks etc.
585 (def-format-directive #\%
(colonp atsignp params
)
586 (when (or colonp atsignp
)
589 "The colon and atsign modifiers cannot be used with this directive."
592 (expand-bind-defaults ((count 1)) params
597 (def-format-directive #\
& (colonp atsignp params
)
598 (when (or colonp atsignp
)
601 "The colon and atsign modifiers cannot be used with this directive."
604 (expand-bind-defaults ((count 1)) params
607 (dotimes (i (1- ,count
))
609 '(fresh-line stream
)))
611 (def-format-directive #\|
(colonp atsignp params
)
612 (when (or colonp atsignp
)
615 "The colon and atsign modifiers cannot be used with this directive."
618 (expand-bind-defaults ((count 1)) params
620 (write-char (code-char form-feed-char-code
) stream
)))
621 '(write-char (code-char form-feed-char-code
) stream
)))
623 (def-format-directive #\~
(colonp atsignp params
)
624 (when (or colonp atsignp
)
627 "The colon and atsign modifiers cannot be used with this directive."
630 (expand-bind-defaults ((count 1)) params
632 (write-char #\~ stream
)))
633 '(write-char #\~ stream
)))
635 (def-complex-format-directive #\newline
(colonp atsignp params directives
)
636 (when (and colonp atsignp
)
638 :complaint
"both colon and atsign modifiers used simultaneously"))
639 (values (expand-bind-defaults () params
641 '(write-char #\newline stream
)
643 (if (and (not colonp
)
645 (simple-string-p (car directives
)))
646 (cons (string-left-trim *format-whitespace-chars
*
651 ;;;; format directives for tabs and simple pretty printing
653 (def-format-directive #\T
(colonp atsignp params
)
655 (expand-bind-defaults ((n 1) (m 1)) params
656 `(pprint-tab ,(if atsignp
:section-relative
:section
)
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
)))))
664 (def-format-directive #\_
(colonp atsignp params
)
665 (expand-bind-defaults () params
666 `(pprint-newline ,(if colonp
675 (def-format-directive #\I
(colonp atsignp params
)
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
)))
683 ;;;; format directive for ~*
685 (def-format-directive #\
* (colonp atsignp params end
)
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
))
698 :complaint
"Index ~W out of bounds. Should have been ~
700 :args
(list ,posn
(length orig-args
))
701 :offset
,(1- end
)))))
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
)))
710 (let ((new-posn (- cur-posn
,n
)))
711 (if (<= 0 new-posn
(length orig-args
))
712 (setf args
(nthcdr new-posn orig-args
))
715 "Index ~W is out of bounds; should have been ~
717 :args
(list new-posn
(length orig-args
))
718 :offset
,(1- end
)))))))
720 (expand-bind-defaults ((n 1)) params
721 (setf *only-simple-args
* nil
)
724 (expand-next-arg)))))
726 ;;;; format directive for indirection
728 (def-format-directive #\? (colonp atsignp params string end
)
731 :complaint
"cannot use the colon modifier with this directive"))
732 (expand-bind-defaults () params
738 "~A~%while processing indirect format string:"
739 :args
(list condition
)
741 :control-string
,string
742 :offset
,(1- end
)))))
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))))))
749 ;;;; format directives for capitalization
751 (def-complex-format-directive #\
( (colonp atsignp params directives
)
752 (let ((close (find-directive directives
#\
) nil
)))
755 :complaint
"no corresponding close parenthesis"))
756 (let* ((posn (position close directives
))
757 (before (subseq directives
0 posn
))
758 (after (nthcdr (1+ posn
) directives
)))
760 (expand-bind-defaults () params
761 `(let ((stream (make-case-frob-stream stream
769 ,@(expand-directive-list before
)))
772 (def-complex-format-directive #\
) ()
774 :complaint
"no corresponding open parenthesis"))
776 ;;;; format directives and support functions for conditionalization
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
)
786 "both colon and atsign modifiers used simultaneously")
790 "Can only specify one section")
791 (expand-bind-defaults () params
792 (expand-maybe-conditional (car sublists
)))))
794 (if (= (length sublists
) 2)
795 (expand-bind-defaults () params
796 (expand-true-false-conditional (car sublists
)
800 "must specify exactly two sections"))
801 (expand-bind-defaults ((index nil
)) params
802 (setf *only-simple-args
* nil
)
804 (case `(or ,index
,(expand-next-arg))))
805 (when last-semi-with-colon-p
806 (push `(t ,@(expand-directive-list (pop sublists
)))
808 (let ((count (length sublists
)))
809 (dolist (sublist sublists
)
810 (push `(,(decf count
)
811 ,@(expand-directive-list sublist
))
813 `(case ,case
,@clauses
)))))
816 (defun parse-conditional-directive (directives)
818 (last-semi-with-colon-p nil
)
819 (remaining directives
))
821 (let ((close-or-semi (find-directive remaining
#\
] t
)))
822 (unless close-or-semi
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
) #\
])
830 (setf last-semi-with-colon-p
831 (format-directive-colonp close-or-semi
)))))
832 (values sublists last-semi-with-colon-p remaining
)))
834 (defun expand-maybe-conditional (sublist)
836 `(let ((prev-args args
)
837 (arg ,(expand-next-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
)
846 (cond ((and new-args
(eq *simple-args
* (cdr new-args
)))
847 (setf *simple-args
* new-args
)
848 `(when ,(caar new-args
)
851 (setf *only-simple-args
* nil
)
855 (defun expand-true-false-conditional (true false
)
856 (let ((arg (expand-next-arg)))
860 ,@(expand-directive-list true
))
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
)
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
)
876 (if (= (length true-args
) (length false-args
))
880 ,(do ((false false-args
(cdr false
))
881 (true true-args
(cdr true
))
882 (bindings nil
(cons `(,(caar false
) ,(caar true
))
884 ((eq true
*simple-args
*)
885 (setf *simple-args
* true-args
)
886 (setf *only-simple-args
*
887 (and true-simple false-simple
))
894 (setf *only-simple-args
* nil
)
898 (def-complex-format-directive #\
; ()
901 "~~; directive not contained within either ~~[...~~] or ~~<...~~>"))
903 (def-complex-format-directive #\
] ()
906 "no corresponding open bracket"))
908 ;;;; format directive for up-and-out
910 (def-format-directive #\^
(colonp atsignp params
)
913 :complaint
"cannot use the at-sign modifier with this directive"))
914 (when (and colonp
(not *up-up-and-out-allowed
*))
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))
924 (setf *only-simple-args
* nil
)
927 '(return-from outside-loop nil
)
930 ;;;; format directives for iteration
932 (def-complex-format-directive #\
{ (colonp atsignp params string end directives
)
933 (let ((close (find-directive directives
#\
} nil
)))
936 :complaint
"no corresponding close brace"))
937 (let* ((closed-with-colon (format-directive-colonp close
))
938 (posn (position close directives
)))
942 (if *orig-args-available
*
948 "~A~%while processing indirect format string:"
949 :args
(list condition
)
951 :control-string
,string
952 :offset
,(1- end
)))))
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)
960 (setf *only-simple-args
* nil
))
962 ,@(unless closed-with-colon
966 `((when (and ,count
(minusp (decf ,count
)))
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))
975 (declare (ignorable orig-args outside-args args
))
977 ,@(compute-insides)))))
979 ,@(when closed-with-colon
982 (compute-block (count)
985 ,(compute-loop count
))
986 (compute-loop count
)))
987 (compute-bindings (count)
989 (compute-block count
)
990 `(let* ((orig-args ,(expand-next-arg))
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
))))))
998 (expand-bind-defaults ((count nil
)) params
1000 `(let ((inside-string ,(expand-next-arg)))
1001 ,(compute-bindings count
))
1002 (compute-bindings count
)))
1004 `(let ((inside-string ,(expand-next-arg)))
1005 ,(compute-bindings nil
))
1006 (compute-bindings nil
)))
1007 (nthcdr (1+ posn
) directives
))))))
1009 (def-complex-format-directive #\
} ()
1010 (error 'format-error
1011 :complaint
"no corresponding open brace"))
1013 ;;;; format directives and support functions for justification
1015 (defparameter *illegal-inside-justification
*
1016 (mapcar (lambda (x) (parse-directive x
0))
1017 '("~W" "~:W" "~@W" "~:@W"
1018 "~_" "~:_" "~@_" "~:@_"
1020 "~I" "~:I" "~@I" "~:@I"
1023 (defun illegal-inside-justification-p (directive)
1024 (member directive
*illegal-inside-justification
*
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
))))))
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
)
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
1042 (let ((count (reduce #'+ (mapcar (lambda (x) (count-if #'illegal-inside-justification-p x
)) segments
))))
1044 ;; ANSI specifies that "an error is signalled" in this
1046 (error 'format-error
1047 :complaint
"~D illegal directive~:P found inside justification block"
1049 :references
(list '(:ansi-cl
:section
(22 3 5 2)))))
1050 (expand-format-justification segments colonp atsignp
1051 first-semi params
)))
1054 (def-complex-format-directive #\
> ()
1055 (error 'format-error
1056 :complaint
"no corresponding open bracket"))
1058 (defun parse-format-logical-block
1059 (segments colonp first-semi close params string end
)
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
)))
1070 (error 'format-error
1072 "cannot include format directives inside the ~
1073 ~:[suffix~;prefix~] segment of ~~<...~~:>"
1074 :args
(list prefix-p
)
1075 :offset
(1- (format-directive-end directive
))
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
)
1086 (extract-string (caddr segments
) nil
)))
1088 (error 'format-error
1089 :complaint
"too many segments for ~~<...~~:>")))))
1090 (when (format-directive-atsignp close
)
1092 (add-fill-style-newlines insides
1095 (format-directive-end first-semi
)
1098 (and first-semi
(format-directive-atsignp first-semi
))
1102 (defun add-fill-style-newlines (list string offset
&optional last-directive
)
1105 (let ((directive (car list
)))
1107 ((simple-string-p directive
)
1108 (let* ((non-space (position #\Space directive
:test
#'char
/=))
1109 (newlinep (and last-directive
1111 (format-directive-character last-directive
)
1114 ((and newlinep non-space
)
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
)))))
1123 (add-fill-style-newlines
1124 (cdr list
) string
(+ offset
(length directive
)))))
1126 (nconc (add-fill-style-newlines-aux directive string offset
)
1127 (add-fill-style-newlines
1128 (cdr list
) string
(+ offset
(length directive
))))))))
1131 (add-fill-style-newlines
1133 (format-directive-end directive
) directive
))))))
1136 (defun add-fill-style-newlines-aux (literal string offset
)
1137 (let ((end (length literal
))
1139 (collect ((results))
1141 (let ((blank (position #\space literal
:start posn
)))
1143 (results (subseq literal posn
))
1145 (let ((non-blank (or (position #\space literal
:start blank
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
))
1158 (defun parse-format-justification (directives)
1159 (let ((first-semi nil
)
1161 (remaining directives
))
1162 (collect ((segments))
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
)
1173 (setf close close-or-semi
)
1176 (setf first-semi close-or-semi
))))
1177 (values (segments) first-semi close remaining
))))
1179 (sb!xc
:defmacro expander-pprint-next-arg
(string offset
)
1182 (error 'format-error
1183 :complaint
"no more arguments"
1184 :control-string
,string
1189 (defun expand-format-logical-block (prefix per-line-p insides suffix atsignp
)
1190 `(let ((arg ,(if atsignp
'args
(expand-next-arg))))
1192 (setf *only-simple-args
* nil
)
1194 (pprint-logical-block
1196 ,(if per-line-p
:per-line-prefix
:prefix
) ,prefix
1200 `((orig-args arg
))))
1201 (declare (ignorable args
,@(unless atsignp
'(orig-args))))
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
)))))))
1209 (defun expand-format-justification (segments colonp atsignp first-semi params
)
1210 (let ((newline-segment-p
1212 (format-directive-colonp first-semi
))))
1213 (expand-bind-defaults
1214 ((mincol 0) (colinc 1) (minpad 0) (padchar #\space
))
1216 `(let ((segments nil
)
1217 ,@(when newline-segment-p
1218 '((newline-segment 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
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
))
1236 (format-justification stream
1237 ,@(if newline-segment-p
1238 '(newline-segment extra-space line-len
)
1240 segments
,colonp
,atsignp
1241 ,mincol
,colinc
,minpad
,padchar
)))))
1243 ;;;; format directive and support function for user-defined method
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
1254 (:arg
(expand-next-arg))
1255 (:remaining
'(length args
))
1258 (,symbol stream
,(expand-next-arg) ,colonp
,atsignp
1259 ,@(param-names))))))
1261 (defun extract-user-fun-name (string start end
)
1262 (let ((slash (position #\
/ string
:start start
:end
(1- end
)
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
)))
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
)))
1285 ((and second-colon
(= second-colon
(1+ first-colon
)))
1286 (subseq name
(1+ second-colon
)))
1288 (subseq name
(1+ first-colon
)))
1292 ;;; compile-time checking for argument mismatch. This code is
1293 ;;; inspired by that of Gerd Moellmann, and comes decorated with
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))
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
)
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.
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
))
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
))
1339 (loop for s in sublists
1341 1 (walk-directive-list s args
)))))
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
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))
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
)
1380 (let ((c (format-directive-character directive
)))
1382 ((find c
"ABCDEFGORSWX$/")
1385 (unless (format-directive-colonp directive
)
1387 ((or (find c
"IT%&|_();>~") (char= c
#\Newline
)))
1388 ;; FIXME: check correspondence of ~( and ~)
1390 (walk-complex-directive walk-justification
))
1392 (walk-complex-directive walk-conditional
))
1394 (walk-complex-directive walk-iteration
))
1396 ;; FIXME: the argument corresponding to this
1397 ;; directive must be a format control.
1399 ((format-directive-atsignp directive
)
1401 (setq max sb
!xc
:call-arguments-limit
))
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
)))))))