Debian Codegraph @

comlib/error.lisp

raw
1 ;;;-*- Mode:LISP; Package:CHAOS; Base:10; Syntax:Common-lisp -*-
2 ;;;
3 ;;; Copyright (c) 2000-2015, Toshimi Sawada. All rights reserved.
4 ;;;
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions
7 ;;; are met:
8 ;;;
9 ;;; * Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
11 ;;;
12 ;;; * Redistributions in binary form must reproduce the above
13 ;;; copyright notice, this list of conditions and the following
14 ;;; disclaimer in the documentation and/or other materials
15 ;;; provided with the distribution.
16 ;;;
17 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
18 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ;;;
29 (in-package :chaos)
30 #|==============================================================================
31 System: CHAOS
32 Module: comlib
33 File: error.lisp
34 ==============================================================================|#
35 #-:chaos-debug
36 (declaim (optimize (speed 3) (safety 0) #-GCL (debug 0)))
37 #+:chaos-debug
38 (declaim (optimize (speed 1) (safety 3) #-GCL (debug 3)))
39
40 ;;; *********************
41 ;;; STANDRD ERROR HANDLER
42 ;;; *********************
43
44 ;;; - catches tag 'chaos-main-error
45 ;;;
46 ;;; << IMPORTANT ASSUMPTION >>
47 ;;; - every chaos AST evaluators report errors by themselves
48 ;;; and then call 'chaos-error with non-nil value.
49 ;;; << SHOULD >>
50 ;;; - users should do anything with-in a body of `with-chaos-error' or
51 ;;; catch 'chaos-main-error by themselves.
52 ;;;
53
54 (declaim (special *suppress-err-handler-msg*))
55 (defvar *suppress-err-handler-msg* nil)
56
57 ;;; SIGNAL STANDRD ERROR
58 ;;;
59 (defun chaos-error (err-value)
60 (flush-all)
61 (throw 'chaos-main-error err-value))
62
63 ;;; HANDLER
64 ;;;
65 (defun chaos-exit-with-error-code (value)
66 (let ((exit-status 1))
67 (if (symbolp value)
68 (format t "~%** Exiting CafeOBJ due to ~s~%" value)
69 (setq exit-status 2))
70 #+:sbcl (sb-ext:exit :code exit-status)
71 #+:allegro (excl:exit exit-status)
72 #-(or :sbcl :allegro) (bye-bye-bye)
73 ))
74
75 (defun get-chaos-error-proc (val)
76 (if *cafeobj-batch*
77 'chaos-exit-with-error-code
78 (if (symbolp val)
79 (get val ':chaos-error-handler)
80 nil)))
81
82 (defmacro with-chaos-error ((&optional error-proc) &body body)
83 (if error-proc
84 `(let ((ret-val nil))
85 (let ((val (catch 'chaos-main-error
86 (setq ret-val
87 (progn ,@body))
88 nil)))
89 (if val
90 (funcall ,error-proc val)
91 ret-val)))
92 `(let ((ret-val nil))
93 (let ((val (catch 'chaos-main-error
94 (setq ret-val
95 (progn ,@body))
96 nil)))
97 (if val
98 (let ((std-proc (get-chaos-error-proc val)))
99 (if std-proc
100 (funcall std-proc val)
101 (chaos-to-top)))
102 ret-val)))))
103
104 (defun chaos-indicate-position ()
105 (unless *suppress-err-handler-msg*
106 (when *chaos-input-source* ; nil means may be from terminal
107 (format t "~%filename: ~a" (namestring *chaos-input-source*))
108 (when (file-position *standard-input*)
109 (format t " in top-level form ending at character position: ~d"
110 (file-position *standard-input*)))
111 (terpri))))
112
113 (defun chaos-to-top (&rest ignore)
114 (declare (ignore ignore))
115 (fresh-line)
116 (chaos-indicate-position)
117 (unless *suppress-err-handler-msg*
118 (format t "~%** returning to top level~%"))
119 (throw 'chaos-top-level-error t))
120
121 (defmacro with-chaos-top-error ((&optional error-proc) &body body)
122 (if error-proc
123 `(let ((ret-val nil))
124 (let ((val (catch 'chaos-top-level-error
125 (setq ret-val
126 (progn ,@body))
127 nil)))
128 (if val
129 (funcall ,error-proc val)
130 ret-val)))
131 `(let ((ret-val nil))
132 (let ((val (catch 'chaos-top-level-error
133 (setq ret-val
134 (progn ,@body))
135 nil)))
136 (if val
137 (let ((std-proc (get-chaos-error-proc val)))
138 (if std-proc
139 (funcall std-proc val)
140 ;; we assume no more error handlers.
141 nil))
142 ret-val)))))
143
144 (defmacro ignoring-chaos-error (&body body)
145 ` (catch 'chaos-top-level-error
146 (catch 'chaos-main-error
147 ,@body)))
148 ;;; EOF