8.6 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.
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.
The literals in a literal set always refer to the phase-0 bindings of
the enclosing module. For example:
In the literal set common-lits, the literal x always
recognizes identifiers bound to the variable x defined in
module 'common.
When a literal set is used with the #:phase phase-expr
option, the literals’ fixed bindings are compared against the binding of
the input literal at the specified phase. Continuing the example:
The occurrence of x in the pattern matches any identifier
whose binding at phase 1 is the x from module
'common.
(define-conventions name-id convention-rule ...) |
|
convention-rule | | = | | (name-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: |
|
|
'(a b c) |
|
|
'(a (b c) 1 (2 3)) |
Local conventions, introduced with the #:local-conventions
keyword argument of syntax-parse and syntax class
definitions, may refer to local bindings:
Examples: |
|
|
> (define (parse-natlist> bound x) | (syntax-parse x | #:local-conventions ([NS (natlist> bound)]) | [NS 'ok])) |
|
> (parse-natlist> 0 #'(1 2 3)) |
'ok |
> (parse-natlist> 5 #'(8 6 4 2)) |
?: expected number > 5 at: 4 |