Skip to content

Commit 7315816

Browse files
committed
yeast: Require context for template lowering
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.
1 parent 7b9ee04 commit 7315816

1 file changed

Lines changed: 42 additions & 21 deletions

File tree

‎shared/yeast-macros/src/lower.rs‎

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ use crate::ast::{
77
Alternative, Cardinality, Content, Field, Node, Pattern, Replacement, Rule, Template,
88
};
99

10-
#[derive(Clone, Copy, Default)]
10+
#[derive(Clone, Copy)]
1111
struct LoweringContext<'a> {
1212
choice_alternative_scope: Option<ChoiceAlternativeScope<'a>>,
13+
ctx: &'a Ident,
1314
}
1415

1516
#[derive(Clone, Copy)]
@@ -69,12 +70,12 @@ impl Rule {
6970
quote! { yeast::OutputId },
7071
&translated_captures_ident,
7172
);
72-
let replacement = self.replacement.lower();
73+
let ctx = Ident::new("ctx", Span::call_site());
74+
let replacement = self.replacement.lower(&ctx);
7375
let guard = self
7476
.guard
7577
.clone()
7678
.unwrap_or_else(|| syn::parse_quote!(true));
77-
let ctx = Ident::new("ctx", Span::call_site());
7879

7980
quote! {
8081
{
@@ -105,16 +106,16 @@ impl Rule {
105106
}
106107

107108
impl Replacement {
108-
fn lower(&self) -> TokenStream {
109+
fn lower(&self, ctx: &Ident) -> TokenStream {
109110
match self {
110-
Replacement::Templates(templates) => Template::lower_list(templates),
111+
Replacement::Templates(templates) => Template::lower_list(templates, ctx),
111112
Replacement::Rust { body, .. } => {
112113
let body = rust_block(body);
113114
quote! {
114115
let __value = ::std::iter::IntoIterator::into_iter(#body)
115116
.collect::<Vec<_>>();
116117
let mut __ids: Vec<yeast::OutputId> = Vec::new();
117-
ctx.extend_field_ids(__value, &mut __ids);
118+
#ctx.extend_field_ids(__value, &mut __ids);
118119
__ids
119120
}
120121
}
@@ -307,15 +308,23 @@ fn capture_bindings<'a>(
307308
}
308309

309310
impl Template {
310-
pub(crate) fn lower_root(&self) -> TokenStream {
311+
pub(crate) fn lower_root(&self, ctx: &Ident) -> TokenStream {
312+
let context = LoweringContext {
313+
choice_alternative_scope: None,
314+
ctx,
315+
};
311316
match self {
312-
Template::Choice(alternatives) => Self::lower_choice(alternatives),
313-
_ => self.lower_single(LoweringContext::default()),
317+
Template::Choice(alternatives) => Self::lower_choice(alternatives, context),
318+
_ => self.lower_single(context),
314319
}
315320
}
316321

317-
pub(crate) fn lower_list(templates: &[Self]) -> TokenStream {
318-
let context = LoweringContext::default();
322+
pub(crate) fn lower_list(templates: &[Self], ctx: &Ident) -> TokenStream {
323+
let context = LoweringContext {
324+
choice_alternative_scope: None,
325+
ctx,
326+
};
327+
let ctx = context.ctx;
319328
let items = templates
320329
.iter()
321330
.map(|template| match template {
@@ -328,15 +337,16 @@ impl Template {
328337
let block = rust_block(block);
329338
isolated_interpolation(
330339
uses_ctx,
340+
ctx,
331341
quote! {
332342
let __value = ::std::iter::IntoIterator::into_iter(#block)
333343
.collect::<Vec<_>>();
334-
ctx.extend_field_ids(__value, &mut __nodes);
344+
#ctx.extend_field_ids(__value, &mut __nodes);
335345
},
336346
)
337347
}
338348
Template::Choice(alternatives) => {
339-
let pick = Self::lower_choice(alternatives);
349+
let pick = Self::lower_choice(alternatives, context);
340350
quote! {
341351
if let ::std::option::Option::Some(__id) = #pick {
342352
__nodes.push(__id);
@@ -356,13 +366,15 @@ impl Template {
356366
}
357367

358368
fn lower_single(&self, context: LoweringContext<'_>) -> TokenStream {
369+
let ctx = context.ctx;
359370
match self {
360371
Template::Node(node) => node.lower(context),
361372
Template::Splice(block) => {
362373
let uses_ctx = block_uses_ctx(block);
363374
let block = rust_block(block);
364375
isolated_interpolation(
365376
uses_ctx,
377+
ctx,
366378
quote! {
367379
::std::convert::Into::<yeast::OutputId>::into(#block)
368380
},
@@ -374,22 +386,27 @@ impl Template {
374386
}
375387
}
376388

377-
fn lower_choice(alternatives: &[Alternative]) -> TokenStream {
389+
fn lower_choice(alternatives: &[Alternative], context: LoweringContext<'_>) -> TokenStream {
378390
let pick = alternatives.iter().rev().fold(
379391
quote! { ::std::option::Option::None },
380-
|fallback, alternative| alternative.lower_candidate(fallback),
392+
|fallback, alternative| alternative.lower_candidate(fallback, context),
381393
);
382394
quote! {{ #pick }}
383395
}
384396
}
385397

386398
impl Alternative {
387-
fn lower_candidate(&self, fallback: TokenStream) -> TokenStream {
399+
fn lower_candidate(
400+
&self,
401+
fallback: TokenStream,
402+
parent_context: LoweringContext<'_>,
403+
) -> TokenStream {
388404
let interpolations = RefCell::new(Vec::new());
389405
let context = LoweringContext {
390406
choice_alternative_scope: Some(ChoiceAlternativeScope {
391407
interpolations: &interpolations,
392408
}),
409+
ctx: parent_context.ctx,
393410
};
394411
let value = self.template.lower_single(context);
395412
let interpolations = interpolations.into_inner();
@@ -456,7 +473,7 @@ impl Alternative {
456473

457474
impl Node {
458475
fn lower(&self, context: LoweringContext<'_>) -> TokenStream {
459-
let ctx = Ident::new("ctx", Span::call_site());
476+
let ctx = context.ctx;
460477
let kind_str = self.kind.to_string();
461478

462479
let node_init = if let Some(content) = &self.content {
@@ -470,6 +487,7 @@ impl Node {
470487
let source_range = fresh_prepared_ident("source_range");
471488
let expression = isolated_interpolation(
472489
uses_ctx,
490+
ctx,
473491
quote! {
474492
let __expr = #block;
475493
yeast::MaybeYeastValue::maybe_yeast_value(&__expr).map(
@@ -506,6 +524,7 @@ impl Node {
506524
} else {
507525
isolated_interpolation(
508526
uses_ctx,
527+
ctx,
509528
quote! {
510529
let __expr = #block;
511530
let __value =
@@ -542,6 +561,7 @@ impl Node {
542561
let block = rust_block(block);
543562
stmts.push(isolated_interpolation(
544563
uses_ctx,
564+
ctx,
545565
quote! {
546566
let __value = ::std::iter::IntoIterator::into_iter(#block)
547567
.collect::<Vec<_>>();
@@ -570,6 +590,7 @@ impl Node {
570590
let block = rust_block(block);
571591
let interpolation = isolated_interpolation(
572592
uses_ctx,
593+
ctx,
573594
quote! {
574595
let __value = ::std::iter::IntoIterator::into_iter(#block)
575596
.collect::<Vec<_>>();
@@ -587,7 +608,7 @@ impl Node {
587608
});
588609
}
589610
Template::Choice(alternatives) => {
590-
let pick = Template::lower_choice(alternatives);
611+
let pick = Template::lower_choice(alternatives, context);
591612
stmts.push(quote! {
592613
let #temp: ::std::option::Option<yeast::OutputId> = #pick;
593614
});
@@ -642,12 +663,12 @@ fn rust_block(block: &syn::Block) -> TokenStream {
642663
quote!({ #(#statements)* })
643664
}
644665

645-
fn isolated_interpolation(uses_ctx: bool, body: TokenStream) -> TokenStream {
666+
fn isolated_interpolation(uses_ctx: bool, ctx: &Ident, body: TokenStream) -> TokenStream {
646667
if uses_ctx {
647668
quote! {{
648-
let __saved_user_ctx = (*ctx.user_ctx).clone();
669+
let __saved_user_ctx = (*#ctx.user_ctx).clone();
649670
let __result = { #body };
650-
*ctx.user_ctx = __saved_user_ctx;
671+
*#ctx.user_ctx = __saved_user_ctx;
651672
__result
652673
}}
653674
} else {

0 commit comments

Comments
 (0)