File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ mod template_parse;
2626/// (kind)* @capture - capture each repeated named-node match
2727/// "literal"* @capture - capture each repeated unnamed-token match
2828/// (kind)? - zero or one named node
29+ /// [(pattern) (pattern)] - query choice: try alternatives in order
2930/// ```
3031///
3132/// Named fields and bare child patterns may be intermixed in any order.
@@ -161,6 +162,9 @@ pub fn trees(input: TokenStream) -> TokenStream {
161162/// - `@name` (no quantifier) → `name: OutputId`
162163/// - `@name` (after `*`/`+`) → `name: Vec<OutputId>`
163164/// - `@name` (after `?`) → `name: Option<OutputId>`
165+ /// - captures in a query choice are unified by name: a capture in every branch
166+ /// remains required, a missing/optional capture becomes optional, and any
167+ /// repeated occurrence makes the binding a vector
164168///
165169/// Raw `@@name` captures use the corresponding input `Id` types instead.
166170///
Original file line number Diff line number Diff line change @@ -178,6 +178,36 @@ Single-pattern and empty query groups are rejected as redundant or meaningless.
178178This does not affect the empty output template ` () ` , which intentionally means
179179“emit no replacement nodes.”
180180
181+ ### Query choices
182+
183+ A query choice tries its alternatives from left to right and merges captures
184+ from the selected alternative:
185+
186+ ``` rust
187+ [
188+ (integer ) @ number
189+ (identifier ) @@ name
190+ ]
191+ ```
192+
193+ An alternative may contain any query pattern. Parenthesize a sequence of
194+ sibling patterns to use it as one alternative:
195+
196+ ``` rust
197+ [
198+ (
199+ (item )* @ items
200+ (separator ) @ separator
201+ )
202+ (single_item ) @ item
203+ ]
204+ ```
205+
206+ Capture bindings are unified by name. A capture present in every alternative
207+ remains required; one missing or optional in any alternative becomes optional;
208+ and any repeated occurrence makes the combined binding a vector. The same name
209+ must consistently use ` @ ` or ` @@ ` .
210+
181211## Template language
182212
183213Templates construct new AST nodes using the ` tree! ` and ` trees! ` macros.
You can’t perform that action at this time.
0 commit comments