[racket] I just can't understand readtables

From: Danny Yoo (dyoo at hashcollision.org)
Date: Mon Apr 29 14:44:54 EDT 2013

Hi Tomas,

For this particular case, you might not even need to use readtables.
Since you're already dealing with making your own Clojure-style
language, you might be able to override the literal datum macro,
#%datum, to rewrite uses of byte strings to regexps.

Here's what this might look like:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#lang racket/base

(module my-custom-language racket/base
  ;; Example of overriding #%datum:
  (provide (except-out (all-from-out racket/base)
                       #%datum)
           (rename-out (#%my-datum #%datum)))

  (require (for-syntax racket/base))

  ;; Here is our substitute definition for processing raw datums in
  ;; our language:
  (define-syntax (#%my-datum stx)
    (syntax-case stx ()
      [(_ . bstring)
       (bytes? (syntax-e #'bstring))
       ;; If we see a raw byte string, translate it to a use of
       ;; a regexp.
       (with-syntax ([an-str (bytes->string/utf-8 (syntax-e #'bstring))])
         (syntax/loc stx
           (regexp an-str)))]
      [(_ . other)
       ;; Otherwise, delegate to the original definition of #%datum:
       (syntax/loc stx
         (#%datum . other))])))


;; Here is an example use of that custom language:
(module test (submod ".." my-custom-language)
  (define r #"hello world, this should be a regexp")
  (printf "~v\n" r)
  (regexp? r))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Posted on the users mailing list.