diff options
author | vallentin <mail@vallentin.dev> | 2021-02-23 08:48:37 +0100 |
---|---|---|
committer | Christian Vallentin <vallentinsource@gmail.com> | 2021-02-23 09:51:27 +0100 |
commit | 5a799af0900882414b2cf5e642e88a3e36845400 (patch) | |
tree | 6f0c5f7523aceb18e1480196304c728444754ace /askama_shared/src/parser.rs | |
parent | bfeaf5d6d8c1be478971c428734775cb9ef950c2 (diff) | |
download | askama-5a799af0900882414b2cf5e642e88a3e36845400.tar.gz askama-5a799af0900882414b2cf5e642e88a3e36845400.tar.bz2 askama-5a799af0900882414b2cf5e642e88a3e36845400.zip |
Reworked constants to be parsed as paths
Diffstat (limited to 'askama_shared/src/parser.rs')
-rw-r--r-- | askama_shared/src/parser.rs | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/askama_shared/src/parser.rs b/askama_shared/src/parser.rs index df3ee8f..ea13738 100644 --- a/askama_shared/src/parser.rs +++ b/askama_shared/src/parser.rs @@ -367,9 +367,11 @@ fn path(i: &[u8]) -> IResult<&[u8], Vec<&str>> { } Err(err) => { if let Ok((i, name)) = identifier(i) { - // If the returned identifier contains both a lowercase and uppercase - // character, then we assume it's a type name, e.g. `Some`. - if name.contains(char::is_uppercase) && name.contains(char::is_lowercase) { + // The returned identifier can be assumed to be path if: + // - Contains both a lowercase and uppercase character, i.e. a type name like `None` + // - Doesn't contain any lowercase characters, i.e. it's a constant + // In short, if it contains any uppercase characters it's a path. + if name.contains(char::is_uppercase) { return Ok((i, vec![name])); } } @@ -1235,17 +1237,32 @@ mod tests { vec![Node::Expr(Ws(false, false), Expr::Var("foo"))], ); assert_eq!( - super::parse("{{ FOO }}", &s).unwrap(), - vec![Node::Expr(Ws(false, false), Expr::Var("FOO"))], + super::parse("{{ foo_bar }}", &s).unwrap(), + vec![Node::Expr(Ws(false, false), Expr::Var("foo_bar"))], ); assert_eq!( super::parse("{{ none }}", &s).unwrap(), vec![Node::Expr(Ws(false, false), Expr::Var("none"))], ); + } + + #[test] + fn test_parse_const() { + let s = Syntax::default(); + + assert_eq!( + super::parse("{{ FOO }}", &s).unwrap(), + vec![Node::Expr(Ws(false, false), Expr::Path(vec!["FOO"]))], + ); + assert_eq!( + super::parse("{{ FOO_BAR }}", &s).unwrap(), + vec![Node::Expr(Ws(false, false), Expr::Path(vec!["FOO_BAR"]))], + ); + assert_eq!( super::parse("{{ NONE }}", &s).unwrap(), - vec![Node::Expr(Ws(false, false), Expr::Var("NONE"))], + vec![Node::Expr(Ws(false, false), Expr::Path(vec!["NONE"]))], ); } @@ -1264,6 +1281,7 @@ mod tests { Expr::PathCall(vec!["Some"], vec![Expr::NumLit("123")],), )], ); + assert_eq!( super::parse("{{ Ok(123) }}", &s).unwrap(), vec![Node::Expr( |