tamer: nir::tplshort: Remove variant enum prefixes

This just cleans up a little before I introduce more code, making this
easier to read.

DEV-13708
main
Mike Gerwitz 2023-03-23 11:41:52 -04:00
parent 9c0e20e58c
commit 120f5bdfef
2 changed files with 40 additions and 45 deletions

View File

@ -68,6 +68,9 @@ use crate::{
};
use std::convert::Infallible;
use Nir::*;
use NirEntity::*;
#[derive(Debug, PartialEq, Eq, Default)]
pub enum TplShortDesugar {
/// Waiting for shorthand template application,
@ -109,7 +112,7 @@ impl ParseState for TplShortDesugar {
//
// The name of the template _without_ the underscore padding is
// the local part of the QName.
(Scanning, Nir::Open(NirEntity::TplApply(Some(qname)), span)) => {
(Scanning, Open(TplApply(Some(qname)), span)) => {
// TODO: Determine whether caching these has any notable
// benefit over repeated heap allocations,
// comparing packages with very few applications and
@ -118,26 +121,21 @@ impl ParseState for TplShortDesugar {
let tpl_name =
format!("_{}_", qname.local_name().lookup_str()).intern();
stack.push(Nir::Ref(SPair(tpl_name, span)));
stack.push(Ref(SPair(tpl_name, span)));
Transition(Scanning)
.ok(Nir::Open(NirEntity::TplApply(None), span))
Transition(Scanning).ok(Open(TplApply(None), span))
}
// Shorthand template params' names do not contain the
// surrounding `@`s.
(
Scanning,
Nir::Open(NirEntity::TplParam(Some((name, val))), span),
) => {
(Scanning, Open(TplParam(Some((name, val))), span)) => {
let pname = format!("@{name}@").intern();
stack.push(Nir::Close(NirEntity::TplParam(None), span));
stack.push(Nir::Text(val));
stack.push(Nir::BindIdent(SPair(pname, name.span())));
stack.push(Close(TplParam(None), span));
stack.push(Text(val));
stack.push(BindIdent(SPair(pname, name.span())));
Transition(Scanning)
.ok(Nir::Open(NirEntity::TplParam(None), span))
Transition(Scanning).ok(Open(TplParam(None), span))
}
// Any tokens that we don't recognize will be passed on unchanged.

View File

@ -30,15 +30,12 @@ fn desugars_nullary() {
let qname = ("t", "tpl-name").unwrap_into();
let tpl = "_tpl-name_".into();
let toks = [
Nir::Open(NirEntity::TplApply(Some(qname)), S1),
Nir::Close(NirEntity::TplApply(None), S2),
];
let toks = [Open(TplApply(Some(qname)), S1), Close(TplApply(None), S2)];
#[rustfmt::skip]
assert_eq!(
Ok(vec![
O(Nir::Open(NirEntity::TplApply(None), S1)),
O(Open(TplApply(None), S1)),
// The span associated with the name of the template to be
// applied is the span of the entire QName from NIR.
// The reason for this is that `t:foo` is transformed into
@ -47,8 +44,8 @@ fn desugars_nullary() {
// representation of the name of the template;
// `foo` is not in itself a valid template name at the
// time of writing.
O(Nir::Ref(SPair(tpl, S1))),
O(Nir::Close(NirEntity::TplApply(None), S2)),
O(Ref(SPair(tpl, S1))),
O(Close(TplApply(None), S2)),
]),
Sut::parse(toks.into_iter()).collect(),
);
@ -70,28 +67,28 @@ fn desugars_unary() {
#[rustfmt::skip]
let toks = vec![
// <t:qname
Nir::Open(NirEntity::TplApply(Some(qname)), S1),
Open(TplApply(Some(qname)), S1),
// foo="foo value"
Nir::Open(NirEntity::TplParam(Some((aname, pval))), S2),
Open(TplParam(Some((aname, pval))), S2),
// Implicit close.
// />
Nir::Close(NirEntity::TplApply(None), S6),
Close(TplApply(None), S6),
];
#[rustfmt::skip]
assert_eq!(
Ok(vec![
O(Nir::Open(NirEntity::TplApply(None), S1)),
O(Nir::Ref(name)),
O(Open(TplApply(None), S1)),
O(Ref(name)),
O(Nir::Open(NirEntity::TplParam(None), S2)),
O(Open(TplParam(None), S2)),
// Derived from `aname` (by padding)
O(Nir::BindIdent(pname)),
O(BindIdent(pname)),
// The value is left untouched.
O(Nir::Text(pval)),
O(Text(pval)),
// Close is derived from open.
O(Nir::Close(NirEntity::TplParam(None), S2)),
O(Nir::Close(NirEntity::TplApply(None), S6)),
O(Close(TplParam(None), S2)),
O(Close(TplApply(None), S6)),
]),
Sut::parse(toks.into_iter()).collect(),
);
@ -101,19 +98,19 @@ fn desugars_unary() {
#[test]
fn does_not_desugar_long_form() {
let name = SPair("_tpl-name_".into(), S2);
let pname = SPair("@param".into(), S4);
let pname = SPair("@param@".into(), S4);
let pval = SPair("value".into(), S5);
#[rustfmt::skip]
let toks = [
Nir::Open(NirEntity::TplApply(None), S1),
Nir::BindIdent(name),
Open(TplApply(None), S1),
BindIdent(name),
Nir::Open(NirEntity::TplParam(None), S3),
Nir::BindIdent(pname),
Nir::Text(pval),
Nir::Close(NirEntity::TplParam(None), S6),
Nir::Close(NirEntity::TplApply(None), S7),
Open(TplParam(None), S3),
BindIdent(pname),
Text(pval),
Close(TplParam(None), S6),
Close(TplApply(None), S7),
];
#[rustfmt::skip]
@ -123,14 +120,14 @@ fn does_not_desugar_long_form() {
// So this is a duplicate of the above,
// mapped over `O`.
Ok(vec![
O(Nir::Open(NirEntity::TplApply(None), S1)),
O(Nir::BindIdent(name)),
O(Open(TplApply(None), S1)),
O(BindIdent(name)),
O(Nir::Open(NirEntity::TplParam(None), S3)),
O(Nir::BindIdent(pname)),
O(Nir::Text(pval)),
O(Nir::Close(NirEntity::TplParam(None), S6)),
O(Nir::Close(NirEntity::TplApply(None), S7)),
O(Open(TplParam(None), S3)),
O(BindIdent(pname)),
O(Text(pval)),
O(Close(TplParam(None), S6)),
O(Close(TplApply(None), S7)),
]),
Sut::parse(toks.into_iter()).collect(),
);