► |  | Syntax: Meta-Programming Helpers

1 | Parsing and Specifying Syntax
2 | Syntax Object Helpers
3 | Datum Pattern Matching
4 | Module-Processing Helpers
5 | Macro Transformer Helpers
6 | Reader Helpers
7 | Parsing for Bodies
8 | Unsafe for Clause Transforms
9 | Source Locations
10 | Preserving Source Locations
11 | Non-Module Compilation And Expansion
12 | Trusting Standard Recertifying Transformers
13 | Attaching Documentation to Exports
14 | Contracts for Macro Subexpressions
15 | Macro Testing
16 | Internal-Definition Context Helpers
| Index

▼ | 1 | Parsing and Specifying Syntax

1.1 | Introduction
1.2 | Examples
1.3 | Parsing Syntax
1.4 | Specifying Syntax with Syntax Classes
1.5 | Syntax Patterns
1.6 | Defining Simple Macros
1.7 | Literal Sets and Conventions
1.8 | Library Syntax Classes and Literal Sets
1.9 | Debugging and Inspection Tools
1.10 | Experimental

On this page:

define-simple-macro
define-syntax-parser

top ← prev up next →

#### 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))

top ← prev up next →