]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/ctm/reader.lisp
Implement CTM isa and ako with atomic core subject protection
[topic-maps.git] / src / ctm / reader.lisp
1 (in-package #:topic-maps.ctm)
2
3 (defstruct (parser (:constructor make-parser (lexer document-iri wildcard-iri wildcard-counter)))
4 lexer document-iri wildcard-iri wildcard-counter lookahead
5 (wildcards (make-hash-table :test #'equal)) (prefixes (make-hash-table :test #'equal)))
6 (defun look (parser)
7 (or (parser-lookahead parser)
8 (setf (parser-lookahead parser) (next-token (parser-lexer parser)))))
9 (defun consume (parser)
10 (prog1 (look parser) (setf (parser-lookahead parser) nil)))
11 (defun punctuation-p (parser character)
12 (and (eq :punctuation (token-kind (look parser)))
13 (eql character (token-value (look parser)))))
14
15 (defun require-wildcard-environment (iri counter)
16 (handler-case (require-document-iri iri)
17 (ctm-rejection () (problem 'ctm-rejection :invalid-wildcard-iri 0 "Wildcard IRI must be an absolute, fragment-free IRI.")))
18 (unless (and (integerp counter) (>= counter 0))
19 (problem 'ctm-rejection :invalid-wildcard-counter 0 "Wildcard counter must be a non-negative integer.")))
20
21 (defun wildcard-locator (parser &optional name)
22 "CTM 3.3.8: cache locators, not Lisp object handles; identity uses INTERN-TOPIC."
23 (or (and name (gethash name (parser-wildcards parser)))
24 (let ((iri (format nil "~A#$__~D~@[.~A~]" (parser-wildcard-iri parser)
25 (incf (parser-wildcard-counter parser)) name)))
26 (when name (setf (gethash name (parser-wildcards parser)) iri)) iri)))
27
28 (defun topic-reference-start-p (token)
29 (or (member (token-kind token) '(:identifier :iri :qname :wildcard))
30 (and (eq :punctuation (token-kind token)) (find (token-value token) "=^["))))
31
32 (defun resolve-qname (parser token)
33 "CTM 3.3.4: concatenate the resolved binding and local part exactly once."
34 (let* ((text (token-value token)) (colon (position #\: text))
35 (prefix (subseq text 0 colon)) (bound (gethash prefix (parser-prefixes parser))))
36 (unless bound
37 (problem 'undefined-prefix :undefined-prefix (token-position token)
38 (format nil "Prefix ~S has no binding." prefix)))
39 ;; Do not re-resolve or percent-decode an already resolved binding.
40 (validate-iri-characters (concatenate 'string bound (subseq text (1+ colon)))
41 (token-position token))))
42
43 (defun read-prefix-directive (parser)
44 "CTM 3.16.1: document-ordered, per-deserialization bindings; reference is an IRI."
45 (consume parser) ; %prefix
46 (let ((name (consume parser)) (reference (consume parser)))
47 (unless (eq :identifier (token-kind name))
48 (problem 'ctm-rejection :expected-prefix-name (token-position name) "Expected a prefix identifier."))
49 (unless (eq :iri (token-kind reference))
50 (problem 'ctm-rejection :expected-prefix-iri (token-position reference)
51 "A prefix binding requires an IRI, not a QName or Topic identifier."))
52 (let* ((iri (resolve-iri (token-value reference) (parser-document-iri parser) (token-position reference)))
53 (old (gethash (token-value name) (parser-prefixes parser))))
54 (when (and old (not (string= old iri)))
55 (problem 'prefix-collision :prefix-collision (token-position name)
56 (format nil "Prefix ~S is already bound to ~S, not ~S." (token-value name) old iri)))
57 (setf (gethash (token-value name) (parser-prefixes parser)) iri))))
58
59 (defun identity-value (parser &optional explicit-kind)
60 (let ((token (consume parser)))
61 (case (token-kind token)
62 (:identifier
63 (when explicit-kind
64 (problem 'ctm-rejection :expected-iri (token-position token) "Explicit = or ^ needs an IRI reference."))
65 (when (member (token-value token) '("isa" "ako") :test #'string=)
66 (problem 'ctm-rejection :reserved-topic-identifier (token-position token) "A relationship keyword is not a Topic identity."))
67 (when (member (token-value token) '("def" "end") :test #'string=)
68 (problem 'ctm-unsupported :unsupported-keyword (token-position token) "This keyword is outside the implemented subset."))
69 (values :item-identifier
70 (concatenate 'string (parser-document-iri parser) "#" (token-value token))))
71 (:iri
72 (values (or explicit-kind :subject-identifier)
73 (resolve-iri (token-value token) (parser-document-iri parser) (token-position token))))
74 (:qname (values (or explicit-kind :subject-identifier) (resolve-qname parser token)))
75 (:wildcard
76 (when explicit-kind
77 (problem 'ctm-rejection :expected-iri (token-position token) "Explicit = or ^ needs an IRI reference."))
78 (values :item-identifier (wildcard-locator parser (token-value token))))
79 (:punctuation
80 (case (token-value token)
81 (#\= (if explicit-kind
82 (problem 'ctm-rejection :expected-iri (token-position token) "Unexpected identity marker.")
83 (identity-value parser :subject-locator)))
84 (#\^ (if explicit-kind
85 (problem 'ctm-rejection :expected-iri (token-position token) "Unexpected identity marker.")
86 (identity-value parser :item-identifier)))
87 (otherwise (problem 'ctm-rejection :expected-topic-identity (token-position token) "Expected topic identity."))))
88 (otherwise (problem 'ctm-rejection :expected-topic-identity (token-position token) "Expected topic identity.")))))
89
90 (defun identity-reference (parser map)
91 (let ((position (token-position (look parser))))
92 (multiple-value-bind (kind iri) (identity-value parser)
93 (handler-case (topic-maps.tmdm:intern-topic map kind iri)
94 (topic-maps.tmdm:core-subject-error (c)
95 (problem 'ctm-rejection (topic-maps.tmdm:core-subject-error-code c) position (princ-to-string c)))))))
96
97 (defun topic-reference (parser map)
98 "CTM 3.3.7: one recursive reference path for Types, Players and Scope themes."
99 (if (punctuation-p parser #\[)
100 (progn
101 (consume parser)
102 (let ((topic (topic-maps.tmdm:intern-topic map :item-identifier (wildcard-locator parser))))
103 (read-topic-tail parser map topic #\] :required t)))
104 (identity-reference parser map)))
105
106 (defun read-scope (parser map)
107 "CTM 3.3.9: optional @ followed by a nonempty comma-separated Topic set.
108 Use the shared Topic reference resolver, including Embedded Topics and Wildcards.
109 The owning information item is not constructed until this function finishes."
110 (when (punctuation-p parser #\@)
111 (consume parser)
112 (let ((themes nil))
113 (loop
114 ;; A reifier marker where a Theme is required is malformed Scope,
115 ;; while a reifier after a complete Scope belongs to its statement.
116 (unless (parser-lookahead parser)
117 (skip-layout (parser-lexer parser))
118 (when (eql #\~ (peek-character (parser-lexer parser)))
119 (problem 'ctm-rejection :expected-scope-theme (lexer-cursor (parser-lexer parser))
120 "Expected a Theme, not a reifier marker.")))
121 (let ((token (look parser)))
122 (unless (topic-reference-start-p token)
123 (problem 'ctm-rejection :expected-scope-theme (token-position token)
124 "Expected a Topic reference after '@' or the scope comma."))
125 (push (topic-reference parser map) themes))
126 (if (punctuation-p parser #\,) (consume parser) (return (nreverse themes)))))))
127
128 (defun literal-iri-reference (parser)
129 "An IRI locator, not a Topic identity. Used for values/datatype syntax."
130 (let ((token (consume parser)))
131 (case (token-kind token)
132 (:iri (resolve-iri (token-value token) (parser-document-iri parser) (token-position token)))
133 (:qname (resolve-qname parser token))
134 (otherwise (problem 'ctm-rejection :expected-datatype-iri (token-position token)
135 "A datatype qualifier requires an IRI reference, not a Topic identifier.")))))
136
137 (defun read-literal (parser &optional (context :occurrence))
138 "Shared CTM 3.4.1 literal reader for Occurrences and Variants."
139 (let ((token (look parser)))
140 (case (token-kind token)
141 (:integer
142 (consume parser)
143 ;; Numeric-literal value policy: represent the unquoted integer value
144 ;; canonically; qualified quoted strings retain their lexical value.
145 (values (topic-maps.tmdm:canonical-integer-lexical-form (token-value token))
146 (topic-maps.tmdm:integer-datatype)))
147 (:decimal
148 (consume parser)
149 ;; Same value-representation policy as bare integers; token spelling is
150 ;; retained by the lexer, qualified strings remain lexical model data.
151 (values (topic-maps.tmdm:canonical-decimal-lexical-form (token-value token))
152 (topic-maps.tmdm:decimal-datatype)))
153 (:date
154 (consume parser)
155 (values (topic-maps.tmdm:canonical-date-lexical-form (token-value token)) (topic-maps.tmdm:date-datatype)))
156 (:datetime
157 (consume parser)
158 (multiple-value-bind (value reason) (topic-maps.tmdm:canonical-datetime-lexical-form (token-value token))
159 (unless value (problem 'ctm-unsupported
160 (if (eq reason :unsupported-bce-timezone-rollover)
161 :datetime-bce-timezone-rollover :datetime-leap-second-context)
162 (token-position token) "DateTime normalization is outside the supported BCE/leap-second calendar context."))
163 (values value (topic-maps.tmdm:datetime-datatype))))
164 (:excluded-literal
165 (problem 'ctm-unsupported (if (eq context :variant) :variant-literal-datatype :occurrence-literal-datatype) (token-position token)
166 "The separate CTM '*' literal is outside this slice."))
167 (:string
168 (consume parser)
169 (if (punctuation-p parser #\^)
170 (let ((caret (consume parser)))
171 (unless (and (punctuation-p parser #\^)
172 (= (token-position (look parser)) (1+ (token-position caret))))
173 (problem 'ctm-rejection :invalid-datatype-marker (token-position caret) "Expected contiguous '^^'."))
174 (consume parser)
175 (let ((datatype (literal-iri-reference parser)))
176 (when (and (string= datatype (topic-maps.tmdm:integer-datatype))
177 (not (topic-maps.tmdm:integer-lexical-p (token-value token))))
178 (problem 'ctm-rejection :invalid-integer-value (token-position token)
179 "Quoted value is not an xsd:integer lexical form."))
180 (when (and (string= datatype (topic-maps.tmdm:decimal-datatype))
181 (not (topic-maps.tmdm:decimal-lexical-p (token-value token))))
182 (problem 'ctm-rejection :invalid-decimal-value (token-position token)
183 "Quoted value is not an xsd:decimal lexical form."))
184 (when (and (string= datatype (topic-maps.tmdm:date-datatype))
185 (not (topic-maps.tmdm:date-lexical-p (token-value token))))
186 (problem 'ctm-rejection :invalid-date-value (token-position token)
187 "Quoted value is not a valid XSD 1.0 date lexical form."))
188 (when (and (string= datatype (topic-maps.tmdm:datetime-datatype))
189 (not (topic-maps.tmdm:datetime-lexical-p (token-value token))))
190 (problem 'ctm-rejection :invalid-datetime-value (token-position token)
191 "Quoted value is not a valid XSD 1.0 dateTime."))
192 ;; TMDM 4.4 requires absolute anyURI content; quoted content is
193 ;; lexical data, never an IRI reference to resolve against the document.
194 (when (and (string= datatype (topic-maps.tmdm:iri-datatype))
195 (not (topic-maps.tmdm::absolute-locator-p (token-value token))))
196 (problem 'ctm-rejection :relative-uri-value (token-position token)
197 "A quoted anyURI value must already be absolute (TMDM 4.4)."))
198 (values (token-value token) datatype)))
199 (values (token-value token) (topic-maps.tmdm:string-datatype))))
200 ((:iri :qname) (values (literal-iri-reference parser) (topic-maps.tmdm:iri-datatype)))
201 (otherwise (problem 'ctm-rejection (if (eq context :variant) :expected-variant-value :expected-occurrence-value)
202 (token-position token) "Expected a supported CTM literal value.")))))
203
204 (defun read-reifier (parser map)
205 "CTM 3.3.10: reuse the full Topic reference resolver, retaining the marker position."
206 (when (punctuation-p parser #\~)
207 (let ((position (token-position (consume parser))))
208 (unless (topic-reference-start-p (look parser))
209 (problem 'ctm-rejection :expected-reifier-topic (token-position (look parser)) "Expected a Topic reference after '~'."))
210 (values (topic-reference parser map) position))))
211 (defun attach-reifier (item topic position)
212 (when topic
213 (handler-case (setf item (topic-maps.tmdm:set-item-reifier item topic))
214 (topic-maps.tmdm:reification-error (c)
215 (problem 'ctm-rejection (topic-maps.tmdm:reification-error-code c) position (princ-to-string c)))
216 (topic-maps.tmdm:variant-error (c)
217 (problem 'ctm-rejection (topic-maps.tmdm:variant-error-code c) position (princ-to-string c)))
218 (topic-maps.tmdm:core-subject-error (c)
219 (problem 'ctm-rejection (topic-maps.tmdm:core-subject-error-code c) position (princ-to-string c)))))
220 item)
221
222 (defun read-identity-tail (parser map topic)
223 (let* ((start (look parser)) (embedded (punctuation-p parser #\[))
224 (type (when embedded (topic-reference parser map))))
225 ;; Identity tail statements add directly to their owner, without constructing
226 ;; an intermediate Topic. Identity-form Type references use the same resolver.
227 (multiple-value-bind (kind iri) (unless embedded (identity-value parser))
228 (if (punctuation-p parser #\:)
229 (progn
230 (consume parser)
231 (multiple-value-bind (value datatype) (read-literal parser)
232 (let* ((type (or type (topic-maps.tmdm:intern-topic map kind iri)))
233 (scope (read-scope parser map)))
234 (multiple-value-bind (r position) (read-reifier parser map)
235 (attach-reifier (topic-maps.tmdm:add-occurrence topic type value :datatype datatype :scope scope) r position))
236 (topic-maps.tmdm:canonical-topic topic))))
237 (if (or embedded (member (token-kind start) '(:identifier :wildcard)))
238 (if (and (eq :identifier (token-kind start)) (punctuation-p parser #\())
239 (problem 'ctm-unsupported :topic-tail (token-position start) "Template invocations are outside this subset.")
240 (problem 'ctm-rejection :expected-occurrence-type-separator (token-position (look parser))
241 "Expected ':' after the Occurrence Type."))
242 (handler-case (topic-maps.tmdm:add-topic-identifier topic kind iri)
243 (topic-maps.tmdm:core-subject-error (c)
244 (problem 'ctm-rejection (topic-maps.tmdm:core-subject-error-code c)
245 (token-position start) (princ-to-string c)))))))))
246
247 (defun read-variant (parser map name)
248 (consume parser)
249 (multiple-value-bind (value datatype) (read-literal parser :variant)
250 (unless (punctuation-p parser #\@)
251 (problem 'ctm-rejection :expected-variant-scope (token-position (look parser)) "A Variant requires an explicit '@' Scope."))
252 (let* ((scope-position (token-position (look parser))) (themes (read-scope parser map)))
253 (multiple-value-bind (r position) (read-reifier parser map)
254 (unless (punctuation-p parser #\))
255 (problem 'ctm-rejection :missing-variant-close (token-position (look parser)) "Expected ')' after the Variant Scope/Reifier."))
256 (consume parser)
257 (handler-case
258 (attach-reifier (topic-maps.tmdm:add-variant name value :datatype datatype
259 :scope (append (topic-maps.tmdm:topic-name-scope name) themes)) r position)
260 (topic-maps.tmdm:variant-error (c)
261 (problem 'ctm-rejection (topic-maps.tmdm:variant-error-code c) scope-position (princ-to-string c))))))))
262
263 (defun read-name (parser map topic)
264 "CTM 3.11: optional Topic reference plus ':' followed by a string.
265 Use the shared Topic reference resolver."
266 (let* ((token (look parser))
267 (typed (topic-reference-start-p token))
268 (type (when typed (topic-reference parser map))))
269 (when typed
270 (unless (punctuation-p parser #\:)
271 (problem 'ctm-rejection :expected-name-type-separator
272 (token-position (look parser)) "Expected ':' after the name type."))
273 (consume parser))
274 (let ((value (consume parser)))
275 (unless (eq :string (token-kind value))
276 (problem 'ctm-rejection :expected-name-value (token-position value)
277 "Expected a quoted name value after '-' or the name type separator."))
278 (let ((scope (read-scope parser map)))
279 (multiple-value-bind (r position) (read-reifier parser map)
280 (let ((name (attach-reifier
281 (if typed (topic-maps.tmdm:add-name topic (token-value value) :type type :scope scope)
282 (topic-maps.tmdm:add-name topic (token-value value) :scope scope)) r position)))
283 (loop while (punctuation-p parser #\() do (read-variant parser map name))))))
284 (topic-maps.tmdm:canonical-topic topic)))
285
286 (defun relationship-keyword-p (token)
287 (and (eq :identifier (token-kind token)) (member (token-value token) '("isa" "ako") :test #'string=)))
288
289 (defun read-type-relationship (parser map topic)
290 "CTM 3.9: expand one isa/ako statement using ordinary same-map Associations."
291 (let* ((keyword (consume parser)) (isa (string= (token-value keyword) "isa")) (next (look parser)))
292 (unless (and (topic-reference-start-p next)
293 (not (and (eq :identifier (token-kind next))
294 (member (token-value next) '("isa" "ako" "def" "end") :test #'string=))))
295 (problem 'ctm-rejection :expected-relationship-topic (token-position next)
296 "Expected one Topic reference after isa or ako."))
297 (let ((target (topic-reference parser map)))
298 (when (punctuation-p parser #\,)
299 (problem 'ctm-rejection :unexpected-relationship-list (token-position (look parser))
300 "isa/ako takes one Topic reference; repeat the keyword in a separate tail element."))
301 (handler-case
302 (flet ((core (local)
303 ;; Resolve locators every time; do not cache replaceable Topic objects.
304 (topic-maps.tmdm:intern-topic map :subject-identifier
305 (concatenate 'string "http://psi.topicmaps.org/iso13250/model/" local))))
306 (let* ((type (core (if isa "type-instance" "supertype-subtype")))
307 (left (core (if isa "instance" "subtype"))) (right (core (if isa "type" "supertype"))))
308 (topic-maps.tmdm:make-association map type (list (list left topic) (list right target)))
309 (topic-maps.tmdm:canonical-topic topic)))
310 (topic-maps.tmdm:core-subject-error (c)
311 (problem 'ctm-rejection (topic-maps.tmdm:core-subject-error-code c)
312 (token-position keyword) (princ-to-string c)))))))
313
314 (defun read-topic-tail (parser map topic terminator &key required)
315 "CTM 3.9: the same Tail reader ends at '.' or the caller's Embedded ']'."
316 (let ((has-tail nil) (seen-tail nil))
317 (loop
318 (cond
319 ((punctuation-p parser terminator)
320 (when (and required (not seen-tail))
321 (problem 'ctm-rejection :expected-topic-tail (token-position (look parser)) "An Embedded Topic requires a nonempty Topic Tail."))
322 (consume parser) (return (topic-maps.tmdm:canonical-topic topic)))
323 ((and (char= terminator #\]) (punctuation-p parser #\.))
324 (problem 'ctm-rejection :missing-embedded-close (token-position (look parser)) "Embedded Topics end with ']', not a period."))
325 ((punctuation-p parser #\-)
326 (when has-tail
327 (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
328 (consume parser)
329 (setf topic (read-name parser map topic))
330 (setf has-tail t seen-tail t))
331 ((and has-tail (punctuation-p parser #\;))
332 (consume parser)
333 (setf has-tail nil))
334 ((eq :eof (token-kind (look parser)))
335 (problem 'ctm-rejection (if (char= terminator #\]) :missing-embedded-close :missing-period)
336 (token-position (look parser)) "Expected the closing Topic/Embedded Topic delimiter."))
337 ((relationship-keyword-p (look parser))
338 (when has-tail
339 (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
340 (setf topic (read-type-relationship parser map topic) has-tail t seen-tail t))
341 ((topic-reference-start-p (look parser))
342 (when has-tail
343 (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
344 (setf topic (read-identity-tail parser map topic) has-tail t seen-tail t))
345 ((punctuation-p parser #\()
346 (problem 'ctm-rejection :unexpected-variant (token-position (look parser)) "A Variant must immediately follow a Topic Name."))
347 (t (problem 'ctm-rejection :unexpected-token (token-position (look parser)) "Unexpected token after topic or name."))))))
348
349 (defun read-association (parser map type)
350 "CTM 3.13: one or more Role declarations; optional Scope and Reifiers."
351 (consume parser) ; opening parenthesis already recognized by the caller
352 (let ((roles nil) (role-reifiers nil))
353 (loop
354 (when (or (punctuation-p parser #\)) (eq :eof (token-kind (look parser))))
355 (problem 'ctm-rejection :expected-role (token-position (look parser)) "Expected a nonempty Association role declaration."))
356 (let* ((role-type (topic-reference parser map))
357 (type-end (lexer-cursor (parser-lexer parser)))
358 (colon (look parser)))
359 (unless (punctuation-p parser #\:)
360 (problem 'ctm-rejection :expected-role-type-separator (token-position colon) "Expected ':' after the Role type."))
361 (consume parser)
362 (let ((player-token (look parser)))
363 ;; CTM role production requires layout on at least one side of ':'.
364 ;; skip-layout handles whitespace and comments; gaps use source offsets.
365 (unless (or (> (token-position colon) type-end)
366 (> (token-position player-token) (1+ (token-position colon))))
367 (problem 'ctm-rejection :role-separator-whitespace (token-position colon)
368 "A Role separator needs whitespace or a comment on at least one side."))
369 (when (or (eq :eof (token-kind player-token))
370 (punctuation-p parser #\)) (punctuation-p parser #\,))
371 (problem 'ctm-rejection :expected-role-player (token-position player-token) "Expected a Topic as the Role player.")))
372 (let ((player (topic-reference parser map)))
373 (push (list role-type player) roles)
374 (multiple-value-bind (r position) (read-reifier parser map)
375 (push (list role-type player r position) role-reifiers))))
376 (cond ((punctuation-p parser #\,) (consume parser))
377 ((punctuation-p parser #\)) (consume parser) (return))
378 ((eq :eof (token-kind (look parser)))
379 (problem 'ctm-rejection :missing-association-close (token-position (look parser)) "Expected ')' after the Roles."))
380 (t (problem 'ctm-rejection :expected-role-separator (token-position (look parser)) "Expected ',' or ')' after a Role."))))
381 (let ((scope (read-scope parser map)))
382 (multiple-value-bind (r position) (read-reifier parser map)
383 (let ((association (topic-maps.tmdm:make-association map type (nreverse roles) :scope scope)))
384 (dolist (entry (nreverse role-reifiers))
385 (destructuring-bind (role-type player reifier marker) entry
386 (let ((role (find-if (lambda (role)
387 (and (eq (topic-maps.tmdm:canonical-topic role-type) (topic-maps.tmdm:association-role-type role))
388 (eq (topic-maps.tmdm:canonical-topic player) (topic-maps.tmdm:association-role-player role))))
389 (topic-maps.tmdm:association-roles association))))
390 (attach-reifier role reifier marker))))
391 (attach-reifier (topic-maps.tmdm:canonical-association association) r position))))))
392
393 (defun deserialize-string (text &key document-iri source-name (wildcard-iri document-iri) (wildcard-counter 0))
394 "Read the documented CTM subset, with per-document prefixes and qualified strings.
395 Return the Map and final Wildcard Counter as two values. Wildcard IRI defaults
396 to Document IRI; local identifiers and relative IRIs still use Document IRI.
397 Source positions are zero-based character offsets; lines/columns are one-based."
398 (check-type text string)
399 (let ((*source-name* source-name) (*source-text* text))
400 (require-document-iri document-iri)
401 (require-wildcard-environment wildcard-iri wildcard-counter)
402 (let ((parser (make-parser (make-lexer text) document-iri wildcard-iri wildcard-counter))
403 (map (topic-maps.tmdm:make-topic-map)))
404 (handler-bind ((topic-maps.tmdm:variant-error
405 (lambda (c) (problem 'ctm-rejection (topic-maps.tmdm:variant-error-code c)
406 (lexer-cursor (parser-lexer parser)) (princ-to-string c))))
407 (topic-maps.tmdm:reification-error
408 (lambda (c) (problem 'ctm-rejection (topic-maps.tmdm:reification-error-code c)
409 (lexer-cursor (parser-lexer parser)) (princ-to-string c))))
410 (topic-maps.tmdm:core-subject-error
411 (lambda (c) (problem 'ctm-rejection (topic-maps.tmdm:core-subject-error-code c)
412 (lexer-cursor (parser-lexer parser)) (princ-to-string c)))))
413 (loop while (eq :directive (token-kind (look parser))) do (read-prefix-directive parser))
414 (multiple-value-bind (r position) (read-reifier parser map) (attach-reifier map r position))
415 (loop until (eq :eof (token-kind (look parser))) do
416 (when (punctuation-p parser #\~)
417 (problem 'ctm-rejection :topic-map-reifier-position (token-position (look parser))
418 "Topic Map Reifier must precede all body declarations."))
419 (if (eq :directive (token-kind (look parser)))
420 (read-prefix-directive parser)
421 (let* ((embedded (punctuation-p parser #\[)) (topic (topic-reference parser map)))
422 (if (punctuation-p parser #\()
423 (read-association parser map topic)
424 (if embedded
425 (problem 'ctm-rejection :embedded-topic-at-top-level (token-position (look parser))
426 "An Embedded Topic at top level must be an Association Type, not a standalone declaration.")
427 (read-topic-tail parser map topic #\.)))))))
428 (values map (parser-wildcard-counter parser)))))
429
430 (defun deserialize-file (pathname &key document-iri source-name (wildcard-iri document-iri) (wildcard-counter 0))
431 "Read a UTF-8 CTM file, with optional UTF-8 BOM, using an explicit Document IRI.
432 Other encoding directives are UNSUPPORTED. File I/O errors propagate unchanged."
433 (let ((*source-name* source-name) (*source-text* ""))
434 (require-document-iri document-iri)
435 (require-wildcard-environment wildcard-iri wildcard-counter)
436 (with-open-file (input pathname :direction :input :element-type '(unsigned-byte 8))
437 (let ((bytes (make-array (file-length input) :element-type '(unsigned-byte 8))))
438 (unless (= (length bytes) (read-sequence bytes input))
439 (error "Input file changed or was truncated during reading."))
440 (deserialize-string (decode-utf8 bytes :strip-bom t)
441 :document-iri document-iri :source-name source-name
442 :wildcard-iri wildcard-iri :wildcard-counter wildcard-counter)))))