[racket] Inner workings of source annotation

From: Alexander D. Knauth (alexander at knauth.org)
Date: Fri Jun 20 17:38:32 EDT 2014

I don’t think it has to map  the elements of the expanded code to anything, it just has to use the source location information already stored in the syntax objects.

For example this draws an arrow from from-loc-stx to to-loc-stx:

#lang racket

(begin-for-syntax
  (define (orig stx)
    (syntax-property stx 'original-for-check-syntax #t))
  )

(define-syntax (draw-arrow stx)
  (let ([sym 'sym]
        [from-loc #'from-loc-stx]
        [to-loc #'to-loc-stx])
    (with-syntax ([from (orig (datum->syntax stx sym from-loc))]
                  [to (orig (datum->syntax stx sym to-loc))])
    #'(let ([from 0])
        to
        (void)))))

(draw-arrow)

Or you can even do something like this, where it can point to any source location:
(this one happens to point to the syntax in begin-for-syntax, but you could potentially get it to point to anything)

#lang racket

(begin-for-syntax
  (define (orig stx)
    (syntax-property stx 'original-for-check-syntax #t))
  )

(define-syntax (draw-arrow stx)
  (let ([sym 'sym]
        [from-loc (list (syntax-source #'this) 3 11 26 6)] ;line: 3, column: 11, position: 26, span: 6
        [to-loc #'to-loc])
    (with-syntax ([from (orig (datum->syntax stx sym from-loc))]
                  [to (orig (datum->syntax stx sym to-loc))])
    #'(let ([from 0])
        to
        (void)))))

(draw-arrow)

On Jun 20, 2014, at 5:07 PM, Jonker, Todd <jonker at amazon.com> wrote:

> Greetings,
> 
> I’m interested in learning how DrRacket’s “Check Syntax” feature works.
> 
> As I understand it, the process is (1) perform full macro-expansion on the source code; (2) map elements of the expanded code back to the source code; (3) use that mapping to apply annotations/markup to the source code. The documentation at http://docs.racket-lang.org/tools/Check_Syntax.html seems to cover step (3), but I’m struggling to understand how (2) works.
> 
> In other words, I’m trying to learn how the lexical information in the expanded syntax objects is mapped back to the appropriate parts of the original syntax objects. I’m reading the code to try to figure out the algorithm but hoping that there’s a paper that describes it more directly.
> 
> Thanks in advance for any pointers,
> 
> --Todd
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140620/d6ed16a9/attachment-0001.html>

Posted on the users mailing list.