14 Contracts for Macro Subexpressions

▼ |  | Syntax: Meta-Programming Helpers

1 | Parsing and Specifying Syntax
2 | Syntax Object Helpers
3 | Datum Pattern Matching
4 | Module-Processing Helpers
5 | Macro Transformer Helpers
6 | Reader Helpers
7 | Parsing for Bodies
8 | Unsafe for Clause Transforms
9 | Source Locations
10 | Preserving Source Locations
11 | Non-Module Compilation And Expansion
12 | Trusting Standard Recertifying Transformers
13 | Attaching Documentation to Exports
14 | Contracts for Macro Subexpressions
15 | Macro Testing
16 | Internal-Definition Context Helpers
| Index

On this page:

wrap-expr/ c

9.3
Racket
top contents ← prev up next →

## 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 |  |  |  |
> > | [ | #:arg? arg? |  |  |  |
> > |  | #:positive pos-blame |  |  |  |
> > |  | #:negative neg-blame |  |  |  |
> > |  | #:name expr-name |  |  |  |
> > |  | #:macro macro-name |  |  |  |
> > |  | #:context context |  |  |  |
> > |  | #:phase phase]) |  | → |  | syntax?
> contract-expr : syntax?
> expr : syntax?
> arg? : any/c = #t
> | pos-blame |  | : |  |
> (or/c syntax? string? module-path-index?
> 'from-macro 'use-site 'unknown)
> |  |  | = |  | 'from-macro
> | neg-blame |  | : |  |
> (or/c syntax? string? module-path-index?
> 'from-macro 'use-site 'unknown)
> |  |  | = |  | 'use-site
> 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)
> phase : exact-integer? = (syntax-local-phase-level)

Returns a syntax object representing an expression that applies the contract represented by contract-expr to the value produced by expr.

The other arguments have the same meaning as for expr/c.

Examples:

> > (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)
> myparameterize1: contract violation
> expected: parameter?
> given: 'whoops
> in: parameter?
> macro argument contract on the parameter argument
> 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)
> app: broke its own contract
> promised: number?
> produced: 'apple
> in: the 1st argument of
> (-> number? number?)
> macro argument contract on the function argument
> contract from: 'mod
> blaming: (quote mod)
> (assuming the contract is correct)
> at: eval:8:0
> > (app (lambda (x) 'pear) 5)
> app: contract violation
> expected: number?
> given: 'pear
> in: the range of
> (-> number? number?)
> macro argument contract on the function argument
> contract from: 'mod
> blaming: top-level
> (assuming the contract is correct)
> at: eval:9:0

Added in version 6.3 of package base.
Changed in version 7.2.0.3: Added the #:arg? keyword argument and changed the default values and interpretation of the #:positive and #:negative arguments.
Changed in version 7.3.0.3: Added the #:phase keyword argument.

top contents ← prev up next →