Grovel timeval and timespec struct definitions rather than hard-coding.
[sbcl.git] / src / code / parse-defmacro-errors.lisp
1 ;;;; error-handling machinery for PARSE-DEFMACRO, separated from
2 ;;;; PARSE-DEFMACRO code itself because the happy path can be handled
3 ;;;; earlier in the bootstrap sequence than DEFINE-CONDITION can be
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
13
14 (in-package "SB!KERNEL")
15
16 (define-condition defmacro-lambda-list-bind-error (error)
17   ((kind :reader defmacro-lambda-list-bind-error-kind
18          :initarg :kind)
19    (name :reader defmacro-lambda-list-bind-error-name
20          :initarg :name
21          :initform nil)))
22
23 ;;; shared logic for REPORTing variants of DEFMACRO-LAMBDA-LIST-BIND-ERROR:
24 ;;; Set up appropriate prettying and indentation on STREAM, print some
25 ;;; boilerplate related to CONDITION (an instance of
26 ;;; DEFMACRO-LAMBDA-LIST-BIND-ERROR), then execute BODY.
27 (defmacro !printing-defmacro-lambda-list-bind-error ((condition stream)
28                                                      &body body)
29   `(%printing-defmacro-lambda-list-bind-error ,condition
30                                               ,stream
31                                               (lambda (,stream)
32                                                 (declare (type stream ,stream))
33                                                 ,@body)))
34 (defun %printing-defmacro-lambda-list-bind-error (condition stream fun)
35   (declare (type stream stream) (type function fun))
36   (pprint-logical-block (stream nil)
37     (format stream
38             "error while parsing arguments to ~A~@[ ~S~]:~2I~:@_"
39             (defmacro-lambda-list-bind-error-kind condition)
40             (defmacro-lambda-list-bind-error-name condition))
41     (pprint-logical-block (stream nil)
42       (funcall fun stream))))
43
44 (define-condition defmacro-bogus-sublist-error
45                   (defmacro-lambda-list-bind-error)
46   ((object :reader defmacro-bogus-sublist-error-object :initarg :object)
47    (lambda-list :reader defmacro-bogus-sublist-error-lambda-list
48                 :initarg :lambda-list))
49   (:report
50    (lambda (condition stream)
51      (!printing-defmacro-lambda-list-bind-error (condition stream)
52        (format stream
53                "bogus sublist ~2I~_~S ~I~_to satisfy lambda-list ~2I~_~:S"
54                (defmacro-bogus-sublist-error-object condition)
55                (defmacro-bogus-sublist-error-lambda-list condition))))))
56
57 (define-condition arg-count-error (defmacro-lambda-list-bind-error)
58   ((args :reader arg-count-error-args :initarg :args)
59    (lambda-list :reader arg-count-error-lambda-list
60                 :initarg :lambda-list)
61    (minimum :reader arg-count-error-minimum :initarg :minimum)
62    (maximum :reader arg-count-error-maximum :initarg :maximum))
63   (:report
64    (lambda (condition stream)
65      (!printing-defmacro-lambda-list-bind-error (condition stream)
66        (format stream
67                "invalid number of elements in ~2I~_~:S ~
68                 ~I~_to satisfy lambda list ~2I~_~:S: ~I~_"
69                (arg-count-error-args condition)
70                (arg-count-error-lambda-list condition))
71        (cond ((null (arg-count-error-maximum condition))
72               (format stream "at least ~W expected"
73                       (arg-count-error-minimum condition)))
74              ((= (arg-count-error-minimum condition)
75                  (arg-count-error-maximum condition))
76               (format stream "exactly ~W expected"
77                       (arg-count-error-minimum condition)))
78              (t
79               (format stream "between ~W and ~W expected"
80                       (arg-count-error-minimum condition)
81                       (arg-count-error-maximum condition))))
82        (format stream ", but ~a found"
83                (if (null (cdr (last (arg-count-error-args condition))))
84                    (length (arg-count-error-args condition))
85                    "not a proper list"))))))
86
87 (define-condition defmacro-lambda-list-broken-key-list-error
88                   (defmacro-lambda-list-bind-error)
89   ((problem :reader defmacro-lambda-list-broken-key-list-error-problem
90             :initarg :problem)
91    (info :reader defmacro-lambda-list-broken-key-list-error-info
92          :initarg :info))
93   (:report (lambda (condition stream)
94              (!printing-defmacro-lambda-list-bind-error (condition stream)
95                (format stream
96                        ;; FIXME: These should probably just be three
97                        ;; subclasses of the base class, so that we don't
98                        ;; need to maintain the set of tags both here and
99                        ;; implicitly wherever this macro is used. (This
100                        ;; might get easier once CLOS is initialized in
101                        ;; cold init.)
102                        (ecase
103                            (defmacro-lambda-list-broken-key-list-error-problem
104                              condition)
105                          (:dotted-list
106                           "dotted keyword/value list: ~S")
107                          (:odd-length
108                           "odd number of elements in keyword/value list: ~S")
109                          (:unknown-keyword
110                           "~{unknown keyword: ~S; expected one of ~
111                            ~{~S~^, ~}~}"))
112                        (defmacro-lambda-list-broken-key-list-error-info
113                          condition))))))