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
main
Mike Gerwitz 2023-04-13 09:30:27 -04:00
parent 590c4b7b06
commit c367666d8e
4 changed files with 53 additions and 14 deletions

View File

@ -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;

View File

@ -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.

View File

@ -102,7 +102,8 @@
<apply-template name="_short-hand-nullary-body_">
<with-param name="@values@" value="___dsgr-bfe___" />
</apply-template>
<template name="___dsgr-bfe___">
<template name="___dsgr-bfe___"
desc="Desugared body of shorthand template application of `_short-hand-nullary-body_`">
<c:product>
<c:sum />
</c:product>
@ -112,9 +113,10 @@
<apply-template name="_short-hand-nary-body_">
<with-param name="@bar@" value="baz" />
<with-param name="@baz@" value="quux" />
<with-param name="@values@" value="___dsgr-cb4___" />
<with-param name="@values@" value="___dsgr-cb5___" />
</apply-template>
<template name="___dsgr-cb4___">
<template name="___dsgr-cb5___"
desc="Desugared body of shorthand template application of `_short-hand-nary-body_`">
<c:sum>
<c:product />
</c:sum>
@ -123,9 +125,10 @@
<template name="_short-hand-nullary-outer_"
desc="Outer template holding an inner" />
<apply-template name="_short-hand-nullary-outer_">
<with-param name="@values@" value="___dsgr-d97___" />
<with-param name="@values@" value="___dsgr-d99___" />
</apply-template>
<template name="___dsgr-d97___">
<template name="___dsgr-d99___"
desc="Desugared body of shorthand template application of `_short-hand-nullary-outer_`">
<template name="_short-hand-nullary-inner-dfn-inner_"
desc="Inner template applied inner" />
<apply-template name="_short-hand-nullary-inner-dfn-inner_" />
@ -134,9 +137,10 @@
<template name="_short-hand-nullary-inner-dfn-outer_"
desc="Define template outer but apply inner" />
<apply-template name="_short-hand-nullary-outer_">
<with-param name="@values@" value="___dsgr-eea___" />
<with-param name="@values@" value="___dsgr-eed___" />
</apply-template>
<template name="___dsgr-eea___">
<template name="___dsgr-eed___"
desc="Desugared body of shorthand template application of `_short-hand-nullary-outer_`">
<apply-template name="_short-hand-nullary-inner-dfn-outer_" />
</template>
@ -144,9 +148,10 @@
desc="Unary with body" />
<apply-template name="_short-hand-unary-with-body_">
<with-param name="@foo@" value="bar" />
<with-param name="@values@" value="___dsgr-fb0___" />
<with-param name="@values@" value="___dsgr-fb4___" />
</apply-template>
<template name="___dsgr-fb0___">
<template name="___dsgr-fb4___"
desc="Desugared body of shorthand template application of `_short-hand-unary-with-body_`">
<template name="_short-hand-unary-with-body-inner_"
desc="Inner template" />
<apply-template name="_short-hand-unary-with-body-inner_" />

View File

@ -108,6 +108,7 @@
<template name="_short-hand-nary-body_" desc="N-ary with body" />
<t:short-hand-nary-body bar="baz" baz="quux">
<c:sum>
@ -120,6 +121,7 @@
<template name="_short-hand-nullary-outer_"
desc="Outer template holding an inner" />
<t:short-hand-nullary-outer>
@ -131,6 +133,7 @@
<template name="_short-hand-nullary-inner-dfn-outer_"
desc="Define template outer but apply inner" />
<t:short-hand-nullary-outer>
@ -140,6 +143,7 @@
<template name="_short-hand-unary-with-body_"
desc="Unary with body" />
<t:short-hand-unary-with-body foo="bar">
@ -152,6 +156,7 @@
<template name="_short-hand-in-expr_"
desc="Template to be applied within an expression" />
<rate yields="shortHandTplInExpr">