[racket] Passing info from reader level

From: Rajah Mahsohn Omega (rajahmahsohn at gmail.com)
Date: Sat Dec 15 16:12:50 EST 2012

So from what I'm gathering the only way to share data with the reader and
expander layer is to add the reader as a submodue in the main.rkt file. So
I've done that and it works but now I have a new problem, provide doesn't
seem to be providing anything. Here is an example main.rkt file that
provides meta-count.


#lang racket
(provide (all-from-out racket)
     meta-count)

(define meta-count 0)

(module* reader syntax/module-reader
     racket
     #:read meta-read
     #:read-syntax meta-read-syntax
     (require (submod "." "..")
          syntax/readerr)

     (define (meta-read in)
       (parameterize ([current-readtable (make-meta-readtable)])
         (read in)))

     (define (meta-read-syntax src in)
       (parameterize ([current-readtable (make-meta-readtable)])
         (read-syntax src in)))

     (define (make-meta-readtable)
       (make-readtable (current-readtable)
               #\^ 'terminating-macro read-meta))
     (define read-meta
       (case-lambda
         [(ch in)
          (rread (object-name in) in)]
         [(ch in src line col pos)
          (rread src in)]))

     (define (rread src in)
       (define-values (line col pos) (port-next-location in))
       (let ([meta (regexp-match #rx"[^ (['`#]+" in)])
         (if meta
         (begin
          (set! meta-count (add1 meta-count))
          (make-special-comment meta))
           (raise-read-error "expected a metadata name" src line col pos
1)))))

So this simply counts the amount of metadata tags and provides the count.
However if I try this:
#lang my-lang
^meta (+ ^meta1 1 2)
(display meta-count)


I should see 2 displayed but I get
"3
meta-count: unbound identifier in module"

I thought if you export data through provide in main.rkt you will be able
to use it after specifying the #lang, why is this happenning?


>Maybe we should have a new `something' so that `#lang something' is
>like `#lang reader', except that it looks for a `reader' submodule.

I think that would be awesome, that way you could specify your own
mini-language or do preprocessing at the reader level by simply creating a
reader submodule in the module.

>Special comments aren't stored by `read' or `read-syntax'; they're just
>discarded. Creating a special comment has no side effect.

I don't understand why the the function is called (make-special-comment)
then, why isn't it just called (discard) or (skip) or something. and why
does it require a name as an argument if it isn't even going to do anything
with it. I guess I'm still a little confused on how special comments are
used.


>One way to attach metadata to a syntax object is to use a syntax
>property. For example, the reader preserves the information that `[1
>2]' is written qith suqare brackets by attaching a 'paren-shape
>property to the result of `read-syntax'.

I think this could be useful, but adding a syntax property is something you
can only do at the reader level right? Can you go back and alter a syntax
property after it has been compiled? Is syntax information lost at a
certain point?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121215/a4232be9/attachment.html>

Posted on the users mailing list.