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

▼ | 1.2 | Examples

1.2.1 | Phases and Reusable Syntax Classes
1.2.2 | Optional Keyword Arguments
1.2.3 | Variants with Uniform Meanings
1.2.4 | Variants with Varied Meanings
1.2.5 | More Keyword Arguments
1.2.6 | Contracts on Macro Sub-expressions

► | 1.2.4 | Variants with Varied Meanings

1.2.4.1 | Syntactic Normalization
1.2.4.2 | Non-syntax-valued Attributes

On this page:

1.2.4.1`` Syntactic Normalization
1.2.4.2`` Non-syntax-valued Attributes

top ← prev up next →

##### 1.2.4`` Variants with Varied Meanings

As explained in the previous section, the meaning of a syntax class can be uniform, or it can be varied; that is, different instances of the syntax class can carry different kinds of information. This section discusses the latter kind of syntax class.

A good example of a syntax class with varied meanings is the for-clause of the for family of special forms.

for-clause |  | = |  | [id seq-expr]
|  | | |  | [(id ...) seq-expr]
|  | | |  | #:when guard-expr

The first two variants carry the same kind of information; both consist of identifiers to bind and a sequence expression. The third variant, however, means something totally different: a condition that determines whether to continue the current iteration of the loop, plus a change in scoping for subsequent seq-exprs. The information of a for-clause must be represented in a way that a client macro can do further case analysis to distinguish the “bind variables from a sequence” case from the “skip or continue this iteration and enter a new scope” case.

This section discusses two ways of representing varied kinds of information.

##### 1.2.4.1`` Syntactic Normalization

One approach is based on the observation that the syntactic variants already constitute a representation of the information they carry. So why not adapt that representation, removing redundancies and eliminating simplifying the syntax to make subsequent re-parsing trivial.

> (define-splicing-syntax-class for-clause
> #:attribute (norm)
> (pattern [var:id seq:expr]
> #:with norm #'[(var) seq])
> (pattern [(var:id ...) seq:expr]
> #:with norm #'[(var ...) seq])
> (pattern (~seq #:when guard:expr)
> #:with norm #'[#:when guard]))

First, note that since the #:when variant consists of two separate terms, we define for-clause as a splicing syntax class. Second, that kind of irregularity is just the sort of thing we’d like to remove so we don’t have to deal with it again later. Thus we represent the normalized syntax as a single term beginning with either a sequence of identifiers (the first two cases) or the keyword #:when (the third case). The two normalized cases are easy to process and easy to tell apart. We have also taken the opportunity to desugar the first case into the second.

A normalized syntactic representation is most useful when the subsequent case analysis is performed by syntax-parse or a similar form.

##### 1.2.4.2`` Non-syntax-valued Attributes

When the information carried by the syntax is destined for complicated processing by Racket code, it is often better to parse it into an intermediate representation using idiomatic Racket data structures, such as lists, hashes, structs, and even objects.

Thus far we have only used syntax pattern variables and the #:with keyword to bind attribues, and the values of the attributes have always been syntax. To bind attributes to values other than syntax, use the #:attr keyword.

> ; A ForClause is either
> ; - (bind-clause (listof identifier) syntax)
> ; - (when-clause syntax)
> (struct bind-clause (vars seq-expr))
> (struct when-clause (guard))
> (define-splicing-syntax-class for-clause
> #:attributes (ast)
> (pattern [var:id seq:expr]
> #:attr ast (bind-clause (list #'var) #'seq))
> (pattern [(var:id ...) seq:expr]
> #:attr ast (bind-clause (syntax->list #'(var ...))
> #'seq))
> (pattern (~seq #:when guard:expr)
> #:attr ast (when-clause #'guard)))

Be careful! If we had used #:with instead of #:attr, the #f would have been coerced to a syntax object before being matched against the pattern default.

Attributes with non-syntax values cannot be used in syntax templates. Use the attribute form to get the value of an attribute.

top ← prev up next →