8.18
top
← prev up next →

More Syntax ClassesπŸ”— i

This library provides additional syntax classes for use with syntax/parse.

1Locally bound transformer bindingsπŸ”— i

syntax class

( local-value [ predicate?
intdef-ctx
#:namename
#:failure-messagefailure-message])syntax class
predicate?:(any/c . -> .any/c )=(const #t)
name:(or/c string? #f)=#f
failure-message:(or/c string? #f)=#f
A syntax class for parsing identifiers bound to transformer bindings. It parses an identifier, then calls syntax-local-value on it and binds the result to an attribute named local-value.

If predicate? is specified, then predicate? will be applied to the result of syntax-local-value , and if the result is #f, then the syntax class will fail to match.

If intdef-ctx is not #f, bindings from all provided definition contexts are considered when determining the local binding. Like the third argument to syntax-local-value , the scopes associated with the provided definition contexts are not used to enrich the matching identifier’s lexical information.

If the identifier is not bound to a transformer binding, or if the binding does not satisfy predicate?, then name will be used when generating a parse error message, if it is not #f. If failure-message is not #f, it will be used instead of the generated message, though the value of name will still be used to show supplemental error information.

Examples:
> (define-syntax print-local
[(_ id:local-value)
(println (attribute id.local-value))
#'(void )]))
> (define-syntax something42)
> (print-localsomething)

42

> (define-syntax print-local-string
(println (attribute id.local-value))
#'(void )]))
> (print-local-stringsomething)

eval:6:0: print-local-string: bad syntax

in: (print-local-string something)

> (define-syntax print-local-string/name
[(_ {~var id (local-value string? #:name"string")})
(println (attribute id.local-value))
#'(void )]))
> (print-local-string/namesomething)

eval:8:0: print-local-string/name: expected string

at: something

in: (print-local-string/name something)

> (define-syntax print-local-string/message
#:name"string"
#:failure-message"identifier was not bound to a string")})
(println (attribute id.local-value))
#'(void )]))
> (print-local-string/messagesomething)

eval:10:0: print-local-string/message: identifier was not

bound to a string

at: something

in: (print-local-string/message something)

parsing context:

while parsing string

term: something

location: eval:10:0

Changed in version 1.2 of package syntax-classes-lib: Added #:name argument.

2Lists and pairs with 'paren-shapeπŸ”— i

syntax class

( paren-shape shape)

shape : any/c
Parses any syntax object that has a 'paren-shape syntax property with a value equal? to shape.

Added in version 1.1 of package syntax-classes-lib.

syntax class

paren-shape/parens

Parses any syntax object that either has #f for the 'paren-shape syntax property or does not have a 'paren-shape syntax property at all.

Added in version 1.1 of package syntax-classes-lib.

syntax class

paren-shape/brackets

Parses any syntax object that has #\[ for the 'paren-shape syntax property.

Added in version 1.1 of package syntax-classes-lib.

syntax class

paren-shape/braces

Parses any syntax object that has #\{ for the 'paren-shape syntax property.

Added in version 1.1 of package syntax-classes-lib.

pattern expander

( ~parens H-pattern. S-pattern)

A pattern expander that parses a list or pair that either has #f for the 'paren-shape syntax property or does not have a 'paren-shape syntax property at all.

Examples:
> (syntax-parse #'(12. "three")
[(~parens a... . rst)
(cons #'(a... )#'rst)])

'(#<syntax:eval:2:0 (1 2)> . #<syntax:eval:2:0 "three">)

> (syntax-parse #'{12. "three"}
[(~parens a... . rst)
(cons #'(a... )#'rst)])

eval:3:0: ?: expected list or pair surrounded by parentheses

at: (1 2 . "three")

in: (1 2 . "three")

Added in version 1.1 of package syntax-classes-lib.

pattern expander

[ ~brackets H-pattern. S-pattern]

A pattern expander that parses a list or pair that has #\[ for the 'paren-shape syntax property.

Examples:
> (syntax-parse #'[12. "three"]
[[~brackets a... . rst]
(cons #'(a... )#'rst)])

'(#<syntax:eval:2:0 (1 2)> . #<syntax:eval:2:0 "three">)

> (syntax-parse #'(12. "three")
[[~brackets a... . rst]
(cons #'(a... )#'rst)])

eval:3:0: ?: expected list or pair surrounded by square

brackets

at: (1 2 . "three")

in: (1 2 . "three")

Added in version 1.1 of package syntax-classes-lib.

pattern expander

{ ~braces H-pattern. S-pattern}

A pattern expander that parses a list or pair that has #\{ for the 'paren-shape syntax property.

Examples:
> (syntax-parse #'{12. "three"}
[{~braces a... . rst}
(cons #'(a... )#'rst)])

'(#<syntax:eval:2:0 (1 2)> . #<syntax:eval:2:0 "three">)

> (syntax-parse #'(12. "three")
[{~braces a... . rst}
(cons #'(a... )#'rst)])

eval:3:0: ?: expected list or pair surrounded by curly

braces

at: (1 2 . "three")

in: (1 2 . "three")

Added in version 1.1 of package syntax-classes-lib.

3Structure type transformer bindingsπŸ”— i

syntax class

struct-id

A syntax class for parsing structure type transformer bindings. Like the local-value syntax class, it will parse an identifier, then call syntax-local-value on it to get a value. This syntax class will only match if the resulting value satisfies struct-info? , and it will then bind a set of attributes:

  • The local-value attribute is bound to the result of syntax-local-value , as it is for the local-value syntax class.

  • The info attribute is bound to the result of (extract-struct-info (attribute local-value)).

  • The descriptor-id attribute is bound to an identifier that is bound to the structure type’s descriptor, or #f if none is known.

  • The constructor-id attribute is bound to an identifier that is bound to the structure type’s constructor, or #f if none is known.

  • The predicate-id attribute is bound to an identifier that is bound to the structure type’s predicate, or #f if none is known.

  • The supertype-id attribute is bound to an identifier or a boolean. If it is an identifier, then the identifier is a structure type transformer binding for the structure’s supertype. If it is #t, then the structure has no supertype. If it is #f, then the structure’s supertype is unknown.

  • The all-fields-visible? attribute is bound to #t if all structure fields are visible to the macro, otherwise it is #f.

  • The num-fields attribute is bound to an exact, nonnegative integer that describes the number of visible fields the structure type has, including supertype fields.

  • The accessor-id attribute is an attribute of ellipsis depth 1 that is bound to identifiers bound to accessors for all visible structure fields, including supertype fields.

  • The mutator-id attribute is like accessor-id, except that it contains identifiers bound to mutators instead of accessors. It is guaranteed to have the same number of elements as accessor-id; however, the value will be #f for each non-mutable field.

  • The field-sym attribute is like accessor-id, except its values are plain symbols corresponding to unprefixed field names, as returned by struct-field-info-list . If field name information is not available, this attribute is bound to #f.

  • The num-supertype-fields attribute is like num-fields, except that it only counts supertype fields, not fields that belong to the structure type itself.

  • The num-own-fields attribute is like num-fields, except that it does not count supertype fields, only fields that belong to the structure type itself.

  • The own-accessor-id attribute is like accessor-id, except that it does not include supertype fields, only fields that belong to the structure type itself.

  • The own-mutator-id attribute is like mutator-id combined with the supertype-excluding behavior of own-accessor-id.

  • The own-field-sym attribute is like field-sym combined with the supertype-excluding behavior of own-accessor-id.

Examples:
> (define-syntax struct-accessors+mutators
[(_ id:struct-id)
#'(list (cons id.accessor-id(?? id.mutator-id#f))... )]))
> (struct foo(bar[baz#:mutable]qux))
> (struct-accessors+mutatorsfoo)

'((#<procedure:foo-bar> . #f)

(#<procedure:foo-baz> . #<procedure:set-foo-baz!>)

(#<procedure:foo-qux> . #f))

Note that while the field-sym and own-field-sym attributes have ellipsis depth 1, they are not syntax valued and therefore cannot be used directly as part of a syntax template. However, they may still be used with datum from syntax/datum as described in Attributes and datum.

Examples:
> (require (for-syntax syntax/datum))
> (define-syntax struct-field-names
[(_ id:struct-id)
#`'#,(datum(id.field-sym...))]))
> (struct foo(barbazqux))
> (struct-field-namesfoo)

'(bar baz qux)

Changed in version 1.3 of package syntax-classes-lib: Added the field-sym and own-field-sym attributes.

top
← prev up next →

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /