]> dreyeck.freedombox.rocks Git - topic-maps.git/blobdiff - src/ctm/reader.lisp
Add string and URI Occurrences with merging and CXTM validation
[topic-maps.git] / src / ctm / reader.lisp
index 488a2f41052ca5804a8dd5710235101a307a492c..32610d6f92ae2e811d329254c46926b14a730727 100644 (file)
        (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 Slice 1."))
+         (problem 'ctm-unsupported :unsupported-keyword (token-position token) "This keyword is outside the implemented subset."))
        (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
-       ;; No prefix declarations are supported in Slice 1. This is a specific
+       ;; No prefix declarations are supported. This is a specific
        ;; semantic rejection, not an I/O failure or an unsupported-parser stub.
        (problem 'undefined-prefix :undefined-prefix (token-position token)
                 (format nil "Prefix ~S has no binding." (subseq (token-value token) 0
   (multiple-value-bind (kind iri) (identity-value parser)
     (topic-maps.tmdm:intern-topic map kind iri)))
 
-(defun read-topic (parser map)
-  (let ((topic (identity-reference parser map)) (has-tail nil))
+(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 (problem 'undefined-prefix :undefined-prefix (token-position token)
+                       "The IRI-reference prefix has no binding."))
+      (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."))))))
+  (let ((token (look parser)))
+    (case (token-kind token)
+      (:string
+       (consume parser)
+       (when (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)
+           (literal-iri-reference parser)
+           (problem 'ctm-unsupported :typed-literal (token-position caret)
+                    "Explicit datatype-qualified literals are outside this subset.")))
+       (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.")))))
+
+(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))
     (loop
       (cond
         ((punctuation-p parser #\.) (consume parser) (return topic))
                  (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 Slice 1 (CTM 3.11)."))
+                      "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 '-'."))
          (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)) '(:iri :qname)))
+             (member (token-kind (look parser)) '(:identifier :iri :qname)))
          (when has-tail
            (problem 'ctm-rejection :expected-separator (token-position (look parser)) "Topic-tail statements need a semicolon separator."))
-         ;; CTM 3.9 adds the locator to this Topic, without creating another
-         ;; Topic just for the assignment. The model performs required merging.
-         (multiple-value-bind (kind iri) (identity-value parser)
-           (setf topic (topic-maps.tmdm:add-topic-identifier topic kind iri)))
-         (setf has-tail t))
-        ((eq :identifier (token-kind (look parser)))
-         (problem 'ctm-unsupported :topic-tail (token-position (look parser)) "Type/template topic tails are outside the implemented subset."))
+         (setf topic (read-identity-tail parser map topic) has-tail t))
+        ((punctuation-p parser #\()
+         (problem 'ctm-unsupported :variant (token-position (look parser)) "Variant names are outside the implemented subset."))
         (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."
+  (consume parser) ; opening parenthesis already recognized by the caller
+  (let ((roles nil) (third-position 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))
+             (type-end (lexer-cursor (parser-lexer parser)))
+             (colon (look parser)))
+        (unless (punctuation-p parser #\:)
+          (problem 'ctm-rejection :expected-role-type-separator (token-position colon) "Expected ':' after the Role type."))
+        (consume parser)
+        (let ((player-token (look parser)))
+          ;; CTM role production requires layout on at least one side of ':'.
+          ;; skip-layout handles whitespace and comments; gaps use source offsets.
+          (unless (or (> (token-position colon) type-end)
+                      (> (token-position player-token) (1+ (token-position colon))))
+            (problem 'ctm-rejection :role-separator-whitespace (token-position colon)
+                     "A Role separator needs whitespace or a comment on at least one side."))
+          (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))
+      (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))))
+
 (defun deserialize-string (text &key document-iri source-name)
-  "Read the documented identity/name CTM subset into a new TMDM Topic Map.
+  "Read the documented identity/name/string-IRI Occurrence/unary-binary Association CTM subset.
 Source positions are zero-based character offsets; lines/columns are one-based."
   (check-type text string)
   (let ((*source-name* source-name) (*source-text* text))
     (require-document-iri document-iri)
     (let ((parser (make-parser (make-lexer text) document-iri))
           (map (topic-maps.tmdm:make-topic-map)))
-      (loop until (eq :eof (token-kind (look parser))) do (read-topic parser map))
+      (loop until (eq :eof (token-kind (look parser))) do
+        (let ((topic (identity-reference parser map)))
+          (if (punctuation-p parser #\()
+              (read-association parser map topic)
+              (read-topic parser map topic))))
       map)))
 
 (defun deserialize-file (pathname &key document-iri source-name)