More Syntax Classes
1 Locally bound transformer bindings
local-value
2 Lists and pairs with 'paren-shape
paren-shape
paren-shape/  parens
paren-shape/  brackets
paren-shape/  braces
~parens
~brackets
~braces
3 Structure type transformer bindings
struct-id
7.7

More Syntax Classes

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

1 Locally bound transformer bindings

 (require syntax/parse/class/local-value)
  package: syntax-classes-lib

syntax class

(local-value [predicate?]
             [intdef-ctx]
             [#:failure-message failure-message])
 
  predicate? : (-> any/c any/c) = (const #t)
  intdef-ctx : (or/c internal-definition-context? (listof internal-definition-context?) #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 failure-message will be used as the error message, if it is supplied.

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

42

> (define-syntax print-local-string
    (syntax-parser
      [(_ {~var id (local-value string?)})
       (println (attribute id.local-value))
       #'(void)]))
> (print-local-string something)

eval:6.0: print-local-string: bad syntax

  in: (print-local-string something)

> (define-syntax print-local-string/message
    (syntax-parser
      [(_ {~var id (local-value string? #:failure-message "identifier was not bound to a string")})
       (println (attribute id.local-value))
       #'(void)]))
> (print-local-string/message something)

eval:8.0: print-local-string/message: identifier was not

bound to a string

  at: something

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

2 Lists and pairs with 'paren-shape

 (require syntax/parse/class/paren-shape)
  package: syntax-classes-lib

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.

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 #'(1 2 . "three")
    [(~parens a ... . rst)
     (cons #'(a ...) #'rst)])

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

> (syntax-parse #'{1 2 . "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 #'[1 2 . "three"]
    [[~brackets a ... . rst]
     (cons #'(a ...) #'rst)])

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

> (syntax-parse #'(1 2 . "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 #'{1 2 . "three"}
    [{~braces a ... . rst}
     (cons #'(a ...) #'rst)])

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

> (syntax-parse #'(1 2 . "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.

3 Structure type transformer bindings

 (require syntax/parse/class/struct-id)
  package: syntax-classes-lib

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:

Due to the nature of the mutator-id attribute, it can be useful to use template from syntax/parse/experimental/template instead of syntax when using mutator ids.

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

'((foo-bar #f) (foo-baz set-foo-baz!) (foo-qux #f))