- (let ((source
- (let ((*source-name* (or source-key resource)) (*source-text* ""))
- ;; Decoding uses the existing strict UTF-8 decoder and BOM policy.
- ;; The key is diagnostic only; RESOURCE remains the Document IRI.
- (deserialize-string (decode-utf8 bytes :strip-bom t) :document-iri resource
- :source-name *source-name* :resource-resolver (parser-resource-resolver parser)
- :max-resource-depth (parser-max-resource-depth parser)))))
- (handler-case (topic-maps.tmdm:import-map-content map source)
- (topic-maps.tmdm:item-identifier-error (c)
- (problem 'ctm-rejection (topic-maps.tmdm:item-identifier-error-code c) position (princ-to-string c)))
- (topic-maps.tmdm:core-subject-error (c)
- (problem 'ctm-rejection (topic-maps.tmdm:core-subject-error-code c) position (princ-to-string c)))
- (topic-maps.tmdm:variant-error (c)
- (problem 'ctm-rejection (topic-maps.tmdm:variant-error-code c) position (princ-to-string c)))
- (topic-maps.tmdm:reification-error (c)
- (problem 'ctm-rejection (topic-maps.tmdm:reification-error-code c) position (princ-to-string c))))))))
+ (let ((text (let ((*source-name* (or source-key resource)) (*source-text* ""))
+ (decode-utf8 bytes :strip-bom t))))
+ (funcall continuation text (or source-key resource))))))
+
+(defun call-with-import-diagnostics (position thunk)
+ ;; Keep source errors at the resource source; model transfer/alias errors are
+ ;; attributed to the caller's directive with its active resource context.
+ (handler-case (funcall thunk)
+ (topic-maps.tmdm:item-identifier-error (c)
+ (problem 'ctm-rejection (topic-maps.tmdm:item-identifier-error-code c) position (princ-to-string c)))
+ (topic-maps.tmdm:core-subject-error (c)
+ (problem 'ctm-rejection (topic-maps.tmdm:core-subject-error-code c) position (princ-to-string c)))
+ (topic-maps.tmdm:variant-error (c)
+ (problem 'ctm-rejection (topic-maps.tmdm:variant-error-code c) position (princ-to-string c)))
+ (topic-maps.tmdm:reification-error (c)
+ (problem 'ctm-rejection (topic-maps.tmdm:reification-error-code c) position (princ-to-string c)))))
+
+(defun read-mergemap-directive (parser map)
+ "CTM 3.16.3: immediately read a separate Map and import only its content."
+ (let* ((start (consume parser))
+ (resource (directive-iri parser :expected-mergemap-resource "Mergemap requires a resource IRI reference."))
+ (notation (directive-iri parser :expected-mergemap-notation "Mergemap requires a notation IRI reference."))
+ (position (token-position start)))
+ (call-with-resource parser resource :mergemap position notation
+ (lambda (text source-key)
+ (let ((source (deserialize-string text :document-iri resource :source-name source-key
+ :resource-resolver (parser-resource-resolver parser)
+ :max-resource-depth (parser-max-resource-depth parser))))
+ (call-with-import-diagnostics position
+ (lambda () (topic-maps.tmdm:import-map-content map source))))))))
+
+(defun add-include-aliases (map child-document parent-document position)
+ "CTM 3.16.2: one snapshot/pass per Include level, original IIs retained.
+Use literal string-prefix matching, not an assumed fragment/path boundary.
+Already resolved locator suffixes are not CTM-percent-decoded again."
+ (let ((aliases
+ (loop for topic in (topic-maps.tmdm:topic-map-topics map) append
+ (loop for identifier in (topic-maps.tmdm:topic-item-identifiers topic)
+ when (starts-with child-document identifier)
+ collect (list topic (resolve-iri (subseq identifier (length child-document))
+ parent-document position :unescape nil))))))
+ (dolist (alias aliases)
+ ;; A preceding addition may replace/merge this Topic. The public operation
+ ;; resolves its retained handle and uses the existing identity machinery.
+ (topic-maps.tmdm:add-topic-identifier (first alias) :item-identifier (second alias))))
+ map)
+
+(defun read-include-directive (parser map)
+ "CTM 3.16.2, with caller-context execution of imported Template bodies."
+ (let* ((start (consume parser)) (position (token-position start))
+ (resource (directive-iri parser :expected-include-resource "Include requires a resource IRI reference.")))
+ (call-with-resource parser resource :include position nil
+ (lambda (text source-key)
+ (multiple-value-bind (source counter child)
+ (deserialize-context text :document-iri resource :source-name source-key
+ :wildcard-iri (parser-document-iri parser) :wildcard-counter (parser-wildcard-counter parser)
+ :resource-resolver (parser-resource-resolver parser)
+ :max-resource-depth (parser-max-resource-depth parser))
+ ;; Reject register conflicts before modifying the caller's Map,
+ ;; registry or Counter. Prefix bindings and named-Wildcard caches are
+ ;; per processor and are deliberately not transferred.
+ (maphash (lambda (key definition)
+ (when (gethash key (parser-templates parser))
+ (problem 'duplicate-template :duplicate-template position
+ (format nil "Included Template ~A/~D is already defined (source ~A, character ~D)."
+ (first key) (second key) (template-definition-source-name definition)
+ (template-definition-position definition)))))
+ (parser-templates child))
+ (call-with-import-diagnostics position
+ (lambda ()
+ (add-include-aliases source resource (parser-document-iri parser) position)
+ (topic-maps.tmdm:import-map-content map source)))
+ (maphash (lambda (key definition) (setf (gethash key (parser-templates parser)) definition))
+ (parser-templates child))
+ (setf (parser-wildcard-counter parser) counter)
+ map)))))