1 (in-package #:topic-maps.ctm)
3 (defvar *active-resource-iris* nil)
5 (defstruct template-definition name parameters body position source-name source-text document-iri)
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)))
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)))))
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.")))
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)))
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) "=^["))))
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))))
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))))
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))))
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))
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))))
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)))
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))))
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.")))))
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)))))))
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 #\[)
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)))
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 #\@)
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)))))))
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.")))))
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))
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)))
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)))
174 (values (topic-maps.tmdm:canonical-date-lexical-form (token-value token)) (topic-maps.tmdm:date-datatype)))
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))))
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."))
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 '^^'."))
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.")))))
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)
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)))))
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 #\:)
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)))))))))
265 (defun read-variant (parser map name)
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."))
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))))))))
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))))
289 (unless (punctuation-p parser #\:)
290 (problem 'ctm-rejection :expected-name-type-separator
291 (token-position (look parser)) "Expected ':' after the name type."))
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)))
306 (defun relationship-keyword-p (token)
307 (and (eq :identifier (token-kind token)) (member (token-value token) '("isa" "ako") :test #'string=)))
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."))
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)))))))
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))
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 #\-)
347 (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
349 (setf topic (read-name parser map topic))
350 (setf has-tail t seen-tail t))
351 ((and has-tail (punctuation-p parser #\;))
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))
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)
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))
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."))))))
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))
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."))
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))))))
418 (defun read-prolog (parser)
419 "CTM 3.5: one initial encoding header, then one optional version 1.0."
420 (when (and (eq :directive (token-kind (look parser)))
421 (string= "encoding" (token-value (look parser))))
422 (let ((token (consume parser)))
423 (unless (zerop (token-position token))
424 (problem 'ctm-rejection :misplaced-encoding (token-position token)
425 "Encoding declaration must be the first characters of the CTM stream."))
426 (multiple-value-bind (name end position)
427 (ctm-encoding-name *source-text* (token-position token) :character)
428 (unless (string-equal name "UTF-8")
429 (problem 'ctm-unsupported :unsupported-encoding position "Only UTF-8 CTM input is implemented."))
430 (setf (lexer-cursor (parser-lexer parser)) end))))
431 (when (and (eq :directive (token-kind (look parser)))
432 (string= "version" (token-value (look parser))))
434 (let ((lexer (parser-lexer parser)))
436 (let ((start (lexer-cursor lexer)))
437 ;; Match the fixed terminal before the numeric-literal lexer. A Topic
438 ;; can follow immediately; extra numeric spelling is not version 1.0.
439 (unless (and (ctm-ascii-prefix-p *source-text* start "1.0")
440 (let ((end (+ start 3)))
441 (not (and (< end (length *source-text*))
442 (find (char *source-text* end) "0123456789.+-")))))
443 (problem 'ctm-rejection :invalid-version start "The only CTM version is the exact spelling 1.0."))
444 (setf (lexer-cursor lexer) (+ start 3))))))
446 (defun deserialize-context (text &key document-iri source-name (wildcard-iri document-iri) (wildcard-counter 0)
447 resource-resolver (max-resource-depth 32))
448 "Read the documented CTM subset, with per-document prefixes and qualified strings.
449 Internal result: Map, final Wildcard Counter and parser environment. Wildcard IRI defaults
450 to Document IRI; local identifiers and relative IRIs still use Document IRI.
451 Source positions are zero-based character offsets; lines/columns are one-based.
452 RESOURCE-RESOLVER, if supplied, is called with an absolute IRI and :PURPOSE,
453 :FROM-DOCUMENT-IRI and :NOTATION, returning octets and an optional source key.
454 There is no implicit retrieval. MAX-RESOURCE-DEPTH bounds nested imports (default
455 32, zero disables them); it is an implementation limit, not a CTM validity rule."
456 (check-type text string)
457 (let ((*source-name* source-name) (*source-text* text)
458 (*active-resource-iris* (cons document-iri *active-resource-iris*)))
459 (require-document-iri document-iri)
460 (require-wildcard-environment wildcard-iri wildcard-counter)
461 (when (and resource-resolver (not (functionp resource-resolver)))
462 (problem 'ctm-resource-error :invalid-resource-resolver 0 "Supply a resolver function or NIL."))
463 (unless (and (integerp max-resource-depth) (>= max-resource-depth 0))
464 (problem 'ctm-resource-error :invalid-resource-depth-limit 0 "Resource depth limit must be a non-negative integer."))
465 (let ((parser (make-parser (make-lexer text) document-iri wildcard-iri wildcard-counter))
466 (map (topic-maps.tmdm:make-topic-map)))
467 (setf (parser-resource-resolver parser) resource-resolver (parser-max-resource-depth parser) max-resource-depth)
468 (handler-bind ((topic-maps.tmdm:variant-error
469 (lambda (c) (problem 'ctm-rejection (topic-maps.tmdm:variant-error-code c)
470 (lexer-cursor (parser-lexer parser)) (princ-to-string c))))
471 (topic-maps.tmdm:reification-error
472 (lambda (c) (problem 'ctm-rejection (topic-maps.tmdm:reification-error-code c)
473 (lexer-cursor (parser-lexer parser)) (princ-to-string c))))
474 (topic-maps.tmdm:core-subject-error
475 (lambda (c) (problem 'ctm-rejection (topic-maps.tmdm:core-subject-error-code c)
476 (lexer-cursor (parser-lexer parser)) (princ-to-string c)))))
478 (loop while (eq :directive (token-kind (look parser))) do (read-directive parser map))
479 (multiple-value-bind (r position) (read-reifier parser map) (attach-reifier map r position))
480 (loop until (eq :eof (token-kind (look parser))) do
481 (when (punctuation-p parser #\~)
482 (problem 'ctm-rejection :topic-map-reifier-position (token-position (look parser))
483 "Topic Map Reifier must precede all body declarations."))
484 (cond ((eq :directive (token-kind (look parser))) (read-directive parser map))
485 ((identifier-p (look parser) "def") (read-template-definition parser))
486 (t (read-statement parser map)))))
487 (values map (parser-wildcard-counter parser) parser))))
489 (defun deserialize-string (text &key document-iri source-name (wildcard-iri document-iri) (wildcard-counter 0)
490 resource-resolver (max-resource-depth 32))
491 "Deserialize CTM using explicit Document IRI and optional controlled resources.
492 Return the Topic Map and final Wildcard Counter. Includes import Templates and
493 Topic aliases; Mergemap imports Map content only. No implicit I/O is performed.
494 The resolver receives :PURPOSE (:INCLUDE or :MERGEMAP), :FROM-DOCUMENT-IRI and
495 :NOTATION (NIL for Include). Source keys are diagnostic, never identity bases."
496 (multiple-value-bind (map counter)
497 (deserialize-context text :document-iri document-iri :source-name source-name
498 :wildcard-iri wildcard-iri :wildcard-counter wildcard-counter
499 :resource-resolver resource-resolver :max-resource-depth max-resource-depth)
500 (values map counter)))
502 (defun deserialize-file (pathname &key document-iri source-name encoding (wildcard-iri document-iri) (wildcard-counter 0)
503 resource-resolver (max-resource-depth 32))
504 "Read CTM octets using an explicit Document IRI and optional supplied ENCODING.
505 Check supplied encoding/BOM/declaration conflicts before strict UTF-8 decoding.
506 Other charsets remain UNSUPPORTED. File I/O errors propagate unchanged."
507 (let ((*source-name* source-name) (*source-text* ""))
508 (require-document-iri document-iri)
509 (require-wildcard-environment wildcard-iri wildcard-counter)
510 (with-open-file (input pathname :direction :input :element-type '(unsigned-byte 8))
511 (let ((bytes (make-array (file-length input) :element-type '(unsigned-byte 8))))
512 (unless (= (length bytes) (read-sequence bytes input))
513 (error "Input file changed or was truncated during reading."))
514 (deserialize-string (decode-ctm-octets bytes :encoding encoding)
515 :document-iri document-iri :source-name source-name
516 :wildcard-iri wildcard-iri :wildcard-counter wildcard-counter
517 :resource-resolver resource-resolver :max-resource-depth max-resource-depth)))))