+(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 (topic-reference-start-p token))
+ (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)))
+ (unless (eq :string (token-kind value))
+ (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 (token-value value) :type type :scope scope)
+ (topic-maps.tmdm:add-name topic (token-value 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)))))))
+
+(defun read-topic-tail (parser map topic terminator &key required)
+ "CTM 3.9: the same Tail reader ends at '.' or the caller's Embedded ']'."
+ (let ((has-tail nil) (seen-tail nil))