[racket] Provide transformers

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Mon Sep 22 09:24:53 EDT 2014

Is this what you are looking for?

#lang racket/base
(require (for-syntax racket/base
                     syntax/parse
                     racket/provide-transform))

(define-syntax test-pred-out
  (make-provide-pre-transformer
   (λ (stx _)
     (syntax-parse stx
       [(_ b:id p:expr)
        (syntax-local-lift-module-end-declaration
         #'(module+ test
             (if (p b)
               (printf "It passed.\n")
               (printf "It didn't pass.\n"))))
        (syntax/loc stx
          b)]))))

;; Example

(define some-integer "17")
(define some-other-integer 17)
(provide (test-pred-out some-integer integer?)
         (test-pred-out some-other-integer integer?))

On Fri, Sep 19, 2014 at 5:41 PM, Jack Firth <jackhfirth at gmail.com> wrote:
> I've been looking into how to design a macro that would specify tests in a
> provide transformer, for example something like
>
>     (provide (test-pred-out some-integer integer?))
>
> I'd like that to expand to:
>
>     (provide some-integer)
>     (module+ test
>       (check-pred integer? some-integer))
>
> From what I can gather from the reference, normally provide forms are done
> with define-provide-syntax, but that macro doesn't let me splice anything
> into the module body. The reference mentions provide pre-transformers, and
> says:
>
> "A provide pre-transformer is applied as part of the first phase of a
> module’s expansion. Since it is used in the first phase, a provide
> pre-transformer can use functions such as syntax-local-lift-expression to
> introduce expressions and definitions in the enclosing module."
>
> So it sounds like what I want is definitely possible, but I have no idea how
> to actually do it. Does anyone have some simple well-explained examples to
> guide me in the right direction?
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>



-- 
Jay McCarthy
http://jeapostrophe.github.io

           "Wherefore, be not weary in well-doing,
      for ye are laying the foundation of a great work.
And out of small things proceedeth that which is great."
                          - D&C 64:33


Posted on the users mailing list.