]> dreyeck.freedombox.rocks Git - topic-maps.git/blobdiff - src/ctm/lexer.lisp
Implement XTM 2.0 identity and instanceOf reader with offline evidence
[topic-maps.git] / src / ctm / lexer.lisp
index 66a5ed8b916b4bf258fe97fa2950b45e86980fa5..15125a32e8b111b90c37755dda4ae6d0afc7aeff 100644 (file)
@@ -1,6 +1,6 @@
 (in-package #:topic-maps.ctm)
 
-(defstruct token kind value (position 0))
+(defstruct token kind value (position 0) end)
 (defstruct (lexer (:constructor make-lexer (text))) text (cursor 0))
 
 (defun peek-character (lexer &optional (delta 0))
           (t (return)))))
 
 (defun excluded-literal-p (text)
-  "Recognize unsupported dateTime/* literal syntax, without constructing values."
-  (labels ((digits (s) (and (plusp (length s)) (every (lambda (c) (find c "0123456789")) 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 (string= text "*")
-        (let ((tee (position #\T text))) (and tee (date (subseq text 0 tee)) (time-literal (subseq text (1+ tee))))))))
+  (string= text "*"))
 
 (defun read-number-token (lexer start)
-  "One token path for Integer, Decimal and Date, plus recognized excluded dateTime/* forms.
+  "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 ";@~()#^<>\""))
+        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))
@@ -117,6 +94,10 @@ A dot followed by a digit can belong to a decimal; a terminal dot stays punctuat
            (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)
@@ -138,6 +119,14 @@ A dot followed by a digit can belong to a decimal; a terminal dot stays punctuat
                 (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 #\?)
+       ;; A named wildcard requires an immediately adjacent identifier.
+       (let ((name-start (lexer-cursor lexer)))
+         (when (name-start-p (peek-character lexer)) (take-character lexer) (scan-name lexer))
+         (make-token :kind :wildcard
+                     :value (when (> (lexer-cursor lexer) name-start)
+                              (subseq text name-start (lexer-cursor lexer)))
+                     :position start)))
       ((char= c #\<)
        (loop while (and (peek-character lexer) (not (eql #\> (peek-character lexer))))
              do (take-character lexer))
@@ -178,19 +167,23 @@ A dot followed by a digit can belong to a decimal; a terminal dot stays punctuat
                 (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 ".;-=^:(),@[]~") (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 the implemented subset." c)))
+       (let ((name (find-if (lambda (name) (starts-with (concatenate 'string "%" name) (subseq text start)))
+                            '("prefix" "mergemap" "include"))))
+         (if name
+             (progn (setf (lexer-cursor lexer) (+ start 1 (length name)))
+                    (make-token :kind :directive :value name :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))))))))
+      ((char= c #\$)
+       (unless (name-start-p (peek-character lexer))
+         (problem 'ctm-rejection :invalid-variable start "A variable requires an identifier."))
+       (take-character lexer) (scan-name lexer)
+       (make-token :kind :variable :value (subseq text (1+ start) (lexer-cursor lexer)) :position start))
       (t (problem 'ctm-rejection :unexpected-character start (format nil "Unexpected character ~S." c))))))