> On Jan 22, 2016, at 9:07 AM, brendan <bren...@cannells.com> wrote:
> 
> I was playing around with parser-tools, which has a convenient syntax in 
> which the elements of a grammar rule are bound in order to $1, $2, etc. This 
> led me to think about how the composability of non-hygienic macros could be 
> limited by name-collisions - not very likely in this case, but easier to 
> imagine for something like, say, anaphoric if. My understanding of Racket's 
> internals is still pretty limited, so I'd like to know if there could be a 
> mechanism with which the programmer could specify prefixes for any bindings 
> introduced by a particular macro, without requiring explicit support from the 
> macro writer?


I was curious, so I tried to write a macro to do that.

I was hoping something like this would work, but it produces
'(prefixed:let-values (((or-part) #f)) (if or-part or-part (or #t)))
Instead of
'(prefixed:let-values (((prefixed:or-part) #f)) (prefixed:if prefixed:or-part 
prefixed:or-part (or #t)))
Which is what I was hoping for. 

#lang racket/base

(require (for-syntax racket/base
                     racket/syntax
                     syntax/parse
                     (only-in colon-match :match define-:match-class)))

(begin-for-syntax
  (define-:match-class stx syntax?)
  ;; syntax-prefix-introduced-ids : Stx Stx Stx -> Stx
  ;; Recurs on inner-stx to prefix all the identifiers with the same
  ;; context as outer-stx.
  (define (syntax-prefix-introduced-ids outer-stx prefix inner-stx)
    (define (recur inner-stx)
      (syntax-prefix-introduced-ids outer-stx prefix inner-stx))
    (define e (syntax-e inner-stx))
    (:match e
      [(? symbol? sym)
       (cond [(bound-identifier=? inner-stx (datum->syntax outer-stx sym))
              (format-id inner-stx "~a~a" prefix inner-stx
                         #:source inner-stx #:props inner-stx)]
             [else
              inner-stx])]
      [(list as:stx ...)
       (datum->syntax inner-stx (map recur as)
                      inner-stx inner-stx)]
      [(list-rest as:stx ... b:stx)
       (datum->syntax inner-stx (append (map recur as) (recur b))
                      inner-stx inner-stx)]
      [_
       inner-stx])))

(define-syntax prefix-introduced-ids
  (syntax-parser
    [(prefix-introduced-ids prefix form)
     (define expanded
       (local-expand #'form
                     (syntax-local-context)
                     #f))
     (syntax-prefix-introduced-ids expanded #'prefix expanded)]))

(syntax->datum
 (expand-once
  #'(prefix-introduced-ids prefixed: (or #false #true))))

-- 
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