[racket] Custom language using syntax/module-reader with #:wrapper1

From: Nick Sivo (nick at kogir.com)
Date: Tue May 29 19:44:57 EDT 2012

Hi,

I'm trying to implement arc as a racket language extension.  To become
familiar with racket's reader infrastructure, I thought I'd start with
something simple:

==================
kogir/arc/lang/reader.rkt:
==================
#lang s-exp syntax/module-reader
(planet kogir/arc/language)
#:wrapper1 arc-read-wrapper

(require (planet kogir/arc/reader))

==============
kogir/arc/reader.rkt:
==============
#lang racket/base

(provide make-arc-readtable
         arc-read-wrapper)

(define (read-square-brackets ch port src line col pos)
  (datum->syntax
   #f
   `(lambda (_)
       ,(read-syntax/recursive src port #\[ #f))
   (let-values ([(l c p) (port-next-location port)])
         (list src line col pos (and pos (- p pos))))))

(define (make-arc-readtable)
  (make-readtable (current-readtable)
                  #\[ 'terminating-macro read-square-brackets))

(define (arc-read-wrapper thunk read-syntax)
  (parameterize ([current-readtable (make-arc-readtable)])
    (thunk)))

================
kogir/arc/language.rkt:
================
#lang racket

(provide (all-defined-out)
         (all-from-out racket))

I expected it to give me a language very similar to racket, but with
arc's [ _ ] lambda syntax sugar:
[+ _ 1] => (lambda (_) (+ _ 1))

When I test in Dr. Racket, it works for reading files, but not in the
interaction window:

======
test.arc:
======
#lang planet kogir/arc

(define test [+ _ 1])

---------------------------------------------------------------------------------------

Welcome to DrRacket, version 5.2.1 [3m].
Language: planet kogir/arc [custom]; memory limit: 128 MB.
> test
#<procedure:test>
> (test 1)
2
> [+ _ 1]
. _: wildcard not allowed as an expression in: _

What pieces am I missing to make the Dr. Racket interaction window
support the new syntax?

 Also, is it correct in kogir/arc/reader.rkt to use
`(lambda (_)
       ,(read-syntax/recursive src port #\[ #f)
or should I use
#`(lambda (_)
       #,(read-syntax/recursive src port #\[ #f)

Both appear to work?

Any pointers you might have would be awesome.  I apologize in advance
if I missed something obvious.

Thanks,
Nick

Posted on the users mailing list.