]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/ctm/reader.lisp
feat(topic-maps): read CTM topic identities and default names
[topic-maps.git] / src / ctm / reader.lisp
1 (in-package #:topic-maps.ctm)
2
3 (defstruct (parser (:constructor make-parser (lexer document-iri))) lexer document-iri lookahead)
4 (defun look (parser)
5 (or (parser-lookahead parser)
6 (setf (parser-lookahead parser) (next-token (parser-lexer parser)))))
7 (defun consume (parser)
8 (prog1 (look parser) (setf (parser-lookahead parser) nil)))
9 (defun punctuation-p (parser character)
10 (and (eq :punctuation (token-kind (look parser)))
11 (eql character (token-value (look parser)))))
12
13 (defun identity-reference (parser map &optional explicit-kind)
14 (let ((token (consume parser)))
15 (case (token-kind token)
16 (:identifier
17 (when explicit-kind
18 (problem 'ctm-rejection :expected-iri (token-position token) "Explicit = or ^ needs an IRI reference."))
19 (when (member (token-value token) '("def" "isa" "ako" "end") :test #'string=)
20 (problem 'ctm-unsupported :unsupported-keyword (token-position token) "This keyword is outside Slice 1."))
21 (topic-maps.tmdm:intern-topic map :item-identifier
22 (concatenate 'string (parser-document-iri parser) "#" (token-value token))))
23 (:iri
24 (topic-maps.tmdm:intern-topic map (or explicit-kind :subject-identifier)
25 (resolve-iri (token-value token) (parser-document-iri parser) (token-position token))))
26 (:qname
27 ;; No prefix declarations are supported in Slice 1. This is a specific
28 ;; semantic rejection, not an I/O failure or an unsupported-parser stub.
29 (problem 'undefined-prefix :undefined-prefix (token-position token)
30 (format nil "Prefix ~S has no binding." (subseq (token-value token) 0
31 (position #\: (token-value token))))))
32 (:punctuation
33 (case (token-value token)
34 (#\= (if explicit-kind
35 (problem 'ctm-rejection :expected-iri (token-position token) "Unexpected identity marker.")
36 (identity-reference parser map :subject-locator)))
37 (#\^ (if explicit-kind
38 (problem 'ctm-rejection :expected-iri (token-position token) "Unexpected identity marker.")
39 (identity-reference parser map :item-identifier)))
40 (otherwise (problem 'ctm-rejection :expected-topic-identity (token-position token) "Expected topic identity."))))
41 (otherwise (problem 'ctm-rejection :expected-topic-identity (token-position token) "Expected topic identity.")))))
42
43 (defun read-topic (parser map)
44 (let ((topic (identity-reference parser map)) (has-tail nil))
45 (loop
46 (cond
47 ((punctuation-p parser #\.) (consume parser) (return topic))
48 ((punctuation-p parser #\-)
49 (when has-tail
50 (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
51 (consume parser)
52 (let ((value (consume parser)))
53 (unless (eq :string (token-kind value))
54 (problem 'ctm-unsupported :typed-name (token-position value) "Only untyped quoted names are implemented."))
55 (topic-maps.tmdm:add-default-name topic (token-value value)))
56 (setf has-tail t))
57 ((and has-tail (punctuation-p parser #\;))
58 (consume parser)
59 (setf has-tail nil)
60 (when (punctuation-p parser #\.) (consume parser) (return topic)))
61 ((eq :eof (token-kind (look parser)))
62 (problem 'ctm-rejection :missing-period (token-position (look parser)) "Topic declaration requires a final period."))
63 ((and (eq :punctuation (token-kind (look parser)))
64 (find (token-value (look parser)) "=^"))
65 (problem 'ctm-unsupported :additional-identity (token-position (look parser)) "Topic-tail identity assignments are outside Slice 1."))
66 ((member (token-kind (look parser)) '(:iri :qname))
67 (problem 'ctm-unsupported :additional-identity (token-position (look parser)) "Topic-tail identity assignments are outside Slice 1."))
68 ((eq :identifier (token-kind (look parser)))
69 (problem 'ctm-unsupported :topic-tail (token-position (look parser)) "Type/identity/template topic tails are outside Slice 1."))
70 (t (problem 'ctm-rejection :unexpected-token (token-position (look parser)) "Unexpected token after topic or name."))))))
71
72 (defun deserialize-string (text &key document-iri source-name)
73 "Read the documented Slice 1 CTM subset into a new TMDM Topic Map.
74 Source positions are zero-based character offsets; lines/columns are one-based."
75 (check-type text string)
76 (let ((*source-name* source-name) (*source-text* text))
77 (require-document-iri document-iri)
78 (let ((parser (make-parser (make-lexer text) document-iri))
79 (map (topic-maps.tmdm:make-topic-map)))
80 (loop until (eq :eof (token-kind (look parser))) do (read-topic parser map))
81 map)))
82
83 (defun deserialize-file (pathname &key document-iri source-name)
84 "Read a UTF-8 CTM file, with optional UTF-8 BOM, using an explicit Document IRI.
85 Other encoding directives are UNSUPPORTED. File I/O errors propagate unchanged."
86 (let ((*source-name* source-name) (*source-text* ""))
87 (require-document-iri document-iri)
88 (with-open-file (input pathname :direction :input :element-type '(unsigned-byte 8))
89 (let ((bytes (make-array (file-length input) :element-type '(unsigned-byte 8))))
90 (unless (= (length bytes) (read-sequence bytes input))
91 (error "Input file changed or was truncated during reading."))
92 (deserialize-string (decode-utf8 bytes :strip-bom t)
93 :document-iri document-iri :source-name source-name)))))