12.9 Expanding Top-Level Forms

► |  | The Racket Reference

1 | Language Model
2 | Notation for Documentation
3 | Syntactic Forms
4 | Datatypes
5 | Structures
6 | Classes and Objects
7 | Units
8 | Contracts
9 | Pattern Matching
10 | Control Flow
11 | Concurrency and Parallelism
12 | Macros
13 | Input and Output
14 | Reflection and Security
15 | Operating System
16 | Memory Management
17 | Unsafe Operations
18 | Running Racket
| Bibliography
| Index

▼ | 12 | Macros

12.1 | Pattern-Based Syntax Matching
12.2 | Syntax Object Content
12.3 | Syntax Object Bindings
12.4 | Syntax Transformers
12.5 | Syntax Parameters
12.6 | Local Binding with Splicing Body
12.7 | Syntax Object Properties
12.8 | Syntax Taints
12.9 | Expanding Top-Level Forms
12.10 | Serializing Syntax
12.11 | File Inclusion
12.12 | Syntax Utilities
12.13 | Phase and Space Utilities

► | 12.9 | Expanding Top-Level Forms

12.9.1 | Information on Expanded Modules

On this page:

expand
expand-syntax
expand-once
expand-syntax-once
expand-to-top-form
expand-syntax-to-top-form
12.9.1`` Information on Expanded Modules

top contents ← prev up next →

#### 12.9`` Expanding Top-Level Forms🔗ℹ

> procedure (expand top-level-form [insp]) → syntax?
> top-level-form : any/c
> insp : inspector? = (current-code-inspector)

Expands all non-primitive syntax in top-level-form, and returns a syntax object for the expanded form that contains only core forms, matching the grammar specified by Fully Expanded Programs.

Before top-level-form is expanded, its lexical context is enriched with namespace-syntax-introduce, just as for eval. Use syntax->datum to convert the returned syntax object into a printable datum.

If insp is not the original code inspector (i.e., the value of (current-code-inspector) when Racket starts), then the result syntax object is tainted.

Here’s an example of using expand on a module:

> (parameterize ([current-namespace (make-base-namespace)])
> (expand
> (datum->syntax
> #f
> '(module foo scheme
> (define a 3)
> (+ a 4)))))

Here’s an example of using expand on a non-top-level form:

> (define-namespace-anchor anchor)
> (parameterize ([current-namespace
> (namespace-anchor->namespace anchor)])
> (expand
> (datum->syntax
> #f
> '(delay (+ 1 2)))))

Changed in version 8.2.0.4 of package base: Added the insp argument and tainting.

> procedure (expand-syntax stx [insp]) → syntax?
> stx : syntax?
> insp : inspector? = (current-code-inspector)

Like (expand stx insp), except that the argument must be a syntax object, and its lexical context is not enriched before expansion.

Changed in version 8.2.0.4 of package base: Added the insp argument and tainting.

> procedure (expand-once top-level-form [insp]) → syntax?
> top-level-form : any/c
> insp : inspector? = (current-code-inspector)

Partially expands top-level-form and returns a syntax object for the partially-expanded expression. Due to limitations in the expansion mechanism, some context information may be lost. In particular, calling expand-once on the result may produce a result that is different from expansion via expand.

Before top-level-form is expanded, its lexical context is enriched with namespace-syntax-introduce, as for eval.

The insp argument determines whether the result is tainted, the same as for expand.

Changed in version 8.2.0.4 of package base: Added the insp argument and tainting.

> procedure (expand-syntax-once stx [insp]) → syntax?
> stx : syntax?
> insp : inspector? = (current-code-inspector)

Like (expand-once stx), except that the argument must be a syntax object, and its lexical context is not enriched before expansion.

Changed in version 8.2.0.4 of package base: Added the insp argument and tainting.

> procedure (expand-to-top-form top-level-form [insp]) → syntax?
> top-level-form : any/c
> insp : inspector? = (current-code-inspector)

Partially expands top-level-form to reveal the outermost syntactic form. This partial expansion is mainly useful for detecting top-level uses of begin. Unlike the result of expand-once, expanding the result of expand-to-top-form with expand produces the same result as using expand on the original syntax.

Before stx-or-sexpr is expanded, its lexical context is enriched with namespace-syntax-introduce, as for eval.

The insp argument determines whether the result is tainted, the same as for expand.

Changed in version 8.2.0.4 of package base: Added the insp argument and tainting.

> procedure (expand-syntax-to-top-form stx [insp]) → syntax?
> stx : syntax?
> insp : inspector? = (current-code-inspector)

Like (expand-to-top-form stx), except that the argument must be a syntax object, and its lexical context is not enriched before expansion.

Changed in version 8.2.0.4 of package base: Added the insp argument and tainting.

##### 12.9.1`` Information on Expanded Modules🔗ℹ

Information for an expanded module declaration is stored in a set of syntax properties (see Syntax Object Properties) attached to the syntax object:

- 'module-body-context — a syntax object whose lexical information corresponds to the inside of the module, so it includes the expansion’s outside-edge scope and its inside-edge scope; that is, the syntax object simulates an identifier that is present in the original module body and inaccessible to manipulation by any macro, so that its lexical information includes bindings for the module’s imports and definitions.

  Added in version 6.4.0.1 of package base.

- 'module-body-inside-context — a syntax object whose lexical information corresponds to an identifier that starts with no lexical context and is moved into the macro, so that it includes only the expansions’s inside-edge scope.

  Added in version 6.4.0.1 of package base.

- 'module-body-context-simple? — a boolean, where #t indicates that the bindings of the module’s body (as recorded in the lexical information of the value of the 'module-body-inside-context property) can be directly reconstructed from modules directly imported into the module, including imported for-syntax, for-meta, and for-template.

  Added in version 6.4.0.1 of package base.

Changed in version 7.0 of package base: Removed 'module-variable-provides, 'module-syntax-provides, 'module-indirect-provides, and 'module-indirect-for-meta-provides properties.

top contents ← prev up next →