4.7 Unquote Binding Macros
Unquote binding forms are similar to normal binding forms, but they appear only under $ within a syntax binding pattern. Unquote binding forms are distinct from normal binding forms because they must match syntax objects; some operators work in both contexts but have different meanings, such as :: and _ for unquote bindings versus :: and _ for normal bindings.
space | |
In addition to the option forms supported by expr.macro, unquote_bind.macro supports a ~kind option, which declares an identifier be bound to context-kind information:
#'term: A context that needs a matcher for terms or spliced sequences.
#'grouplet: A context where the Group syntax class could be used to match a nonempty term sequence.
#'group: A context to match a whole group, which is like #'grouplet, but where an identifier defaults to the Group syntax class instead of Term.
#'multi: A context to match a sequence of groups.
#'block: A context to match a sequence of groups within a block.
These context are tried last to first for possibilities that apply at the use site of an unquote binding form. If an unquote binding form is not compatible with the given context, it can return unquote_bind_meta.pack_invalid to have the next possibility tried.
> unquote_bind.macro 'dots':
'«'$('...')'»'
> match Syntax.make_group(['...', '...', '...'])
| '$dots ...': "all dots"
"all dots"
> syntax_class Wrapped
| '($content)'
| '[$content]'
| '{$content}'
> unquote_bind.macro 'wrapped $(id :: Identifier)':
> match '{x} [y] (z)'
| '$(wrapped a) ...': [a, ...]
['x', 'y', 'z']
function | ||
| ||
| ||
function | ||
The annot_meta.unpack_kind function takes a parsed unquote binding form and reports the kind of context that it was parsed for. A #false result indicates that parsing in a given context was unsuccessful. A #'id result indicates that stx can adapt to any context, like an identifier can.
The unquote_bind_meta.pack_invalid function returns a syntax object that represents a failure of an unquote binding form for a particular context. This kind of failure normally triggers a retry in a narrower kind of context.
syntax class | ||||||||
| ||||||||
| ||||||||
syntax class | ||||||||
| ||||||||
| ||||||||
syntax class | ||||||||
|
Analogous to expr_meta.Parsed, etc., but for unquote bindings.
Unlike expr_meta.Parsed, a kind argument specifies the kind of context for the escape as described in unquote_bind.macro.