]> dreyeck.freedombox.rocks Git - topic-maps.git/blobdiff - src/ctm/reader.lisp
test(ctm): register original empty2 prolog contract
[topic-maps.git] / src / ctm / reader.lisp
index c510c6319cca2396d48c087544bf4d49688c9f68..3edec0928ee4198dd5ff4e14376bbc683f23624f 100644 (file)
@@ -2,7 +2,7 @@
 
 (defvar *active-resource-iris* nil)
 
 
 (defvar *active-resource-iris* nil)
 
-(defstruct template-definition name parameters body position)
+(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)
 ;; 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)
@@ -415,10 +415,38 @@ Use the shared Topic reference resolver."
                 (attach-reifier role reifier marker))))
           (attach-reifier (topic-maps.tmdm:canonical-association association) r position))))))
 
                 (attach-reifier role reifier marker))))
           (attach-reifier (topic-maps.tmdm:canonical-association association) r position))))))
 
-(defun deserialize-string (text &key document-iri source-name (wildcard-iri document-iri) (wildcard-counter 0)
+(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 (string-equal name "UTF-8")
+          (problem 'ctm-unsupported :unsupported-encoding position "Only UTF-8 CTM input is 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.
                                       resource-resolver (max-resource-depth 32))
   "Read the documented CTM subset, with per-document prefixes and qualified strings.
-Return the Map and final Wildcard Counter as two values. Wildcard IRI defaults
+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,
 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,
@@ -446,6 +474,7 @@ There is no implicit retrieval. MAX-RESOURCE-DEPTH bounds nested imports (defaul
                      (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)))))
                      (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
         (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
@@ -455,12 +484,26 @@ There is no implicit retrieval. MAX-RESOURCE-DEPTH bounds nested imports (defaul
           (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)))))
           (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)))))
+      (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 (wildcard-iri document-iri) (wildcard-counter 0)
+(defun deserialize-file (pathname &key document-iri source-name encoding (wildcard-iri document-iri) (wildcard-counter 0)
                                         resource-resolver (max-resource-depth 32))
                                         resource-resolver (max-resource-depth 32))
-  "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."
+  "Read CTM octets using an explicit Document IRI and optional supplied ENCODING.
+Check supplied encoding/BOM/declaration conflicts before strict UTF-8 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)
   (let ((*source-name* source-name) (*source-text* ""))
     (require-document-iri document-iri)
     (require-wildcard-environment wildcard-iri wildcard-counter)
@@ -468,7 +511,7 @@ Other encoding directives are UNSUPPORTED. File I/O errors propagate unchanged."
       (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."))
       (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)
+        (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)))))
                             :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)))))