On this page:
6.1 Contracts
syntax-datum/ c
syntax-listof/ c
syntax-list/ c
6.2 Syntax Lists
syntax-map
6.3 Syntax Conversions
to-syntax
to-datum
6.4 Syntax Errors
current-syntax
syntax-error
6.5 Pattern Bindings
with-syntax*
Version: 4.1.3

6 Syntax Objects

 (require (planet cce/scheme:2:0/syntax))

This module provides tools for macro transformers.

6.1 Contracts

(syntax-datum/c datum/c)  flat-contract?
  datum/c : any/c

Recognizes syntax objects stx such that (syntax->datum stx) satisfies datum/c.

(syntax-listof/c elem/c)  flat-contract?
  elem/c : any/c

Recognizes syntax objects stx such that (syntax->list stx) satisfies (listof elem/c).

(syntax-list/c elem/c ...)  flat-contract?
  elem/c : any/c

Recognizes syntax objects stx such that (syntax->list stx) satisfies (list/c elem/c ...).

6.2 Syntax Lists

(syntax-map f stx)  (listof A)
  f : (-> syntax? A)
  stx : syntax?

Performs (map f (syntax->list stx)).

Examples:

  > (syntax-map syntax-e #'(a (b c) d))

  (a (#<syntax:1:0> #<syntax:1:0>) d)

6.3 Syntax Conversions

(to-syntax datum    
  [#:stx stx    
  #:src src    
  #:ctxt ctxt    
  #:prop prop    
  #:cert cert])  syntax?
  datum : any/c
  stx : (or/c false/c syntax?) = #f
  src : (or/c false/c syntax?) = stx
  ctxt : (or/c false/c syntax?) = stx
  prop : (or/c false/c syntax?) = stx
  cert : (or/c false/c syntax?) = stx

A wrapper for datum->syntax with keyword arguments.

The "master" keyword #:stx sets all attributes from a single syntax object, defaulting to #f for unadorned syntax objects.

The individual keywords #:src, #:ctxt, #:prop, and #:cert override #:stx for individual syntax object attributes. They control source location information, lexical context information, syntax object properties, and syntax certificates, respectively.

Examples:

  (define blank-stx (to-syntax 'car))
  > blank-stx

  #<syntax>

  > (syntax-e blank-stx)

  car

  > (free-identifier=? blank-stx #'car)

  #f

  (define full-stx (to-syntax 'car #:stx #'here))
  > full-stx

  #<syntax:5:0>

  > (syntax-e full-stx)

  car

  > (free-identifier=? full-stx #'car)

  #t

  (define partial-stx (to-syntax 'car #:ctxt #'here))
  > partial-stx

  #<syntax>

  > (syntax-e partial-stx)

  car

  > (free-identifier=? partial-stx #'car)

  #t

(to-datum x)  (not/c syntax?)
  x : any/c

A wrapper for syntax->datum. Produces (syntax->datum x) if x is a syntax object and x otherwise.

Examples:

  > (to-datum #'(a b c))

  (a b c)

  > (to-datum (list #'a #'b #'c))

  (#<syntax:2:0> #<syntax:2:0> #<syntax:2:0>)

6.4 Syntax Errors

current-syntax : (parameter/c (or/c syntax? false/c))

A parameter that may be used to store the current syntax object being transformed. It is not used by the expander; you have to assign to it yourself. This parameter is used by syntax-error, below. It defaults to #f.

(syntax-error stx fmt arg ...)  none/c
  stx : syntax?
  fmt : string?
  arg : any/c

Raises a syntax error based on the locations of (current-syntax) and stx, with (format fmt arg ...) as its message.

Examples:

  (define stx #'(a b c))
  > (parameterize ([current-syntax #f])
      (syntax-error stx "~s location" 'general))

  eval:1:0: a: general location in: (a b c)

  > (parameterize ([current-syntax stx])
      (syntax-error (car (syntax-e stx)) "~s location" 'specific))

  eval:1:0: a: specific location at: a in: (a b c)

6.5 Pattern Bindings

(with-syntax* ([pattern expr] ...) body ...+)

Like with-syntax, but with nested scope.

Examples:

  > (with-syntax* ([a #'id] [b #'a]) (syntax-e #'b))

  id