+ (let ((position (token-position (look parser))))
+ (multiple-value-bind (kind iri) (identity-value parser)
+ (handler-case (if (eq kind :bound-topic) (topic-maps.tmdm:canonical-topic iri)
+ (topic-maps.tmdm:intern-topic map kind iri))
+ (topic-maps.tmdm:core-subject-error (c)
+ (problem 'ctm-rejection (topic-maps.tmdm:core-subject-error-code c) position (princ-to-string c)))))))
+
+(defun topic-reference (parser map)
+ "CTM 3.3.7: one recursive reference path for Types, Players and Scope themes."
+ (if (punctuation-p parser #\[)
+ (progn
+ (consume parser)
+ (let ((topic (topic-maps.tmdm:intern-topic map :item-identifier (wildcard-locator parser))))
+ (read-topic-tail parser map topic #\] :required t)))
+ (identity-reference parser map)))
+
+(defun read-scope (parser map)
+ "CTM 3.3.9: optional @ followed by a nonempty comma-separated Topic set.
+Use the shared Topic reference resolver, including Embedded Topics and Wildcards.
+The owning information item is not constructed until this function finishes."
+ (when (punctuation-p parser #\@)
+ (consume parser)
+ (let ((themes nil))
+ (loop
+ ;; A reifier marker where a Theme is required is malformed Scope,
+ ;; while a reifier after a complete Scope belongs to its statement.
+ (unless (parser-lookahead parser)
+ (skip-layout (parser-lexer parser))
+ (when (eql #\~ (peek-character (parser-lexer parser)))
+ (problem 'ctm-rejection :expected-scope-theme (lexer-cursor (parser-lexer parser))
+ "Expected a Theme, not a reifier marker.")))
+ (let ((token (look parser)))
+ (unless (topic-reference-start-p token)
+ (problem 'ctm-rejection :expected-scope-theme (token-position token)
+ "Expected a Topic reference after '@' or the scope comma."))
+ (push (topic-reference parser map) themes))
+ (if (punctuation-p parser #\,) (consume parser) (return (nreverse themes)))))))
+
+(defun literal-iri-reference (parser)
+ "An IRI locator, not a Topic identity. Used for values/datatype syntax."
+ (let ((token (consume parser)))
+ (case (token-kind token)
+ (:iri (resolve-iri (token-value token) (parser-document-iri parser) (token-position token)))
+ (:qname (resolve-qname parser token))
+ (otherwise (problem 'ctm-rejection :expected-datatype-iri (token-position token)
+ "A datatype qualifier requires an IRI reference, not a Topic identifier.")))))
+
+(defun read-literal (parser &optional (context :occurrence))
+ "Shared CTM 3.4.1 literal reader for Occurrences and Variants."
+ (let ((token (look parser)))
+ (case (token-kind token)
+ (:variable (consume parser) (bound-literal parser token))
+ (:integer
+ (consume parser)
+ ;; Numeric-literal value policy: represent the unquoted integer value
+ ;; canonically; qualified quoted strings retain their lexical value.
+ (values (topic-maps.tmdm:canonical-integer-lexical-form (token-value token))
+ (topic-maps.tmdm:integer-datatype)))
+ (:decimal
+ (consume parser)
+ ;; Same value-representation policy as bare integers; token spelling is
+ ;; retained by the lexer, qualified strings remain lexical model data.
+ (values (topic-maps.tmdm:canonical-decimal-lexical-form (token-value token))
+ (topic-maps.tmdm:decimal-datatype)))
+ (:date
+ (consume parser)
+ (values (topic-maps.tmdm:canonical-date-lexical-form (token-value token)) (topic-maps.tmdm:date-datatype)))
+ (:datetime
+ (consume parser)
+ (multiple-value-bind (value reason) (topic-maps.tmdm:canonical-datetime-lexical-form (token-value token))
+ (unless value (problem 'ctm-unsupported
+ (if (eq reason :unsupported-bce-timezone-rollover)
+ :datetime-bce-timezone-rollover :datetime-leap-second-context)
+ (token-position token) "DateTime normalization is outside the supported BCE/leap-second calendar context."))
+ (values value (topic-maps.tmdm:datetime-datatype))))
+ (:ctm-integer
+ (consume parser)
+ ;; CTM 3.4.1 [33]: opaque lexical value, unrelated to Wildcards/counters.
+ (values "*" "http://psi.topicmaps.org/iso13250/ctm-integer"))
+ (:string
+ (consume parser)
+ (if (punctuation-p parser #\^)
+ (let ((caret (consume parser)))
+ (unless (and (punctuation-p parser #\^)
+ (= (token-position (look parser)) (1+ (token-position caret))))
+ (problem 'ctm-rejection :invalid-datatype-marker (token-position caret) "Expected contiguous '^^'."))
+ (consume parser)
+ (let ((datatype (literal-iri-reference parser)))
+ (when (and (string= datatype (topic-maps.tmdm:integer-datatype))
+ (not (topic-maps.tmdm:integer-lexical-p (token-value token))))
+ (problem 'ctm-rejection :invalid-integer-value (token-position token)
+ "Quoted value is not an xsd:integer lexical form."))
+ (when (and (string= datatype (topic-maps.tmdm:decimal-datatype))
+ (not (topic-maps.tmdm:decimal-lexical-p (token-value token))))
+ (problem 'ctm-rejection :invalid-decimal-value (token-position token)
+ "Quoted value is not an xsd:decimal lexical form."))
+ (when (and (string= datatype (topic-maps.tmdm:date-datatype))
+ (not (topic-maps.tmdm:date-lexical-p (token-value token))))
+ (problem 'ctm-rejection :invalid-date-value (token-position token)
+ "Quoted value is not a valid XSD 1.0 date lexical form."))
+ (when (and (string= datatype (topic-maps.tmdm:datetime-datatype))
+ (not (topic-maps.tmdm:datetime-lexical-p (token-value token))))
+ (problem 'ctm-rejection :invalid-datetime-value (token-position token)
+ "Quoted value is not a valid XSD 1.0 dateTime."))
+ ;; TMDM 4.4 requires absolute anyURI content; quoted content is
+ ;; lexical data, never an IRI reference to resolve against the document.
+ (when (and (string= datatype (topic-maps.tmdm:iri-datatype))
+ (not (topic-maps.tmdm::absolute-locator-p (token-value token))))
+ (problem 'ctm-rejection :relative-uri-value (token-position token)
+ "A quoted anyURI value must already be absolute (TMDM 4.4)."))
+ (values (token-value token) datatype)))
+ (values (token-value token) (topic-maps.tmdm:string-datatype))))
+ ((:iri :qname) (values (literal-iri-reference parser) (topic-maps.tmdm:iri-datatype)))
+ (otherwise (problem 'ctm-rejection (if (eq context :variant) :expected-variant-value :expected-occurrence-value)
+ (token-position token) "Expected a supported CTM literal value.")))))
+
+(defun read-reifier (parser map)
+ "CTM 3.3.10: reuse the full Topic reference resolver, retaining the marker position."
+ (when (punctuation-p parser #\~)
+ (let ((position (token-position (consume parser))))
+ (unless (topic-reference-start-p (look parser))
+ (problem 'ctm-rejection :expected-reifier-topic (token-position (look parser)) "Expected a Topic reference after '~'."))
+ (values (topic-reference parser map) position))))
+(defun attach-reifier (item topic position)
+ (when topic
+ (handler-case (setf item (topic-maps.tmdm:set-item-reifier item topic))
+ (topic-maps.tmdm:reification-error (c)
+ (problem 'ctm-rejection (topic-maps.tmdm:reification-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:core-subject-error (c)
+ (problem 'ctm-rejection (topic-maps.tmdm:core-subject-error-code c) position (princ-to-string c)))))
+ item)
+
+(defun read-identity-tail (parser map topic)
+ (let* ((start (look parser)) (embedded (punctuation-p parser #\[))
+ (type (when embedded (topic-reference parser map))))
+ ;; Identity tail statements add directly to their owner, without constructing
+ ;; an intermediate Topic. Identity-form Type references use the same resolver.
+ (multiple-value-bind (kind iri) (unless embedded (identity-value parser))
+ (if (punctuation-p parser #\:)
+ (progn
+ (consume parser)
+ (multiple-value-bind (value datatype) (read-literal parser)
+ (let* ((type (or type (if (eq kind :bound-topic) (topic-maps.tmdm:canonical-topic iri)
+ (topic-maps.tmdm:intern-topic map kind iri))))
+ (scope (read-scope parser map)))
+ (multiple-value-bind (r position) (read-reifier parser map)
+ (attach-reifier (topic-maps.tmdm:add-occurrence topic type value :datatype datatype :scope scope) r position))
+ (topic-maps.tmdm:canonical-topic topic))))
+ (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)))))))