It is the same error, but I think it is occurring at a different place.
Running raco macro-stepper on name-test.rkt  the original error was
happening in define-values (because format-id was not used). After making
the format-id changes it occurs during the expansion of use-name when
expanding the line macro. Therefore I don't think that putting it in #%top
will fix your problem because it is now a hygiene error. I'm not entirely
sure why it complains but it seems like it is related to 2.4 Internal
definitions from Macros that work together (
https://www.cs.utah.edu/plt/publications/jfp12-draft-fcdf.pdf), local-expand
<https://docs.racket-lang.org/reference/stxtrans.html#%28def._%28%28quote._~23~25kernel%29._local-expand%29%29>
and syntax-local-make-definition-context
<https://docs.racket-lang.org/reference/stxtrans.html#%28def._%28%28quote._~23~25kernel%29._syntax-local-make-definition-context%29%29>
seem like they might be what is needed? Best,
Tom

PS I've encountered similar issues a number of times and haven't taken the
time to figure out the right way to deal with it, so your issue is a nice
clear cut example.

On Tue, Aug 13, 2019 at 7:45 PM Jonathan Simpson <jjsim...@gmail.com> wrote:

> On Tuesday, August 13, 2019 at 10:41:33 PM UTC-4, Jonathan Simpson wrote:
>>
>> I changed the lines in named-query from
>>
>> [name (datum->syntax stx (string->symbol (syntax->datum #'magic-name)))
>> to
>> (format-id stx "~a" (syntax-e #'magic-name))
>>
>>
> Of course this should be
>
> (datum->syntax stx (string->symbol (syntax->datum #'magic-name)))
> to
> (format-id stx "~a" (syntax-e #'magic-name))
>
>
>
>> and I still get the same error. I also switched to using format-id where
>> I use the name, with no change either. Am I using this correctly?
>>
>> The format-id change is easier to read, so it is worth keeping even if it
>> doesn't solve this issue.
>>
>> -- Jonathan
>>
>> On Tuesday, August 13, 2019 at 10:10:25 PM UTC-4, Tom Gillespie wrote:
>>>
>>> Without have read this carefully, are you maybe looking for
>>> https://docs.racket-lang.org/reference/syntax-util.html#%28def._%28%28lib._racket%2Fsyntax..rkt%29._format-id%29%29?
>>> If you just pass string->symbol and then use datum->syntax you will get
>>> binding errors.
>>> Tom
>>>
>>> On Tue, Aug 13, 2019 at 7:03 PM Jonathan Simpson <jjsi...@gmail.com>
>>> wrote:
>>>
>>>> I have a bug I'm trying to fix in #lang magic(
>>>> https://github.com/jjsimpso/magic). I'm pretty sure I know what the
>>>> cause is and I know what I want to do, but I don't know how to do it. The
>>>> situation is this:
>>>>
>>>> In the following magic code, a new function(called a named query in
>>>> magic) tga-image is defined and later used inside the definition of the
>>>> function test-scope. The issue is that tga-image is an unbound identifier
>>>> in test-scope. If tga-image is used outside of a function there isn't a
>>>> problem because the syntax object passed to use has the binding. What I
>>>> would like to do is define tga-image at the top level. When test-scope is
>>>> being expanded it doesn't find the binding for tga-image and tags it with
>>>> #%top, so I think putting tga-image at the top level will solve my problem.
>>>> I also think that this is not a bad solution for my use case.
>>>>
>>>> #lang magic
>>>>
>>>>
>>>> # display tga bitmap image information
>>>> 0 name  tga-image
>>>> >2 byte <34        Targa image data
>>>>
>>>>
>>>> 0 name  test-scope
>>>> >2 byte <34        Test Scope
>>>> >0 use  tga-image
>>>>
>>>> After parsing, this code becomes:
>>>>
>>>> (module magic-mod magic/expander
>>>>   (magic
>>>>    #f
>>>>    #f
>>>>    (named-query
>>>>     (name-line (offset 0) (name-type "name") "tga-image")
>>>>     (level)
>>>>     (line (offset 2) (type (numeric "byte")) (test (numtest "<" 34)) 
>>>> (message
>>>> "Targa image data")))
>>>>    (named-query
>>>>     (name-line (offset 0) (name-type "name") "test-scope")
>>>>     (level)
>>>>     (line (offset 2) (type (numeric "byte")) (test (numtest "<" 34)) 
>>>> (message
>>>> "Test Scope"))
>>>>     (level)
>>>>     (line (offset 0) (type (use "use")) (test (use-name "tga-image"
>>>> ))))))
>>>>
>>>> This is the most relevant code in my expander (I highlighted creation
>>>> of name's binding in red):
>>>>
>>>> (define-syntax (named-query stx)
>>>>   (syntax-case stx (name-line)
>>>>     [(_ (name-line (_ 0) (_ "name") magic-name))
>>>>      (with-syntax ([name (datum->syntax stx (string->symbol
>>>> (syntax->datum #'magic-name)))])
>>>>        #'(define name
>>>>            (lambda (new-offset) (void))))]
>>>>     [(_ (name-line (_ 0) (_ "name") magic-name) . rst)
>>>>      (with-syntax ([name (datum->syntax stx (string->symbol
>>>> (syntax->datum #'magic-name)))]
>>>>                    [modified-rst (cons (datum->syntax #'rst
>>>> always-true-line) #'rst)])
>>>>        #'(define name
>>>>            (lambda (new-offset)
>>>>              (syntax-parameterize ([name-offset
>>>> (make-rename-transformer #'new-offset)])
>>>>                ;(printf "name: offset = ~a~n" name-offset)
>>>>                (query . modified-rst)))))]))
>>>>
>>>> (define-syntax (magic-module-begin stx)
>>>>   (define (query? expr)
>>>>     (if (and (pair? expr)
>>>>              (equal? (car expr) 'query))
>>>>         #t
>>>>         #f))
>>>>   (define (named-query? expr)
>>>>     (if (and (pair? expr)
>>>>              (equal? (car expr) 'named-query))
>>>>         #t
>>>>         #f))
>>>>   (define (wrap-with-delimiter-print expr)
>>>>     (list 'when* expr '(printf "*** ")))
>>>>
>>>>   (let ([exprs (cdadr (syntax->datum stx))])
>>>>     ;(display queries)
>>>>     (let ([queries (filter query? exprs)]
>>>>           [named-queries (filter named-query? exprs)])
>>>>       #`(#%module-begin
>>>>          #,@named-queries
>>>>          (define (magic-query)
>>>>            (or #,@queries))
>>>>          (define (magic-query-run-all)
>>>>            ; any-true? creates a binding for last-level-offset which we
>>>> probably don't want here. investigate.
>>>>            (any-true? #,@(map wrap-with-delimiter-print queries)))
>>>>          (provide magic-query magic-query-run-all)))))
>>>>
>>>> The full code is at
>>>> https://github.com/jjsimpso/magic/blob/master/expander.rkt.
>>>>
>>>> If there isn't an easy way to pass the top level syntax object into
>>>> named-query, then I may have to do something with syntax parameters. I
>>>> think that would be more complicated since I'm potentially defining lots of
>>>> functions.
>>>>
>>>> Any help is appreciated! I thought this would be an easy one, but I've
>>>> spent a while on it already. Hopefully there is a simple solution.
>>>>
>>>> -- Jonathan
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Racket Users" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to racket...@googlegroups.com.
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/racket-users/ee664882-1dec-4bb8-b66b-bd6ee80316dc%40googlegroups.com
>>>> <https://groups.google.com/d/msgid/racket-users/ee664882-1dec-4bb8-b66b-bd6ee80316dc%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/94501425-0fae-4cea-86b0-1aaa1ac574cc%40googlegroups.com
> <https://groups.google.com/d/msgid/racket-users/94501425-0fae-4cea-86b0-1aaa1ac574cc%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CA%2BG3_POWFvT5FTrFkZQWVoyMSCOJ-MHqjGEKvLA9UaSHqQ7L9w%40mail.gmail.com.

Reply via email to