[plt-scheme] define own escape sequences?
On Jun 15, Eli Barzilay wrote:
>
> There is no hook for extending the string syntax, so you'll need a new
> reader macro for strings. If you want all of mzscheme's escapes, then
> the tedious part in this will be all the numeric escapes (octal and
> hex). So depending on the problem you're trying to solve, it might be
> easier to define a new kind of escape, or maybe use something like the
> scribble syntax (http://docs.plt-scheme.org/scribble/reader.html).
Actually, another way to hack this would be to just hook on the
mzscheme reader, after regexp-replacing the contents of the string.
Here's an example that constructs such a readtable -- with a string it
uses the mzscheme parser after replacing all `\@' with `\\@'. If
you're using v4, it would be very easy to go from this code to a
reader for a `#lang <foo>' language that has this syntax for strings.
-------------------------------------------------------------------------------
#lang scheme
(require syntax/readerr)
(define (make-my-readtable rt)
(define (parse-string bytes)
(parameterize ([current-readtable rt]) ; use the original reader
(let ([bytes (regexp-replace*
#rx#"\\\\." bytes
(lambda (m) (if (equal? m #"\\@") #"\\\\@" m)))])
(read (open-input-bytes (bytes-append #"\"" bytes #"\""))))))
(make-readtable
rt #\" 'terminating-macro
(lambda (char inp src line col pos)
(define bytes
(cadr (or (regexp-try-match #rx#"^((?:[^\\]+?|\\\\.)*?)\"" inp)
(raise-read-eof-error "expected a closing '\"'"
src line col pos #f))))
(datum->syntax #f (parse-string bytes)
(list src line col pos (+ 2 (bytes-length bytes)))))))
(define (my-read . args)
(parameterize ([current-readtable (make-my-readtable (current-readtable))])
(apply read args)))
-------------------------------------------------------------------------------
--
((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
http://www.barzilay.org/ Maze is Life!