Factor out the parsing of partial template names into its own parser for reuse as an rvalue.

This commit is contained in:
Tom Alexander 2020-05-30 15:45:44 -04:00
parent 4932a4bb6f
commit 3352b777ae
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 42 additions and 29 deletions

View File

@ -11,5 +11,13 @@
{ {
"petname": "spot" "petname": "spot"
} }
],
"array_petname": [
{
"petname": [
"foo",
"bar"
]
}
] ]
} }

View File

@ -6,6 +6,22 @@ Hello {name}, nice {pet}{~n}
Hello {name}, nice {pet}{~n} Hello {name}, nice {pet}{~n}
{/people} {/people}
Direct Parameters{~n}
================={~n}
{#people name="chris" pet=petname petname="whiskers"}
Hello {name}, nice {pet}{~n}
{/people}
{#people}
{#truthy name="chris" pet=petname petname="whiskers"}
Hello {name}, nice {pet}{~n}
{/truthy}
{/people}
{#people name="chris" pet=petname petname="whiskers"}
{#other_petname}
Hello {name}, nice {pet}{~n}
{/other_petname}
{/people}
Reference Parameters{~n} Reference Parameters{~n}
===================={~n} ===================={~n}
{#people name="chris" pet="{petname}" petname="whiskers"} {#people name="chris" pet="{petname}" petname="whiskers"}
@ -33,20 +49,3 @@ Reference Parameters{~n}
Hello {name}, nice {pet}{~n} Hello {name}, nice {pet}{~n}
{/other_petname} {/other_petname}
{/people} {/people}
Direct Parameters{~n}
================={~n}
{#people name="chris" pet=petname petname="whiskers"}
Hello {name}, nice {pet}{~n}
{/people}
{#people}
{#truthy name="chris" pet=petname petname="whiskers"}
Hello {name}, nice {pet}{~n}
{/truthy}
{/people}
{#people name="chris" pet=petname petname="whiskers"}
{#other_petname}
Hello {name}, nice {pet}{~n}
{/other_petname}
{/people}

View File

@ -110,13 +110,14 @@ pub struct Partial<'a> {
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum OwnedLiteral { pub enum OwnedLiteral {
LString(String),
LPositiveInteger(u64), LPositiveInteger(u64),
LString(String),
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum RValue<'a> { pub enum RValue<'a> {
RVPath(Path<'a>), RVPath(Path<'a>),
// RVTemplate(Vec<PartialNameElement>),
RVLiteral(OwnedLiteral), RVLiteral(OwnedLiteral),
} }
@ -308,6 +309,20 @@ fn postitive_integer_literal(i: &str) -> IResult<&str, u64> {
)(i) )(i)
} }
fn template_string_rvalue(i: &str) -> IResult<&str, Vec<PartialNameElement>> {
let (i, template_string) = verify(quoted_string, |s: &String| {
partial_quoted_tag(s.as_str()).is_ok()
})(i)?;
let (_remaining, parsed_template_elements) = partial_quoted_tag(template_string.as_str())
.expect("A successful parse was verified earlier with a call to verify()");
let converted_template_elements = parsed_template_elements
.into_iter()
.map(|e| e.into())
.collect();
Ok((i, converted_template_elements))
}
/// Either a literal or a path to a value /// Either a literal or a path to a value
fn rvalue(i: &str) -> IResult<&str, RValue> { fn rvalue(i: &str) -> IResult<&str, RValue> {
alt(( alt((
@ -528,9 +543,7 @@ fn partial_with_quoted_tag<'a>(
let (i, (name, maybe_explicit_context, params)) = delimited( let (i, (name, maybe_explicit_context, params)) = delimited(
tag(open_matcher), tag(open_matcher),
tuple(( tuple((
verify(quoted_string, |s: &String| { template_string_rvalue,
partial_quoted_tag(s.as_str()).is_ok()
}),
opt(preceded(tag(":"), path)), opt(preceded(tag(":"), path)),
opt(delimited( opt(delimited(
space1, space1,
@ -541,17 +554,10 @@ fn partial_with_quoted_tag<'a>(
tag("/}"), tag("/}"),
)(i)?; )(i)?;
let (_remaining, template_name_elements) = partial_quoted_tag(name.as_str())
.expect("A successful parse was verified earlier with a call to verify()");
let partial_name_elements = template_name_elements
.into_iter()
.map(|e| e.into())
.collect();
Ok(( Ok((
i, i,
Partial { Partial {
name: partial_name_elements, name: name,
explicit_context: maybe_explicit_context, explicit_context: maybe_explicit_context,
params: params.unwrap_or(Vec::new()), params: params.unwrap_or(Vec::new()),
}, },