[racket] generate-temporaries with source location?

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Fri Jul 15 01:41:16 EDT 2011

It's easy to add this:

(define (generate-temporaries/locs xs)
  (for/list {[x (in-list (if (syntax? xs) (syntax->list xs) xs)]}
    (define sym (if (identifier? x) (syntax-e x) 'fresh))
    (define src (if (syntax? x) x #f))
    (define mark (make-syntax-introducer))
    (mark (datum->syntax #f sym src))))

Carl Eastlund

On Fri, Jul 15, 2011 at 1:29 AM, Danny Yoo <dyoo at cs.wpi.edu> wrote:
> I'd like the ability to generate a bunch of temporaries, but with
> specific source locations.  I'm having an awkward time fixing an
> embarrassing bug in my chaining-compare library.
>
>
> I have the following expression:
>
>    (with-syntax ([rhs-value (datum->syntax #f
>                                            (gensym 'rhs-value)
>                                            (list (syntax-source #'rhs)
>                                                  (syntax-line #'rhs)
>                                                  (syntax-column #'rhs)
>                                                  (syntax-position #'rhs)
>                                                  (syntax-span #'rhs)))])
>        ...)
>
>
> and I feel really cheesy doing the gensym here!  I'd love to replace this with:
>
>    (with-syntax ([(rhs-value) (generate-temporaries #'(rhs)])
>        ...)
>
> except that the resulting temporary variable doesn't have the correct
> source location of the input, and having the 'rhs-value' syntax have
> the right source location is critical.
>
>
> In my imagination, I'd like to be able to say:
>
>     (with-syntax ([(rhs-value) (generate-temporaries/locs #'(rhs) #'(rhs))])
>         ...)
>
> and have this hypothetical "generate-temporaries/locs" adopt the
> source locations from the second argument.  Is this easy to build, or
> does such functionality already exist?



Posted on the users mailing list.