On 03/15/2017 12:33 PM, Dan Liebgold wrote:
Here's what works, not using the inner ellipses:


But I'd prefer to use the ellipses......

Here are three ways of doing that:

Method 1: use with-syntax to plant the inner ellipsis

  (define-syntax (test stx)
    (syntax-case stx ()
      ((_ n e ...)
       (with-syntax ([ooo (quote-syntax ...)])
         #'(define-syntax (n stx)
             (syntax-case stx ()
               ((_ e0 ooo)
                #''(n (e ...) e0 ooo)

Method 2: use "ellipsis escaping": wrapping a subtemplate with (... _) interprets the other ellipses within it as literals

  (define-syntax (test stx)
    (syntax-case stx ()
      ((_ n e ...)

       #'(define-syntax (n stx)
           (syntax-case stx ()
             ((_ e0 (... ...))
              #''(n (e ...) e0 (... ...))

Method 3: use a compile-time helper function to make the inner transformer

    (define (make-inner-transformer n es)
      (lambda (stx)
        (syntax-case stx ()
          [(_ e0 ...)
           (with-syntax ([n n]
                         [(e ...) es])
             #''(n (e ...) e0 ...))]))))

  (define-syntax (test stx)
    (syntax-case stx ()
      ((_ n e ...)
       #'(define-syntax n
           (make-inner-transformer (quote-syntax n)
                                   (quote-syntax (e ...)))))))

The third method is clunky for this tiny example, but for some larger examples it is far simpler than trying to generate the syntax of the transformer function. It also helps avoid bloat in expanded/compiled code by using closures instead of substitution.


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