[racket-dev] Preserving source location in macro-generating macros, in collects

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Fri Oct 7 11:56:35 EDT 2011

On Fri, Oct 7, 2011 at 10:35 AM, Neil Toronto <neil.toronto at gmail.com> wrote:
> Why I care: I wrote a macro that both defines a contracted function and
> defines a macro that expands to a Scribble 'defproc' with the same contract.
> With the source locations gone (recursively), Scribble can't format the
> contracts, and I end up with things like (real-in01) instead of (real-in 0
> 1).
> (I know about scribble/srcdoc, but dependencies keep me from using it.)

Can you say more about the dependencies issue?

> Suppose I have a "location reporter" macro, which is generated by a macro:
> #lang racket
> (require (for-syntax syntax/srcloc))
> (provide location-reporter)
> (define-syntax (make-location-reporter stx)
>  (syntax-case stx ()
>    [(_ name)  #'(define-syntax (name inner-stx)
>                   (syntax-case inner-stx ()
>                     [(_)  #`#,(source-location->string #'name)]))]))
> (make-location-reporter location-reporter)
> If I compile this in DrRacket, require it from another module, and do
>    (location-reporter)
> it expands to
>    "<collects>/plot/location-reporter.rkt:13.24"
> as I expect. But if I compile it as part of "raco setup", it expands to
>    "<collects>/plot/location-reporter.rkt"
> I understand that stripping source locations is an optimization. But is
> there a way to turn it off, or preserve them, recursively, another way?
> Also, exactly how is the location information disappearing? Does it happen
> when 'name' above is double-syntax-quoted?

When a syntax object is compiled and written to a .zo file, all that
information is stripped. You have to write use another function at
compile time that walks over the syntax objects and records the source
locations and then saves them in some way that is not a syntax object
literal. Then restore them.


Posted on the dev mailing list.