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