]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/ctm/conditions.lisp
Provide pinned CXML sources and record failed XML qualification
[topic-maps.git] / src / ctm / conditions.lisp
1 (in-package #:topic-maps.ctm)
2
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 (member (getf frame :purpose) '(:mergemap :include))
20 (format stream "~% via %~(~A~) in ~A at character ~D -> ~A~@[ (source ~A)~]"
21 (getf frame :purpose)
22 (or (getf frame :source) (getf frame :document-iri)) (getf frame :position)
23 (getf frame :resource-iri) (getf frame :resource-source)))))))
24 (define-condition ctm-rejection (ctm-error) ())
25 (define-condition undefined-prefix (ctm-rejection) ())
26 (define-condition prefix-collision (ctm-rejection) ())
27 (define-condition ctm-unsupported (ctm-error) ())
28 ;; Retrieval/configuration failures are not evidence of malformed CTM.
29 (define-condition ctm-resource-error (ctm-error) ())
30 (defvar *resource-context* nil)
31
32 (define-condition template-error (ctm-rejection) ())
33 (define-condition duplicate-template (template-error) ())
34 (define-condition undefined-template (template-error) ())
35 (define-condition template-arity-error (template-error) ())
36 (define-condition unbound-template-variable (template-error) ())
37 (define-condition invalid-template-argument (template-error) ())
38 (define-condition invalid-template-body (template-error) ())
39 (defvar *template-context* nil)
40 (defvar *source-name* nil)
41 (defvar *source-text* "")
42
43 (defun problem (type code position message &optional (unit :character))
44 (let ((line (when (eq unit :character) 1)) (column (when (eq unit :character) 1))
45 (previous-cr nil))
46 (when line
47 (loop for c across (subseq *source-text* 0 (min position (length *source-text*))) do
48 (cond ((char= c #\Return) (incf line) (setf column 1 previous-cr t))
49 ((char= c #\Newline) (unless previous-cr (incf line))
50 (setf column 1 previous-cr nil))
51 (t (incf column) (setf previous-cr nil)))))
52 (error type :code code :message message :position position :line line
53 :column column :source *source-name* :position-unit unit :context (copy-tree (append *template-context* *resource-context*)))))