1.6 Defining Simple Macros

► |  | 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 | Unwindable State
1.10 | Configuring Error Reporting
1.11 | Debugging and Inspection Tools
1.12 | Experimental
1.13 | Minimal Library

On this page:

define-syntax-parse-rule
define-syntax-parser
define-simple-macro

top contents ← prev up next →

#### 1.6`` Defining Simple Macros🔗ℹ

(require syntax/parse/define) | package: base

The syntax/parse/define library provides for-syntax all of syntax/parse, as well as providing some new forms.

> > syntax
> > (define-syntax-parse-rule (macro-id . pattern) pattern-directive ...
> > template)

Defines a macro named macro-id; equivalent to the following:

> (define-syntax (macro-id stx)
> (syntax-parse stx
> #:track-literals
> [((~var macro-id id) . pattern) pattern-directive ... #'template]))

Examples:

> > (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 ...+)

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

> > 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.

top contents ← prev up next →