[plt-scheme] scribble/reader

From: Eli Barzilay (eli at barzilay.org)
Date: Mon Jun 8 13:07:45 EDT 2009

The API for the Scribble reader is now extended in the repository.
Previously, the `make-at-readtable' function could be used to create a
customized reader, but getting a customized `-inside' reader was
impossible without knowing exactly how the readtable is organized.

The extension is a new `make-at-reader' function, which can create
either a `read' or a `read-syntax' function based on a `#:syntax?'
keyword argument, and they can create an "inside" reader based on an
`#:inside?' keyword.  With this, it is possible to easily implement a
`scribble/text'-like language that uses a different command character.
For example, the code below does just that, and uses a backslash.


On a related note, I have recently played with combining this kind of
backslash-based preprocessor combined with some `#%top' and `#%app'
magic to create a language where unbound identifiers will just spit
their own forms out -- for example, if there is no binding for `emph',
then this:

  foo \emph{bar} baz

will lead to spitting out the exact same line on its output.  The
result is a Scheme/latex hybrid language, where latex commands work,
but you can extend or redefine commands in Scheme, which in general is
much better than dealing with <foo>ing la/tex (read "<foo>" with some
word that customizes this sentence according to your personal horror
experiences).

This is not working completely right though, for example, `\\' doesn't
work right, because the second `\' is read as part of a scheme
identifier; \foo[x, y]{blah} doesn't work right because the bracketed
part is read in Scheme, and, of course, you might have a Scheme
binding that happens to shadow a latex command name.  The most obvious
example of this is `\begin' -- which I solved by making it also spit
out its own form if it is written as a scribble expression.

Overall, my opinion about it is not really clear yet.  On one hand,
choosing a different character is better because none of these
problems happen; but on the other hand I find it extremely useful to
just type the text without explicitly knowing which commands are
implemented in Scheme, and which in latex.  This is a strong point:
using the same syntax means that I can start with the usual `\emph',
then extend it in latex because of some reason, and then switch to
Scheme if it becomes too complex.

Still, it's a cute hack, and I can put it up somewhere if anyone's
interested in trying it out.  Maybe there is some way to fix things up
in the future so it is no longer a hack...


-------------------------------------------------------------------------------
#lang s-exp syntax/module-reader

scribble/text/textlang

#:read        read-inside
#:read-syntax read-syntax-inside
#:whole-body-readers? #t

(require (prefix-in scribble: scribble/reader))

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

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

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.