Skip to content

Instantly share code, notes, and snippets.

@ktakashi
Created October 26, 2013 20:17
Show Gist options
  • Save ktakashi/7174032 to your computer and use it in GitHub Desktop.
Save ktakashi/7174032 to your computer and use it in GitHub Desktop.
Reader macro idea on Chaton
(library (string)
(export :export-reader-macro)
(import (rnrs)
(srfi :6 basic-string-ports)
(sagittarius)
(sagittarius reader)
(sagittarius control)
(text parse))
;; the result will be (string-concatenate (list of string and symbol))
(define %string-concatenate #'string-concatenate)
(define %quasi #'quasiquote)
(define %unq #'unquote)
(define-dispatch-macro #\# #\" (|#"-reader| port c param)
(let loop ((out (open-output-string)) (r '()))
(let1 c (get-char port)
(if (eof-object? c)
(error '|#"-reader| "unexepcted EOF")
(case c
((#\")
;; if the result is null means no symbol so just return
;; the buffer string.
(if (null? r)
(get-output-string out)
`(,%string-concatenate
,(list %quasi
(reverse! (cons (get-output-string out) r))))))
((#\\)
(put-char out (get-char port))
(loop out r))
((#\~)
;; ,|expr| or ,expr\s check |
(let* ((nc (lookahead-char port))
(name (if (char=? nc #\|)
;; read until next #\|
(let1 t (next-token '(#\|) '(#\|)
"unexpected EOF" port)
;; discard #\|
(get-char port)
(string->symbol t))
;; next expression must be symbol
(read port))))
(loop (open-output-string)
(cons (list %unq name)
(cons (get-output-string out) r)))))
(else
(put-char out c)
(loop out r)))))))
)
#!read-macro=string
(print #"just a string")
(let ((hoge "hoge"))
(print #"test ~hoge ~|hoge|fuga \~hoge"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment