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

From: Nick Sivo (nick at kogir.com)
Date: Wed May 30 18:15:51 EDT 2012

Thanks for the ideas, Matthias.

I don't currently provide my own #%top-interaction.  After reading the
documentation and running some tests, it appears #%top-interaction is
called too late - after the line in the interaction window has already
been read with the wrong readtable.

Eventually I discovered syntax/module-reader's #:language-info:
keyword and added a runtime configuration that replaces the
current-readtable.  It *appears* to work, but if anyone knows of a
"more correct/idomatic" way to do it I'm all ears.

Cheers,
Nick

On Wed, May 30, 2012 at 9:18 AM, Matthias Felleisen
<matthias at ccs.neu.edu> wrote:
>
> Is it possible that you missed out on #%top-interaction?
>
> http://docs.racket-lang.org/reference/__top-interaction.html?q=%23%25top#(form._((lib._racket/private/base..rkt)._~23~25top-interaction))
>
>
> On May 29, 2012, at 7:44 PM, Nick Sivo wrote:
>
>> 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
>> ____________________
>>  Racket Users list:
>>  http://lists.racket-lang.org/users
>


Posted on the users mailing list.