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)
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 (print-banner :reader format-error-print-banner :initarg :print-banner
22 (:report %print-format-error))
24 (defun %print-format-error (condition stream)
26 "~:[~;error in format: ~]~
28 (format-error-print-banner condition)
29 (format-error-complaint condition)
30 (format-error-args condition)
31 (format-error-control-string condition)
32 (format-error-offset condition)))
34 (def!struct format-directive
35 (string (missing-arg) :type simple-string)
36 (start (missing-arg) :type (and unsigned-byte fixnum))
37 (end (missing-arg) :type (and unsigned-byte fixnum))
38 (character (missing-arg) :type base-char)
39 (colonp nil :type (member t nil))
40 (atsignp nil :type (member t nil))
41 (params nil :type list))
42 (def!method print-object ((x format-directive) stream)
43 (print-unreadable-object (x stream)
44 (write-string (format-directive-string x)
46 :start (format-directive-start x)
47 :end (format-directive-end x))))
49 ;;;; TOKENIZE-CONTROL-STRING
51 (defun tokenize-control-string (string)
52 (declare (simple-string string))
57 (let ((next-directive (or (position #\~ string :start index) end)))
58 (when (> next-directive index)
59 (push (subseq string index next-directive) result))
60 (when (= next-directive end)
62 (let ((directive (parse-directive string next-directive)))
63 (push directive result)
64 (setf index (format-directive-end directive)))))
67 (defun parse-directive (string start)
68 (let ((posn (1+ start)) (params nil) (colonp nil) (atsignp nil)
69 (end (length string)))
73 :complaint "String ended before directive was found."
74 :control-string string
78 (when (or colonp atsignp)
80 :complaint "parameters found after #\\: or #\\@ modifier"
81 :control-string string
84 (let ((char (get-char)))
85 (cond ((or (char<= #\0 char #\9) (char= char #\+) (char= char #\-))
87 (multiple-value-bind (param new-posn)
88 (parse-integer string :start posn :junk-allowed t)
89 (push (cons posn param) params)
100 (push (cons posn :arg) params)
110 (push (cons posn :remaining) params)
121 (push (cons posn (get-char)) params)
123 (unless (char= (get-char) #\,)
127 (push (cons posn nil) params))
131 :complaint "too many colons supplied"
132 :control-string string
138 :complaint "too many #\\@ characters supplied"
139 :control-string string
143 (when (char= (schar string (1- posn)) #\,)
145 (push (cons (1- posn) nil) params))
148 (let ((char (get-char)))
149 (when (char= char #\/)
150 (let ((closing-slash (position #\/ string :start (1+ posn))))
152 (setf posn closing-slash)
154 :complaint "no matching closing slash"
155 :control-string string
157 (make-format-directive
158 :string string :start start :end (1+ posn)
159 :character (char-upcase char)
160 :colonp colonp :atsignp atsignp
161 :params (nreverse params))))))
165 (sb!xc:defmacro formatter (control-string)
166 `#',(%formatter control-string))
168 (defun %formatter (control-string)
170 (catch 'need-orig-args
171 (let* ((*simple-args* nil)
172 (*only-simple-args* t)
173 (guts (expand-control-string control-string))
175 (dolist (arg *simple-args*)
179 :complaint "required argument missing"
180 :control-string ,control-string
183 (return `(lambda (stream &optional ,@args &rest args)
186 (let ((*orig-args-available* t)
187 (*only-simple-args* nil))
188 `(lambda (stream &rest orig-args)
189 (let ((args orig-args))
190 ,(expand-control-string control-string)
193 (defun expand-control-string (string)
194 (let* ((string (etypecase string
198 (coerce string 'simple-string))))
199 (*default-format-error-control-string* string)
200 (directives (tokenize-control-string string)))
202 ,@(expand-directive-list directives))))
204 (defun expand-directive-list (directives)
206 (remaining-directives directives))
208 (unless remaining-directives
210 (multiple-value-bind (form new-directives)
211 (expand-directive (car remaining-directives)
212 (cdr remaining-directives))
214 (setf remaining-directives new-directives)))
217 (defun expand-directive (directive more-directives)
221 (aref *format-directive-expanders*
222 (char-code (format-directive-character directive))))
223 (*default-format-error-offset*
224 (1- (format-directive-end directive))))
226 (funcall expander directive more-directives)
228 :complaint "unknown directive"))))
230 (values `(write-string ,directive stream)
233 (defmacro-mundanely expander-next-arg (string offset)
237 :complaint "no more arguments"
238 :control-string ,string
241 (defun expand-next-arg (&optional offset)
242 (if (or *orig-args-available* (not *only-simple-args*))
243 `(,*expander-next-arg-macro*
244 ,*default-format-error-control-string*
245 ,(or offset *default-format-error-offset*))
246 (let ((symbol (gensym "FORMAT-ARG-")))
247 (push (cons symbol (or offset *default-format-error-offset*))
251 (defmacro expand-bind-defaults (specs params &body body)
252 (once-only ((params params))
254 (collect ((expander-bindings) (runtime-bindings))
256 (destructuring-bind (var default) spec
257 (let ((symbol (gensym)))
262 (let* ((param-and-offset (pop ,params))
263 (offset (car param-and-offset))
264 (param (cdr param-and-offset)))
266 (:arg `(or ,(expand-next-arg offset)
269 (setf *only-simple-args* nil)
273 `(let ,(expander-bindings)
274 `(let ,(list ,@(runtime-bindings))
279 "too many parameters, expected no more than ~W"
280 :args (list ,(length specs))
281 :offset (caar ,params)))
286 :complaint "too many parameters, expected none"
287 :offset (caar ,params)))
290 ;;;; format directive machinery
292 ;;; FIXME: only used in this file, could be SB!XC:DEFMACRO in EVAL-WHEN
293 (defmacro def-complex-format-directive (char lambda-list &body body)
294 (let ((defun-name (intern (format nil
295 "~:@(~:C~)-FORMAT-DIRECTIVE-EXPANDER"
298 (directives (if lambda-list (car (last lambda-list)) (gensym))))
300 (defun ,defun-name (,directive ,directives)
302 `((let ,(mapcar (lambda (var)
304 (,(symbolicate "FORMAT-DIRECTIVE-" var)
306 (butlast lambda-list))
308 `((declare (ignore ,directive ,directives))
310 (%set-format-directive-expander ,char #',defun-name))))
312 ;;; FIXME: only used in this file, could be SB!XC:DEFMACRO in EVAL-WHEN
313 (defmacro def-format-directive (char lambda-list &body body)
314 (let ((directives (gensym))
316 (body-without-decls body))
318 (let ((form (car body-without-decls)))
319 (unless (and (consp form) (eq (car form) 'declare))
321 (push (pop body-without-decls) declarations)))
322 (setf declarations (reverse declarations))
323 `(def-complex-format-directive ,char (,@lambda-list ,directives)
325 (values (progn ,@body-without-decls)
328 (eval-when (:compile-toplevel :load-toplevel :execute)
330 (defun %set-format-directive-expander (char fn)
331 (setf (aref *format-directive-expanders* (char-code (char-upcase char))) fn)
334 (defun %set-format-directive-interpreter (char fn)
335 (setf (aref *format-directive-interpreters*
336 (char-code (char-upcase char)))
340 (defun find-directive (directives kind stop-at-semi)
342 (let ((next (car directives)))
343 (if (format-directive-p next)
344 (let ((char (format-directive-character next)))
345 (if (or (char= kind char)
346 (and stop-at-semi (char= char #\;)))
349 (cdr (flet ((after (char)
350 (member (find-directive (cdr directives)
361 (find-directive (cdr directives) kind stop-at-semi)))))
365 ;;;; format directives for simple output
367 (def-format-directive #\A (colonp atsignp params)
369 (expand-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
372 `(format-princ stream ,(expand-next-arg) ',colonp ',atsignp
373 ,mincol ,colinc ,minpad ,padchar))
375 `(or ,(expand-next-arg) "()")
379 (def-format-directive #\S (colonp atsignp params)
381 (expand-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
384 `(format-prin1 stream ,(expand-next-arg) ,colonp ,atsignp
385 ,mincol ,colinc ,minpad ,padchar)))
387 `(let ((arg ,(expand-next-arg)))
390 (princ "()" stream))))
392 `(prin1 ,(expand-next-arg) stream))))
394 (def-format-directive #\C (colonp atsignp params)
395 (expand-bind-defaults () params
397 `(format-print-named-character ,(expand-next-arg) stream)
399 `(prin1 ,(expand-next-arg) stream)
400 `(write-char ,(expand-next-arg) stream)))))
402 (def-format-directive #\W (colonp atsignp params)
403 (expand-bind-defaults () params
404 (if (or colonp atsignp)
405 `(let (,@(when colonp
406 '((*print-pretty* t)))
408 '((*print-level* nil)
409 (*print-length* nil))))
410 (output-object ,(expand-next-arg) stream))
411 `(output-object ,(expand-next-arg) stream))))
413 ;;;; format directives for integer output
415 (defun expand-format-integer (base colonp atsignp params)
416 (if (or colonp atsignp params)
417 (expand-bind-defaults
418 ((mincol 0) (padchar #\space) (commachar #\,) (commainterval 3))
420 `(format-print-integer stream ,(expand-next-arg) ,colonp ,atsignp
421 ,base ,mincol ,padchar ,commachar
423 `(write ,(expand-next-arg) :stream stream :base ,base :radix nil
426 (def-format-directive #\D (colonp atsignp params)
427 (expand-format-integer 10 colonp atsignp params))
429 (def-format-directive #\B (colonp atsignp params)
430 (expand-format-integer 2 colonp atsignp params))
432 (def-format-directive #\O (colonp atsignp params)
433 (expand-format-integer 8 colonp atsignp params))
435 (def-format-directive #\X (colonp atsignp params)
436 (expand-format-integer 16 colonp atsignp params))
438 (def-format-directive #\R (colonp atsignp params)
440 (expand-bind-defaults
441 ((base 10) (mincol 0) (padchar #\space) (commachar #\,)
444 `(format-print-integer stream ,(expand-next-arg) ,colonp ,atsignp
446 ,padchar ,commachar ,commainterval))
449 `(format-print-old-roman stream ,(expand-next-arg))
450 `(format-print-roman stream ,(expand-next-arg)))
452 `(format-print-ordinal stream ,(expand-next-arg))
453 `(format-print-cardinal stream ,(expand-next-arg))))))
455 ;;;; format directive for pluralization
457 (def-format-directive #\P (colonp atsignp params end)
458 (expand-bind-defaults () params
462 (*orig-args-available*
463 `(if (eq orig-args args)
465 :complaint "no previous argument"
467 (do ((arg-ptr orig-args (cdr arg-ptr)))
468 ((eq (cdr arg-ptr) args)
471 (unless *simple-args*
473 :complaint "no previous argument"))
474 (caar *simple-args*))
476 (/show0 "THROWing NEED-ORIG-ARGS from tilde-P")
477 (throw 'need-orig-args nil)))))
479 `(write-string (if (eql ,arg 1) "y" "ies") stream)
480 `(unless (eql ,arg 1) (write-char #\s stream))))))
482 ;;;; format directives for floating point output
484 (def-format-directive #\F (colonp atsignp params)
488 "The colon modifier cannot be used with this directive."))
489 (expand-bind-defaults ((w nil) (d nil) (k nil) (ovf nil) (pad #\space)) params
490 `(format-fixed stream ,(expand-next-arg) ,w ,d ,k ,ovf ,pad ,atsignp)))
492 (def-format-directive #\E (colonp atsignp params)
496 "The colon modifier cannot be used with this directive."))
497 (expand-bind-defaults
498 ((w nil) (d nil) (e nil) (k 1) (ovf nil) (pad #\space) (mark nil))
500 `(format-exponential stream ,(expand-next-arg) ,w ,d ,e ,k ,ovf ,pad ,mark
503 (def-format-directive #\G (colonp atsignp params)
507 "The colon modifier cannot be used with this directive."))
508 (expand-bind-defaults
509 ((w nil) (d nil) (e nil) (k nil) (ovf nil) (pad #\space) (mark nil))
511 `(format-general stream ,(expand-next-arg) ,w ,d ,e ,k ,ovf ,pad ,mark ,atsignp)))
513 (def-format-directive #\$ (colonp atsignp params)
514 (expand-bind-defaults ((d 2) (n 1) (w 0) (pad #\space)) params
515 `(format-dollars stream ,(expand-next-arg) ,d ,n ,w ,pad ,colonp
518 ;;;; format directives for line/page breaks etc.
520 (def-format-directive #\% (colonp atsignp params)
521 (when (or colonp atsignp)
524 "The colon and atsign modifiers cannot be used with this directive."
527 (expand-bind-defaults ((count 1)) params
532 (def-format-directive #\& (colonp atsignp params)
533 (when (or colonp atsignp)
536 "The colon and atsign modifiers cannot be used with this directive."
539 (expand-bind-defaults ((count 1)) params
542 (dotimes (i (1- ,count))
544 '(fresh-line stream)))
546 (def-format-directive #\| (colonp atsignp params)
547 (when (or colonp atsignp)
550 "The colon and atsign modifiers cannot be used with this directive."
553 (expand-bind-defaults ((count 1)) params
555 (write-char (code-char form-feed-char-code) stream)))
556 '(write-char (code-char form-feed-char-code) stream)))
558 (def-format-directive #\~ (colonp atsignp params)
559 (when (or colonp atsignp)
562 "The colon and atsign modifiers cannot be used with this directive."
565 (expand-bind-defaults ((count 1)) params
567 (write-char #\~ stream)))
568 '(write-char #\~ stream)))
570 (def-complex-format-directive #\newline (colonp atsignp params directives)
571 (when (and colonp atsignp)
573 :complaint "both colon and atsign modifiers used simultaneously"))
574 (values (expand-bind-defaults () params
576 '(write-char #\newline stream)
578 (if (and (not colonp)
580 (simple-string-p (car directives)))
581 (cons (string-left-trim *format-whitespace-chars*
586 ;;;; format directives for tabs and simple pretty printing
588 (def-format-directive #\T (colonp atsignp params)
590 (expand-bind-defaults ((n 1) (m 1)) params
591 `(pprint-tab ,(if atsignp :section-relative :section)
594 (expand-bind-defaults ((colrel 1) (colinc 1)) params
595 `(format-relative-tab stream ,colrel ,colinc))
596 (expand-bind-defaults ((colnum 1) (colinc 1)) params
597 `(format-absolute-tab stream ,colnum ,colinc)))))
599 (def-format-directive #\_ (colonp atsignp params)
600 (expand-bind-defaults () params
601 `(pprint-newline ,(if colonp
610 (def-format-directive #\I (colonp atsignp params)
614 "cannot use the at-sign modifier with this directive"))
615 (expand-bind-defaults ((n 0)) params
616 `(pprint-indent ,(if colonp :current :block) ,n stream)))
618 ;;;; format directive for ~*
620 (def-format-directive #\* (colonp atsignp params end)
625 "both colon and atsign modifiers used simultaneously")
626 (expand-bind-defaults ((posn 0)) params
627 (unless *orig-args-available*
628 (/show0 "THROWing NEED-ORIG-ARGS from tilde-@*")
629 (throw 'need-orig-args nil))
630 `(if (<= 0 ,posn (length orig-args))
631 (setf args (nthcdr ,posn orig-args))
633 :complaint "Index ~W out of bounds. Should have been ~
635 :args (list ,posn (length orig-args))
636 :offset ,(1- end)))))
638 (expand-bind-defaults ((n 1)) params
639 (unless *orig-args-available*
640 (/show0 "THROWing NEED-ORIG-ARGS from tilde-:*")
641 (throw 'need-orig-args nil))
642 `(do ((cur-posn 0 (1+ cur-posn))
643 (arg-ptr orig-args (cdr arg-ptr)))
645 (let ((new-posn (- cur-posn ,n)))
646 (if (<= 0 new-posn (length orig-args))
647 (setf args (nthcdr new-posn orig-args))
650 "Index ~W is out of bounds; should have been ~
652 :args (list new-posn (length orig-args))
653 :offset ,(1- end)))))))
655 (expand-bind-defaults ((n 1)) params
656 (setf *only-simple-args* nil)
659 (expand-next-arg)))))
661 ;;;; format directive for indirection
663 (def-format-directive #\? (colonp atsignp params string end)
666 :complaint "cannot use the colon modifier with this directive"))
667 (expand-bind-defaults () params
673 "~A~%while processing indirect format string:"
674 :args (list condition)
676 :control-string ,string
677 :offset ,(1- end)))))
679 (if *orig-args-available*
680 `(setf args (%format stream ,(expand-next-arg) orig-args args))
681 (throw 'need-orig-args nil))
682 `(%format stream ,(expand-next-arg) ,(expand-next-arg))))))
684 ;;;; format directives for capitalization
686 (def-complex-format-directive #\( (colonp atsignp params directives)
687 (let ((close (find-directive directives #\) nil)))
690 :complaint "no corresponding close parenthesis"))
691 (let* ((posn (position close directives))
692 (before (subseq directives 0 posn))
693 (after (nthcdr (1+ posn) directives)))
695 (expand-bind-defaults () params
696 `(let ((stream (make-case-frob-stream stream
704 ,@(expand-directive-list before)))
707 (def-complex-format-directive #\) ()
709 :complaint "no corresponding open parenthesis"))
711 ;;;; format directives and support functions for conditionalization
713 (def-complex-format-directive #\[ (colonp atsignp params directives)
714 (multiple-value-bind (sublists last-semi-with-colon-p remaining)
715 (parse-conditional-directive directives)
721 "both colon and atsign modifiers used simultaneously")
725 "Can only specify one section")
726 (expand-bind-defaults () params
727 (expand-maybe-conditional (car sublists)))))
729 (if (= (length sublists) 2)
730 (expand-bind-defaults () params
731 (expand-true-false-conditional (car sublists)
735 "must specify exactly two sections"))
736 (expand-bind-defaults ((index (expand-next-arg))) params
737 (setf *only-simple-args* nil)
739 (when last-semi-with-colon-p
740 (push `(t ,@(expand-directive-list (pop sublists)))
742 (let ((count (length sublists)))
743 (dolist (sublist sublists)
744 (push `(,(decf count)
745 ,@(expand-directive-list sublist))
747 `(case ,index ,@clauses)))))
750 (defun parse-conditional-directive (directives)
752 (last-semi-with-colon-p nil)
753 (remaining directives))
755 (let ((close-or-semi (find-directive remaining #\] t)))
756 (unless close-or-semi
758 :complaint "no corresponding close bracket"))
759 (let ((posn (position close-or-semi remaining)))
760 (push (subseq remaining 0 posn) sublists)
761 (setf remaining (nthcdr (1+ posn) remaining))
762 (when (char= (format-directive-character close-or-semi) #\])
764 (setf last-semi-with-colon-p
765 (format-directive-colonp close-or-semi)))))
766 (values sublists last-semi-with-colon-p remaining)))
768 (defun expand-maybe-conditional (sublist)
770 `(let ((prev-args args)
771 (arg ,(expand-next-arg)))
773 (setf args prev-args)
774 ,@(expand-directive-list sublist)))))
775 (if *only-simple-args*
776 (multiple-value-bind (guts new-args)
777 (let ((*simple-args* *simple-args*))
778 (values (expand-directive-list sublist)
780 (cond ((eq *simple-args* (cdr new-args))
781 (setf *simple-args* new-args)
782 `(when ,(caar new-args)
785 (setf *only-simple-args* nil)
789 (defun expand-true-false-conditional (true false)
790 (let ((arg (expand-next-arg)))
794 ,@(expand-directive-list true))
796 ,@(expand-directive-list false)))))
797 (if *only-simple-args*
798 (multiple-value-bind (true-guts true-args true-simple)
799 (let ((*simple-args* *simple-args*)
800 (*only-simple-args* t))
801 (values (expand-directive-list true)
804 (multiple-value-bind (false-guts false-args false-simple)
805 (let ((*simple-args* *simple-args*)
806 (*only-simple-args* t))
807 (values (expand-directive-list false)
810 (if (= (length true-args) (length false-args))
814 ,(do ((false false-args (cdr false))
815 (true true-args (cdr true))
816 (bindings nil (cons `(,(caar false) ,(caar true))
818 ((eq true *simple-args*)
819 (setf *simple-args* true-args)
820 (setf *only-simple-args*
821 (and true-simple false-simple))
828 (setf *only-simple-args* nil)
832 (def-complex-format-directive #\; ()
835 "~~; directive not contained within either ~~[...~~] or ~~<...~~>"))
837 (def-complex-format-directive #\] ()
840 "no corresponding open bracket"))
842 ;;;; format directive for up-and-out
844 (def-format-directive #\^ (colonp atsignp params)
847 :complaint "cannot use the at-sign modifier with this directive"))
848 (when (and colonp (not *up-up-and-out-allowed*))
850 :complaint "attempt to use ~~:^ outside a ~~:{...~~} construct"))
851 `(when ,(case (length params)
855 (setf *only-simple-args* nil)
857 (1 (expand-bind-defaults ((count 0)) params
859 (2 (expand-bind-defaults ((arg1 0) (arg2 0)) params
861 (t (expand-bind-defaults ((arg1 0) (arg2 0) (arg3 0)) params
862 `(<= ,arg1 ,arg2 ,arg3))))
864 '(return-from outside-loop nil)
867 ;;;; format directives for iteration
869 (def-complex-format-directive #\{ (colonp atsignp params string end directives)
870 (let ((close (find-directive directives #\} nil)))
873 :complaint "no corresponding close brace"))
874 (let* ((closed-with-colon (format-directive-colonp close))
875 (posn (position close directives)))
879 (if *orig-args-available*
885 "~A~%while processing indirect format string:"
886 :args (list condition)
888 :control-string ,string
889 :offset ,(1- end)))))
891 (%format stream inside-string orig-args args))))
892 (throw 'need-orig-args nil))
893 (let ((*up-up-and-out-allowed* colonp))
894 (expand-directive-list (subseq directives 0 posn)))))
895 (compute-loop-aux (count)
897 (setf *only-simple-args* nil))
899 ,@(unless closed-with-colon
903 `((when (and ,count (minusp (decf ,count)))
906 (let ((*expander-next-arg-macro* 'expander-next-arg)
907 (*only-simple-args* nil)
908 (*orig-args-available* t))
909 `((let* ((orig-args ,(expand-next-arg))
912 (declare (ignorable orig-args outside-args args))
914 ,@(compute-insides)))))
916 ,@(when closed-with-colon
921 (expand-bind-defaults ((count nil)) params
922 (compute-loop-aux count))
923 (compute-loop-aux nil)))
932 `(let* ((orig-args ,(expand-next-arg))
934 (declare (ignorable orig-args args))
935 ,(let ((*expander-next-arg-macro* 'expander-next-arg)
936 (*only-simple-args* nil)
937 (*orig-args-available* t))
939 (values (if (zerop posn)
940 `(let ((inside-string ,(expand-next-arg)))
943 (nthcdr (1+ posn) directives))))))
945 (def-complex-format-directive #\} ()
947 :complaint "no corresponding open brace"))
949 ;;;; format directives and support functions for justification
951 (defparameter *illegal-inside-justification*
952 (mapcar (lambda (x) (parse-directive x 0))
953 '("~W" "~:W" "~@W" "~:@W"
954 "~_" "~:_" "~@_" "~:@_"
956 "~I" "~:I" "~@I" "~:@I"
959 (defun illegal-inside-justification-p (directive)
960 (member directive *illegal-inside-justification*
962 (and (format-directive-p x)
963 (format-directive-p y)
964 (eql (format-directive-character x) (format-directive-character y))
965 (eql (format-directive-colonp x) (format-directive-colonp y))
966 (eql (format-directive-atsignp x) (format-directive-atsignp y))))))
968 (def-complex-format-directive #\< (colonp atsignp params string end directives)
969 (multiple-value-bind (segments first-semi close remaining)
970 (parse-format-justification directives)
972 (if (format-directive-colonp close)
973 (multiple-value-bind (prefix per-line-p insides suffix)
974 (parse-format-logical-block segments colonp first-semi
975 close params string end)
976 (expand-format-logical-block prefix per-line-p insides
978 (let ((count (apply #'+ (mapcar (lambda (x) (count-if #'illegal-inside-justification-p x)) segments))))
980 ;; ANSI specifies that "an error is signalled" in this
983 :complaint "~D illegal directive~:P found inside justification block"
985 (expand-format-justification segments colonp atsignp
989 (def-complex-format-directive #\> ()
991 :complaint "no corresponding open bracket"))
993 (defun parse-format-logical-block
994 (segments colonp first-semi close params string end)
997 :complaint "No parameters can be supplied with ~~<...~~:>."
998 :offset (caar params)))
999 (multiple-value-bind (prefix insides suffix)
1000 (multiple-value-bind (prefix-default suffix-default)
1001 (if colonp (values "(" ")") (values nil ""))
1002 (flet ((extract-string (list prefix-p)
1003 (let ((directive (find-if #'format-directive-p list)))
1005 (error 'format-error
1007 "cannot include format directives inside the ~
1008 ~:[suffix~;prefix~] segment of ~~<...~~:>"
1009 :args (list prefix-p)
1010 :offset (1- (format-directive-end directive)))
1011 (apply #'concatenate 'string list)))))
1012 (case (length segments)
1013 (0 (values prefix-default nil suffix-default))
1014 (1 (values prefix-default (car segments) suffix-default))
1015 (2 (values (extract-string (car segments) t)
1016 (cadr segments) suffix-default))
1017 (3 (values (extract-string (car segments) t)
1019 (extract-string (caddr segments) nil)))
1021 (error 'format-error
1022 :complaint "too many segments for ~~<...~~:>")))))
1023 (when (format-directive-atsignp close)
1025 (add-fill-style-newlines insides
1028 (format-directive-end first-semi)
1031 (and first-semi (format-directive-atsignp first-semi))
1035 (defun add-fill-style-newlines (list string offset)
1037 (let ((directive (car list)))
1038 (if (simple-string-p directive)
1039 (nconc (add-fill-style-newlines-aux directive string offset)
1040 (add-fill-style-newlines (cdr list)
1042 (+ offset (length directive))))
1044 (add-fill-style-newlines (cdr list)
1046 (format-directive-end directive)))))
1049 (defun add-fill-style-newlines-aux (literal string offset)
1050 (let ((end (length literal))
1052 (collect ((results))
1054 (let ((blank (position #\space literal :start posn)))
1056 (results (subseq literal posn))
1058 (let ((non-blank (or (position #\space literal :start blank
1061 (results (subseq literal posn non-blank))
1062 (results (make-format-directive
1063 :string string :character #\_
1064 :start (+ offset non-blank) :end (+ offset non-blank)
1065 :colonp t :atsignp nil :params nil))
1066 (setf posn non-blank))
1071 (defun parse-format-justification (directives)
1072 (let ((first-semi nil)
1074 (remaining directives))
1075 (collect ((segments))
1077 (let ((close-or-semi (find-directive remaining #\> t)))
1078 (unless close-or-semi
1079 (error 'format-error
1080 :complaint "no corresponding close bracket"))
1081 (let ((posn (position close-or-semi remaining)))
1082 (segments (subseq remaining 0 posn))
1083 (setf remaining (nthcdr (1+ posn) remaining)))
1084 (when (char= (format-directive-character close-or-semi)
1086 (setf close close-or-semi)
1089 (setf first-semi close-or-semi))))
1090 (values (segments) first-semi close remaining))))
1092 (sb!xc:defmacro expander-pprint-next-arg (string offset)
1095 (error 'format-error
1096 :complaint "no more arguments"
1097 :control-string ,string
1102 (defun expand-format-logical-block (prefix per-line-p insides suffix atsignp)
1103 `(let ((arg ,(if atsignp 'args (expand-next-arg))))
1105 (setf *only-simple-args* nil)
1107 (pprint-logical-block
1109 ,(if per-line-p :per-line-prefix :prefix) ,prefix
1113 `((orig-args arg))))
1114 (declare (ignorable args ,@(unless atsignp '(orig-args))))
1116 ,@(let ((*expander-next-arg-macro* 'expander-pprint-next-arg)
1117 (*only-simple-args* nil)
1118 (*orig-args-available* t))
1119 (expand-directive-list insides)))))))
1121 (defun expand-format-justification (segments colonp atsignp first-semi params)
1122 (let ((newline-segment-p
1124 (format-directive-colonp first-semi))))
1125 (expand-bind-defaults
1126 ((mincol 0) (colinc 1) (minpad 0) (padchar #\space))
1128 `(let ((segments nil)
1129 ,@(when newline-segment-p
1130 '((newline-segment nil)
1134 ,@(when newline-segment-p
1135 `((setf newline-segment
1136 (with-output-to-string (stream)
1137 ,@(expand-directive-list (pop segments))))
1138 ,(expand-bind-defaults
1140 (line-len '(or (sb!impl::line-length stream) 72)))
1141 (format-directive-params first-semi)
1142 `(setf extra-space ,extra line-len ,line-len))))
1143 ,@(mapcar (lambda (segment)
1144 `(push (with-output-to-string (stream)
1145 ,@(expand-directive-list segment))
1148 (format-justification stream
1149 ,@(if newline-segment-p
1150 '(newline-segment extra-space line-len)
1152 segments ,colonp ,atsignp
1153 ,mincol ,colinc ,minpad ,padchar)))))
1155 ;;;; format directive and support function for user-defined method
1157 (def-format-directive #\/ (string start end colonp atsignp params)
1158 (let ((symbol (extract-user-fun-name string start end)))
1159 (collect ((param-names) (bindings))
1160 (dolist (param-and-offset params)
1161 (let ((param (cdr param-and-offset)))
1162 (let ((param-name (gensym)))
1163 (param-names param-name)
1164 (bindings `(,param-name
1166 (:arg (expand-next-arg))
1167 (:remaining '(length args))
1170 (,symbol stream ,(expand-next-arg) ,colonp ,atsignp
1171 ,@(param-names))))))
1173 (defun extract-user-fun-name (string start end)
1174 (let ((slash (position #\/ string :start start :end (1- end)
1177 (error 'format-error
1178 :complaint "malformed ~~/ directive"))
1179 (let* ((name (string-upcase (let ((foo string))
1180 ;; Hack alert: This is to keep the compiler
1181 ;; quiet about deleting code inside the
1182 ;; subseq expansion.
1183 (subseq foo (1+ slash) (1- end)))))
1184 (first-colon (position #\: name))
1185 (second-colon (if first-colon (position #\: name :start (1+ first-colon))))
1186 (package-name (if first-colon
1187 (subseq name 0 first-colon)
1188 "COMMON-LISP-USER"))
1189 (package (find-package package-name)))
1191 ;; FIXME: should be PACKAGE-ERROR? Could we just use
1192 ;; FIND-UNDELETED-PACKAGE-OR-LOSE?
1193 (error 'format-error
1194 :complaint "no package named ~S"
1195 :args (list package-name)))
1197 ((and second-colon (= second-colon (1+ first-colon)))
1198 (subseq name (1+ second-colon)))
1200 (subseq name (1+ first-colon)))