[racket] Changing at-expressions to other-glyph-expressions

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Fri Apr 19 22:48:56 EDT 2013

At Fri, 19 Apr 2013 16:42:40 -0700, Matthew Butterick wrote:
> Thanks. Yes, I did see that in the docs. The problem with this technique is
> that once I wrap the input text in @list| (or @list|^, or ... etc.) to get
> the benefit of @-escaping, then I lose the ability to do defines within the
> body of that text. (Throws error "define not allowed in expression
> context.")

You could use `begin' instead of list:

 #lang scribble/base
 
 @; Everything within the following will be escaped.  Use |^ to unescape.
 @begin|^{
  
 this is an example with @ signs in it.

 |^@(define x "at")

 @ is |^@x

 }^|


> Meanwhile, I can think of about a zillion unicode glyphs that are
> definitely NOT in any of these source files and never will be.

The `scribble/reader' library provides `make-at-reader', which lets you
pick the escape character. You can use that with `#reader', which lets
you pick a reader.

Unfortunately, there's a bug that makes `make-at-reader; work only for
ASCII characters; I've just pushed a fix for that to the git repo. For
the example below, I've used a tilde to avoid the bug, but you could
use any Unicode character with a new build from source or with the next
nightly build.

----------------------------------------
;; tilde.rkt:
----------------------------------------
#lang racket/base
(require (only-in scribble/reader make-at-reader))

(provide (rename-out [tilde-read read]
                     [tilde-read-syntax read-syntax]))

(define read-inner
  (make-at-reader #:command-char #\~
                  #:syntax? #t
                  #:inside? #t))

(define (tilde-read p)
  (syntax->datum
   (tilde-read-syntax (object-name p) p)))

(define (tilde-read-syntax name p)
  (define i (read-inner name p))
  (datum->syntax i (cons 'begin i) i))

----------------------------------------
;; example.scrbl
----------------------------------------
#lang scribble/base
@#reader "tilde.rkt"

This is an example with @ signs and a definition.

~(define x "at")

Example: @ is ``~x''.


Posted on the users mailing list.