Add reader for skribe format.
authorrekado <rekado@elephly.net>
Sun, 16 Aug 2015 03:11:20 +0000 (05:11 +0200)
committerrekado <rekado@elephly.net>
Mon, 17 Aug 2015 20:56:02 +0000 (22:56 +0200)
skribe.scm [new file with mode: 0644]

diff --git a/skribe.scm b/skribe.scm
new file mode 100644 (file)
index 0000000..c7d8380
--- /dev/null
@@ -0,0 +1,58 @@
+(define-module (skribe)
+  #:use-module ((system reader) #:renamer (symbol-prefix-proc 'r:))
+  #:export (%skribe-reader))
+
+;; Taken from Skribilo
+(define (make-colon-free-token-reader tr)
+  ;; Stolen from `guile-reader' 0.3.
+  "If token reader @var{tr} handles the @code{:} (colon) character, remove it
+from its specification and return the new token reader."
+  (let* ((spec (r:token-reader-specification tr))
+        (proc (r:token-reader-procedure tr)))
+    (r:make-token-reader (filter (lambda (chr)
+                                  (not (char=? chr #\:)))
+                                spec)
+                        proc)))
+
+(define &sharp-reader
+  ;; The reader for what comes after a `#' character.
+  (let* ((dsssl-keyword-reader  ;; keywords à la `#!key'
+          (r:make-token-reader #\!
+                              (r:token-reader-procedure
+                               (r:standard-token-reader 'keyword)))))
+      (r:make-reader (cons dsssl-keyword-reader
+                          (map r:standard-token-reader
+                               '(character srfi-4 vector
+                                 number+radix boolean
+                                 srfi30-block-comment
+                                 srfi62-sexp-comment)))
+                    #f ;; use default fault handler
+                    'reader/record-positions)))
+
+(define (make-skribe-reader)
+  (let ((colon-keywords ;; keywords à la `:key' fashion
+        (r:make-token-reader #\:
+                             (r:token-reader-procedure
+                              (r:standard-token-reader 'keyword))))
+       (symbol-misc-chars-tr
+        ;; Make sure `:' is handled only by the keyword token reader.
+        (make-colon-free-token-reader
+         (r:standard-token-reader 'r6rs-symbol-misc-chars))))
+
+    ;; Note: we use the `r6rs-symbol-*' and `r6rs-number' token readers since
+    ;; they consider square brackets as delimiters.
+    (r:make-reader (cons* (r:make-token-reader #\# &sharp-reader)
+                         colon-keywords
+                         symbol-misc-chars-tr
+                         (map r:standard-token-reader
+                              `(whitespace
+                                sexp string r6rs-number
+                                r6rs-symbol-lower-case
+                                r6rs-symbol-upper-case
+                                quote-quasiquote-unquote
+                                semicolon-comment
+                                skribe-exp)))
+                  #f ;; use the default fault handler
+                  'reader/record-positions)))
+
+(define %skribe-reader (make-skribe-reader))