From de4f42062757b5ebcc58b828f12738e81709faba Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 5 Apr 2020 19:34:46 -0400 Subject: [PATCH] separate out the parser for key because partials don't use a full path, only a key --- src/parser/parser.rs | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/parser/parser.rs b/src/parser/parser.rs index b2d9add..3ecff88 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -91,6 +91,9 @@ enum TemplateElement<'a> { TETag(DustTag<'a>), } +/// Any element significant to dust that isn't plain text +/// +/// These elements are always wrapped in curly braces fn dust_tag(i: &str) -> IResult<&str, DustTag> { alt(( map(special, DustTag::DTSpecial), @@ -102,6 +105,7 @@ fn dust_tag(i: &str) -> IResult<&str, DustTag> { ))(i) } +/// Special characters fn special(i: &str) -> IResult<&str, Special> { delimited( tag("{~"), @@ -116,27 +120,29 @@ fn special(i: &str) -> IResult<&str, Special> { )(i) } +/// Part of a dust template that does not get rendered fn comment(i: &str) -> IResult<&str, Comment> { map(delimited(tag("{!"), take_until("!}"), tag("!}")), |body| { Comment { value: body } })(i) } -fn path(i: &str) -> IResult<&str, Path> { - map( - separated_list( - tag("."), - recognize(tuple(( - one_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$"), - opt(is_a( - "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$0123456789-", - )), - ))), - ), - |body| Path { keys: body }, - )(i) +/// A single element of a path +fn key(i: &str) -> IResult<&str, &str> { + recognize(tuple(( + one_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$"), + opt(is_a( + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$0123456789-", + )), + )))(i) } +/// A series of keys separated by '.' to reference a variable in the context +fn path(i: &str) -> IResult<&str, Path> { + map(separated_list(tag("."), key), |body| Path { keys: body })(i) +} + +/// Display a value from the context fn reference(i: &str) -> IResult<&str, Reference> { let (remaining, (p, filters)) = delimited(tag("{"), tuple((path, many0(filter))), tag("}"))(i)?; Ok((