On this page:
6.1 Contracts
syntax-datum/ c
syntax-listof/ c
syntax-list/ c
6.2 Syntax Lists
6.3 Syntax Conversions
6.4 Syntax Errors
6.5 Pattern Bindings
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)).


  > (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.


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


  > (syntax-e blank-stx)


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


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


  > (syntax-e full-stx)


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


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


  > (syntax-e partial-stx)


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


(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.


  > (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.


  (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.


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