1 ;;;; that part of the condition system which can or should come early
2 ;;;; (mostly macro-related)
4 ;;;; This software is part of the SBCL system. See the README file for
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.
13 (in-package "SB!KERNEL")
15 ;;; a list of lists of restarts
16 (defvar *restart-clusters
* '())
18 ;;; an ALIST (condition . restarts) which records the restarts currently
19 ;;; associated with Condition
20 (defvar *condition-restarts
* ())
22 (defvar *handler-clusters
* nil
)
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
)
32 (print-unreadable-object (restart stream
:type t
:identity t
)
33 (prin1 (restart-name restart
) stream
))
34 (restart-report restart stream
)))
36 (defun compute-restarts (&optional condition
)
38 "Return a list of all the currently active restarts ordered from most recently
39 established to less recently established. If CONDITION is specified, then only
40 restarts associated with CONDITION (or with no condition) will be returned."
43 (dolist (alist *condition-restarts
*)
44 (if (eq (car alist
) condition
)
45 (setq associated
(cdr alist
))
46 (setq other
(append (cdr alist
) other
))))
48 (dolist (restart-cluster *restart-clusters
*)
49 (dolist (restart restart-cluster
)
50 (when (and (or (not condition
)
51 (member restart associated
)
52 (not (member restart other
)))
53 (funcall (restart-test-function restart
) condition
))
58 (setf (fdocumentation 'restart-name
'function
)
59 "Return the name of the given restart object.")
61 (defun restart-report (restart stream
)
62 (funcall (or (restart-report-function restart
)
63 (let ((name (restart-name restart
)))
65 (if name
(format stream
"~S" name
)
66 (format stream
"~S" restart
)))))
69 (defun find-restart (identifier &optional condition
)
71 "Return the first restart identified by IDENTIFIER. If IDENTIFIER is a symbol,
72 then the innermost applicable restart with that name is returned. If IDENTIFIER
73 is a restart, it is returned if it is currently active. Otherwise NIL is
74 returned. If CONDITION is specified and not NIL, then only restarts associated
75 with that condition (or with no condition) will be returned."
77 (if (typep identifier
'restart
)
78 (and (find-if (lambda (cluster) (find identifier cluster
)) *restart-clusters
*)
80 (find identifier
(compute-restarts condition
) :key
#'restart-name
)))
82 ;;; helper for the various functions which are ANSI-spec'ed to do
83 ;;; something with a restart or signal CONTROL-ERROR if there is none
84 (defun find-restart-or-control-error (identifier &optional condition
)
85 (or (find-restart identifier condition
)
86 (error 'simple-control-error
87 :format-control
"No restart ~S is active~@[ for ~S~]."
88 :format-arguments
(list identifier condition
))))
90 (defun invoke-restart (restart &rest values
)
92 "Calls the function associated with the given restart, passing any given
93 arguments. If the argument restart is not a restart or a currently active
94 non-nil restart name, then a CONTROL-ERROR is signalled."
95 (/show
"entering INVOKE-RESTART" restart
)
96 (let ((real-restart (find-restart-or-control-error restart
)))
97 (apply (restart-function real-restart
) values
)))
99 (defun interactive-restart-arguments (real-restart)
100 (let ((interactive-function (restart-interactive-function real-restart
)))
101 (if interactive-function
102 (funcall interactive-function
)
105 (defun invoke-restart-interactively (restart)
107 "Calls the function associated with the given restart, prompting for any
108 necessary arguments. If the argument restart is not a restart or a
109 currently active non-NIL restart name, then a CONTROL-ERROR is signalled."
110 (let* ((real-restart (find-restart-or-control-error restart
))
111 (args (interactive-restart-arguments real-restart
)))
112 (apply (restart-function real-restart
) args
)))
114 (defun assert-error (assertion places datum
&rest arguments
)
115 (let ((cond (if datum
116 (coerce-to-condition datum
120 (make-condition 'simple-error
121 :format-control
"The assertion ~S failed."
122 :format-arguments
(list assertion
)))))
126 :report
(lambda (stream)
127 (format stream
"Retry assertion")
130 " with new value~P for ~{~S~^, ~}."
133 (format stream
".")))
136 ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
137 ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
138 ;;; and by CHECK-TYPE.
139 (defun read-evaluated-form ()
140 (format *query-io
* "~&Type a form to be evaluated:~%")
141 (list (eval (read *query-io
*))))
143 (defun check-type-error (place place-value type type-string
)
150 "The value of ~S is ~S, which is not ~:[of type ~S~;~:*~A~]."
151 :format-arguments
(list place place-value type-string type
))))
152 (restart-case (error condition
)
154 :report
(lambda (stream)
155 (format stream
"Supply a new value for ~S." place
))
156 :interactive read-evaluated-form
159 (defun case-body-error (name keyform keyform-value expected-type keys
)
164 :expected-type expected-type
167 :report
(lambda (stream)
168 (format stream
"Supply a new value for ~S." keyform
))
169 :interactive read-evaluated-form