► |  | Syntax: Meta-Programming Helpers

1 | Syntax Object Helpers
2 | Module-Processing Helpers
3 | Macro Transformer Helpers
4 | Reader Helpers
5 | Non-Module Compilation And Expansion
6 | Trusting Standard Recertifying Transformers
7 | Attaching Documentation to Exports
8 | Parsing and classifying syntax
| Index

▼ | 8 | Parsing and classifying syntax

8.1 | Quick Start
8.2 | Parsing and classifying syntax
8.3 | Syntax patterns
8.4 | Literal sets and Conventions
8.5 | Library syntax classes and literal sets

On this page:

define-literal-set
define-conventions

top← prev up next →

#### 8.4`` Literal sets and Conventions

Sometimes the same literals are recognized in a number of different places. The most common example is the literals for fully expanded programs, which are used in many analysis and transformation tools. Specifying literals individually is burdensome and error-prone. As a remedy, syntax/parse offers literal sets. A literal set is defined via define-literal-set and used via the #:literal-set option of syntax-parse.

(define-literal-set name-id (literal ...))

literal |  | = |  | literal-id
|  | | |  | (pattern-id literal-id)

Defines name as a literal set. Each literal can have a separate pattern-id and literal-id. The pattern-id determines what identifiers in the pattern are treated as literals. The literal-id determines what identifiers the literal matches.

Examples:

> (define-literal-set def-litset
(define-values define-syntaxes))

> (syntax-parse #'(define-syntaxes (x) 12)
#:literal-sets (def-litset)
[(define-values (x:id ...) e:expr) 'v]
[(define-syntaxes (x:id ...) e:expr) 's])

s

(define-conventions name-id (id-pattern syntax-class) ...)

name-pattern |  | = |  | exact-id
|  | | |  | name-rx
syntax-class |  | = |  | syntax-class-id
|  | | |  | (syntax-class-id expr ...)

Defines conventions that supply default syntax classes for pattern variables. A pattern variable that has no explicit syntax class is checked against each id-pattern, and the first one that matches determines the syntax class for the pattern. If no id-pattern matches, then the pattern variable has no syntax class.

Examples:

> (define-conventions xyz-as-ids
[x id] [y id] [z id])

> (syntax-parse #'(a b c 1 2 3)
#:conventions (xyz-as-ids)
[(x ... n ...) (syntax->datum #'(x ...))])

(a b c)

> (define-conventions xn-prefixes
[#rx"^x" id]
[#rx"^n" nat])

> (syntax-parse #'(a b c 1 2 3)
#:conventions (xn-prefixes)
[(x0 x ... n0 n ...)
(syntax->datum #'(x0 (x ...) n0 (n ...)))])

(a (b c) 1 (2 3))

top← prev up next →