Skip to content

yeast: Add support for associating schemas with tree!/trees! calls. - #23120

Draft
tausbn wants to merge 8 commits into
mainfrom
tausbn/yeast-tree-schema
Draft

tausbn wants to merge 8 commits into
mainfrom
tausbn/yeast-tree-schema

Conversation

@tausbn

@tausbn tausbn commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

We want to be able to check tree! templates against the given output schema, but having to specify the schema path directly in each call would get old fast.

So, instead we add a macro, tree_schema! that creates a local module (with a user-specified name) that exposes the tree! macro in such a way that the schema is baked into uses of that macro automatically.

Essentially, the outer tree!(...) expands into

tree_with_schema!(SCHEMA, ...)

which means each invocations now knows exactly what schema applies to the tree template. Note that we do not currently perform any checks -- that will come in a future PR.


Can be reviewed commit-by-commit.

@tausbn tausbn added the no-change-note-required This PR does not need a change note label Nov 12, 2026
Pass the generated or caller-spanned `ctx` identifier through every
template lowering path instead of falling back to an implicitly
constructed identifier.

This is needed primarily due to macro hygiene. Eventually we will be
introducing macros that define macros, and here it's problematic that we
rely on an implicit `ctx` variable to be present. So instead, we pass a
suitable `ctx` identifier through the entire lowering, and interpolate
that (using `#ctx`) instead.
Add hidden `tree!` and `trees!` variants that inject a schema dependency
and preserve access to the caller's `ctx` binding.

Essentially, these inner variants (`tree_with_schema!` etc.) will be
used internally with an interpolated reference to the output schema.
Thus, when we write `tree!(...)` this turns into
`tree_with_schema(SCHEMA, ...)`, which enables that local macro call to
(eventually) check the tree template against that schema.
Adds a new macro, `tree_schema!` which takes two arguments: an
identifier and the name of an output schema. When the macro is executed,
it then creates a local module with the same name as the specified
identifier, which exports `tree` and `trees` macros and also the schema.

These can then be imported in order to get access to these macros.
Declare the shared AST schema once and import its `tree!` wrapper for
Swift lowering helpers and rule bodies.
I think in general we'll be happier if all uses of `tree!`/`trees!` have
an (implicitly) associated schema. Thus, we don't need the generic
schema-less versions anymore.
Provide `tree_with_context!` and `trees_with_context!` for
macro-generated templates whose forwarded tokens cannot reliably
identify the local `ctx` binding.

This is essentially only needed if you're doing really peculiar stuff
with macros, but Copilot insisted, and it seemed harmless to add this.

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

Both builders skip interpolation isolation when the explicit context is not named ctx, allowing mutations to leak into subsequent interpolations.

1 open issue
What changed in this PR

This PR associates YEAST tree builders with output schemas through generated namespaces, preparing for future template validation without repeating schema paths at each call.

Changes:

  • Adds tree_schema! namespaces exposing schema contents and tree-building macros.
  • Adds explicit-context builders and threads the selected context through template lowering.
  • Migrates Swift and test usage, with documentation and namespace/forwarding tests. Schema validation remains deferred.
File Description
unified/​extractor/​src/​languages/​swift/​swift.rs Uses a schema-bound Swift tree builder.
shared/​yeast/​tests/​test.rs Migrates builders and tests namespaces and forwarded templates.
shared/​yeast/​src/​lib.rs Exports the namespace macro and hidden builder entry points.
shared/​yeast/​doc/​yeast.md Documents schema namespaces and explicit-context usage.
shared/​yeast-macros/​src/​parse.rs Generates namespaces and parses schema paths and contexts.
shared/​yeast-macros/​src/​lower.rs Threads context identifiers through template lowering.
shared/​yeast-macros/​src/​lib.rs Defines schema-aware procedural macro entry points.

Review effort: Balanced (auto)

Comment thread shared/yeast-macros/src/lower.rs Resolved

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