diff options
author | Christian Vallentin <mail@vallentin.dev> | 2020-12-02 20:46:29 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-02 20:46:29 +0100 |
commit | 810d5ad5067085cc5e4be19903b5804b1ff13430 (patch) | |
tree | 1f0607e8b590f3733e17136aa77fc12fe8394b6a /askama_shared/src/parser.rs | |
parent | a199defeca2dfc6aa3e972acca82c96db07f99e9 (diff) | |
download | askama-810d5ad5067085cc5e4be19903b5804b1ff13430.tar.gz askama-810d5ad5067085cc5e4be19903b5804b1ff13430.tar.bz2 askama-810d5ad5067085cc5e4be19903b5804b1ff13430.zip |
Allow paths to start with `::` (#393)
Diffstat (limited to '')
-rw-r--r-- | askama_shared/src/parser.rs | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/askama_shared/src/parser.rs b/askama_shared/src/parser.rs index 7224d02..8210ed0 100644 --- a/askama_shared/src/parser.rs +++ b/askama_shared/src/parser.rs @@ -1,7 +1,7 @@ use nom::branch::alt; use nom::bytes::complete::{escaped, is_not, tag, take_until}; use nom::character::complete::{anychar, char, digit1}; -use nom::combinator::{complete, map, opt}; +use nom::combinator::{complete, map, opt, value}; use nom::error::ParseError; use nom::multi::{many0, many1, separated_list0, separated_list1}; use nom::sequence::{delimited, pair, tuple}; @@ -312,10 +312,12 @@ fn expr_var_call(i: &[u8]) -> IResult<&[u8], Expr> { } fn path(i: &[u8]) -> IResult<&[u8], Vec<&str>> { + let root = opt(value("", ws(tag("::")))); let tail = separated_list1(ws(tag("::")), identifier); - let (i, (start, _, rest)) = tuple((identifier, ws(tag("::")), tail))(i)?; - - let mut path = vec![start]; + let (i, (root, start, _, rest)) = tuple((root, identifier, ws(tag("::")), tail))(i)?; + let mut path = Vec::new(); + path.extend(root); + path.push(start); path.extend(rest); Ok((i, path)) } @@ -1137,6 +1139,25 @@ mod tests { } #[test] + fn test_parse_root_path() { + let syntax = Syntax::default(); + assert_eq!( + super::parse("{{ std::string::String::new() }}", &syntax).unwrap(), + vec![Node::Expr( + WS(false, false), + Expr::PathCall(vec!["std", "string", "String", "new"], vec![]), + )], + ); + assert_eq!( + super::parse("{{ ::std::string::String::new() }}", &syntax).unwrap(), + vec![Node::Expr( + WS(false, false), + Expr::PathCall(vec!["", "std", "string", "String", "new"], vec![]), + )], + ); + } + + #[test] fn change_delimiters_parse_filter() { let syntax = Syntax { expr_start: "{~", |