1.8 Library Syntax Classes and Literal Sets

► |  | 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 | Debugging and Inspection Tools
1.11 | Experimental

► | 1.8 | Library Syntax Classes and Literal Sets

1.8.1 | Syntax Classes
1.8.2 | Literal Sets
1.8.3 | Function Headers

On this page:

1.8.1`` Syntax Classes
expr
identifier
boolean
char
keyword
number
integer
exact-integer
exact-nonnegative-integer
exact-positive-integer
id
nat
str
static
expr/ c
1.8.2`` Literal Sets
kernel-literals
1.8.3`` Function Headers
function-header
formal
formals

top ← prev up next →

#### 1.8`` Library Syntax Classes and Literal Sets

##### 1.8.1`` Syntax Classes

> syntax class expr

Matches anything except a keyword literal (to distinguish expressions from the start of a keyword argument sequence). The term is not otherwise inspected, since it is not feasible to check if it is actually a valid expression.

> syntax class identifier
> syntax class boolean
> syntax class char
> syntax class keyword
> syntax class number
> syntax class integer
> syntax class exact-integer
> syntax class exact-nonnegative-integer
> syntax class exact-positive-integer

Match syntax satisfying the corresponding predicates.

> syntax class string
> syntax class bytes

As special cases, Racket’s string and bytes bindings are also interpreted as syntax classes that recognize literal strings and bytes, respectively.

Added in version 6.9.0.4 of package base.

> syntax class id

Alias for identifier.

> syntax class nat

Alias for exact-nonnegative-integer.

> syntax class str

Alias for string.

> syntax class (static predicate description) → syntax class
> predicate : (-> any/c any/c)
> description : (or/c string? #f)

The static syntax class matches an identifier that is bound in the syntactic environment to static information (see syntax-local-value) satisfying the given predicate. If the term does not match, the description argument is used to describe the expected syntax.

When used outside of the dynamic extent of a macro transformer (see syntax-transforming?), matching fails.

The attribute value contains the value the name is bound to.

If matching succeeds, static additionally adds the matched identifier to the current syntax-parse state under the key 'literals using syntax-parse-state-cons!, in the same way as identifiers matched using #:literals or ~literal.

Changed in version 6.90.0.29 of package base: Changed to add matched identifiers to the syntax-parse state under the key 'literals.

> > syntax class
> > (expr/c |  | contract-expr |  |  |  |
> > | [ | #:positive pos-blame |  |  |  |
> > |  | #:negative neg-blame |  |  |  |
> > |  | #:name expr-name |  |  |  |
> > |  | #:macro macro-name |  |  |  |
> > |  | #:context ctx]) |  | → |  | syntax class
> contract-expr : syntax?
> | pos-blame |  | : |  | (or/c syntax? string? module-path-index? 'from-macro 'use-site 'unknown)
> |  |  | = |  | 'use-site
> | neg-blame |  | : |  | (or/c syntax? string? module-path-index? 'from-macro 'use-site 'unknown)
> |  |  | = |  | 'from-macro
> expr-name : (or/c identifier? string? symbol?) = #f
> macro-name : (or/c identifier? string? symbol?) = #f
> ctx : (or/c syntax? #f) = determined automatically

Accepts an expression (expr) and computes an attribute c that represents the expression wrapped with the contract represented by contract-expr.

The contract’s positive blame represents the obligations of the expression being wrapped. The negative blame represents the obligations of the macro imposing the contract—the ultimate user of expr/c. By default, the positive blame is taken as the module currently being expanded, and the negative blame is inferred from the definition site of the macro (itself inferred from the context argument), but both blame locations can be overridden.

The pos-blame and neg-blame arguments are turned into blame locations as follows:

- If the argument is a string, it is used directly as the blame label.

- If the argument is syntax, its source location is used to produce the blame label.

- If the argument is a module path index, its resolved module path is used.

- If the argument is 'from-macro, the macro is inferred from either the macro-name argument (if macro-name is an identifier) or the context argument, and the module where it is defined is used as the blame location. If neither an identifier macro-name nor a context argument is given, the location is "unknown".

- If the argument is 'use-site, the module being expanded is used.

- If the argument is 'unknown, the blame label is "unknown".

The macro-name argument is used to determine the macro’s binding, if it is an identifier. If expr-name is given, macro-name is also included in the contract error message. If macro-name is omitted or #f, but context is a syntax object, then macro-name is determined from context.

If expr-name is not #f, it is used in the contract’s error message to describe the expression the contract is applied to.

The context argument is used, when necessary, to infer the macro name for the negative blame party and the contract error message. The context should be either an identifier or a syntax pair with an identifier in operator position; in either case, that identifier is taken as the macro ultimately requesting the contract wrapping.

See Contracts on Macro Sub-expressions for an example.

Important: Make sure when using expr/c to use the c attribute. The expr/c syntax class does not change how pattern variables are bound; it only computes an attribute that represents the checked expression.

##### 1.8.2`` Literal Sets

> syntax kernel-literals

Literal set containing the identifiers for fully-expanded code (Fully Expanded Programs). The set contains all of the forms listed by kernel-form-identifier-list, plus module, #%plain-module-begin, #%require, and #%provide.

Note that the literal-set uses the names #%plain-lambda and #%plain-app, not lambda and #%app.

##### 1.8.3`` Function Headers

(require syntax/parse/lib/function-header)
| package: base

> syntax class function-header

Matches a the formals found in function headers. Including keyword and rest arguments.

> syntax class formal

Matches a single formal that can be used in a function header.

> syntax class formals

Matches a list of formals that would be used in a function header.
top ← prev up next →