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