17.3.3 Using #lang s-exp syntax/module-reader
Parsing a module body is usually not as trivial as in "literal.rkt". A more typical module parser must iterate to parse multiple forms for a module body. A language is also more likely to extend Racket syntax – perhaps through a readtable – instead of replacing Racket syntax completely.
The syntax/module-reader module language abstracts over common parts of a language implementation to simplify the creation of new languages. In its most basic form, a language implemented with syntax/module-reader simply specifies the module language to be used for the language, in which case the reader layer of the language is the same as Racket. For example, with
#lang racket (provide (except-out (all-from-out racket) lambda) (rename-out [lambda function]))
|#lang reader "raquet.rkt"|
|(define identity (function (x) x))|
implements and exports the identity function, since "raquet-mlang.rkt" exports lambda as function.
The syntax/module-reader language accepts many optional specifications to adjust other features of the language. For example, an alternate read and read-syntax for parsing the language can be spcified with #:read and #:read-syntax, respectively. The following "dollar-racket.rkt" language uses "dollar.rkt" (see Readtables) to build a language that is like racket but with a $ escape to simple infix arithmetic:
The following module uses "dollar-racket.rkt" to implement a cost function using a $ escape: