X-Git-Url: https://dreyeck.freedombox.rocks/gitweb/topic-maps.git/blobdiff_plain/437ad53ea7539d36db3fa871d0931650b7a6a03e..8741d155d948c7317945eee423a977940cad995e:/src/ctm/reader.lisp?ds=inline diff --git a/src/ctm/reader.lisp b/src/ctm/reader.lisp index 32610d6..f73c242 100644 --- a/src/ctm/reader.lisp +++ b/src/ctm/reader.lisp @@ -1,6 +1,7 @@ (in-package #:topic-maps.ctm) -(defstruct (parser (:constructor make-parser (lexer document-iri))) lexer document-iri lookahead) +(defstruct (parser (:constructor make-parser (lexer document-iri))) + lexer document-iri lookahead (prefixes (make-hash-table :test #'equal))) (defun look (parser) (or (parser-lookahead parser) (setf (parser-lookahead parser) (next-token (parser-lexer parser))))) @@ -10,6 +11,33 @@ (and (eq :punctuation (token-kind (look parser))) (eql character (token-value (look parser))))) +(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)) + (prefix (subseq text 0 colon)) (bound (gethash prefix (parser-prefixes parser)))) + (unless bound + (problem 'undefined-prefix :undefined-prefix (token-position token) + (format nil "Prefix ~S has no binding." prefix))) + ;; Do not re-resolve or percent-decode an already resolved binding. + (validate-iri-characters (concatenate 'string bound (subseq text (1+ colon))) + (token-position token)))) + +(defun read-prefix-directive (parser) + "CTM 3.16.1: document-ordered, per-deserialization bindings; reference is an IRI." + (consume parser) ; %prefix + (let ((name (consume parser)) (reference (consume parser))) + (unless (eq :identifier (token-kind name)) + (problem 'ctm-rejection :expected-prefix-name (token-position name) "Expected a prefix identifier.")) + (unless (eq :iri (token-kind reference)) + (problem 'ctm-rejection :expected-prefix-iri (token-position reference) + "A prefix binding requires an IRI, not a QName or Topic identifier.")) + (let* ((iri (resolve-iri (token-value reference) (parser-document-iri parser) (token-position reference))) + (old (gethash (token-value name) (parser-prefixes parser)))) + (when (and old (not (string= old iri))) + (problem 'prefix-collision :prefix-collision (token-position name) + (format nil "Prefix ~S is already bound to ~S, not ~S." (token-value name) old iri))) + (setf (gethash (token-value name) (parser-prefixes parser)) iri)))) + (defun identity-value (parser &optional explicit-kind) (let ((token (consume parser))) (case (token-kind token) @@ -23,12 +51,7 @@ (: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. 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 - (position #\: (token-value token)))))) + (:qname (values (or explicit-kind :subject-identifier) (resolve-qname parser token))) (:punctuation (case (token-value token) (#\= (if explicit-kind @@ -49,76 +72,74 @@ (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.")) + (: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 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) + (: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-timezone) :datetime-timezone-canonicalization :datetime-leap-second-context) + (token-position token) "DateTime value normalization requires an unsupported timezone/leap 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) - (when (punctuation-p 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) - (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))) + (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 a quoted string or IRI-form Occurrence value."))))) + "Expected an integer, decimal, date, dateTime, quoted string or IRI-form Occurrence value."))))) (defun read-identity-tail (parser map topic) (let ((start (look parser))) @@ -216,7 +237,7 @@ (topic-maps.tmdm:make-association map type (nreverse roles)))) (defun deserialize-string (text &key document-iri source-name) - "Read the documented identity/name/string-IRI Occurrence/unary-binary Association CTM subset. + "Read the documented CTM subset, with per-document prefixes and qualified strings. Source positions are zero-based character offsets; lines/columns are one-based." (check-type text string) (let ((*source-name* source-name) (*source-text* text)) @@ -224,10 +245,12 @@ Source positions are zero-based character offsets; lines/columns are one-based." (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 - (let ((topic (identity-reference parser map))) - (if (punctuation-p parser #\() - (read-association parser map topic) - (read-topic parser map topic)))) + (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))) (defun deserialize-file (pathname &key document-iri source-name)