]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/ctm/conditions.lisp
Implement CTM Templates with contextual argument binding
[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 (define-condition ctm-rejection (ctm-error) ())
19 (define-condition undefined-prefix (ctm-rejection) ())
20 (define-condition prefix-collision (ctm-rejection) ())
21 (define-condition ctm-unsupported (ctm-error) ())
22
23 (define-condition template-error (ctm-rejection) ())
24 (define-condition duplicate-template (template-error) ())
25 (define-condition undefined-template (template-error) ())
26 (define-condition template-arity-error (template-error) ())
27 (define-condition unbound-template-variable (template-error) ())
28 (define-condition invalid-template-argument (template-error) ())
29 (define-condition invalid-template-body (template-error) ())
30 (defvar *template-context* nil)
31 (defvar *source-name* nil)
32 (defvar *source-text* "")
33
34 (defun problem (type code position message &optional (unit :character))
35 (let ((line (when (eq unit :character) 1)) (column (when (eq unit :character) 1))
36 (previous-cr nil))
37 (when line
38 (loop for c across (subseq *source-text* 0 (min position (length *source-text*))) do
39 (cond ((char= c #\Return) (incf line) (setf column 1 previous-cr t))
40 ((char= c #\Newline) (unless previous-cr (incf line))
41 (setf column 1 previous-cr nil))
42 (t (incf column) (setf previous-cr nil)))))
43 (error type :code code :message message :position position :line line
44 :column column :source *source-name* :position-unit unit :context (copy-tree *template-context*))))