Parse self-references and add a test for sections.

This commit is contained in:
Tom Alexander 2020-04-28 19:02:43 -04:00
parent 71181fbd9a
commit 41ad6179d1
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
14 changed files with 26 additions and 1 deletions

View File

@ -0,0 +1 @@
{"names": ["Alice", "Bob", "Chris"]}

View File

@ -0,0 +1,3 @@
{#names}
Hello {.}!
{/names}

View File

@ -0,0 +1 @@
{"things": ["Alice", "Bob", "Chris"]}

View File

@ -0,0 +1 @@
{"things": {}}

View File

@ -0,0 +1 @@
{"things": {"name": "Alice", "keyboard": "K-Type"}}

View File

@ -0,0 +1 @@
{"there_are_no_things": 4}

View File

@ -0,0 +1 @@
{"things": "just a string"}

View File

@ -0,0 +1 @@
{"things": false}

View File

@ -0,0 +1 @@
{"things": null}

View File

@ -0,0 +1 @@
{"things": 0}

View File

@ -0,0 +1 @@
{"things": ""}

View File

@ -0,0 +1 @@
{"things": []}

View File

@ -0,0 +1,5 @@
{#things}
Thing: {.}
{:else}
No things
{/things}

View File

@ -58,6 +58,9 @@ pub struct Comment<'a> {
value: &'a str,
}
/// A series of keys separated by '.' to reference a variable in the context
///
/// Special case: If the path is just "." then keys will be an empty vec
#[derive(Clone, Debug, PartialEq)]
pub struct Path<'a> {
pub keys: Vec<&'a str>,
@ -197,7 +200,10 @@ fn key(i: &str) -> IResult<&str, &str> {
/// A series of keys separated by '.' to reference a variable in the context
fn path(i: &str) -> IResult<&str, Path> {
map(separated_list1(tag("."), key), |body| Path { keys: body })(i)
alt((
map(separated_list1(tag("."), key), |body| Path { keys: body }),
value(Path { keys: Vec::new() }, tag(".")),
))(i)
}
/// Either a literal or a path to a value