0.pre7.88:
[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 ;;; We save space in macro definitions by calling this function.
17 (defun arg-count-error (error-kind name arg lambda-list minimum maximum)
18   (let (#-sb-xc-host
19         (sb!debug:*stack-top-hint* (nth-value 1 (find-caller-name-and-frame))))
20     (error 'arg-count-error
21            :kind error-kind
22            :name name
23            :argument arg
24            :lambda-list lambda-list
25            :minimum minimum :maximum maximum)))
26
27 (define-condition defmacro-lambda-list-bind-error (error)
28   ((kind :reader defmacro-lambda-list-bind-error-kind
29          :initarg :kind)
30    (name :reader defmacro-lambda-list-bind-error-name
31          :initarg :name
32          :initform nil)))
33
34 (defun print-defmacro-ll-bind-error-intro (condition stream)
35   (format stream
36           "error while parsing arguments to ~A~@[ ~S~]:~%"
37           (defmacro-lambda-list-bind-error-kind condition)
38           (defmacro-lambda-list-bind-error-name condition)))
39
40 (define-condition defmacro-bogus-sublist-error
41                   (defmacro-lambda-list-bind-error)
42   ((object :reader defmacro-bogus-sublist-error-object :initarg :object)
43    (lambda-list :reader defmacro-bogus-sublist-error-lambda-list
44                 :initarg :lambda-list))
45   (:report
46    (lambda (condition stream)
47      (print-defmacro-ll-bind-error-intro condition stream)
48      (format stream
49              "bogus sublist:~%  ~S~%to satisfy lambda-list:~%  ~:S~%"
50              (defmacro-bogus-sublist-error-object condition)
51              (defmacro-bogus-sublist-error-lambda-list condition)))))
52
53 (define-condition arg-count-error (defmacro-lambda-list-bind-error)
54   ((argument :reader arg-count-error-argument :initarg :argument)
55    (lambda-list :reader arg-count-error-lambda-list
56                 :initarg :lambda-list)
57    (minimum :reader arg-count-error-minimum :initarg :minimum)
58    (maximum :reader arg-count-error-maximum :initarg :maximum))
59   (:report
60    (lambda (condition stream)
61      (print-defmacro-ll-bind-error-intro condition stream)
62      (format stream
63              "invalid number of elements in:~%  ~:S~%~
64              to satisfy lambda list:~%  ~:S~%"
65              (arg-count-error-argument condition)
66              (arg-count-error-lambda-list condition))
67      (cond ((null (arg-count-error-maximum condition))
68             (format stream "at least ~W expected"
69                     (arg-count-error-minimum condition)))
70            ((= (arg-count-error-minimum condition)
71                (arg-count-error-maximum condition))
72             (format stream "exactly ~W expected"
73                     (arg-count-error-minimum condition)))
74            (t
75             (format stream "between ~W and ~W expected"
76                     (arg-count-error-minimum condition)
77                     (arg-count-error-maximum condition))))
78      (format stream ", but ~W found"
79              (length (arg-count-error-argument condition))))))
80
81 (define-condition defmacro-ll-broken-key-list-error
82                   (defmacro-lambda-list-bind-error)
83   ((problem :reader defmacro-ll-broken-key-list-error-problem
84             :initarg :problem)
85    (info :reader defmacro-ll-broken-key-list-error-info :initarg :info))
86   (:report (lambda (condition stream)
87              (print-defmacro-ll-bind-error-intro condition stream)
88              (format stream
89                      ;; FIXME: These should probably just be three
90                      ;; subclasses of the base class, so that we don't
91                      ;; need to maintain the set of tags both here and
92                      ;; implicitly wherever this macro is used.
93                      (ecase
94                          (defmacro-ll-broken-key-list-error-problem condition)
95                        (:dotted-list
96                         "dotted keyword/value list: ~S")
97                        (:odd-length
98                         "odd number of elements in keyword/value list: ~S")
99                        (:unknown-keyword
100                         "~{unknown keyword: ~S; expected one of ~{~S~^, ~}~}"))
101                      (defmacro-ll-broken-key-list-error-info condition)))))