1 (in-package #:topic-maps.ctm)
3 (define-condition ctm-error (error)
4 ((code :initarg :code :reader error-code)
5 (message :initarg :message :reader error-message)
6 (position :initarg :position :reader error-position)
7 (line :initarg :line :reader error-line)
8 (column :initarg :column :reader error-column)
9 (source :initarg :source :reader error-source)
10 (position-unit :initarg :position-unit :initform :character :reader error-position-unit)
11 (context :initarg :context :initform nil :reader error-context))
12 (:report (lambda (condition stream)
13 (format stream "~A~@[ in ~A~] at ~A ~D~@[ (line ~D~]~@[, column ~D)~]: ~A"
14 (error-code condition) (error-source condition)
15 (error-position-unit condition) (error-position condition)
16 (error-line condition) (error-column condition)
17 (error-message condition))
18 (dolist (frame (reverse (error-context condition)))
19 (when (eq :mergemap (getf frame :purpose))
20 (format stream "~% via %mergemap in ~A at character ~D -> ~A~@[ (source ~A)~]"
21 (or (getf frame :source) (getf frame :document-iri)) (getf frame :position)
22 (getf frame :resource-iri) (getf frame :resource-source)))))))
23 (define-condition ctm-rejection (ctm-error) ())
24 (define-condition undefined-prefix (ctm-rejection) ())
25 (define-condition prefix-collision (ctm-rejection) ())
26 (define-condition ctm-unsupported (ctm-error) ())
27 ;; Retrieval/configuration failures are not evidence of malformed CTM.
28 (define-condition ctm-resource-error (ctm-error) ())
29 (defvar *resource-context* nil)
31 (define-condition template-error (ctm-rejection) ())
32 (define-condition duplicate-template (template-error) ())
33 (define-condition undefined-template (template-error) ())
34 (define-condition template-arity-error (template-error) ())
35 (define-condition unbound-template-variable (template-error) ())
36 (define-condition invalid-template-argument (template-error) ())
37 (define-condition invalid-template-body (template-error) ())
38 (defvar *template-context* nil)
39 (defvar *source-name* nil)
40 (defvar *source-text* "")
42 (defun problem (type code position message &optional (unit :character))
43 (let ((line (when (eq unit :character) 1)) (column (when (eq unit :character) 1))
46 (loop for c across (subseq *source-text* 0 (min position (length *source-text*))) do
47 (cond ((char= c #\Return) (incf line) (setf column 1 previous-cr t))
48 ((char= c #\Newline) (unless previous-cr (incf line))
49 (setf column 1 previous-cr nil))
50 (t (incf column) (setf previous-cr nil)))))
51 (error type :code code :message message :position position :line line
52 :column column :source *source-name* :position-unit unit :context (copy-tree (append *template-context* *resource-context*)))))