0.9.5.45: COMPUTE-RESTARTS with :TEST & no condition
[sbcl.git] / src / code / target-error.lisp
1 ;;;; that part of the condition system which can or should come early
2 ;;;; (mostly macro-related)
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!KERNEL")
14 \f
15 ;;; a list of lists of restarts
16 (defvar *restart-clusters* '())
17
18 ;;; an ALIST (condition . restarts) which records the restarts currently
19 ;;; associated with Condition
20 (defvar *condition-restarts* ())
21
22 (defvar *handler-clusters* nil)
23
24 (defstruct (restart (:copier nil) (:predicate nil))
25   (name (missing-arg) :type symbol :read-only t)
26   (function (missing-arg) :type function)
27   (report-function nil :type (or null function))
28   (interactive-function nil :type (or null function))
29   (test-function (lambda (cond) (declare (ignore cond)) t) :type function))
30 (def!method print-object ((restart restart) stream)
31   (if *print-escape*
32       (print-unreadable-object (restart stream :type t :identity t)
33         (prin1 (restart-name restart) stream))
34       (restart-report restart stream)))
35
36 (defun compute-restarts (&optional condition)
37   #!+sb-doc
38   "Return a list of all the currently active restarts ordered from most
39    recently established to less recently established. If CONDITION is
40    specified, then only restarts associated with CONDITION (or with no
41    condition) will be returned."
42   (let ((associated ())
43         (other ()))
44     (dolist (alist *condition-restarts*)
45       (if (eq (car alist) condition)
46           (setq associated (cdr alist))
47           (setq other (append (cdr alist) other))))
48     (collect ((res))
49       (dolist (restart-cluster *restart-clusters*)
50         (dolist (restart restart-cluster)
51           (when (and (or (not condition)
52                          (member restart associated)
53                          (not (member restart other)))
54                      (or (not condition)
55                          (funcall (restart-test-function restart)
56                                   condition)))
57             (res restart))))
58       (res))))
59
60 #!+sb-doc
61 (setf (fdocumentation 'restart-name 'function)
62       "Return the name of the given restart object.")
63
64 (defun restart-report (restart stream)
65   (funcall (or (restart-report-function restart)
66                (let ((name (restart-name restart)))
67                  (lambda (stream)
68                    (if name (format stream "~S" name)
69                        (format stream "~S" restart)))))
70            stream))
71
72 (defun find-restart (name &optional condition)
73   #!+sb-doc
74   "Return the first restart named NAME. If NAME names a restart, the restart
75    is returned if it is currently active. If no such restart is found, NIL is
76    returned. It is an error to supply NIL as a name. If CONDITION is specified
77    and not NIL, then only restarts associated with that condition (or with no
78    condition) will be returned."
79   (let ((restarts (compute-restarts condition)))
80     (declare (type list restarts))
81     (find-if (lambda (x)
82                (or (eq x name)
83                    (eq (restart-name x) name)))
84              restarts)))
85
86 ;;; helper for the various functions which are ANSI-spec'ed to do
87 ;;; something with a restart or signal CONTROL-ERROR if there is none
88 (defun find-restart-or-control-error (identifier &optional condition)
89   (or (find-restart identifier condition)
90       (error 'simple-control-error
91              :format-control "No restart ~S is active~@[ for ~S~]."
92              :format-arguments (list identifier condition))))
93
94 (defun invoke-restart (restart &rest values)
95   #!+sb-doc
96   "Calls the function associated with the given restart, passing any given
97    arguments. If the argument restart is not a restart or a currently active
98    non-nil restart name, then a CONTROL-ERROR is signalled."
99   (/show "entering INVOKE-RESTART" restart)
100   (let ((real-restart (find-restart-or-control-error restart)))
101     (apply (restart-function real-restart) values)))
102
103 (defun interactive-restart-arguments (real-restart)
104   (let ((interactive-function (restart-interactive-function real-restart)))
105     (if interactive-function
106         (funcall interactive-function)
107         '())))
108
109 (defun invoke-restart-interactively (restart)
110   #!+sb-doc
111   "Calls the function associated with the given restart, prompting for any
112    necessary arguments. If the argument restart is not a restart or a
113    currently active non-NIL restart name, then a CONTROL-ERROR is signalled."
114   (let* ((real-restart (find-restart-or-control-error restart))
115          (args (interactive-restart-arguments real-restart)))
116     (apply (restart-function real-restart) args)))
117 \f
118 (defun assert-error (assertion places datum &rest arguments)
119   (let ((cond (if datum
120                 (coerce-to-condition datum
121                                                     arguments
122                                                     'simple-error
123                                                     'error)
124                 (make-condition 'simple-error
125                                 :format-control "The assertion ~S failed."
126                                 :format-arguments (list assertion)))))
127     (restart-case
128         (error cond)
129       (continue ()
130                 :report (lambda (stream)
131                           (format stream "Retry assertion")
132                           (if places
133                               (format stream
134                                       " with new value~P for ~{~S~^, ~}."
135                                       (length places)
136                                       places)
137                               (format stream ".")))
138                 nil))))
139
140 ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
141 ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
142 ;;; and by CHECK-TYPE.
143 (defun read-evaluated-form ()
144   (format *query-io* "~&Type a form to be evaluated:~%")
145   (list (eval (read *query-io*))))
146
147 (defun check-type-error (place place-value type type-string)
148   (let ((condition
149          (make-condition
150           'simple-type-error
151           :datum place-value
152           :expected-type type
153           :format-control
154           "The value of ~S is ~S, which is not ~:[of type ~S~;~:*~A~]."
155           :format-arguments (list place place-value type-string type))))
156     (restart-case (error condition)
157       (store-value (value)
158         :report (lambda (stream)
159                   (format stream "Supply a new value for ~S." place))
160         :interactive read-evaluated-form
161         value))))
162
163 (defun case-body-error (name keyform keyform-value expected-type keys)
164   (restart-case
165       (error 'case-failure
166              :name name
167              :datum keyform-value
168              :expected-type expected-type
169              :possibilities keys)
170     (store-value (value)
171       :report (lambda (stream)
172                 (format stream "Supply a new value for ~S." keyform))
173       :interactive read-evaluated-form
174       value)))