]> dreyeck.freedombox.rocks Git - topic-maps.git/blobdiff - src/ctm/lexer.lisp
Qualify pinned CXML encodings with reproducible local patches
[topic-maps.git] / src / ctm / lexer.lisp
index 6f577ffb823127193494da1cee952167731e1113..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))
@@ -85,7 +85,7 @@
 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))
@@ -119,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))
@@ -159,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))))))