Skip to content

yeast: Add query_match! macro - #23225

Open
tausbn wants to merge 13 commits into
mainfrom
tausbn/yeast-query-match
Open

tausbn wants to merge 13 commits into
mainfrom
tausbn/yeast-query-match

Conversation

@tausbn

@tausbn tausbn commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

This macro can be thought of as a "local" variation of the rules! macro. It uses essentially the same syntax, and allows picking a match arm based on whether a query matches or not. Queries can use all of the existing machinery, with a few crucial differences:

  • All captures are raw, even if prefixed with @@. This is because a query_match! may be done outside of a translation context, so there's not necessarily a sensible notion of "eager translation".
  • The right hand side of a match arm must be a Rust block, not a tree. (It can of course just create the tree using the tree! macro instead.)

If no query matches, the entire match panics. Use a wildcard match like _ => { None } or similar if this behaviour is undesirable.

We also introduce a query_match_schema! macro for setting up the query_match! macro in a local module, just as we did previously with tree_schema!. Eventually, this will allow us to check the queries against the schema. Another benefit of this is that we can instantiate the schema separately for the input and output ASTs, and thus run queries against the output AST.


Should be reviewed commit-by-commit.

Getting these out of the way so they don't take up space in later
commits.
Make query matching and capture storage generic over `TreeId` so the
same runtime can match both input and output ASTs.
Capture bindings created with call-site hygiene are invisible through
generated `macro_rules!` wrappers. Preserve each capture's pattern span
and allow unused bindings now that they belong to user code.

(If the above is unclear, don't worry about it. It's just something we
need to do to get the macros to behave.)
We parse these in much the same way as top-level rules (and reuse some
of the machinery), the only difference being that the right hand side of
a match arm must be a Rust block -- it can't be a directly embedded
tree.
Try arms in order against the AST selected by the node ID type. Guards
and bodies receive independent raw captures, while bodies remain inline
so `return` and `continue` affect their enclosing scope.

Keep the selected `BuildCtx` separate from guard-local `ctx` and `ast`
bindings so explicit context identifiers cannot redirect or shadow guard
access.
This is an "internal" variant of `query_match!` that expects to receive
(a path to) the relevant schema as an argument. This will almost never
be used directly -- rather, the `query_match_schema!` macro we define
later will take care of defining a local `query_match!` macro that bakes
in this schema path. That way, each invocation of the macro knows what
schema to (eventually -- not in this PR) check validity against.
This is the macro that was mentioned in the previous commit. Like
`tree_schema!`, it creates a local module that exposes its own
`query_match!` macro with the given schema path baked in.
Adds a bunch of tests covering required, optional, and repeated raw
captures, ordered arm selection, catch-all handling, guard bindings, and
guard fallthrough.
Verify that matcher bodies preserve enclosing `return` and `continue`,
and that unmatched nodes report their kind and source location.
Having previously made the query matching mechanism agnostic in whether
it's running on the input or output AST, the only thing we need to do to
add support for output matching is to tell Rust where the AST lives (in
the `output` field, as opposed to the `ast` field).
File this one under "things we'll almost certainly never need, but
Copilot complained about it". Essentially, if we ever end up writing a
macro that needs to mess with the context, then the previous macros
won't do, due to `macro_rules!`'s enforcement of hygiene. So, we add
`query_match_with_context!` that takes the context as an explicit
argument, for the rare case where this is needed.
Document schema namespaces, ordered arms, raw captures, guards, output
matching, and explicit contexts.
By using `query_match!` we avoid having to inspect the node structure
directly (which is a bit awkward in plain Rust).
@tausbn tausbn added the no-change-note-required This PR does not need a change note label Nov 24, 2026
@tausbn
tausbn requested an adaptive review from Copilot November 24, 2026 15:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Generated temporaries can shadow caller context bindings and capture storage, causing compilation failures and broken capture extraction.

1 open issue
What changed in this PR

Adds schema-scoped query_match! macros to YEAST for imperative matching against input and output ASTs, and adopts output matching in the Swift extractor.

Changes:

  • Adds ordered query arms, optional guards, raw captures, and explicit-context matching.
  • Generalizes query matching and capture storage to support output node IDs.
  • Documents the API and adds tests for matching behavior, context handling, and output borrowing.
File Description
unified/​extractor/​src/​languages/​swift/​swift.rs Uses output queries to detect explicit constructors.
shared/​yeast/​tests/​test.rs Adds matcher behavior and context-handling tests.
shared/​yeast/​src/​query.rs Supports querying input and output ASTs.
shared/​yeast/​src/​lib.rs Exports the new matcher macros.
shared/​yeast/​src/​captures.rs Generalizes capture storage over node ID types.
shared/​yeast/​doc/​yeast.md Documents matcher namespaces, guards, and contexts.
shared/​yeast-macros/​src/​rule_parse.rs Makes guard parsing reusable.
shared/​yeast-macros/​src/​parse.rs Parses matcher arms and generates schema namespaces.
shared/​yeast-macros/​src/​lower.rs Generates matching, capture bindings, and fallback behavior.
shared/​yeast-macros/​src/​lib.rs Adds procedural macro entry points.
shared/​yeast-macros/​src/​ast.rs Defines matcher and arm representations.

Review effort: Balanced (adaptive)

Comment thread shared/yeast-macros/src/lower.rs Resolved
@tausbn
tausbn marked this pull request as ready for review November 24, 2026 15:35
@tausbn
tausbn requested a review from a team as a code owner November 24, 2026 15:35

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation no-change-note-required This PR does not need a change note Unified

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants