1.6 Defining Simple Macros
(require syntax/parse/define) | package: base |
syntax
(define-simple-macro (macro-id . pattern) pattern-directive ... template)
Defines a macro named macro-id; equivalent to the following:
(define-syntax (macro-id stx) (syntax-parse stx [(macro-id . pattern) pattern-directive ... #'template]))
Examples:
> (define-simple-macro (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-simple-macro (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))
syntax
(define-syntax-parser macro-id parse-option ... clause ...+)
Defines a macro named macro-id; equivalent to:
(define-syntax macro-id (syntax-parser parse-option ... clause ...))
Examples:
> (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))