+(defun identity-reference (parser map)
+ (multiple-value-bind (kind iri) (identity-value parser)
+ (topic-maps.tmdm:intern-topic map kind iri)))
+
+(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-occurrence-literal (parser)
+ (let ((token (look parser)))
+ (case (token-kind 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))))
+ (:excluded-literal
+ (problem 'ctm-unsupported :occurrence-literal-datatype (token-position token)
+ "The separate CTM '*' literal is outside this slice."))
+ (: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 :expected-occurrence-value (token-position token)
+ "Expected an integer, decimal, date, dateTime, quoted string or IRI-form Occurrence value.")))))
+
+(defun read-identity-tail (parser map topic)
+ (let ((start (look parser)))
+ (multiple-value-bind (kind iri) (identity-value parser)
+ (if (punctuation-p parser #\:)
+ (progn
+ (consume parser)
+ (multiple-value-bind (value datatype) (read-occurrence-literal parser)
+ (let ((type (topic-maps.tmdm:intern-topic map kind iri)))
+ (topic-maps.tmdm:add-occurrence topic type value :datatype datatype)
+ (topic-maps.tmdm:canonical-topic topic))))
+ (if (eq :identifier (token-kind start))
+ (if (punctuation-p parser #\()
+ (problem 'ctm-unsupported :topic-tail (token-position start) "Template invocations are outside this subset.")
+ (problem 'ctm-rejection :expected-occurrence-type-separator (token-position (look parser))
+ "Expected ':' after the Occurrence Type."))
+ (topic-maps.tmdm:add-topic-identifier topic kind iri))))))
+
+(defun read-topic (parser map &optional (topic (identity-reference parser map)))
+ (let ((has-tail nil))