11.7 Syntax Object Properties
Every syntax object has an associated syntax property list, which can be queried or extended with syntax-property. Properties are not preserved for a syntax-quoted syntax object in a compiled form that is marshaled to a byte string.
In read-syntax, the reader attaches a 'paren-shape property to any pair or vector syntax object generated from parsing a pair [ and ] or { and }; the property value is #\[ in the former case, and #\{ in the latter case. The syntax form copies any 'paren-shape property from the source of a template to corresponding generated syntax.
Both the syntax input to a transformer and the syntax result of a transformer may have associated properties. The two sets of properties are merged by the syntax expander: each property in the original and not present in the result is copied to the result, and the values of properties present in both are combined with cons (result value first, original value second).
Before performing the merge, however, the syntax expander automatically adds a property to the original syntax object using the key 'origin. If the source syntax has no 'origin property, it is set to the empty list. Then, still before the merge, the identifier that triggered the macro expansion (as syntax) is consed onto the 'origin property so far. The 'origin property thus records (in reverse order) the sequence of macro expansions that produced an expanded expression. Usually, the 'origin value is an immutable list of identifiers. However, a transformer might return syntax that has already been expanded, in which case an 'origin list can contain other lists after a merge. The syntax-track-origin procedure implements this tracking.
Besides 'origin tracking for general macro expansion, Racket adds properties to expanded syntax (often using syntax-track-origin) to record additional expansion details:
When a begin form is spliced into a sequence with internal definitions (see Internal Definitions), syntax-track-origin is applied to every spliced element from the begin body. The second argument to syntax-track-origin is the begin form, and the third argument is the begin keyword (extracted from the spliced form).
When an internal define-values or define-syntaxes form is converted into a letrec-syntaxes+values form (see Internal Definitions), syntax-track-origin is applied to each generated binding clause. The second argument to syntax-track-origin is the converted form, and the third argument is the define-values or define-syntaxes keyword form the converted form.
When a letrec-syntaxes+values expression is fully expanded, syntax bindings disappear, and the result is either a letrec-values form (if the unexpanded form contained non-syntax bindings), or only the body of the letrec-syntaxes+values form (wrapped with begin if the body contained multiple expressions). To record the disappeared syntax bindings, a property is added to the expansion result: an immutable list of identifiers from the disappeared bindings, as a 'disappeared-binding property.
When a subtyping struct form is expanded, the identifier used to reference the base type does not appear in the expansion. Therefore, the struct transformer adds the identifier to the expansion result as a 'disappeared-use property.
When a reference to an unexported or protected identifier from a module is discovered (and the reference is certified; see Syntax Certificates), the 'protected property is added to the identifier with a #t value.
When read-syntax or read-honu-syntax generates a syntax object, it attaches a property to the object (using a private key) to mark the object as originating from a read. The syntax-original? predicate looks for the property to recognize such syntax objects. (See Syntax Object Content for more information.)
See Information on Expanded Modules for information about properties generated by the expansion of a module declaration. See lambda and Inferred Value Names for information about properties recognized when compiling a procedure. See current-compile for information on properties and byte codes.
(syntax-property stx key v) → syntax? |
stx : syntax? |
key : any/c |
v : any/c |
(syntax-property stx key) → any |
stx : syntax? |
key : any/c |
The two-argument form returns an arbitrary property value associated to stx with the key key, or #f if no value is associated to stx for key.
(syntax-property-symbol-keys stx) → list? |
stx : syntax? |
| |||||||||||||||||||||
new-stx : syntax? | |||||||||||||||||||||
orig-stx : syntax? | |||||||||||||||||||||
id-stx : syntax? |
For example, the expression
(or x y)
expands to
(let ((or-part x)) (if or-part or-part (or y)))
which, in turn, expands to
(let-values ([(or-part) x]) (if or-part or-part y))
The syntax object for the final expression will have an 'origin property whose value is (list (quote-syntax let) (quote-syntax or)).