12.10 Serializing Syntax

► |  | 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

On this page:

syntax-serialize
syntax-deserialize

top contents ← prev up next →

#### 12.10`` Serializing Syntax🔗ℹ

> > procedure
> > (syntax-serialize |
> > |  | stx |
> > |  | #:preserve-property-keys preserve-property-keys |
> > | [ | #:provides-namespace provides-namespace |
> > |  | #:base-module-path-index base-module-path-index]) |
> → any/c
> stx : syntax?
> preserve-property-keys : (listof symbol?)
> | provides-namespace |  | : |  | (or/c namespace? #f)
> |  |  | = |  | (current-namespace)
> base-module-path-index : (or/c module-path-index? #f) = #f

Converts stx to a serialized form that is suitable for use with s-exp->fasl or serialize. Although stx could be serialized with (compile `(quote-syntax ,stx)) and then writing the compiled form, syntax-serialize provides more control over serialization:

- The preserve-property-keys lists syntax-property keys to whose values should be preserved in serialization, even if the property value was not added as preserved with syntax-property (so it would be discarded in compiled form). The values associated with the properties to preserve must be serializable in the sense required by syntax-property for a preserved property.

- The provides-namespace argument constrains how much the serialized syntax object can rely on bulk bindings, which are shared binding tables provided by exporting modules. If provides-namespace is #f, then complete binding information is recorded in the syntax object’s serialized form, and no bulk bindings will be needed from the namespace at deserialization. Otherwise, bulk bindings will be used only for modules declared in provides-namespace (i.e., the deserialize-time namespace will have the same module declarations as provides-namespace); note that supplying a namespace with no module bindings is equivalent to supplying #f.

- The base-module-path-index argument specifies a module path index to which binding information in stx is relative. For example, if a syntax object originates from quote-syntax in the body of a module, then base-module-path-index could usefully be the enclosing module’s module path index as produced by (variable-reference->module-path-index (#%variable-reference)) within the module. On deserialization, a different module path index can be supplied to substitute in place of base-module-path-index, which shifts any binding that is relative to the serialize-time module’s identity to be relative to the module identity supplied at deserialize time. If base-module-path-index is #f, then no shifting is supported at deserialize time, and any base-module-path-index supplied at that time is ignored.

A serialized syntax object is otherwise similar to compiled code: it is version-specific, and deserialization will require a sufficiently powerful code inspector.

Added in version 8.0.0.13 of package base.

> > procedure
> > (syntax-deserialize |
> > |  | v |
> > | [ | #:base-module-path-index base-module-path-index]) |
> → syntax?
> v : any/c
> base-module-path-index : (or/c module-path-index? #f) = #f

Converts the result of syntax-serialize back to a syntax object. See syntax-serialize for more information.

Added in version 8.0.0.13 of package base.

top contents ← prev up next →