Switch parameterized blocks to having a Path for a name and taking a parser for a name matcher in the goal of merging with the conditional blocks.

This commit is contained in:
Tom Alexander 2020-05-25 21:17:58 -04:00
parent 2527baeff4
commit dc92973313
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -109,7 +109,7 @@ pub struct NamedBlock<'a> {
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct ParameterizedBlock<'a> { pub struct ParameterizedBlock<'a> {
pub name: &'a str, pub path: Path<'a>,
pub explicit_context: Option<Path<'a>>, pub explicit_context: Option<Path<'a>>,
pub params: Vec<KVPair<'a>>, pub params: Vec<KVPair<'a>>,
pub contents: Option<Body<'a>>, pub contents: Option<Body<'a>>,
@ -225,20 +225,29 @@ fn dust_tag(i: &str) -> IResult<&str, DustTag> {
named_block("{<", DustTag::DTInlinePartial), named_block("{<", DustTag::DTInlinePartial),
partial("{>", DustTag::DTPartial), partial("{>", DustTag::DTPartial),
map( map(
parameterized_block("{@", "gte"), parameterized_block("{@", &tag_to_path("gte")),
DustTag::DTHelperGreaterThanOrEquals, DustTag::DTHelperGreaterThanOrEquals,
), ),
map( map(
parameterized_block("{@", "lte"), parameterized_block("{@", &tag_to_path("lte")),
DustTag::DTHelperLessThanOrEquals, DustTag::DTHelperLessThanOrEquals,
), ),
map(parameterized_block("{@", "eq"), DustTag::DTHelperEquals),
map(parameterized_block("{@", "ne"), DustTag::DTHelperNotEquals),
map( map(
parameterized_block("{@", "gt"), parameterized_block("{@", &tag_to_path("eq")),
DustTag::DTHelperEquals,
),
map(
parameterized_block("{@", &tag_to_path("ne")),
DustTag::DTHelperNotEquals,
),
map(
parameterized_block("{@", &tag_to_path("gt")),
DustTag::DTHelperGreaterThan, DustTag::DTHelperGreaterThan,
), ),
map(parameterized_block("{@", "lt"), DustTag::DTHelperLessThan), map(
parameterized_block("{@", &tag_to_path("lt")),
DustTag::DTHelperLessThan,
),
))(i) ))(i)
} }
@ -289,6 +298,14 @@ fn path(i: &str) -> IResult<&str, Path> {
))(i) ))(i)
} }
fn tag_to_path<'a>(text: &'static str) -> impl Fn(&'a str) -> IResult<&str, Path<'a>> {
move |i: &'a str| map(tag(text), |t| Path { keys: vec![t] })(i)
}
fn key_to_path<'a>(i: &'a str) -> IResult<&str, Path<'a>> {
map(key, |k| Path { keys: vec![k] })(i)
}
/// Just digits, no signs or decimals /// Just digits, no signs or decimals
fn postitive_integer_literal(i: &str) -> IResult<&str, u64> { fn postitive_integer_literal(i: &str) -> IResult<&str, u64> {
map( map(
@ -469,42 +486,51 @@ where
} }
} }
fn parameterized_block<'a>( fn parameterized_block<'a, F>(
open_matcher: &'static str, open_matcher: &'static str,
tag_name: &'static str, name_matcher: F,
) -> impl FnMut(&'a str) -> IResult<&'a str, ParameterizedBlock<'a>> { ) -> impl FnMut(&'a str) -> IResult<&'a str, ParameterizedBlock<'a>>
where
F: Copy + Fn(&'a str) -> IResult<&'a str, Path<'a>>,
{
alt(( alt((
parameterized_block_with_body(open_matcher, tag_name), parameterized_block_with_body(open_matcher, name_matcher),
parameterized_self_closing_block(open_matcher, tag_name), parameterized_self_closing_block(open_matcher, name_matcher),
)) ))
} }
fn parameterized_block_with_body<'a>( fn parameterized_block_with_body<'a, F>(
open_matcher: &'static str, open_matcher: &'static str,
tag_name: &'static str, name_matcher: F,
) -> impl Fn(&'a str) -> IResult<&'a str, ParameterizedBlock<'a>> { ) -> impl Fn(&'a str) -> IResult<&'a str, ParameterizedBlock<'a>>
where
F: Copy + Fn(&'a str) -> IResult<&'a str, Path<'a>>,
{
move |i: &'a str| { move |i: &'a str| {
let (i, (name, maybe_explicit_context, params, inner, maybe_else, _closing_name)) = let (i, (opening_name, maybe_explicit_context, params, inner, maybe_else, _closing_name)) =
tuple(( verify(
preceded(tag(open_matcher), tag(tag_name)), tuple((
opt(preceded(tag(":"), path)), preceded(tag(open_matcher), name_matcher),
terminated( opt(preceded(tag(":"), path)),
opt(delimited( terminated(
space1, opt(delimited(
separated_list1(space1, key_value_pair), space1,
space0, separated_list1(space1, key_value_pair),
)), space0,
tag("}"), )),
), tag("}"),
opt(body), ),
opt(preceded(tag("{:else}"), opt(body))), opt(body),
delimited(tag("{/"), tag(tag_name), tag("}")), opt(preceded(tag("{:else}"), opt(body))),
))(i)?; delimited(tag("{/"), name_matcher, tag("}")),
)),
|(open, _maybe_explicit, _params, _inn, _maybe_else, close)| open == close,
)(i)?;
Ok(( Ok((
i, i,
ParameterizedBlock { ParameterizedBlock {
name: name, path: opening_name,
explicit_context: maybe_explicit_context, explicit_context: maybe_explicit_context,
params: params.unwrap_or(Vec::new()), params: params.unwrap_or(Vec::new()),
contents: inner, contents: inner,
@ -514,15 +540,18 @@ fn parameterized_block_with_body<'a>(
} }
} }
fn parameterized_self_closing_block<'a>( fn parameterized_self_closing_block<'a, F>(
open_matcher: &'static str, open_matcher: &'static str,
tag_name: &'static str, name_matcher: F,
) -> impl Fn(&'a str) -> IResult<&'a str, ParameterizedBlock<'a>> { ) -> impl Fn(&'a str) -> IResult<&'a str, ParameterizedBlock<'a>>
where
F: Copy + Fn(&'a str) -> IResult<&'a str, Path<'a>>,
{
move |i: &'a str| { move |i: &'a str| {
let (i, (name, maybe_explicit_context, params)) = delimited( let (i, (opening_name, maybe_explicit_context, params)) = delimited(
tag(open_matcher), tag(open_matcher),
tuple(( tuple((
tag(tag_name), name_matcher,
opt(preceded(tag(":"), path)), opt(preceded(tag(":"), path)),
opt(delimited( opt(delimited(
space1, space1,
@ -536,7 +565,7 @@ fn parameterized_self_closing_block<'a>(
Ok(( Ok((
i, i,
ParameterizedBlock { ParameterizedBlock {
name: name, path: opening_name,
explicit_context: maybe_explicit_context, explicit_context: maybe_explicit_context,
params: params.unwrap_or(Vec::new()), params: params.unwrap_or(Vec::new()),
contents: None, contents: None,
@ -1325,7 +1354,7 @@ mod tests {
Ok(( Ok((
"", "",
DustTag::DTHelperEquals(ParameterizedBlock { DustTag::DTHelperEquals(ParameterizedBlock {
name: "eq", path: Path { keys: vec!["eq"] },
explicit_context: None, explicit_context: None,
params: vec![ params: vec![
KVPair { KVPair {
@ -1362,7 +1391,7 @@ mod tests {
Ok(( Ok((
"", "",
DustTag::DTHelperEquals(ParameterizedBlock { DustTag::DTHelperEquals(ParameterizedBlock {
name: "eq", path: Path { keys: vec!["eq"] },
explicit_context: None, explicit_context: None,
params: vec![ params: vec![
KVPair { KVPair {
@ -1388,7 +1417,7 @@ mod tests {
Ok(( Ok((
"", "",
DustTag::DTHelperEquals(ParameterizedBlock { DustTag::DTHelperEquals(ParameterizedBlock {
name: "eq", path: Path { keys: vec!["eq"] },
explicit_context: Some(Path { explicit_context: Some(Path {
keys: vec!["foo", "bar"] keys: vec!["foo", "bar"]
}), }),
@ -1427,7 +1456,7 @@ mod tests {
Ok(( Ok((
"", "",
DustTag::DTHelperEquals(ParameterizedBlock { DustTag::DTHelperEquals(ParameterizedBlock {
name: "eq", path: Path { keys: vec!["eq"] },
explicit_context: Some(Path { explicit_context: Some(Path {
keys: vec!["foo", "bar"] keys: vec!["foo", "bar"]
}), }),