+ (if (or embedded (member (token-kind start) '(:identifier :wildcard :variable)))
+ (problem 'ctm-rejection :expected-occurrence-type-separator (token-position (look parser))
+ "Expected ':' after the Occurrence Type.")
+ (handler-case (topic-maps.tmdm:add-topic-identifier topic kind iri)
+ (topic-maps.tmdm:core-subject-error (c)
+ (problem 'ctm-rejection (topic-maps.tmdm:core-subject-error-code c)
+ (token-position start) (princ-to-string c)))))))))
+
+(defun read-variant (parser map name)
+ (consume parser)
+ (multiple-value-bind (value datatype) (read-literal parser :variant)
+ (unless (punctuation-p parser #\@)
+ (problem 'ctm-rejection :expected-variant-scope (token-position (look parser)) "A Variant requires an explicit '@' Scope."))
+ (let* ((scope-position (token-position (look parser))) (themes (read-scope parser map)))
+ (multiple-value-bind (r position) (read-reifier parser map)
+ (unless (punctuation-p parser #\))
+ (problem 'ctm-rejection :missing-variant-close (token-position (look parser)) "Expected ')' after the Variant Scope/Reifier."))
+ (consume parser)
+ (handler-case
+ (attach-reifier (topic-maps.tmdm:add-variant name value :datatype datatype
+ :scope (append (topic-maps.tmdm:topic-name-scope name) themes)) r position)
+ (topic-maps.tmdm:variant-error (c)
+ (problem 'ctm-rejection (topic-maps.tmdm:variant-error-code c) scope-position (princ-to-string c))))))))
+
+(defun read-name (parser map topic)
+ "CTM 3.11: optional Topic reference plus ':' followed by a string.
+Use the shared Topic reference resolver."
+ (let* ((token (look parser))
+ (typed (and (topic-reference-start-p token)
+ (or (not (eq :variable (token-kind token))) (next-punctuation-p parser #\:))))
+ (type (when typed (topic-reference parser map))))
+ (when typed
+ (unless (punctuation-p parser #\:)
+ (problem 'ctm-rejection :expected-name-type-separator
+ (token-position (look parser)) "Expected ':' after the name type."))
+ (consume parser))
+ (let* ((value (consume parser))
+ (name-value (if (eq :variable (token-kind value)) (bound-name parser value) (token-value value))))
+ (unless (member (token-kind value) '(:string :variable))
+ (problem 'ctm-rejection :expected-name-value (token-position value)
+ "Expected a quoted name value after '-' or the name type separator."))
+ (let ((scope (read-scope parser map)))
+ (multiple-value-bind (r position) (read-reifier parser map)
+ (let ((name (attach-reifier
+ (if typed (topic-maps.tmdm:add-name topic name-value :type type :scope scope)
+ (topic-maps.tmdm:add-name topic name-value :scope scope)) r position)))
+ (loop while (punctuation-p parser #\() do (read-variant parser map name))))))
+ (topic-maps.tmdm:canonical-topic topic)))
+
+(defun relationship-keyword-p (token)
+ (and (eq :identifier (token-kind token)) (member (token-value token) '("isa" "ako") :test #'string=)))
+
+(defun read-type-relationship (parser map topic)
+ "CTM 3.9: expand one isa/ako statement using ordinary same-map Associations."
+ (let* ((keyword (consume parser)) (isa (string= (token-value keyword) "isa")) (next (look parser)))
+ (unless (and (topic-reference-start-p next)
+ (not (and (eq :identifier (token-kind next))
+ (member (token-value next) '("isa" "ako" "def" "end") :test #'string=))))
+ (problem 'ctm-rejection :expected-relationship-topic (token-position next)
+ "Expected one Topic reference after isa or ako."))
+ (let ((target (topic-reference parser map)))
+ (when (punctuation-p parser #\,)
+ (problem 'ctm-rejection :unexpected-relationship-list (token-position (look parser))
+ "isa/ako takes one Topic reference; repeat the keyword in a separate tail element."))
+ (handler-case
+ (flet ((core (local)
+ ;; Resolve locators every time; do not cache replaceable Topic objects.
+ (topic-maps.tmdm:intern-topic map :subject-identifier
+ (concatenate 'string "http://psi.topicmaps.org/iso13250/model/" local))))
+ (let* ((type (core (if isa "type-instance" "supertype-subtype")))
+ (left (core (if isa "instance" "subtype"))) (right (core (if isa "type" "supertype"))))
+ (topic-maps.tmdm:make-association map type (list (list left topic) (list right target)))
+ (topic-maps.tmdm:canonical-topic topic)))
+ (topic-maps.tmdm:core-subject-error (c)
+ (problem 'ctm-rejection (topic-maps.tmdm:core-subject-error-code c)
+ (token-position keyword) (princ-to-string c)))))))