Discussion:
[racket] source locations of syntax objects in a required file
Alexander D. Knauth
2015-03-08 18:19:19 UTC
Permalink
If I have these files:
test.rkt:
#lang racket
(define stx #'here)
(syntax-source stx)
(syntax-line stx)
(syntax-column stx)
(syntax-position stx)
(syntax-span stx)
require-test.rkt:
#lang racket
(require "test.rkt")

Then when I run test.rkt, it prints:
#<path:/Users/Alex/Documents/DrRacket/srcloc/test.rkt>
2
14
28
4
As expected. But when I run require-test.rkt, it prints:
#f
#f
#f
#f
0

What happened to the source locations? Why is it doing this?

____________________
Racket Users list:
http://lists.racket-lang.org/users
Robby Findler
2015-03-08 21:01:53 UTC
Permalink
Syntax object constants, when compiled, have their source location
information discarded. So you're running in drracket, I guess, with
automatic compilation on. The usual way around this is to write a
macro that explicitly tracks the source locations that you care about
and use it (instead of using quote-syntax).

Robby


On Sun, Mar 8, 2015 at 1:19 PM, Alexander D. Knauth
Post by Alexander D. Knauth
#lang racket
(define stx #'here)
(syntax-source stx)
(syntax-line stx)
(syntax-column stx)
(syntax-position stx)
(syntax-span stx)
#lang racket
(require "test.rkt")
#<path:/Users/Alex/Documents/DrRacket/srcloc/test.rkt>
2
14
28
4
#f
#f
#f
#f
0
What happened to the source locations? Why is it doing this?
____________________
http://lists.racket-lang.org/users
____________________
Racket Users list:
http://lists.racket-lang.org/users
Eric Dobson
2015-03-09 03:06:53 UTC
Permalink
For an example match does this:
https://github.com/plt/racket/blob/master/racket/collects/racket/match/gen-match.rkt#L36

Added in this commit
https://github.com/plt/racket/commit/fc8ed9772a701062dff2b928fb99d90e01b7f177
Post by Robby Findler
Syntax object constants, when compiled, have their source location
information discarded. So you're running in drracket, I guess, with
automatic compilation on. The usual way around this is to write a
macro that explicitly tracks the source locations that you care about
and use it (instead of using quote-syntax).
Robby
On Sun, Mar 8, 2015 at 1:19 PM, Alexander D. Knauth
Post by Alexander D. Knauth
#lang racket
(define stx #'here)
(syntax-source stx)
(syntax-line stx)
(syntax-column stx)
(syntax-position stx)
(syntax-span stx)
#lang racket
(require "test.rkt")
#<path:/Users/Alex/Documents/DrRacket/srcloc/test.rkt>
2
14
28
4
#f
#f
#f
#f
0
What happened to the source locations? Why is it doing this?
____________________
http://lists.racket-lang.org/users
____________________
http://lists.racket-lang.org/users
Robby Findler
2015-03-09 03:15:05 UTC
Permalink
I can't recall the name anymore, but there is a library somewhere to
help with this too.

Robby
Post by Eric Dobson
https://github.com/plt/racket/blob/master/racket/collects/racket/match/gen-match.rkt#L36
Added in this commit
https://github.com/plt/racket/commit/fc8ed9772a701062dff2b928fb99d90e01b7f177
Post by Robby Findler
Syntax object constants, when compiled, have their source location
information discarded. So you're running in drracket, I guess, with
automatic compilation on. The usual way around this is to write a
macro that explicitly tracks the source locations that you care about
and use it (instead of using quote-syntax).
Robby
On Sun, Mar 8, 2015 at 1:19 PM, Alexander D. Knauth
Post by Alexander D. Knauth
#lang racket
(define stx #'here)
(syntax-source stx)
(syntax-line stx)
(syntax-column stx)
(syntax-position stx)
(syntax-span stx)
#lang racket
(require "test.rkt")
#<path:/Users/Alex/Documents/DrRacket/srcloc/test.rkt>
2
14
28
4
#f
#f
#f
#f
0
What happened to the source locations? Why is it doing this?
____________________
http://lists.racket-lang.org/users
____________________
http://lists.racket-lang.org/users
____________________
Racket Users list:
http://lists.racket-lang.org/users
Alexander D. Knauth
2015-03-09 03:45:18 UTC
Permalink
Do you mean syntax/location ?

But that wouldn’t work for what I was doing. What I ended up doing was defining a macro that defines another macro and calls the new macro with the syntax-objects, and puts the code that checks the source locations in the definition of the new macro.
Post by Robby Findler
I can't recall the name anymore, but there is a library somewhere to
help with this too.
Robby
Post by Eric Dobson
https://github.com/plt/racket/blob/master/racket/collects/racket/match/gen-match.rkt#L36
Added in this commit
https://github.com/plt/racket/commit/fc8ed9772a701062dff2b928fb99d90e01b7f177
Post by Robby Findler
Syntax object constants, when compiled, have their source location
information discarded. So you're running in drracket, I guess, with
automatic compilation on. The usual way around this is to write a
macro that explicitly tracks the source locations that you care about
and use it (instead of using quote-syntax).
Robby
On Sun, Mar 8, 2015 at 1:19 PM, Alexander D. Knauth
Post by Alexander D. Knauth
#lang racket
(define stx #'here)
(syntax-source stx)
(syntax-line stx)
(syntax-column stx)
(syntax-position stx)
(syntax-span stx)
#lang racket
(require "test.rkt")
#<path:/Users/Alex/Documents/DrRacket/srcloc/test.rkt>
2
14
28
4
#f
#f
#f
#f
0
What happened to the source locations? Why is it doing this?
____________________
http://lists.racket-lang.org/users
____________________
http://lists.racket-lang.org/users
____________________
Racket Users list:
http://lists.racket-lang.org/users
Jay McCarthy
2015-03-09 03:53:01 UTC
Permalink
Here's what I do:

#lang racket/base
(require (for-syntax racket/base)
syntax/location
syntax/srcloc)
(define-syntax (mega-quote-syntax stx)
(syntax-case stx ()
[(_ arg)
(syntax/loc stx
(datum->syntax #f 'arg
(build-source-location-list
(quote-srcloc arg))))]))
(provide mega-quote-syntax)

On Sun, Mar 8, 2015 at 11:45 PM, Alexander D. Knauth
Post by Alexander D. Knauth
Do you mean syntax/location ?
But that wouldn’t work for what I was doing. What I ended up doing was defining a macro that defines another macro and calls the new macro with the syntax-objects, and puts the code that checks the source locations in the definition of the new macro.
Post by Robby Findler
I can't recall the name anymore, but there is a library somewhere to
help with this too.
Robby
Post by Eric Dobson
https://github.com/plt/racket/blob/master/racket/collects/racket/match/gen-match.rkt#L36
Added in this commit
https://github.com/plt/racket/commit/fc8ed9772a701062dff2b928fb99d90e01b7f177
Post by Robby Findler
Syntax object constants, when compiled, have their source location
information discarded. So you're running in drracket, I guess, with
automatic compilation on. The usual way around this is to write a
macro that explicitly tracks the source locations that you care about
and use it (instead of using quote-syntax).
Robby
On Sun, Mar 8, 2015 at 1:19 PM, Alexander D. Knauth
Post by Alexander D. Knauth
#lang racket
(define stx #'here)
(syntax-source stx)
(syntax-line stx)
(syntax-column stx)
(syntax-position stx)
(syntax-span stx)
#lang racket
(require "test.rkt")
#<path:/Users/Alex/Documents/DrRacket/srcloc/test.rkt>
2
14
28
4
#f
#f
#f
#f
0
What happened to the source locations? Why is it doing this?
____________________
http://lists.racket-lang.org/users
____________________
http://lists.racket-lang.org/users
____________________
http://lists.racket-lang.org/users
--
Jay McCarthy
http://jeapostrophe.github.io

"Wherefore, be not weary in well-doing,
for ye are laying the foundation of a great work.
And out of small things proceedeth that which is great."
- D&C 64:33

____________________
Racket Users l
Loading...