www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

auto-syntax-e.scrbl (2506B)


      1 #lang scribble/manual
      2 @require[scribble/example
      3          @for-label[auto-syntax-e
      4                     racket/base
      5                     syntax/parse]]
      6 
      7 @title{auto-syntax-e}
      8 @author[@author+email["Suzanne Soy" "racket@suzanne.soy"]]
      9 
     10 @defmodule[auto-syntax-e]
     11 
     12 This package allows using syntax pattern variables outside of syntax
     13 templates: when @racket[_x] is bound as a syntax pattern variable, writing
     14 @racket[_x] then becomes roughly equivalent to
     15 @racket[(syntax->datum #'_x-ddd)], where @racket[_x-ddd] is @racket[x] wrapped
     16 under the appropriate number of ellipses. If the pattern variable is bound by
     17 @racket[syntax-parse] and contains non-syntax parts (e.g. it was bound within
     18 an @racket[~optional] clause, or using @racket[#:attr]), they are left
     19 unchanged.
     20 
     21 @defform[(auto-with-syntax ([patᵢ eᵢ] ...) body ...)]{
     22  Like @racket[(with-syntax ([patᵢ eᵢ] ...) body ...)], but the syntax pattern
     23  variables bound by the @racket[patᵢ ...] can be used outside of syntax patterns
     24  (they are implicitly transformed using @racket[syntax->datum]):
     25 
     26  @examples[#:eval ((make-eval-factory '(auto-syntax-e)))
     27            (auto-with-syntax ([x #'123])
     28              (list (add1 x) #'x))]}
     29 
     30 @defform[(auto-syntax (pvarᵢ ...) body ...)]{
     31  Re-binds the syntax pattern variables @racket[pvarᵢ ...], so that can be used
     32  outside of syntax patterns like in @racket[auto-with-syntax]:
     33 
     34  @examples[#:eval ((make-eval-factory '(auto-syntax-e syntax/parse)))
     35            (syntax-parse #'(1 2 3)
     36              [(x:nat y:nat ...)
     37               (auto-syntax (x y)
     38                 (list (map add1 (cons x y)) #'(x y ...)))])
     39            (syntax-parse #'(1 2 3)
     40              [({~seq x:nat {~optional y:nat}} ...)
     41               (auto-syntax (x y)
     42                 (list (map cons x y)
     43                       (attribute x)
     44                       (attribute y)))])]
     45 
     46  When one of the @racket[pvarᵢ ...] is not a syntax pattern variable, it is
     47  ignored and the existing binding, if any, is left untouched.
     48 
     49  Note that it is not necessary to specify the ellipsis-depth of each
     50  @racket[pvarᵢ].}
     51 
     52 @defform[(auto-syntax-case stx-expression (literal ...)
     53                            [patᵢ maybe-guardᵢ bodyᵢ]
     54                            ...)
     55          #:grammar
     56          [(maybe-guardᵢ (code:line)
     57                         (code:line guard-expression))]]{
     58  Like @racket[syntax-case], but the syntax pattern variables bound by the
     59  @racket[patᵢ ...] can be used outside of templates like in
     60  @racket[auto-with-syntax].}