]> dreyeck.freedombox.rocks Git - topic-maps.git/blobdiff - src/ctm/lexer.lisp
Implement XTM occurrence reification with native conformance evidence
[topic-maps.git] / src / ctm / lexer.lisp
index 7b5af30d629dac9a1a83130b63a9665746296526..2cb1fec9f23a1bbabfeb51d8f003a8da725b1a31 100644 (file)
           (t (return)))))
 
 (defun read-ctm-string (lexer start)
-  (when (and (eql #\" (peek-character lexer)) (eql #\" (peek-character lexer 1)))
-    (problem 'ctm-unsupported :triple-quoted-string start "Triple quoted literals are outside Slice 1."))
-  (with-output-to-string (out)
-    (loop for c = (take-character lexer) do
-      (cond ((null c) (problem 'ctm-rejection :unterminated-string start "Unterminated quoted string."))
-            ((char= c #\") (return))
-            ((char= c #\\)
-             (let ((escape (take-character lexer)))
-               (case escape
-                 (#\n (write-char #\Newline out)) (#\r (write-char #\Return out))
-                 (#\t (write-char #\Tab out)) (#\" (write-char #\" out)) (#\\ (write-char #\\ out))
-                 ((#\u #\U)
-                  (let ((code 0))
-                    (dotimes (i (if (char= escape #\u) 4 6))
-                      (let* ((digit (take-character lexer)) (value (and digit (digit-char-p digit 16))))
-                        (unless value
-                          (problem 'ctm-rejection :invalid-unicode-escape (1- (lexer-cursor lexer)) "Expected hexadecimal escape digit."))
-                        (setf code (+ (* code 16) value))))
-                    (write-char (scalar-character code start) out)))
-                 (otherwise (problem 'ctm-rejection :invalid-string-escape (1- (lexer-cursor lexer)) "Unknown string escape.")))))
-            (t (write-char c out))))))
+  ;; NEXT-TOKEN has consumed the first quote. Triple strings share the exact
+  ;; same escape/scalar path; only their opening and closing delimiters differ.
+  (let ((triple (and (eql #\" (peek-character lexer))
+                     (eql #\" (peek-character lexer 1)))))
+    (when triple (incf (lexer-cursor lexer) 2))
+    (with-output-to-string (out)
+      (loop for c = (take-character lexer) do
+        (cond ((null c)
+               (problem 'ctm-rejection :unterminated-string start
+                        (if triple "Unterminated triple-quoted string." "Unterminated quoted string.")))
+              ((char= c #\")
+               (cond ((not triple) (return))
+                     ((and (eql #\" (peek-character lexer))
+                           (eql #\" (peek-character lexer 1)))
+                      (incf (lexer-cursor lexer) 2) (return))
+                     (t (write-char c out))))
+              ((char= c #\\)
+               (let ((escape (take-character lexer)))
+                 (case escape
+                   (#\n (write-char #\Newline out)) (#\r (write-char #\Return out))
+                   (#\t (write-char #\Tab out)) (#\" (write-char #\" out)) (#\\ (write-char #\\ out))
+                   ((#\u #\U)
+                    (let ((code 0))
+                      (dotimes (i (if (char= escape #\u) 4 6))
+                        (let* ((digit (take-character lexer)) (value (and digit (digit-char-p digit 16))))
+                          (unless value
+                            (problem 'ctm-rejection :invalid-unicode-escape (1- (lexer-cursor lexer)) "Expected hexadecimal escape digit."))
+                          (setf code (+ (* code 16) value))))
+                      (write-char (scalar-character code start) out)))
+                   (otherwise (problem 'ctm-rejection :invalid-string-escape (1- (lexer-cursor lexer)) "Unknown string escape.")))))
+              (t (write-char c out)))))))
 
 (defun scan-name (lexer)
   (loop
                  (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 +108,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))
@@ -172,14 +182,14 @@ A dot followed by a digit can belong to a decimal; a terminal dot stays punctuat
        ;; Production [71] has a literal terminal followed by an identifier;
        ;; unlike Role separators it does not require intervening whitespace.
        (let ((name (find-if (lambda (name) (starts-with (concatenate 'string "%" name) (subseq text start)))
-                            '("prefix" "mergemap"))))
+                            '("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-unsupported :unsupported-directive start
-                        (format nil "Directive %~A is outside the implemented subset."
+               (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))