From c367666d8eb0d242ff94755c4a4b46e8096ced23 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Thu, 13 Apr 2023 09:30:27 -0400 Subject: [PATCH] tamer: nir::tplshort: Generate @desc for generated template This is required by the XSLT-based compiler, since the `xmli` we're producing acts as a new source file. DEV-13708 --- tamer/src/nir/tplshort.rs | 35 ++++++++++++++++++++++---- tamer/src/nir/tplshort/test.rs | 4 +++ tamer/tests/xmli/template/expected.xml | 23 ++++++++++------- tamer/tests/xmli/template/src.xml | 5 ++++ 4 files changed, 53 insertions(+), 14 deletions(-) diff --git a/tamer/src/nir/tplshort.rs b/tamer/src/nir/tplshort.rs index 95d087ba..7032388f 100644 --- a/tamer/src/nir/tplshort.rs +++ b/tamer/src/nir/tplshort.rs @@ -80,6 +80,7 @@ use arrayvec::ArrayVec; use super::{Nir, NirEntity}; use crate::{ + fmt::{DisplayWrapper, TtQuote}, parse::prelude::*, span::Span, sym::{ @@ -111,8 +112,9 @@ pub enum TplShortDesugar { Scanning, /// A shorthand template application associated with the provided - /// [`Span`] was encountered and shorthand params are being desugared. - DesugaringParams(Span), + /// [`SPair`] was encountered and shorthand params are being + /// desugared. + DesugaringParams(SPair), } impl Display for TplShortDesugar { @@ -160,9 +162,10 @@ impl ParseState for TplShortDesugar { let tpl_name = format!("_{}_", qname.local_name().lookup_str()).intern(); - stack.push(Ref(SPair(tpl_name, span))); + let name = SPair(tpl_name, span); + stack.push(Ref(name)); - Transition(DesugaringParams(span)).ok(Open(TplApply, span)) + Transition(DesugaringParams(name)).ok(Open(TplApply, span)) } // Shorthand template params' names do not contain the @@ -186,8 +189,10 @@ impl ParseState for TplShortDesugar { // and then place the body into that template. // // TODO: This does not handle nested template applications. - (DesugaringParams(ospan), tok @ Open(..)) => { + (DesugaringParams(name), tok @ Open(..)) => { + let ospan = name.span(); let gen_name = gen_tpl_name_at_offset(ospan); + let gen_desc = values_tpl_desc(name); // The spans are awkward here because we are streaming, // and so don't have much choice but to use the opening @@ -197,6 +202,7 @@ impl ParseState for TplShortDesugar { // yet-to-be-defined means. // // note: reversed (stack) + stack.push(Desc(gen_desc)); stack.push(BindIdent(SPair(gen_name, ospan))); stack.push(Open(Tpl, ospan)); @@ -258,5 +264,24 @@ fn gen_tpl_name_at_offset(span: Span) -> SymbolId { format!("___dsgr-{:x}___", span.offset()).intern() } +/// Generate a description for the template generated from the body of a +/// shorthand template application. +/// +/// Descriptions are required on templates, +/// but we should also provide something that is useful in debugging. +/// The description will contain the name of the template being applied and +/// will share the same span as the provided [`SPair`] `applying`'s, +/// having been derived from it. +fn values_tpl_desc(applying: SPair) -> SPair { + SPair( + format!( + "Desugared body of shorthand template application of {}", + TtQuote::wrap(applying.symbol()) + ) + .intern(), + applying.span(), + ) +} + #[cfg(test)] mod test; diff --git a/tamer/src/nir/tplshort/test.rs b/tamer/src/nir/tplshort/test.rs index 56cd2895..6e442e85 100644 --- a/tamer/src/nir/tplshort/test.rs +++ b/tamer/src/nir/tplshort/test.rs @@ -126,6 +126,7 @@ fn desugars_body_into_tpl_with_ref_in_values_param() { // but it does allow us to be perfectly precise in the output // assertion. let gen_name = gen_tpl_name_at_offset(S1); + let gen_desc = values_tpl_desc(name); #[rustfmt::skip] assert_eq!( @@ -146,6 +147,7 @@ fn desugars_body_into_tpl_with_ref_in_values_param() { // It is closed and so expandable. // | O(Open(Tpl, S1)), // / O(BindIdent(SPair(gen_name, S1))), //<` + O(Desc(gen_desc)), // And here we have the body of the above // shorthand application. @@ -181,6 +183,7 @@ fn desugar_nested_apply() { ]; let gen_name_outer = gen_tpl_name_at_offset(S1); + let gen_desc = values_tpl_desc(name_outer); #[rustfmt::skip] assert_eq!( @@ -197,6 +200,7 @@ fn desugar_nested_apply() { // | O(Open(Tpl, S1)), // / O(BindIdent(SPair(gen_name_outer, S1))), //<` + O(Desc(gen_desc)), // And within this template, // we generate another application. diff --git a/tamer/tests/xmli/template/expected.xml b/tamer/tests/xmli/template/expected.xml index 486e1e89..eee388f0 100644 --- a/tamer/tests/xmli/template/expected.xml +++ b/tamer/tests/xmli/template/expected.xml @@ -102,7 +102,8 @@ -