[racket] reconstructing syntax objects

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Thu Nov 17 19:25:37 EST 2011

On Thu, Nov 17, 2011 at 5:38 PM, Jon Rafkind <rafkind at cs.utah.edu> wrote:
> On 11/17/2011 01:24 PM, Jon Rafkind wrote:
>>
>> On 11/17/2011 01:20 PM, Ryan Culpepper wrote:
>>>
>>> On 11/17/2011 12:28 PM, Jon Rafkind wrote:
>>>>
>>>> I have some code that deconstructs and reconstructs a syntax object. I'm
>>>> pretty sure this code is at fault for not adding the proper lexical
>>>> context back on so I get an error about 'no #%app bound'.
>>>>
>>>> (syntax-parse form
>>>>  [(form ...)
>>>>   (with-syntax ([(form* ...) (map honu->racket (syntax->list #'(form
>>>> ...)))])
>>>>     (datum->syntax forms
>>>>                    #'(form* ...)
>>>>                    forms
>>>>                    forms))]
>>>>
>>>> I had hoped that datum->syntax would add the original lexical context
>>>> back on, is it not doing what I hoped?
>>>
>>> No, the datum->syntax there is a no-op, because #'(form* ...) is already
>>> a syntax object, and datum->syntax stops when it hits something that's
>>> already a syntax object.
>>>
>>> You should do something like this instead:
>>>
>>>  (datum->syntax forms
>>>                 (map honu->racket (syntax->list #'(form ...)))
>>>                 forms
>>>                 forms)
>>>
>>
>> Ok thanks, that solved the problem. For future reference if I have a more
>> complicated use case is there a way to put the original lexical context back
>> on the new syntax object?
>>
>> If such a function could exist it should go into unstable/syntax since I
>> keep forgetting how to reconstruct the lexical context.
>
> FWIW converting a syntax object to a list and then passing that to
> datum->syntax works as well.
>
> (with-syntax ([(form* ...) (map honu->racket (syntax->list #'(form ...)))])
>  (datum->syntax forms (syntax->list #'(form* ...)) forms forms))

 (with-syntax ([(form* ...) e])
   (syntax->list #'(form* ...)))

is just a slower version of 'e' (unless e is a syntax object or if
there is an error, neither of which apply here).

Robby



Posted on the users mailing list.