14 Contracts for Macro Subexpressions
(require syntax/contract) | package: base |
This library provides a procedure wrap-expr/c for applying contracts to macro subexpressions.
procedure
(wrap-expr/c contract-expr expr [ #:positive pos-blame #:negative neg-blame #:name expr-name #:macro macro-name #:context context]) → syntax? contract-expr : syntax? expr : syntax?
pos-blame :
(or/c syntax? string? module-path-index? 'from-macro 'use-site 'unknown) = 'use-site
neg-blame :
(or/c syntax? string? module-path-index? 'from-macro 'use-site 'unknown) = 'from-macro expr-name : (or/c identifier? symbol? string? #f) = #f macro-name : (or/c identifier? symbol? string? #f) = #f context : (or/c syntax? #f) = (current-syntax-context)
The other arguments have the same meaning as for expr/c.
> (define-syntax (myparameterize1 stx) (syntax-case stx () [(_ ([p v]) body) (with-syntax ([cp (wrap-expr/c #'parameter? #'p #:name "the parameter argument" #:context stx)]) #'(parameterize ([cp v]) body))]))
> (myparameterize1 ([current-input-port (open-input-string "(1 2 3)")]) (read)) '(1 2 3)
> (myparameterize1 (['whoops 'something]) 'whatever) the parameter argument of myparameterize1: broke its own
contract
promised: parameter?
produced: 'whoops
in: parameter?
contract from: top-level
blaming: top-level
(assuming the contract is correct)
at: eval:4.0
> (module mod racket (require (for-syntax syntax/contract)) (define-syntax (app stx) (syntax-case stx () [(app f arg) (with-syntax ([cf (wrap-expr/c #'(-> number? number?) #'f #:name "the function argument" #:context stx)]) #'(cf arg))])) (provide app)) > (require 'mod) > (app add1 5) 6
> (app add1 'apple) the function argument of app: contract violation
expected: number?
given: 'apple
in: the 1st argument of
(-> number? number?)
contract from: top-level
blaming: (quote mod)
(assuming the contract is correct)
at: eval:8.0
> (app (lambda (x) 'pear) 5) the function argument of app: broke its own contract
promised: number?
produced: 'pear
in: the range of
(-> number? number?)
contract from: top-level
blaming: top-level
(assuming the contract is correct)
at: eval:9.0
Added in version 6.3 of package base.