1.6 Defining Simple Macros
(require syntax/parse/define) | package: base |
syntax
(define-syntax-parse-rule (macro-id . pattern) pattern-directive ... template)
(define-syntax (macro-id stx) (syntax-parse stx #:track-literals [((~var macro-id id) . pattern) pattern-directive ... #'template]))
> (define-syntax-parse-rule (fn x:id rhs:expr) (lambda (x) rhs)) > ((fn x x) 17) 17
> (fn 1 2) fn: expected identifier
at: 1
in: (fn 1 2)
> (define-syntax-parse-rule (fn2 x y rhs) #:declare x id #:declare y id #:declare rhs expr (lambda (x y) rhs)) > ((fn2 a b (+ a b)) 3 4) 7
> (fn2 a #:b 'c) fn2: expected identifier
at: #:b
in: (fn2 a #:b (quote c))
Added in version 7.9.0.22 of package base.
syntax
(define-syntax-parser macro-id parse-option ... clause ...+)
(define-syntax macro-id (syntax-parser parse-option ... clause ...))
> (define-syntax-parser fn3 [(fn3 x:id rhs:expr) #'(lambda (x) rhs)] [(fn3 x:id y:id rhs:expr) #'(lambda (x y) rhs)]) > ((fn3 x x) 17) 17
> ((fn3 a b (+ a b)) 3 4) 7
> (fn3 1 2) fn3: expected identifier
at: 1
in: (fn3 1 2)
> (fn3 a #:b 'c) fn3: expected expression or expected identifier
at: #:b
in: (fn3 a #:b (quote c))
syntax
(define-simple-macro (macro-id . pattern) pattern-directive ... template)
NOTE: This macro is deprecated; use define-syntax-parse-rule, instead.
Re-exports define-syntax-parse-rule for backward-compatibility.
Changed in version 6.12.0.3 of package base: Changed pattern head to (~var macro-id id) from
macro-id, allowing tilde-prefixed identifiers or
identifiers containing colons to be used as macro-id
without producing a syntax error.
Changed in version 6.90.0.29: Changed to always use the #:track-literals
syntax-parse option.