]> dreyeck.freedombox.rocks Git - topic-maps.git/blobdiff - src/ctm/lexer.lisp
Canonicalize DateTime timezone offsets with explicit baseline discrepancies
[topic-maps.git] / src / ctm / lexer.lisp
index 6331cde1dc9449229f02e7ab1c6d55115be15a69..04289a61fa99d39847f7bd6b474944a7f773af43 100644 (file)
                  (return))))
           (t (return)))))
 
+(defun excluded-literal-p (text)
+  (string= text "*"))
+
+(defun read-number-token (lexer start)
+  "One token path for Integer, Decimal, Date and DateTime; * remains excluded.
+A dot followed by a digit can belong to a decimal; a terminal dot stays punctuation."
+  (setf (lexer-cursor lexer) start)
+  (loop for c = (peek-character lexer)
+        while (and c (not (whitespace-p c)) (not (find c ";@~()#^<>\""))
+                   (or (not (char= c #\.))
+                       (and (peek-character lexer 1) (find (peek-character lexer 1) "0123456789"))))
+        do (take-character lexer))
+  (let ((value (subseq (lexer-text lexer) start (lexer-cursor lexer))))
+    (cond ((topic-maps.tmdm:integer-lexical-p value)
+           (make-token :kind :integer :value value :position start))
+          ((and (find #\. value) (topic-maps.tmdm:decimal-lexical-p value))
+           (make-token :kind :decimal :value value :position start))
+          ((position #\T value)
+           (multiple-value-bind (parts reason) (topic-maps.tmdm::datetime-parts value)
+             (unless parts (problem 'ctm-rejection reason start "Expected a valid XSD 1.0 dateTime."))
+             (make-token :kind :datetime :value value :position start)))
+          ((excluded-literal-p value) (make-token :kind :excluded-literal :value value :position start))
+          ((position #\- value :start (if (and (plusp (length value)) (find (char value 0) "+-")) 1 0))
+           (multiple-value-bind (a date-end end reason) (topic-maps.tmdm::date-lexical-bounds value)
+             (unless (and a (= date-end end))
+               (problem 'ctm-rejection (or reason :invalid-date-syntax) start
+                        "Expected a valid timezone-free XSD 1.0 date: nonzero year, valid month/day and no leading plus or extended-year zeroes."))
+             (make-token :kind :date :value value :position start)))
+          (t (problem 'ctm-rejection :invalid-occurrence-literal start "Malformed numeric/date literal.")))))
+
 (defun next-token (lexer)
   (skip-layout lexer)
   (let* ((start (lexer-cursor lexer)) (c (take-character lexer))
          (text (lexer-text lexer)))
     (cond
       ((null c) (make-token :kind :eof :position start))
+      ((or (find c "0123456789+*")
+           (and (find c "-.") (peek-character lexer) (find (peek-character lexer) "0123456789"))
+           (and (char= c #\-) (eql #\. (peek-character lexer))
+                (peek-character lexer 1) (find (peek-character lexer 1) "0123456789")))
+       (read-number-token lexer start))
       ((char= c #\") (make-token :kind :string :value (read-ctm-string lexer start) :position start))
       ((char= c #\<)
        (loop while (and (peek-character lexer) (not (eql #\> (peek-character lexer))))
                  (eql #\/ (peek-character lexer 1)) (eql #\/ (peek-character lexer 2)))
             (incf (lexer-cursor lexer) 3)
             (loop while (and (peek-character lexer) (not (whitespace-p (peek-character lexer)))
-                             (not (find (peek-character lexer) "<>\"{}|^`\\[](")))
+                             (not (find (peek-character lexer) "<>\"{}|^`\\[]()")))
                   do (take-character lexer))
             (loop while (and (> (lexer-cursor lexer) start)
                              (find (char text (1- (lexer-cursor lexer))) ";.)"))
             ;; Otherwise ':' remains a separate token, including name types
             ;; followed by a string with or without intervening layout.
             (take-character lexer)
-            (scan-name lexer)
+            (if (name-start-p (peek-character lexer))
+                (scan-name lexer)
+                ;; CTM 3.3.4 production [11]: numeric-leading local parts
+                ;; contain name-part characters, which do not include dots.
+                (loop while (name-part-p (peek-character lexer)) do (take-character lexer)))
             (make-token :kind :qname :value (subseq text start (lexer-cursor lexer)) :position start))
            (t (make-token :kind :identifier :value head :position start)))))
-      ((find c ".;-=^:") (make-token :kind :punctuation :value c :position start))
-      ((find c "%?[$@~()")
+      ((find c ".;-=^:(),") (make-token :kind :punctuation :value c :position start))
+      ((char= c #\%)
+       ;; Production [71] has a literal terminal followed by an identifier;
+       ;; unlike Role separators it does not require intervening whitespace.
+       (if (starts-with "%prefix" (subseq text start))
+           (progn (setf (lexer-cursor lexer) (+ start 7))
+                  (make-token :kind :directive :value "prefix" :position start))
+           (progn
+             (scan-name lexer)
+             (problem 'ctm-unsupported :unsupported-directive start
+                      (format nil "Directive %~A is outside the implemented subset."
+                              (subseq text (1+ start) (lexer-cursor lexer)))))))
+      ((find c "?[$@~")
        (problem 'ctm-unsupported :unsupported-construct start
-                (format nil "Construct starting with ~S is outside Slice 1." c)))
+                (format nil "Construct starting with ~S is outside the implemented subset." c)))
       (t (problem 'ctm-rejection :unexpected-character start (format nil "Unexpected character ~S." c))))))