]> dreyeck.freedombox.rocks Git - topic-maps.git/blobdiff - src/ctm/reader.lisp
docs: design draft for a read-only SHOP3 adapter over TMDM objects
[topic-maps.git] / src / ctm / reader.lisp
index cc449c82d5e329f659a634691835dfcd6fc5d179..f8741a7e8b59aab4f711aeed6fd3d37729047dcb 100644 (file)
@@ -1,16 +1,50 @@
 (in-package #:topic-maps.ctm)
 
-(defstruct (parser (:constructor make-parser (lexer document-iri)))
-  lexer document-iri lookahead (prefixes (make-hash-table :test #'equal)))
+(defvar *active-resource-iris* nil)
+
+(defstruct template-definition name parameters body position source-name source-text document-iri)
+;; Validated concrete syntax, never TMDM information items or Lisp forms.
+(defstruct ctm-statement kind tokens position)
+(defstruct template-argument kind token topic value datatype plain-string-p)
+(defstruct (parser (:constructor make-parser (lexer document-iri wildcard-iri wildcard-counter)))
+  lexer document-iri wildcard-iri wildcard-counter lookahead resource-resolver (max-resource-depth 32)
+  replay-p remaining recording recorded bindings
+  (templates (make-hash-table :test #'equal))
+  (wildcards (make-hash-table :test #'equal)) (prefixes (make-hash-table :test #'equal)))
 (defun look (parser)
   (or (parser-lookahead parser)
-      (setf (parser-lookahead parser) (next-token (parser-lexer parser)))))
+      (setf (parser-lookahead parser)
+        (if (parser-replay-p parser)
+            (let ((token (or (pop (parser-remaining parser))
+                             (make-token :kind :eof :position (lexer-cursor (parser-lexer parser))))))
+              (setf (lexer-cursor (parser-lexer parser)) (or (token-end token) (token-position token))) token)
+            (let ((token (next-token (parser-lexer parser))))
+              (setf (token-end token) (lexer-cursor (parser-lexer parser))) token)))))
 (defun consume (parser)
-  (prog1 (look parser) (setf (parser-lookahead parser) nil)))
+  (let ((token (look parser)))
+    (when (parser-recording parser) (push token (parser-recorded parser)))
+    (setf (parser-lookahead parser) nil) token))
 (defun punctuation-p (parser character)
   (and (eq :punctuation (token-kind (look parser)))
        (eql character (token-value (look parser)))))
 
+(defun require-wildcard-environment (iri counter)
+  (handler-case (require-document-iri iri)
+    (ctm-rejection () (problem 'ctm-rejection :invalid-wildcard-iri 0 "Wildcard IRI must be an absolute, fragment-free IRI.")))
+  (unless (and (integerp counter) (>= counter 0))
+    (problem 'ctm-rejection :invalid-wildcard-counter 0 "Wildcard counter must be a non-negative integer.")))
+
+(defun wildcard-locator (parser &optional name)
+  "CTM 3.3.8: cache locators, not Lisp object handles; identity uses INTERN-TOPIC."
+  (or (and name (gethash name (parser-wildcards parser)))
+      (let ((iri (format nil "~A#$__~D~@[.~A~]" (parser-wildcard-iri parser)
+                         (incf (parser-wildcard-counter parser)) name)))
+        (when name (setf (gethash name (parser-wildcards parser)) iri)) iri)))
+
+(defun topic-reference-start-p (token)
+  (or (member (token-kind token) '(:identifier :iri :qname :wildcard :variable))
+      (and (eq :punctuation (token-kind token)) (find (token-value token) "=^["))))
+
 (defun resolve-qname (parser token)
   "CTM 3.3.4: concatenate the resolved binding and local part exactly once."
   (let* ((text (token-value token)) (colon (position #\: text))
 (defun identity-value (parser &optional explicit-kind)
   (let ((token (consume parser)))
     (case (token-kind token)
+      (:variable (bound-identity parser token explicit-kind))
       (:identifier
        (when explicit-kind
          (problem 'ctm-rejection :expected-iri (token-position token) "Explicit = or ^ needs an IRI reference."))
-       (when (member (token-value token) '("def" "isa" "ako" "end") :test #'string=)
-         (problem 'ctm-unsupported :unsupported-keyword (token-position token) "This keyword is outside the implemented subset."))
+       (when (member (token-value token) '("isa" "ako") :test #'string=)
+         (problem 'ctm-rejection :reserved-topic-identifier (token-position token) "A relationship keyword is not a Topic identity."))
+       (when (member (token-value token) '("def" "end") :test #'string=)
+         (problem 'ctm-rejection :unexpected-template-keyword (token-position token) "Unexpected template delimiter."))
        (values :item-identifier
           (concatenate 'string (parser-document-iri parser) "#" (token-value token))))
       (:iri
        (values (or explicit-kind :subject-identifier)
           (resolve-iri (token-value token) (parser-document-iri parser) (token-position token))))
       (:qname (values (or explicit-kind :subject-identifier) (resolve-qname parser token)))
+      (:wildcard
+       (when explicit-kind
+         (problem 'ctm-rejection :expected-iri (token-position token) "Explicit = or ^ needs an IRI reference."))
+       (values :item-identifier (wildcard-locator parser (token-value token))))
       (:punctuation
        (case (token-value token)
          (#\= (if explicit-kind
       (otherwise (problem 'ctm-rejection :expected-topic-identity (token-position token) "Expected topic identity.")))))
 
 (defun identity-reference (parser map)
-  (multiple-value-bind (kind iri) (identity-value parser)
-    (topic-maps.tmdm:intern-topic map kind iri)))
+  (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."
       (otherwise (problem 'ctm-rejection :expected-datatype-iri (token-position token)
                           "A datatype qualifier requires an IRI reference, not a Topic identifier.")))))
 
-(defun excluded-literal-p (text)
-  "Recognize unsupported numeric/date literal syntax, without constructing values."
-  (labels ((digits (s) (and (plusp (length s)) (every (lambda (c) (find c "0123456789")) s)))
-           (numeric (s)
-             (let* ((s (if (and (plusp (length s)) (find (char s 0) "+-")) (subseq s 1) s))
-                    (dot (position #\. s)))
-               (if dot (and (or (zerop dot) (digits (subseq s 0 dot))) (digits (subseq s (1+ dot)))) (digits s))))
-           (date (s)
-             (let* ((s (if (and (plusp (length s)) (char= (char s 0) #\-)) (subseq s 1) s))
-                    (dash (position #\- s)))
-               (and dash (>= dash 4) (digits (subseq s 0 dash))
-                    (= (length s) (+ dash 6)) (char= (char s (+ dash 3)) #\-)
-                    (digits (subseq s (1+ dash) (+ dash 3))) (digits (subseq s (+ dash 4)))
-                    (<= 1 (parse-integer s :start (1+ dash) :end (+ dash 3)) 12)
-                    (<= 1 (parse-integer s :start (+ dash 4)) 31))))
-           (time-literal (s)
-             (and (>= (length s) 8) (digits (subseq s 0 2)) (char= (char s 2) #\:)
-                  (digits (subseq s 3 5)) (char= (char s 5) #\:) (digits (subseq s 6 8))
-                  (let* ((rest (subseq s 8))
-                         (end (position-if (lambda (c) (find c "Z+-")) rest))
-                         (fraction (subseq rest 0 end)) (zone (if end (subseq rest end) "")))
-                    (and (or (zerop (length fraction))
-                             (and (char= (char fraction 0) #\.) (digits (subseq fraction 1))))
-                         (or (member zone '("" "Z") :test #'string=)
-                             (and (= 6 (length zone)) (find (char zone 0) "+-")
-                                  (digits (subseq zone 1 3)) (char= (char zone 3) #\:)
-                                  (digits (subseq zone 4 6)))))))))
-    (or (numeric text) (date text) (string= text "*")
-        (let ((tee (position #\T text))) (and tee (date (subseq text 0 tee)) (time-literal (subseq text (1+ tee))))))))
-
-(defun read-occurrence-literal (parser)
-  ;; The dedicated lexer already preserves multiline strings and escape values.
-  ;; Numeric/date sugar is recognized only to report an unsupported construct.
-  (unless (parser-lookahead parser)
-    (let ((lexer (parser-lexer parser)))
-      (skip-layout lexer)
-      (let ((c (peek-character lexer)) (start (lexer-cursor lexer)))
-        (when (and c (or (find c "0123456789+*")
-                        (and (find c "-.") (peek-character lexer 1)
-                             (find (peek-character lexer 1) "0123456789"))))
-          (let* ((end (position-if (lambda (c) (or (whitespace-p c) (find c ";@~)")))
-                                  (lexer-text lexer) :start start))
-                 (text (subseq (lexer-text lexer) start end))
-                 (text (if (and (plusp (length text)) (char= (char text (1- (length text))) #\.))
-                           (subseq text 0 (1- (length text))) text)))
-            (problem (if (excluded-literal-p text) 'ctm-unsupported 'ctm-rejection)
-                     (if (excluded-literal-p text) :occurrence-literal-datatype :invalid-occurrence-literal)
-                     start "Only string and IRI-form Occurrence literals are implemented."))))))
+(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 #\^)
              (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))
              (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 a quoted string or IRI-form Occurrence value.")))))
+      (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)))
-    (multiple-value-bind (kind iri) (identity-value parser)
+  (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-occurrence-literal parser)
-              (let ((type (topic-maps.tmdm:intern-topic map kind iri)))
-                (topic-maps.tmdm:add-occurrence topic type value :datatype datatype)
+            (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 (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))))))
+          (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)))))))
 
-(defun read-topic (parser map &optional (topic (identity-reference parser map)))
-  (let ((has-tail nil))
+(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))
     (loop
       (cond
-        ((punctuation-p parser #\.) (consume parser) (return topic))
+        ((punctuation-p parser terminator)
+         (when (and required (not seen-tail))
+           (problem 'ctm-rejection :expected-topic-tail (token-position (look parser)) "An Embedded Topic requires a nonempty Topic Tail."))
+         (consume parser) (return (topic-maps.tmdm:canonical-topic topic)))
+        ((and (char= terminator #\]) (punctuation-p parser #\.))
+         (problem 'ctm-rejection :missing-embedded-close (token-position (look parser)) "Embedded Topics end with ']', not a period."))
         ((punctuation-p parser #\-)
          (when has-tail
            (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
          (consume parser)
-         (let ((value (consume parser)))
-           (when (member (token-kind value) '(:identifier :iri :qname))
-             ;; Recognize the typed-name syntax, without resolving its type
-             ;; or creating information items for unsupported name semantics.
-             (unless (punctuation-p parser #\:)
-               (problem 'ctm-rejection :expected-name-type-separator
-                        (token-position (look parser)) "Expected ':' after the name type."))
-             (consume parser)
-             (let ((literal (consume parser)))
-               (unless (eq :string (token-kind literal))
-                 (problem 'ctm-rejection :expected-name-value (token-position literal)
-                          "Expected a quoted name value after the name type separator.")))
-             (problem 'ctm-unsupported :typed-name (token-position value)
-                      "Typed names are outside the implemented subset (CTM 3.11)."))
-           (unless (eq :string (token-kind value))
-             (problem 'ctm-rejection :expected-name-value (token-position value)
-                      "Expected a quoted name value after '-'."))
-           (topic-maps.tmdm:add-default-name topic (token-value value)))
-         (setf has-tail t))
+         (setf topic (read-name parser map topic))
+         (setf has-tail t seen-tail t))
         ((and has-tail (punctuation-p parser #\;))
          (consume parser)
-         (setf has-tail nil)
-         (when (punctuation-p parser #\.) (consume parser) (return topic)))
+         (setf has-tail nil))
         ((eq :eof (token-kind (look parser)))
-         (problem 'ctm-rejection :missing-period (token-position (look parser)) "Topic declaration requires a final period."))
-        ((or (and (eq :punctuation (token-kind (look parser)))
-                  (find (token-value (look parser)) "=^"))
-             (member (token-kind (look parser)) '(:identifier :iri :qname)))
+         (problem 'ctm-rejection (if (char= terminator #\]) :missing-embedded-close :missing-period)
+                  (token-position (look parser)) "Expected the closing Topic/Embedded Topic delimiter."))
+        ((relationship-keyword-p (look parser))
          (when has-tail
            (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
-         (setf topic (read-identity-tail parser map topic) has-tail t))
+         (setf topic (read-type-relationship parser map topic) has-tail t seen-tail t))
+        ((tail-invocation-p parser)
+         (when has-tail
+           (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
+         (read-template-invocation parser map topic)
+         (setf topic (topic-maps.tmdm:canonical-topic topic) has-tail t seen-tail t))
+        ((topic-reference-start-p (look parser))
+         (when has-tail
+           (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
+         (setf topic (read-identity-tail parser map topic) has-tail t seen-tail t))
         ((punctuation-p parser #\()
-         (problem 'ctm-unsupported :variant (token-position (look parser)) "Variant names are outside the implemented subset."))
+         (problem 'ctm-rejection :unexpected-variant (token-position (look parser)) "A Variant must immediately follow a Topic Name."))
         (t (problem 'ctm-rejection :unexpected-token (token-position (look parser)) "Unexpected token after topic or name."))))))
 
 (defun read-association (parser map type)
-  "CTM 3.13, restricted to one or two role declarations; no scope/reifier."
+  "CTM 3.13: one or more Role declarations; optional Scope and Reifiers."
   (consume parser) ; opening parenthesis already recognized by the caller
-  (let ((roles nil) (third-position nil))
+  (let ((roles nil) (role-reifiers nil))
     (loop
       (when (or (punctuation-p parser #\)) (eq :eof (token-kind (look parser))))
         (problem 'ctm-rejection :expected-role (token-position (look parser)) "Expected a nonempty Association role declaration."))
-      (when (= 2 (length roles)) (setf third-position (token-position (look parser))))
-      (let* ((role-type (identity-reference parser map))
+      (let* ((role-type (topic-reference parser map))
              (type-end (lexer-cursor (parser-lexer parser)))
              (colon (look parser)))
         (unless (punctuation-p parser #\:)
           (when (or (eq :eof (token-kind player-token))
                     (punctuation-p parser #\)) (punctuation-p parser #\,))
             (problem 'ctm-rejection :expected-role-player (token-position player-token) "Expected a Topic as the Role player.")))
-        (push (list role-type (identity-reference parser map)) roles))
+        (let ((player (topic-reference parser map)))
+          (push (list role-type player) roles)
+          (multiple-value-bind (r position) (read-reifier parser map)
+            (push (list role-type player r position) role-reifiers))))
       (cond ((punctuation-p parser #\,) (consume parser))
             ((punctuation-p parser #\)) (consume parser) (return))
             ((eq :eof (token-kind (look parser)))
              (problem 'ctm-rejection :missing-association-close (token-position (look parser)) "Expected ')' after the Roles."))
             (t (problem 'ctm-rejection :expected-role-separator (token-position (look parser)) "Expected ',' or ')' after a Role."))))
-    (when (> (length roles) 2)
-      (problem 'ctm-unsupported :association-arity third-position "Associations with more than two Role declarations are outside Slice 3."))
-    (topic-maps.tmdm:make-association map type (nreverse roles))))
+    (let ((scope (read-scope parser map)))
+      (multiple-value-bind (r position) (read-reifier parser map)
+        (let ((association (topic-maps.tmdm:make-association map type (nreverse roles) :scope scope)))
+          (dolist (entry (nreverse role-reifiers))
+            (destructuring-bind (role-type player reifier marker) entry
+              (let ((role (find-if (lambda (role)
+                            (and (eq (topic-maps.tmdm:canonical-topic role-type) (topic-maps.tmdm:association-role-type role))
+                                 (eq (topic-maps.tmdm:canonical-topic player) (topic-maps.tmdm:association-role-player role))))
+                                  (topic-maps.tmdm:association-roles association))))
+                (attach-reifier role reifier marker))))
+          (attach-reifier (topic-maps.tmdm:canonical-association association) r position))))))
 
-(defun deserialize-string (text &key document-iri source-name)
+(defun read-prolog (parser)
+  "CTM 3.5: one initial encoding header, then one optional version 1.0."
+  (when (and (eq :directive (token-kind (look parser)))
+             (string= "encoding" (token-value (look parser))))
+    (let ((token (consume parser)))
+      (unless (zerop (token-position token))
+        (problem 'ctm-rejection :misplaced-encoding (token-position token)
+                 "Encoding declaration must be the first characters of the CTM stream."))
+      (multiple-value-bind (name end position)
+          (ctm-encoding-name *source-text* (token-position token) :character)
+        (unless (ctm-encoding-kind name)
+          (problem 'ctm-unsupported :unsupported-encoding position "Only UTF-8 and ISO-8859-5 CTM input are implemented."))
+        (setf (lexer-cursor (parser-lexer parser)) end))))
+  (when (and (eq :directive (token-kind (look parser)))
+             (string= "version" (token-value (look parser))))
+    (consume parser)
+    (let ((lexer (parser-lexer parser)))
+      (skip-layout lexer)
+      (let ((start (lexer-cursor lexer)))
+        ;; Match the fixed terminal before the numeric-literal lexer. A Topic
+        ;; can follow immediately; extra numeric spelling is not version 1.0.
+        (unless (and (ctm-ascii-prefix-p *source-text* start "1.0")
+                     (let ((end (+ start 3)))
+                       (not (and (< end (length *source-text*))
+                                 (find (char *source-text* end) "0123456789.+-")))))
+          (problem 'ctm-rejection :invalid-version start "The only CTM version is the exact spelling 1.0."))
+        (setf (lexer-cursor lexer) (+ start 3))))))
+
+(defun deserialize-context (text &key document-iri source-name (wildcard-iri document-iri) (wildcard-counter 0)
+                                      resource-resolver (max-resource-depth 32))
   "Read the documented CTM subset, with per-document prefixes and qualified strings.
-Source positions are zero-based character offsets; lines/columns are one-based."
+Internal result: Map, final Wildcard Counter and parser environment. Wildcard IRI defaults
+to Document IRI; local identifiers and relative IRIs still use Document IRI.
+Source positions are zero-based character offsets; lines/columns are one-based.
+RESOURCE-RESOLVER, if supplied, is called with an absolute IRI and :PURPOSE,
+:FROM-DOCUMENT-IRI and :NOTATION, returning octets and an optional source key.
+There is no implicit retrieval. MAX-RESOURCE-DEPTH bounds nested imports (default
+32, zero disables them); it is an implementation limit, not a CTM validity rule."
   (check-type text string)
-  (let ((*source-name* source-name) (*source-text* text))
+  (let ((*source-name* source-name) (*source-text* text)
+        (*active-resource-iris* (cons document-iri *active-resource-iris*)))
     (require-document-iri document-iri)
-    (let ((parser (make-parser (make-lexer text) document-iri))
+    (require-wildcard-environment wildcard-iri wildcard-counter)
+    (when (and resource-resolver (not (functionp resource-resolver)))
+      (problem 'ctm-resource-error :invalid-resource-resolver 0 "Supply a resolver function or NIL."))
+    (unless (and (integerp max-resource-depth) (>= max-resource-depth 0))
+      (problem 'ctm-resource-error :invalid-resource-depth-limit 0 "Resource depth limit must be a non-negative integer."))
+    (let ((parser (make-parser (make-lexer text) document-iri wildcard-iri wildcard-counter))
           (map (topic-maps.tmdm:make-topic-map)))
-      (loop until (eq :eof (token-kind (look parser))) do
-        (if (eq :directive (token-kind (look parser)))
-            (read-prefix-directive parser)
-            (let ((topic (identity-reference parser map)))
-              (if (punctuation-p parser #\()
-                  (read-association parser map topic)
-                  (read-topic parser map topic)))))
-      map)))
+      (setf (parser-resource-resolver parser) resource-resolver (parser-max-resource-depth parser) max-resource-depth)
+      (handler-bind ((topic-maps.tmdm:variant-error
+                       (lambda (c) (problem 'ctm-rejection (topic-maps.tmdm:variant-error-code c)
+                                             (lexer-cursor (parser-lexer parser)) (princ-to-string c))))
+                     (topic-maps.tmdm:reification-error
+                       (lambda (c) (problem 'ctm-rejection (topic-maps.tmdm:reification-error-code c)
+                                             (lexer-cursor (parser-lexer parser)) (princ-to-string c))))
+                     (topic-maps.tmdm:core-subject-error
+                       (lambda (c) (problem 'ctm-rejection (topic-maps.tmdm:core-subject-error-code c)
+                                             (lexer-cursor (parser-lexer parser)) (princ-to-string c)))))
+        (read-prolog parser)
+        (loop while (eq :directive (token-kind (look parser))) do (read-directive parser map))
+        (multiple-value-bind (r position) (read-reifier parser map) (attach-reifier map r position))
+        (loop until (eq :eof (token-kind (look parser))) do
+          (when (punctuation-p parser #\~)
+            (problem 'ctm-rejection :topic-map-reifier-position (token-position (look parser))
+                     "Topic Map Reifier must precede all body declarations."))
+          (cond ((eq :directive (token-kind (look parser))) (read-directive parser map))
+                ((identifier-p (look parser) "def") (read-template-definition parser))
+                (t (read-statement parser map)))))
+      (values map (parser-wildcard-counter parser) parser))))
+
+(defun deserialize-string (text &key document-iri source-name (wildcard-iri document-iri) (wildcard-counter 0)
+                                      resource-resolver (max-resource-depth 32))
+  "Deserialize CTM using explicit Document IRI and optional controlled resources.
+Return the Topic Map and final Wildcard Counter. Includes import Templates and
+Topic aliases; Mergemap imports Map content only. No implicit I/O is performed.
+The resolver receives :PURPOSE (:INCLUDE or :MERGEMAP), :FROM-DOCUMENT-IRI and
+:NOTATION (NIL for Include). Source keys are diagnostic, never identity bases."
+  (multiple-value-bind (map counter)
+      (deserialize-context text :document-iri document-iri :source-name source-name
+        :wildcard-iri wildcard-iri :wildcard-counter wildcard-counter
+        :resource-resolver resource-resolver :max-resource-depth max-resource-depth)
+    (values map counter)))
 
-(defun deserialize-file (pathname &key document-iri source-name)
-  "Read a UTF-8 CTM file, with optional UTF-8 BOM, using an explicit Document IRI.
-Other encoding directives are UNSUPPORTED. File I/O errors propagate unchanged."
+(defun deserialize-file (pathname &key document-iri source-name encoding (wildcard-iri document-iri) (wildcard-counter 0)
+                                        resource-resolver (max-resource-depth 32))
+  "Read CTM octets using an explicit Document IRI and optional supplied ENCODING.
+Check supplied encoding/BOM/declaration conflicts before UTF-8 or ISO-8859-5 decoding.
+Other charsets remain UNSUPPORTED. File I/O errors propagate unchanged."
   (let ((*source-name* source-name) (*source-text* ""))
     (require-document-iri document-iri)
+    (require-wildcard-environment wildcard-iri wildcard-counter)
     (with-open-file (input pathname :direction :input :element-type '(unsigned-byte 8))
       (let ((bytes (make-array (file-length input) :element-type '(unsigned-byte 8))))
         (unless (= (length bytes) (read-sequence bytes input))
           (error "Input file changed or was truncated during reading."))
-        (deserialize-string (decode-utf8 bytes :strip-bom t)
-                            :document-iri document-iri :source-name source-name)))))
+        (deserialize-string (decode-ctm-octets bytes :encoding encoding)
+                            :document-iri document-iri :source-name source-name
+                            :wildcard-iri wildcard-iri :wildcard-counter wildcard-counter
+                            :resource-resolver resource-resolver :max-resource-depth max-resource-depth)))))