syntax-parse-pattern-expanders.scrbl (2857B)
1 #lang scribble/manual 2 @require[racket/require 3 "utils.rkt" 4 @for-label[phc-toolkit/syntax-parse 5 racket/base 6 syntax/parse]] 7 8 @(def-orig orig [syntax/parse] 9 ~or 10 ~literal 11 ~parse 12 ~bind) 13 14 @title{Pattern expanders} 15 @author{@author+email["Suzanne Soy" "racket@suzanne.soy"]} 16 17 @declare-exporting[phc-toolkit/syntax-parse 18 #:use-sources 19 [(submod (lib "phc-toolkit/syntax-parse.rkt") typed)]] 20 21 @defform[#:kind "pattern expander" 22 (~either alt ...)]{ 23 Like @orig:~or, but with no special behaviour when present under ellipses. 24 The use case for this is that @racket[({~or {~and 1 x} {~and 2 x}} ...)] would 25 match any list of @racket[1]s and @racket[2]s in any order, but it complains 26 that the attribute is bound twice, since both alternatives within the 27 @racket[~or] are understood as separate patterns, not mutually-exclusive 28 choices. On the other hand @racket[({~either {~and 1 x} {~and 2 x}} ...)] still 29 matches @racket[(2 1 1 1 2 2 1)], and successfully binds all the elements to 30 @racket[x ...].} 31 32 @defform[#:kind "pattern expander" 33 (~lit alt ...)]{ 34 Alias for @|orig:~literal|.} 35 36 @defform[#:kind "pattern expander" 37 (~with pat val)]{ 38 Alias for @|orig:~parse|, can be used semantically when @racket[#:with] would 39 have been used in a syntax class definition.} 40 41 @defform[#:kind "pattern expander" 42 (~attr attr-name val)]{ 43 Alias for @racket[(#,orig:~bind [attr-name val])], can be used semantically 44 when @racket[#:attr] would have been used in a syntax class definition.} 45 46 @(define ttern 47 @seclink["stxparse-patterns" 48 #:doc '(lib "syntax/scribblings/syntax.scrbl")]{pattern}) 49 50 @defform[#:kind "pattern expander" 51 (~optkw kw #,ttern ...) 52 #:contracts 53 [(kw keyword?)]]{ 54 A shorthand for: 55 56 @racketblock[{~optional {~seq {~and _name kw} #,ttern ...}}] 57 58 where @racket[_name] is derived from the keyword, so that 59 @racket[~optkw #:foo] binds the pattern variable @racket[foo].} 60 61 62 @defform[#:kind "pattern expander" 63 (~optkw… kw #,ttern ...) 64 #:contracts 65 [(kw keyword?)]]{ 66 A shorthand for: 67 68 @racketblock[(~optional {~seq {~and _name kw} #,ttern ...} 69 #:name "the kw keyword")] 70 71 where the occurrence of @racket["kw"] within the string is replaced by the 72 actual @racket[kw] keywords, and where the @racket[_name] is derived from the 73 keyword, so that @racket[~optkw #:foo] binds the pattern variable 74 @racket[foo], and uses the name @racket["the #:foo keyword"]. 75 76 This form can only be used where an 77 @tech[#:doc '(lib "syntax/scribblings/syntax.scrbl")]{ellipsis-head pattern} 78 is allowed.} 79 80 81 @defform[#:kind "pattern expander" 82 (~maybe #,ttern ...)]{ 83 A shorthand for: 84 85 @racketblock[(~optional {~seq #,ttern ...})]} 86 87