3.6 Binding Macros
space | |
value | |
A compile-time value that identifies the same space as bind. See also SpaceMeta.
definition | |
> tickets
[1, 2, 3]
def: value does not satisfy annotation
value: "oops"
annotation: matching(List[, _ :: String, ...])
See Bindings and Low-Level Protocol for examples using the low-level protocol.
See Syntax Tracking for information about expansion tracking, which applies automatically for some pattern forms in macro_patterns.
Packs binding information that is represented by a syntax object with two parts: infoer_id and data, combined in the form
'(infoer_id, data)'
The infoer_id identifier must be bound to a transformer with bind.infoer, and data is propagated to that transformer to get initial binding information and further “continuations” for generating matching and variable-defining forms.
The optional track_stxes argument is passed along to syntax_meta.track_origin on the resulting syntax object to connect expansion tracking from the track_stxes members to the packed binding expansion; see also Syntax Tracking.
The representation of packed information as a syntax object is unspecified and meant to be opaque.
See Bindings and Low-Level Protocol for more explanation and for examples.
Changed in version 1.1.0.2 of package rhombus-lib: Added the ~track argument.
Packs multiple-value binding information that is represented by a syntax object with four parts: source_form, arity, infoer_id, and data, combined in the form
'(source_form, arity, infoer_id, data)'
The source_form form is the original binding form that is used to report misuse of multiple-value bindings in single-value contexts. The arity nonnegative integer specifies how many values the binding matches. The infoer_id and data parts are as in bind_meta.pack.
The representation of packed information as a syntax object is unspecified and meant to be opaque.
See Bindings and Low-Level Protocol for more explanation and for examples.
Added in version 1.1.0.3 of package rhombus-lib.
The inverse of bind_meta.pack, normally used only internally in the expander. Multiple-value bindings of arity 1 are “unpacked” to single-value bindings by lowering them.
Changed in version 1.1.0.3 of package rhombus-lib: Added support for multiple-value bindings.
The inverse of bind_meta.pack_values, normally used only internally in the expander. Single-value bindings are “unpacked” to multiple-value bindings of arity 1 by lifting them.
Added in version 1.1.0.3 of package rhombus-lib.
Reports the arity encoded by the binding form represented by packed bind_stx. A single-value bindings always has arity 1, while a multiple-value binding has its arity from the unpacked form.
Added in version 1.1.0.3 of package rhombus-lib.
definition | |||
|
See Bindings and Low-Level Protocol for more explanation and for examples.
function | ||||||||
| ||||||||
|
Packs binding information that is specific to a use of a binding form, which means that “downward” static information has been provided to an infoer, and the infoer converts that to initial information about bindings plus “continuations” to generate the matching expression and variable definitions.
The syntax object stx must have the following shape:
'(ann_string,
name_id_or_ids,
((static_key, static_value), ...),
((defined_id,
[var_use, ...],
((var_static_key, var_static_value), ...)),
...),
oncer_id,
matcher_id,
committer_id,
binder_id,
data)'
The ann_string term is for error reporting when a value fails to match the binding. It describes the binder’s matching requirements in the form of an annotation.
The name_id_or_ids term is for error reporting and reflection in the sense that it is used as the inferred name for a value matched to the binder, in case such a name is relevant. It is either an identifier (for single-value bindings) or a parenthesized sequence of identifiers (for multiple-value bindings).
The static_key–static_value pairs describe “upward” static information for anything that successfully matches the binding. The “upward” information can be useful to an enclosing binding form.
The defined_ids are the identifiers that are ultimately bound by the binding form. Each identifier to be defined has associated uses through var_use values, and each identifier has “downward” static information through the var_static_key–var_static_value pairs. Each defined_id is bound in the statinfo space only when at least one var_static_key–var_static_value pair is present. Like var_static_keys, the meaning of var_uses is up to cooperating parts in general, but some shapes are recognized by built-in forms:
~repet (sequencer, ...) indicates that the variable can be used as repetition at a depth corresponding to the number of sequencers, and also as an expression if the number sequencers is zero;
~no_let indicates that the variable’s is not compatible with let, because it needs to be bound early and bind.forward_sequence is not used to compensate for early binding; and
~extends ns_id indicates that the variable is bound as an extension of the namespace ns_id.
The oncer_id, matcher_id, committer_id, and binder_id identifiers provide the “continuation” of the binder’s expansion to generate a matching expression and a definition sequence. Each identifier in evidence_id_tree should be defined in the output of matcher_id; a tree with the same shape as evidence_id_tree, but with each identifier potentially replaced by a different one, is provided as input to committer_id and binder_id.
The data term is propagated to the use of matcher_id, committer_id, and binder_id, providing a communication channel from an infoer to a matcher, committer, or binder.
The representation of packed information as a syntax object is unspecified and meant to be opaque.
See Bindings and Low-Level Protocol for more explanation and for examples.
Changed in version 1.1.0.2 of package rhombus-lib: Removed the ~track argument.
The inverse of bind_meta.pack_info, which is useful for unpacking information about the expansion of nested binding forms as produced by bind_meta.get_info or bind_meta.get_values_info.
Initiates the expansion of the binding form represented by packed bind_stx in a context that supplies the “downward” static information represented by static_info.
Static information is represented by a syntax object that has the following shape:
'((key_id, val), ...)'
The result is a syntax object that represents the initial expansion of the binding form as a packed syntax object, whose form is unspecified and intended to be opaque. Use bind_meta.unpack_info to convert the packed syntax object and expose information about the binding’s expansion.
The bind_stx form can also be a multiple-value binding of arity 1, in which case it is lowered to a single-value binding first. If it is a multiple-value binding of any other arities, a syntax error is reported using its source_form.
See Bindings and Low-Level Protocol for an example.
Changed in version 1.1.0.3 of package rhombus-lib: Added support for multiple-value bindings.
function | ||||
|
Initiates the expansion of the multiple-value binding form represented by packed bind_stx in a context that supplies the “downward” static information represented by static_infos. Each syntax object in static_infos must have the shape as described in bind_meta.get_info, and the number of syntax objects in static_infos must be the same as the arity encoded by bind_stx.
The result is the same kind of opaque syntax object as the result of bind_meta.get_info.
The bind_stx form can also be a single-value binding, in which case it is lifted to a multiple-value binding of arity 1 first.
See Bindings and Low-Level Protocol for an example.
Added in version 1.1.0.3 of package rhombus-lib.
Takes the initialized binding-form expansion produced by bind_meta.get_info and reports whether the binding is immediate in the same sense as just a variable: the binding always matches, and no work is required to convert or unpack the matched value. Multiple-value bindings are never immediate in this sense.
definition | |||
|
The definitions produced by a oncer should not define identifiers supplied by a user of the binding form, because those names would not be adjusted by let. Instead, such definitions should be deferred to the binder function’s result. If early binding is unavoidable (as in where or when, for example, where some binding is needed in the process of matching), using bind.forward_sequence around definitions may restore cooperation with let.
See Bindings and Low-Level Protocol for more explanation and for examples.
definition | ||||
|
When using a matcher unpacked via bind_meta.unpack_info, the group given for success_pattern can be an immediate block formed with : (without a preceding term), and in that case, the groups of the block are spliced into a definition context where the binding is used.
Any definitions produced by a matcher should not define identifiers supplied by a user of the binding form, because those names would not be adjusted by let. Instead, such definitions should be deferred to the binder function’s result. If early binding is unavoidable (as in where or when, for example, where some binding is needed in the process of matching), using bind.forward_sequence around definitions may restore cooperation with let.
See Bindings and Low-Level Protocol for more explanation and for examples.
definition | ||||
|
The definitions produced by a committer should not define identifiers supplied by a user of the binding form, because those names would not be adjusted by let. Instead, such definitions should be deferred to the binder function’s result. If early binding is unavoidable (as in where or when, for example, where some binding is needed in the process of matching), using bind.forward_sequence around definitions may restore cooperation with let.
See Bindings and Low-Level Protocol for more explanation and for examples.
definition | ||||
|
The definitions produced by a binder should not refer to each other, because they may be adjusted by let expansion. Bindings that need to be referenced by generated bindings should be in the committer function’s output.
See Bindings and Low-Level Protocol for more explanation and for examples.
definition | |||
|
In the case of a definition sequence produced by a bind.oncer, bind.matcher, or bind.committer step, if the step was driven by a use of let, then the definitions are expanded with let-style binding instead of def-style binding, which means that they are visible only to later expressions and definitions within the enclosing definition context. This form is intended for use by those forms when they need to bind identifiers early.
Added in version 1.1.0.2 of package rhombus-lib.
syntax class | ||||||
| ||||||
| ||||||
syntax class | ||||||
| ||||||
| ||||||
syntax class | ||||||
| ||||||
| ||||||
syntax class | ||||||
|
Analogous to expr_meta.Parsed, etc., but for bindings.
syntax class | ||||||
|
Matches forms that combine a binding with an optional keyword and optional default-value expression, like bind_maybe_kw_opt for fun.
The parsed field holds a parsed binding form.
The maybe_keyword field is #false if no keyword is present, otherwise it is a keyword syntax object.
The maybe_expr field is #false if no default-value expression is provided, otherwise it is a group syntax object for the expression.
syntax class | ||||||||
|
Matches a sequence of terms (possibly empty) for a result annotation, like maybe_res_annot for fun.
The count field holds an integer for the number of expected results, or #false if no annotation is declared (i.e., any number of results is expected).
The is_predicate field is #true if all annotations are predicate annotations, or #false if at least one of them is a converter annotation.
The maybe_converter field is #false if no annotation is declared or if it is unchecked, otherwise it is a syntax object for a function expression; the resulting function expects count plus two arguments: each original result followed by a success function of count arguments and a failure function of zero arguments.
The static_info field holds static information for the result (in unpacked form; see statinfo_meta.pack); when count is not 1, then static_info has a single key statinfo_meta.values_key whose value is (packed) static information for each value (see statinfo_meta.pack_group).
The annotation_string field describes the annotation, which is useful for throwing an exception when conversion fails.
function | |||||
| |||||
function | |||||
Like expr_meta.relative_precedence and expr_meta.ends_parse, but for binding operators.