]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/ctm/reader.lisp
Implement Topic-set Scope across TMDM, CTM and CXTM
[topic-maps.git] / src / ctm / reader.lisp
1 (in-package #:topic-maps.ctm)
2
3 (defstruct (parser (:constructor make-parser (lexer document-iri)))
4 lexer document-iri lookahead (prefixes (make-hash-table :test #'equal)))
5 (defun look (parser)
6 (or (parser-lookahead parser)
7 (setf (parser-lookahead parser) (next-token (parser-lexer parser)))))
8 (defun consume (parser)
9 (prog1 (look parser) (setf (parser-lookahead parser) nil)))
10 (defun punctuation-p (parser character)
11 (and (eq :punctuation (token-kind (look parser)))
12 (eql character (token-value (look parser)))))
13
14 (defun resolve-qname (parser token)
15 "CTM 3.3.4: concatenate the resolved binding and local part exactly once."
16 (let* ((text (token-value token)) (colon (position #\: text))
17 (prefix (subseq text 0 colon)) (bound (gethash prefix (parser-prefixes parser))))
18 (unless bound
19 (problem 'undefined-prefix :undefined-prefix (token-position token)
20 (format nil "Prefix ~S has no binding." prefix)))
21 ;; Do not re-resolve or percent-decode an already resolved binding.
22 (validate-iri-characters (concatenate 'string bound (subseq text (1+ colon)))
23 (token-position token))))
24
25 (defun read-prefix-directive (parser)
26 "CTM 3.16.1: document-ordered, per-deserialization bindings; reference is an IRI."
27 (consume parser) ; %prefix
28 (let ((name (consume parser)) (reference (consume parser)))
29 (unless (eq :identifier (token-kind name))
30 (problem 'ctm-rejection :expected-prefix-name (token-position name) "Expected a prefix identifier."))
31 (unless (eq :iri (token-kind reference))
32 (problem 'ctm-rejection :expected-prefix-iri (token-position reference)
33 "A prefix binding requires an IRI, not a QName or Topic identifier."))
34 (let* ((iri (resolve-iri (token-value reference) (parser-document-iri parser) (token-position reference)))
35 (old (gethash (token-value name) (parser-prefixes parser))))
36 (when (and old (not (string= old iri)))
37 (problem 'prefix-collision :prefix-collision (token-position name)
38 (format nil "Prefix ~S is already bound to ~S, not ~S." (token-value name) old iri)))
39 (setf (gethash (token-value name) (parser-prefixes parser)) iri))))
40
41 (defun identity-value (parser &optional explicit-kind)
42 (let ((token (consume parser)))
43 (case (token-kind token)
44 (:identifier
45 (when explicit-kind
46 (problem 'ctm-rejection :expected-iri (token-position token) "Explicit = or ^ needs an IRI reference."))
47 (when (member (token-value token) '("def" "isa" "ako" "end") :test #'string=)
48 (problem 'ctm-unsupported :unsupported-keyword (token-position token) "This keyword is outside the implemented subset."))
49 (values :item-identifier
50 (concatenate 'string (parser-document-iri parser) "#" (token-value token))))
51 (:iri
52 (values (or explicit-kind :subject-identifier)
53 (resolve-iri (token-value token) (parser-document-iri parser) (token-position token))))
54 (:qname (values (or explicit-kind :subject-identifier) (resolve-qname parser token)))
55 (:punctuation
56 (case (token-value token)
57 (#\= (if explicit-kind
58 (problem 'ctm-rejection :expected-iri (token-position token) "Unexpected identity marker.")
59 (identity-value parser :subject-locator)))
60 (#\^ (if explicit-kind
61 (problem 'ctm-rejection :expected-iri (token-position token) "Unexpected identity marker.")
62 (identity-value parser :item-identifier)))
63 (otherwise (problem 'ctm-rejection :expected-topic-identity (token-position token) "Expected topic identity."))))
64 (otherwise (problem 'ctm-rejection :expected-topic-identity (token-position token) "Expected topic identity.")))))
65
66 (defun identity-reference (parser map)
67 (multiple-value-bind (kind iri) (identity-value parser)
68 (topic-maps.tmdm:intern-topic map kind iri)))
69
70 (defun read-scope (parser map)
71 "CTM 3.3.9: optional @ followed by a nonempty comma-separated Topic set.
72 Use the existing identity resolver; embedded/wildcard Topics remain unsupported.
73 The owning information item is not constructed until this function finishes."
74 (when (punctuation-p parser #\@)
75 (consume parser)
76 (let ((themes nil))
77 (loop
78 ;; A reifier marker where a Theme is required is malformed Scope,
79 ;; even though reification after a complete Scope remains unsupported.
80 (unless (parser-lookahead parser)
81 (skip-layout (parser-lexer parser))
82 (when (eql #\~ (peek-character (parser-lexer parser)))
83 (problem 'ctm-rejection :expected-scope-theme (lexer-cursor (parser-lexer parser))
84 "Expected a Theme, not a reifier marker.")))
85 (let ((token (look parser)))
86 (unless (or (member (token-kind token) '(:identifier :iri :qname))
87 (and (eq :punctuation (token-kind token)) (find (token-value token) "=^")))
88 (problem 'ctm-rejection :expected-scope-theme (token-position token)
89 "Expected a Topic reference after '@' or the scope comma."))
90 (push (identity-reference parser map) themes))
91 (if (punctuation-p parser #\,) (consume parser) (return (nreverse themes)))))))
92
93 (defun literal-iri-reference (parser)
94 "An IRI locator, not a Topic identity. Used for values/datatype syntax."
95 (let ((token (consume parser)))
96 (case (token-kind token)
97 (:iri (resolve-iri (token-value token) (parser-document-iri parser) (token-position token)))
98 (:qname (resolve-qname parser token))
99 (otherwise (problem 'ctm-rejection :expected-datatype-iri (token-position token)
100 "A datatype qualifier requires an IRI reference, not a Topic identifier.")))))
101
102 (defun read-occurrence-literal (parser)
103 (let ((token (look parser)))
104 (case (token-kind token)
105 (:integer
106 (consume parser)
107 ;; Numeric-literal value policy: represent the unquoted integer value
108 ;; canonically; qualified quoted strings retain their lexical value.
109 (values (topic-maps.tmdm:canonical-integer-lexical-form (token-value token))
110 (topic-maps.tmdm:integer-datatype)))
111 (:decimal
112 (consume parser)
113 ;; Same value-representation policy as bare integers; token spelling is
114 ;; retained by the lexer, qualified strings remain lexical model data.
115 (values (topic-maps.tmdm:canonical-decimal-lexical-form (token-value token))
116 (topic-maps.tmdm:decimal-datatype)))
117 (:date
118 (consume parser)
119 (values (topic-maps.tmdm:canonical-date-lexical-form (token-value token)) (topic-maps.tmdm:date-datatype)))
120 (:datetime
121 (consume parser)
122 (multiple-value-bind (value reason) (topic-maps.tmdm:canonical-datetime-lexical-form (token-value token))
123 (unless value (problem 'ctm-unsupported
124 (if (eq reason :unsupported-bce-timezone-rollover)
125 :datetime-bce-timezone-rollover :datetime-leap-second-context)
126 (token-position token) "DateTime normalization is outside the supported BCE/leap-second calendar context."))
127 (values value (topic-maps.tmdm:datetime-datatype))))
128 (:excluded-literal
129 (problem 'ctm-unsupported :occurrence-literal-datatype (token-position token)
130 "The separate CTM '*' literal is outside this slice."))
131 (:string
132 (consume parser)
133 (if (punctuation-p parser #\^)
134 (let ((caret (consume parser)))
135 (unless (and (punctuation-p parser #\^)
136 (= (token-position (look parser)) (1+ (token-position caret))))
137 (problem 'ctm-rejection :invalid-datatype-marker (token-position caret) "Expected contiguous '^^'."))
138 (consume parser)
139 (let ((datatype (literal-iri-reference parser)))
140 (when (and (string= datatype (topic-maps.tmdm:integer-datatype))
141 (not (topic-maps.tmdm:integer-lexical-p (token-value token))))
142 (problem 'ctm-rejection :invalid-integer-value (token-position token)
143 "Quoted value is not an xsd:integer lexical form."))
144 (when (and (string= datatype (topic-maps.tmdm:decimal-datatype))
145 (not (topic-maps.tmdm:decimal-lexical-p (token-value token))))
146 (problem 'ctm-rejection :invalid-decimal-value (token-position token)
147 "Quoted value is not an xsd:decimal lexical form."))
148 (when (and (string= datatype (topic-maps.tmdm:date-datatype))
149 (not (topic-maps.tmdm:date-lexical-p (token-value token))))
150 (problem 'ctm-rejection :invalid-date-value (token-position token)
151 "Quoted value is not a valid XSD 1.0 date lexical form."))
152 (when (and (string= datatype (topic-maps.tmdm:datetime-datatype))
153 (not (topic-maps.tmdm:datetime-lexical-p (token-value token))))
154 (problem 'ctm-rejection :invalid-datetime-value (token-position token)
155 "Quoted value is not a valid XSD 1.0 dateTime."))
156 ;; TMDM 4.4 requires absolute anyURI content; quoted content is
157 ;; lexical data, never an IRI reference to resolve against the document.
158 (when (and (string= datatype (topic-maps.tmdm:iri-datatype))
159 (not (topic-maps.tmdm::absolute-locator-p (token-value token))))
160 (problem 'ctm-rejection :relative-uri-value (token-position token)
161 "A quoted anyURI value must already be absolute (TMDM 4.4)."))
162 (values (token-value token) datatype)))
163 (values (token-value token) (topic-maps.tmdm:string-datatype))))
164 ((:iri :qname) (values (literal-iri-reference parser) (topic-maps.tmdm:iri-datatype)))
165 (otherwise (problem 'ctm-rejection :expected-occurrence-value (token-position token)
166 "Expected an integer, decimal, date, dateTime, quoted string or IRI-form Occurrence value.")))))
167
168 (defun read-identity-tail (parser map topic)
169 (let ((start (look parser)))
170 (multiple-value-bind (kind iri) (identity-value parser)
171 (if (punctuation-p parser #\:)
172 (progn
173 (consume parser)
174 (multiple-value-bind (value datatype) (read-occurrence-literal parser)
175 (let* ((type (topic-maps.tmdm:intern-topic map kind iri))
176 (scope (read-scope parser map)))
177 (topic-maps.tmdm:add-occurrence topic type value :datatype datatype :scope scope)
178 (topic-maps.tmdm:canonical-topic topic))))
179 (if (eq :identifier (token-kind start))
180 (if (punctuation-p parser #\()
181 (problem 'ctm-unsupported :topic-tail (token-position start) "Template invocations are outside this subset.")
182 (problem 'ctm-rejection :expected-occurrence-type-separator (token-position (look parser))
183 "Expected ':' after the Occurrence Type."))
184 (topic-maps.tmdm:add-topic-identifier topic kind iri))))))
185
186 (defun read-name (parser map topic)
187 "CTM 3.11: optional Topic reference plus ':' followed by a string.
188 Use the existing identity resolver; embedded Topics remain unsupported."
189 (let* ((token (look parser))
190 (typed (or (member (token-kind token) '(:identifier :iri :qname))
191 (and (eq :punctuation (token-kind token))
192 (find (token-value token) "=^"))))
193 (type (when typed (identity-reference parser map))))
194 (when typed
195 (unless (punctuation-p parser #\:)
196 (problem 'ctm-rejection :expected-name-type-separator
197 (token-position (look parser)) "Expected ':' after the name type."))
198 (consume parser))
199 (let ((value (consume parser)))
200 (unless (eq :string (token-kind value))
201 (problem 'ctm-rejection :expected-name-value (token-position value)
202 "Expected a quoted name value after '-' or the name type separator."))
203 (let ((scope (read-scope parser map)))
204 (if typed
205 (topic-maps.tmdm:add-name topic (token-value value) :type type :scope scope)
206 (topic-maps.tmdm:add-name topic (token-value value) :scope scope))))
207 (topic-maps.tmdm:canonical-topic topic)))
208
209 (defun read-topic (parser map &optional (topic (identity-reference parser map)))
210 (let ((has-tail nil))
211 (loop
212 (cond
213 ((punctuation-p parser #\.) (consume parser) (return topic))
214 ((punctuation-p parser #\-)
215 (when has-tail
216 (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
217 (consume parser)
218 (setf topic (read-name parser map topic))
219 (setf has-tail t))
220 ((and has-tail (punctuation-p parser #\;))
221 (consume parser)
222 (setf has-tail nil)
223 (when (punctuation-p parser #\.) (consume parser) (return topic)))
224 ((eq :eof (token-kind (look parser)))
225 (problem 'ctm-rejection :missing-period (token-position (look parser)) "Topic declaration requires a final period."))
226 ((or (and (eq :punctuation (token-kind (look parser)))
227 (find (token-value (look parser)) "=^"))
228 (member (token-kind (look parser)) '(:identifier :iri :qname)))
229 (when has-tail
230 (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
231 (setf topic (read-identity-tail parser map topic) has-tail t))
232 ((punctuation-p parser #\()
233 (problem 'ctm-unsupported :variant (token-position (look parser)) "Variant names are outside the implemented subset."))
234 (t (problem 'ctm-rejection :unexpected-token (token-position (look parser)) "Unexpected token after topic or name."))))))
235
236 (defun read-association (parser map type)
237 "CTM 3.13, restricted to one or two role declarations; optional scope, no reifier."
238 (consume parser) ; opening parenthesis already recognized by the caller
239 (let ((roles nil) (third-position nil))
240 (loop
241 (when (or (punctuation-p parser #\)) (eq :eof (token-kind (look parser))))
242 (problem 'ctm-rejection :expected-role (token-position (look parser)) "Expected a nonempty Association role declaration."))
243 (when (= 2 (length roles)) (setf third-position (token-position (look parser))))
244 (let* ((role-type (identity-reference parser map))
245 (type-end (lexer-cursor (parser-lexer parser)))
246 (colon (look parser)))
247 (unless (punctuation-p parser #\:)
248 (problem 'ctm-rejection :expected-role-type-separator (token-position colon) "Expected ':' after the Role type."))
249 (consume parser)
250 (let ((player-token (look parser)))
251 ;; CTM role production requires layout on at least one side of ':'.
252 ;; skip-layout handles whitespace and comments; gaps use source offsets.
253 (unless (or (> (token-position colon) type-end)
254 (> (token-position player-token) (1+ (token-position colon))))
255 (problem 'ctm-rejection :role-separator-whitespace (token-position colon)
256 "A Role separator needs whitespace or a comment on at least one side."))
257 (when (or (eq :eof (token-kind player-token))
258 (punctuation-p parser #\)) (punctuation-p parser #\,))
259 (problem 'ctm-rejection :expected-role-player (token-position player-token) "Expected a Topic as the Role player.")))
260 (push (list role-type (identity-reference parser map)) roles))
261 (cond ((punctuation-p parser #\,) (consume parser))
262 ((punctuation-p parser #\)) (consume parser) (return))
263 ((eq :eof (token-kind (look parser)))
264 (problem 'ctm-rejection :missing-association-close (token-position (look parser)) "Expected ')' after the Roles."))
265 (t (problem 'ctm-rejection :expected-role-separator (token-position (look parser)) "Expected ',' or ')' after a Role."))))
266 (when (> (length roles) 2)
267 (problem 'ctm-unsupported :association-arity third-position "Associations with more than two Role declarations are outside Slice 3."))
268 (let ((scope (read-scope parser map)))
269 (topic-maps.tmdm:make-association map type (nreverse roles) :scope scope))))
270
271 (defun deserialize-string (text &key document-iri source-name)
272 "Read the documented CTM subset, with per-document prefixes and qualified strings.
273 Source positions are zero-based character offsets; lines/columns are one-based."
274 (check-type text string)
275 (let ((*source-name* source-name) (*source-text* text))
276 (require-document-iri document-iri)
277 (let ((parser (make-parser (make-lexer text) document-iri))
278 (map (topic-maps.tmdm:make-topic-map)))
279 (loop until (eq :eof (token-kind (look parser))) do
280 (if (eq :directive (token-kind (look parser)))
281 (read-prefix-directive parser)
282 (let ((topic (identity-reference parser map)))
283 (if (punctuation-p parser #\()
284 (read-association parser map topic)
285 (read-topic parser map topic)))))
286 map)))
287
288 (defun deserialize-file (pathname &key document-iri source-name)
289 "Read a UTF-8 CTM file, with optional UTF-8 BOM, using an explicit Document IRI.
290 Other encoding directives are UNSUPPORTED. File I/O errors propagate unchanged."
291 (let ((*source-name* source-name) (*source-text* ""))
292 (require-document-iri document-iri)
293 (with-open-file (input pathname :direction :input :element-type '(unsigned-byte 8))
294 (let ((bytes (make-array (file-length input) :element-type '(unsigned-byte 8))))
295 (unless (= (length bytes) (read-sequence bytes input))
296 (error "Input file changed or was truncated during reading."))
297 (deserialize-string (decode-utf8 bytes :strip-bom t)
298 :document-iri document-iri :source-name source-name)))))