X-Git-Url: https://dreyeck.freedombox.rocks/gitweb/topic-maps.git/blobdiff_plain/f1270a43720ab1454bebfb3d0a9e3555554a7ab9..1ef5768ce6913f0c04da111d5f525b6d50a27e0b:/src/ctm/lexer.lisp diff --git a/src/ctm/lexer.lisp b/src/ctm/lexer.lisp index c314010..ebc0301 100644 --- a/src/ctm/lexer.lisp +++ b/src/ctm/lexer.lisp @@ -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)) @@ -77,11 +77,11 @@ (return)))) (t (return))))) -(defun excluded-literal-p (text) +(defun ctm-integer-literal-p (text) (string= text "*")) (defun read-number-token (lexer start) - "One token path for Integer, Decimal, Date and DateTime; * remains excluded. + "One token path for Integer, Decimal, Date, DateTime and the exact * literal. 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) @@ -98,7 +98,7 @@ A dot followed by a digit can belong to a decimal; a terminal dot stays punctuat (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)) + ((ctm-integer-literal-p value) (make-token :kind :ctm-integer :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)) @@ -171,15 +171,19 @@ A dot followed by a digit can belong to a decimal; a terminal dot stays punctuat ((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" "encoding" "version")))) + (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-rejection :unknown-directive start + (format nil "Unknown CTM directive %~A." + (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))))))