X-Git-Url: https://dreyeck.freedombox.rocks/gitweb/topic-maps.git/blobdiff_plain/17dd58775e25ab495e3c83deb4b5a36f5e5a124b..6a2f8446afca0915882f0d4a06cb891ccfcdc96d:/src/ctm/lexer.lisp diff --git a/src/ctm/lexer.lisp b/src/ctm/lexer.lisp index a795fbc..04289a6 100644 --- a/src/ctm/lexer.lisp +++ b/src/ctm/lexer.lisp @@ -78,37 +78,10 @@ (t (return))))) (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)))))))) + (string= text "*")) (defun read-number-token (lexer start) - "One lexical path for supported integers and recognized excluded number/date 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) @@ -119,7 +92,19 @@ A dot followed by a digit can belong to a decimal; a terminal dot stays punctuat (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)