Again, you have an issue of scope. Using `(syntax-local-introduce
(datum->syntax #f 'root))` creates an identifier with no scopes that also
won't get the macro-introduction scope from `md-module-begin`. This works
for `doc` in the previous example, because `handle` can also introduce a
`doc` identifier with no scopes, but now you want your use of `root` to be
bound by a definition of `root` in the source syntax, which will not have
no scopes: it will have the module scope for the module in which it
appears. I've included a modification of my last example that creates a
`root` identifier with the right scopes. I don't know that I would actually
do this this way, though. In general, I like the explicitly recursive
example I originally sent because you can explicitly communicate things
like synthesized identifiers to the next expansion step.

To debug issues like this in the future, the macro stepper can show you the
scopes present on various identifiers, so you could see that your use of
`root` was missing a scope that was present on the definition of `root` you
wanted to bind it.

#lang racket/base

(module lang racket/base
  (require syntax/wrap-modbeg
           (for-syntax racket/base
                       syntax/parse))
  (provide (except-out (all-from-out racket/base)
                       #%module-begin)
           (rename-out [md-module-begin #%module-begin]))
  (define-syntax handle
    (syntax-parser
      [(_ b:expr)
       #:with acc (syntax-local-introduce (datum->syntax #f 'acc))
       #`(set! acc (string-append acc b))]))
  (define-syntax wrapping-modbeg
    (make-wrapping-module-begin #'handle))
  (define-syntax md-module-begin
    (syntax-parser
      [(_ expr1 ...)
       #:with acc (syntax-local-introduce (datum->syntax #f 'acc))
       #:with root (datum->syntax this-syntax 'root)
       #'(wrapping-modbeg (define acc "")
                          expr1 ...
                          (define doc (root acc))
                          (provide doc))])))

(module example (submod ".." lang)
  (define (root arg)
    (list 'root arg))
  "Example")

(require (submod "." example))

doc


-Philip

On Sat, Aug 18, 2018 at 10:04 PM, Vityou <zlee...@gmail.com> wrote:

> Thanks, that makes sense.  However, I tried doing this with a variable
> defined within a program, but it doesn't work.  I would like the user to
> define their our `root` function to wrap everything, however, when I try to
> access it with `syntax-local-introduce` it isn't found.  I have:
>
> (define-for-syntax md-module-begin
>   (syntax-parser
>     [(_ (expr1 ...))
>      #:with accumulated (syntax-local-introduce (datum->syntax #f
> 'accumulated))
>      #:with root (syntax-local-introduce (datum->syntax #f 'root))
>      #`(wrapping-modbeg (define accumulated "")
>                         expr1 ...
>                         (define doc (root (parse-markdown accumulated)))
>                         (provide doc)
>                         (print doc))]))
>
> For my module begin and:
>
> **asdf**
>
> @"asdf2"
>
> @(define (root x . y) '(test "asdf"))
>
> @(define x 5)
> x Is @x
>
> @(string-append "test " "string")
> <asdf>123</asdf>
>
> as an example program.  However, it complains that `root` is undefined.
>
> On Friday, August 17, 2018 at 9:51:38 PM UTC-6, Philip McGrath wrote:
>
>> The error is `set!` reporting that `doc` is undefined, because the `doc`
>> identifier from `md-module-begin` is introduced hygienically. Also, I
>> believe your pattern for `md-module-begin` has an extra set of parentheses.
>> Here is a working version:
>>
>> #lang racket/base
>>
>> #lang racket/base
>>
>> (module lang racket/base
>>   (require syntax/wrap-modbeg
>>            (for-syntax racket/base
>>                        syntax/parse))
>>   (provide (except-out (all-from-out racket/base)
>>                        #%module-begin)
>>            (rename-out [md-module-begin #%module-begin]))
>>   (define-syntax handle
>>     (syntax-parser
>>       [(_ b:expr)
>>        #:with doc (syntax-local-introduce (datum->syntax #f 'doc))
>>        #`(set! doc (string-append doc b))]))
>>   (define-syntax wrapping-modbeg
>>     (make-wrapping-module-begin #'handle))
>>   (define-syntax md-module-begin
>>     (syntax-parser
>>       [(_ expr1 ...)
>>        #:with doc (syntax-local-introduce (datum->syntax #f 'doc))
>>        #'(wrapping-modbeg (define doc "")
>>                           expr1 ...)])))
>>
>> (module example (submod ".." lang)
>>   (provide doc)
>>   "Example")
>>
>> (require (submod "." example))
>>
>> doc
>>
>>
>>
>> -Philip
>>
>> On Sat, Aug 18, 2018 at 3:36 AM, Vityou <zle...@gmail.com> wrote:
>>
>>> Stupid question, but how would I saving them in a global variable?  So
>>> far I have this:
>>>
>>> (define-syntax handle
>>>   (syntax-parser
>>>     [(_ b:expr) #`(set! #,(syntax-local-introduce (datum->syntax #f
>>> 'doc)) (string-append #,(syntax-local-introduce (datum->syntax #f 'doc))
>>> b))]))
>>>
>>> (define-syntax wrapping-modbeg
>>>   (make-wrapping-module-begin #'handle))
>>>
>>> (define-syntax md-module-begin
>>>   (syntax-parser
>>>     [(_ (expr1 ...)) #'(wrapping-modbeg (define doc "")
>>>                                         expr1 ...)]))
>>>
>>> However, it complains that `set!` isn't defined.  I thought it would be
>>> defined since my module provides `racket/base`.
>>>
>>> --
>>> 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...@googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
> 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.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to