aboutsummaryrefslogtreecommitdiffstats
path: root/askama_parser
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-07-02 11:28:16 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-07-31 10:27:15 +0200
commitfaf2e34cbf2e2049f2f473005e3c28dcc0d0a949 (patch)
treee77f04e31593ff380f339269e618b4e563c9f9c9 /askama_parser
parent2376cc6815a83aec7b0b76a671fabe4c22441aea (diff)
downloadaskama-faf2e34cbf2e2049f2f473005e3c28dcc0d0a949.tar.gz
askama-faf2e34cbf2e2049f2f473005e3c28dcc0d0a949.tar.bz2
askama-faf2e34cbf2e2049f2f473005e3c28dcc0d0a949.zip
parser: add top-level Ast type
Diffstat (limited to '')
-rw-r--r--askama_parser/src/lib.rs73
-rw-r--r--askama_parser/src/tests.rs190
2 files changed, 148 insertions, 115 deletions
diff --git a/askama_parser/src/lib.rs b/askama_parser/src/lib.rs
index 970fb2e..9559ee0 100644
--- a/askama_parser/src/lib.rs
+++ b/askama_parser/src/lib.rs
@@ -25,12 +25,12 @@ mod tests;
mod _parsed {
use std::mem;
- use super::{parse, Node, ParseError, Syntax};
+ use super::{Ast, Node, ParseError, Syntax};
pub struct Parsed {
#[allow(dead_code)]
source: String,
- nodes: Vec<Node<'static>>,
+ ast: Ast<'static>,
}
impl Parsed {
@@ -39,57 +39,64 @@ mod _parsed {
// internally we will transmute it to `&'static str` to satisfy the compiler.
// However, we only expose the nodes with a lifetime limited to `self`.
let src = unsafe { mem::transmute::<&str, &'static str>(source.as_str()) };
- let nodes = match parse(src, syntax) {
- Ok(nodes) => nodes,
+ let ast = match Ast::from_str(src, syntax) {
+ Ok(ast) => ast,
Err(e) => return Err(e),
};
- Ok(Self { source, nodes })
+ Ok(Self { source, ast })
}
// The return value's lifetime must be limited to `self` to uphold the unsafe invariant.
pub fn nodes(&self) -> &[Node<'_>] {
- &self.nodes
+ &self.ast.nodes
}
}
}
pub use _parsed::Parsed;
-pub fn parse<'a>(src: &'a str, syntax: &Syntax<'_>) -> Result<Vec<Node<'a>>, ParseError> {
- match Node::parse(src, &State::new(syntax)) {
- Ok((left, res)) => {
- if !left.is_empty() {
- Err(ParseError(format!("unable to parse template:\n\n{left:?}")))
- } else {
- Ok(res)
+#[derive(Debug)]
+pub struct Ast<'a> {
+ nodes: Vec<Node<'a>>,
+}
+
+impl<'a> Ast<'a> {
+ pub fn from_str(src: &'a str, syntax: &Syntax<'_>) -> Result<Self, ParseError> {
+ match Node::parse(src, &State::new(syntax)) {
+ Ok((left, nodes)) => {
+ if !left.is_empty() {
+ Err(ParseError(format!("unable to parse template:\n\n{left:?}")))
+ } else {
+ Ok(Self { nodes })
+ }
}
- }
- Err(nom::Err::Error(err)) | Err(nom::Err::Failure(err)) => {
- let nom::error::Error { input, .. } = err;
- let offset = src.len() - input.len();
- let (source_before, source_after) = src.split_at(offset);
+ Err(nom::Err::Error(err)) | Err(nom::Err::Failure(err)) => {
+ let nom::error::Error { input, .. } = err;
+ let offset = src.len() - input.len();
+ let (source_before, source_after) = src.split_at(offset);
- let source_after = match source_after.char_indices().enumerate().take(41).last() {
- Some((40, (i, _))) => format!("{:?}...", &source_after[..i]),
- _ => format!("{source_after:?}"),
- };
+ let source_after = match source_after.char_indices().enumerate().take(41).last() {
+ Some((40, (i, _))) => format!("{:?}...", &source_after[..i]),
+ _ => format!("{source_after:?}"),
+ };
- let (row, last_line) = source_before.lines().enumerate().last().unwrap();
- let column = last_line.chars().count();
+ let (row, last_line) = source_before.lines().enumerate().last().unwrap();
+ let column = last_line.chars().count();
- let msg = format!(
- "problems parsing template source at row {}, column {} near:\n{}",
- row + 1,
- column,
- source_after,
- );
+ let msg = format!(
+ "problems parsing template source at row {}, column {} near:\n{}",
+ row + 1,
+ column,
+ source_after,
+ );
- Err(ParseError(msg))
- }
+ Err(ParseError(msg))
+ }
- Err(nom::Err::Incomplete(_)) => Err(ParseError("parsing incomplete".into())),
+ Err(nom::Err::Incomplete(_)) => Err(ParseError("parsing incomplete".into())),
+ }
}
}
diff --git a/askama_parser/src/tests.rs b/askama_parser/src/tests.rs
index 0e785eb..5b3b66e 100644
--- a/askama_parser/src/tests.rs
+++ b/askama_parser/src/tests.rs
@@ -1,4 +1,4 @@
-use super::{Expr, Node, Syntax, Whitespace, Ws};
+use super::{Ast, Expr, Node, Syntax, Whitespace, Ws};
fn check_ws_split(s: &str, res: &(&str, &str, &str)) {
match super::split_ws_parts(s) {
@@ -25,7 +25,7 @@ fn test_ws_splitter() {
#[test]
#[should_panic]
fn test_invalid_block() {
- super::parse("{% extend \"blah\" %}", &Syntax::default()).unwrap();
+ Ast::from_str("{% extend \"blah\" %}", &Syntax::default()).unwrap();
}
#[test]
@@ -33,22 +33,22 @@ fn test_parse_filter() {
use Expr::*;
let syntax = Syntax::default();
assert_eq!(
- super::parse("{{ strvar|e }}", &syntax).unwrap(),
+ Ast::from_str("{{ strvar|e }}", &syntax).unwrap().nodes,
vec![Node::Expr(Ws(None, None), Filter("e", vec![Var("strvar")]),)],
);
assert_eq!(
- super::parse("{{ 2|abs }}", &syntax).unwrap(),
+ Ast::from_str("{{ 2|abs }}", &syntax).unwrap().nodes,
vec![Node::Expr(Ws(None, None), Filter("abs", vec![NumLit("2")]),)],
);
assert_eq!(
- super::parse("{{ -2|abs }}", &syntax).unwrap(),
+ Ast::from_str("{{ -2|abs }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
Filter("abs", vec![Unary("-", NumLit("2").into())]),
)],
);
assert_eq!(
- super::parse("{{ (1 - 2)|abs }}", &syntax).unwrap(),
+ Ast::from_str("{{ (1 - 2)|abs }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
Filter(
@@ -65,11 +65,11 @@ fn test_parse_filter() {
fn test_parse_numbers() {
let syntax = Syntax::default();
assert_eq!(
- super::parse("{{ 2 }}", &syntax).unwrap(),
+ Ast::from_str("{{ 2 }}", &syntax).unwrap().nodes,
vec![Node::Expr(Ws(None, None), Expr::NumLit("2"),)],
);
assert_eq!(
- super::parse("{{ 2.5 }}", &syntax).unwrap(),
+ Ast::from_str("{{ 2.5 }}", &syntax).unwrap().nodes,
vec![Node::Expr(Ws(None, None), Expr::NumLit("2.5"),)],
);
}
@@ -79,16 +79,16 @@ fn test_parse_var() {
let s = Syntax::default();
assert_eq!(
- super::parse("{{ foo }}", &s).unwrap(),
+ Ast::from_str("{{ foo }}", &s).unwrap().nodes,
vec![Node::Expr(Ws(None, None), Expr::Var("foo"))],
);
assert_eq!(
- super::parse("{{ foo_bar }}", &s).unwrap(),
+ Ast::from_str("{{ foo_bar }}", &s).unwrap().nodes,
vec![Node::Expr(Ws(None, None), Expr::Var("foo_bar"))],
);
assert_eq!(
- super::parse("{{ none }}", &s).unwrap(),
+ Ast::from_str("{{ none }}", &s).unwrap().nodes,
vec![Node::Expr(Ws(None, None), Expr::Var("none"))],
);
}
@@ -98,16 +98,16 @@ fn test_parse_const() {
let s = Syntax::default();
assert_eq!(
- super::parse("{{ FOO }}", &s).unwrap(),
+ Ast::from_str("{{ FOO }}", &s).unwrap().nodes,
vec![Node::Expr(Ws(None, None), Expr::Path(vec!["FOO"]))],
);
assert_eq!(
- super::parse("{{ FOO_BAR }}", &s).unwrap(),
+ Ast::from_str("{{ FOO_BAR }}", &s).unwrap().nodes,
vec![Node::Expr(Ws(None, None), Expr::Path(vec!["FOO_BAR"]))],
);
assert_eq!(
- super::parse("{{ NONE }}", &s).unwrap(),
+ Ast::from_str("{{ NONE }}", &s).unwrap().nodes,
vec![Node::Expr(Ws(None, None), Expr::Path(vec!["NONE"]))],
);
}
@@ -117,11 +117,11 @@ fn test_parse_path() {
let s = Syntax::default();
assert_eq!(
- super::parse("{{ None }}", &s).unwrap(),
+ Ast::from_str("{{ None }}", &s).unwrap().nodes,
vec![Node::Expr(Ws(None, None), Expr::Path(vec!["None"]))],
);
assert_eq!(
- super::parse("{{ Some(123) }}", &s).unwrap(),
+ Ast::from_str("{{ Some(123) }}", &s).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
Expr::Call(
@@ -132,14 +132,14 @@ fn test_parse_path() {
);
assert_eq!(
- super::parse("{{ Ok(123) }}", &s).unwrap(),
+ Ast::from_str("{{ Ok(123) }}", &s).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
Expr::Call(Box::new(Expr::Path(vec!["Ok"])), vec![Expr::NumLit("123")]),
)],
);
assert_eq!(
- super::parse("{{ Err(123) }}", &s).unwrap(),
+ Ast::from_str("{{ Err(123) }}", &s).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
Expr::Call(Box::new(Expr::Path(vec!["Err"])), vec![Expr::NumLit("123")]),
@@ -150,7 +150,9 @@ fn test_parse_path() {
#[test]
fn test_parse_var_call() {
assert_eq!(
- super::parse("{{ function(\"123\", 3) }}", &Syntax::default()).unwrap(),
+ Ast::from_str("{{ function(\"123\", 3) }}", &Syntax::default())
+ .unwrap()
+ .nodes,
vec![Node::Expr(
Ws(None, None),
Expr::Call(
@@ -166,14 +168,14 @@ fn test_parse_path_call() {
let s = Syntax::default();
assert_eq!(
- super::parse("{{ Option::None }}", &s).unwrap(),
+ Ast::from_str("{{ Option::None }}", &s).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
Expr::Path(vec!["Option", "None"])
)],
);
assert_eq!(
- super::parse("{{ Option::Some(123) }}", &s).unwrap(),
+ Ast::from_str("{{ Option::Some(123) }}", &s).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
Expr::Call(
@@ -184,7 +186,9 @@ fn test_parse_path_call() {
);
assert_eq!(
- super::parse("{{ self::function(\"123\", 3) }}", &s).unwrap(),
+ Ast::from_str("{{ self::function(\"123\", 3) }}", &s)
+ .unwrap()
+ .nodes,
vec![Node::Expr(
Ws(None, None),
Expr::Call(
@@ -199,7 +203,9 @@ fn test_parse_path_call() {
fn test_parse_root_path() {
let syntax = Syntax::default();
assert_eq!(
- super::parse("{{ std::string::String::new() }}", &syntax).unwrap(),
+ Ast::from_str("{{ std::string::String::new() }}", &syntax)
+ .unwrap()
+ .nodes,
vec![Node::Expr(
Ws(None, None),
Expr::Call(
@@ -209,7 +215,9 @@ fn test_parse_root_path() {
)],
);
assert_eq!(
- super::parse("{{ ::std::string::String::new() }}", &syntax).unwrap(),
+ Ast::from_str("{{ ::std::string::String::new() }}", &syntax)
+ .unwrap()
+ .nodes,
vec![Node::Expr(
Ws(None, None),
Expr::Call(
@@ -224,41 +232,43 @@ fn test_parse_root_path() {
fn test_rust_macro() {
let syntax = Syntax::default();
assert_eq!(
- super::parse("{{ vec!(1, 2, 3) }}", &syntax).unwrap(),
+ Ast::from_str("{{ vec!(1, 2, 3) }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
Expr::RustMacro(vec!["vec"], "1, 2, 3",),
)],
);
assert_eq!(
- super::parse("{{ alloc::vec!(1, 2, 3) }}", &syntax).unwrap(),
+ Ast::from_str("{{ alloc::vec!(1, 2, 3) }}", &syntax)
+ .unwrap()
+ .nodes,
vec![Node::Expr(
Ws(None, None),
Expr::RustMacro(vec!["alloc", "vec"], "1, 2, 3",),
)],
);
assert_eq!(
- super::parse("{{a!()}}", &syntax).unwrap(),
+ Ast::from_str("{{a!()}}", &syntax).unwrap().nodes,
[Node::Expr(Ws(None, None), Expr::RustMacro(vec!["a"], ""))],
);
assert_eq!(
- super::parse("{{a !()}}", &syntax).unwrap(),
+ Ast::from_str("{{a !()}}", &syntax).unwrap().nodes,
[Node::Expr(Ws(None, None), Expr::RustMacro(vec!["a"], ""))],
);
assert_eq!(
- super::parse("{{a! ()}}", &syntax).unwrap(),
+ Ast::from_str("{{a! ()}}", &syntax).unwrap().nodes,
[Node::Expr(Ws(None, None), Expr::RustMacro(vec!["a"], ""))],
);
assert_eq!(
- super::parse("{{a ! ()}}", &syntax).unwrap(),
+ Ast::from_str("{{a ! ()}}", &syntax).unwrap().nodes,
[Node::Expr(Ws(None, None), Expr::RustMacro(vec!["a"], ""))],
);
assert_eq!(
- super::parse("{{A!()}}", &syntax).unwrap(),
+ Ast::from_str("{{A!()}}", &syntax).unwrap().nodes,
[Node::Expr(Ws(None, None), Expr::RustMacro(vec!["A"], ""),)],
);
assert_eq!(
- &*super::parse("{{a.b.c!( hello )}}", &syntax)
+ &*Ast::from_str("{{a.b.c!( hello )}}", &syntax)
.unwrap_err()
.to_string(),
"problems parsing template source at row 1, column 7 near:\n\"!( hello )}}\"",
@@ -273,7 +283,7 @@ fn change_delimiters_parse_filter() {
..Syntax::default()
};
- super::parse("{= strvar|e =}", &syntax).unwrap();
+ Ast::from_str("{= strvar|e =}", &syntax).unwrap();
}
#[test]
@@ -281,7 +291,7 @@ fn test_precedence() {
use Expr::*;
let syntax = Syntax::default();
assert_eq!(
- super::parse("{{ a + b == c }}", &syntax).unwrap(),
+ Ast::from_str("{{ a + b == c }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
BinOp(
@@ -292,7 +302,9 @@ fn test_precedence() {
)],
);
assert_eq!(
- super::parse("{{ a + b * c - d / e }}", &syntax).unwrap(),
+ Ast::from_str("{{ a + b * c - d / e }}", &syntax)
+ .unwrap()
+ .nodes,
vec![Node::Expr(
Ws(None, None),
BinOp(
@@ -308,7 +320,9 @@ fn test_precedence() {
)],
);
assert_eq!(
- super::parse("{{ a * (b + c) / -d }}", &syntax).unwrap(),
+ Ast::from_str("{{ a * (b + c) / -d }}", &syntax)
+ .unwrap()
+ .nodes,
vec![Node::Expr(
Ws(None, None),
BinOp(
@@ -324,7 +338,9 @@ fn test_precedence() {
)],
);
assert_eq!(
- super::parse("{{ a || b && c || d && e }}", &syntax).unwrap(),
+ Ast::from_str("{{ a || b && c || d && e }}", &syntax)
+ .unwrap()
+ .nodes,
vec![Node::Expr(
Ws(None, None),
BinOp(
@@ -346,7 +362,7 @@ fn test_associativity() {
use Expr::*;
let syntax = Syntax::default();
assert_eq!(
- super::parse("{{ a + b + c }}", &syntax).unwrap(),
+ Ast::from_str("{{ a + b + c }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
BinOp(
@@ -357,7 +373,7 @@ fn test_associativity() {
)],
);
assert_eq!(
- super::parse("{{ a * b * c }}", &syntax).unwrap(),
+ Ast::from_str("{{ a * b * c }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
BinOp(
@@ -368,7 +384,7 @@ fn test_associativity() {
)],
);
assert_eq!(
- super::parse("{{ a && b && c }}", &syntax).unwrap(),
+ Ast::from_str("{{ a && b && c }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
BinOp(
@@ -379,7 +395,7 @@ fn test_associativity() {
)],
);
assert_eq!(
- super::parse("{{ a + b - c + d }}", &syntax).unwrap(),
+ Ast::from_str("{{ a + b - c + d }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
BinOp(
@@ -395,7 +411,9 @@ fn test_associativity() {
)],
);
assert_eq!(
- super::parse("{{ a == b != c > d > e == f }}", &syntax).unwrap(),
+ Ast::from_str("{{ a == b != c > d > e == f }}", &syntax)
+ .unwrap()
+ .nodes,
vec![Node::Expr(
Ws(None, None),
BinOp(
@@ -427,7 +445,7 @@ fn test_odd_calls() {
use Expr::*;
let syntax = Syntax::default();
assert_eq!(
- super::parse("{{ a[b](c) }}", &syntax).unwrap(),
+ Ast::from_str("{{ a[b](c) }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
Call(
@@ -437,7 +455,7 @@ fn test_odd_calls() {
)],
);
assert_eq!(
- super::parse("{{ (a + b)(c) }}", &syntax).unwrap(),
+ Ast::from_str("{{ (a + b)(c) }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
Call(
@@ -451,7 +469,7 @@ fn test_odd_calls() {
)],
);
assert_eq!(
- super::parse("{{ a + b(c) }}", &syntax).unwrap(),
+ Ast::from_str("{{ a + b(c) }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
BinOp(
@@ -462,7 +480,7 @@ fn test_odd_calls() {
)],
);
assert_eq!(
- super::parse("{{ (-a)(b) }}", &syntax).unwrap(),
+ Ast::from_str("{{ (-a)(b) }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
Call(
@@ -472,7 +490,7 @@ fn test_odd_calls() {
)],
);
assert_eq!(
- super::parse("{{ -a(b) }}", &syntax).unwrap(),
+ Ast::from_str("{{ -a(b) }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
Unary("-", Box::new(Call(Box::new(Var("a")), vec![Var("b")])),),
@@ -485,91 +503,97 @@ fn test_parse_comments() {
let s = &Syntax::default();
assert_eq!(
- super::parse("{##}", s).unwrap(),
+ Ast::from_str("{##}", s).unwrap().nodes,
vec![Node::Comment(Ws(None, None))],
);
assert_eq!(
- super::parse("{#- #}", s).unwrap(),
+ Ast::from_str("{#- #}", s).unwrap().nodes,
vec![Node::Comment(Ws(Some(Whitespace::Suppress), None))],
);
assert_eq!(
- super::parse("{# -#}", s).unwrap(),
+ Ast::from_str("{# -#}", s).unwrap().nodes,
vec![Node::Comment(Ws(None, Some(Whitespace::Suppress)))],
);
assert_eq!(
- super::parse("{#--#}", s).unwrap(),
+ Ast::from_str("{#--#}", s).unwrap().nodes,
vec![Node::Comment(Ws(
Some(Whitespace::Suppress),
Some(Whitespace::Suppress)
))],
);
assert_eq!(
- super::parse("{#- foo\n bar -#}", s).unwrap(),
+ Ast::from_str("{#- foo\n bar -#}", s).unwrap().nodes,
vec![Node::Comment(Ws(
Some(Whitespace::Suppress),
Some(Whitespace::Suppress)
))],
);
assert_eq!(
- super::parse("{#- foo\n {#- bar\n -#} baz -#}", s).unwrap(),
+ Ast::from_str("{#- foo\n {#- bar\n -#} baz -#}", s)
+ .unwrap()
+ .nodes,
vec![Node::Comment(Ws(
Some(Whitespace::Suppress),
Some(Whitespace::Suppress)
))],
);
assert_eq!(
- super::parse("{#+ #}", s).unwrap(),
+ Ast::from_str("{#+ #}", s).unwrap().nodes,
vec![Node::Comment(Ws(Some(Whitespace::Preserve), None))],
);
assert_eq!(
- super::parse("{# +#}", s).unwrap(),
+ Ast::from_str("{# +#}", s).unwrap().nodes,
vec![Node::Comment(Ws(None, Some(Whitespace::Preserve)))],
);
assert_eq!(
- super::parse("{#++#}", s).unwrap(),
+ Ast::from_str("{#++#}", s).unwrap().nodes,
vec![Node::Comment(Ws(
Some(Whitespace::Preserve),
Some(Whitespace::Preserve)
))],
);
assert_eq!(
- super::parse("{#+ foo\n bar +#}", s).unwrap(),
+ Ast::from_str("{#+ foo\n bar +#}", s).unwrap().nodes,
vec![Node::Comment(Ws(
Some(Whitespace::Preserve),
Some(Whitespace::Preserve)
))],
);
assert_eq!(
- super::parse("{#+ foo\n {#+ bar\n +#} baz -+#}", s).unwrap(),
+ Ast::from_str("{#+ foo\n {#+ bar\n +#} baz -+#}", s)
+ .unwrap()
+ .nodes,
vec![Node::Comment(Ws(
Some(Whitespace::Preserve),
Some(Whitespace::Preserve)
))],
);
assert_eq!(
- super::parse("{#~ #}", s).unwrap(),
+ Ast::from_str("{#~ #}", s).unwrap().nodes,
vec![Node::Comment(Ws(Some(Whitespace::Minimize), None))],
);
assert_eq!(
- super::parse("{# ~#}", s).unwrap(),
+ Ast::from_str("{# ~#}", s).unwrap().nodes,
vec![Node::Comment(Ws(None, Some(Whitespace::Minimize)))],
);
assert_eq!(
- super::parse("{#~~#}", s).unwrap(),
+ Ast::from_str("{#~~#}", s).unwrap().nodes,
vec![Node::Comment(Ws(
Some(Whitespace::Minimize),
Some(Whitespace::Minimize)
))],
);
assert_eq!(
- super::parse("{#~ foo\n bar ~#}", s).unwrap(),
+ Ast::from_str("{#~ foo\n bar ~#}", s).unwrap().nodes,
vec![Node::Comment(Ws(
Some(Whitespace::Minimize),
Some(Whitespace::Minimize)
))],
);
assert_eq!(
- super::parse("{#~ foo\n {#~ bar\n ~#} baz -~#}", s).unwrap(),
+ Ast::from_str("{#~ foo\n {#~ bar\n ~#} baz -~#}", s)
+ .unwrap()
+ .nodes,
vec![Node::Comment(Ws(
Some(Whitespace::Minimize),
Some(Whitespace::Minimize)
@@ -577,7 +601,9 @@ fn test_parse_comments() {
);
assert_eq!(
- super::parse("{# foo {# bar #} {# {# baz #} qux #} #}", s).unwrap(),
+ Ast::from_str("{# foo {# bar #} {# {# baz #} qux #} #}", s)
+ .unwrap()
+ .nodes,
vec![Node::Comment(Ws(None, None))],
);
}
@@ -587,73 +613,73 @@ fn test_parse_tuple() {
use super::Expr::*;
let syntax = Syntax::default();
assert_eq!(
- super::parse("{{ () }}", &syntax).unwrap(),
+ Ast::from_str("{{ () }}", &syntax).unwrap().nodes,
vec![Node::Expr(Ws(None, None), Tuple(vec![]),)],
);
assert_eq!(
- super::parse("{{ (1) }}", &syntax).unwrap(),
+ Ast::from_str("{{ (1) }}", &syntax).unwrap().nodes,
vec![Node::Expr(Ws(None, None), Group(Box::new(NumLit("1"))),)],
);
assert_eq!(
- super::parse("{{ (1,) }}", &syntax).unwrap(),
+ Ast::from_str("{{ (1,) }}", &syntax).unwrap().nodes,
vec![Node::Expr(Ws(None, None), Tuple(vec![NumLit("1")]),)],
);
assert_eq!(
- super::parse("{{ (1, ) }}", &syntax).unwrap(),
+ Ast::from_str("{{ (1, ) }}", &syntax).unwrap().nodes,
vec![Node::Expr(Ws(None, None), Tuple(vec![NumLit("1")]),)],
);
assert_eq!(
- super::parse("{{ (1 ,) }}", &syntax).unwrap(),
+ Ast::from_str("{{ (1 ,) }}", &syntax).unwrap().nodes,
vec![Node::Expr(Ws(None, None), Tuple(vec![NumLit("1")]),)],
);
assert_eq!(
- super::parse("{{ (1 , ) }}", &syntax).unwrap(),
+ Ast::from_str("{{ (1 , ) }}", &syntax).unwrap().nodes,
vec![Node::Expr(Ws(None, None), Tuple(vec![NumLit("1")]),)],
);
assert_eq!(
- super::parse("{{ (1, 2) }}", &syntax).unwrap(),
+ Ast::from_str("{{ (1, 2) }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
Tuple(vec![NumLit("1"), NumLit("2")]),
)],
);
assert_eq!(
- super::parse("{{ (1, 2,) }}", &syntax).unwrap(),
+ Ast::from_str("{{ (1, 2,) }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
Tuple(vec![NumLit("1"), NumLit("2")]),
)],
);
assert_eq!(
- super::parse("{{ (1, 2, 3) }}", &syntax).unwrap(),
+ Ast::from_str("{{ (1, 2, 3) }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
Tuple(vec![NumLit("1"), NumLit("2"), NumLit("3")]),
)],
);
assert_eq!(
- super::parse("{{ ()|abs }}", &syntax).unwrap(),
+ Ast::from_str("{{ ()|abs }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
Filter("abs", vec![Tuple(vec![])]),
)],
);
assert_eq!(
- super::parse("{{ () | abs }}", &syntax).unwrap(),
+ Ast::from_str("{{ () | abs }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
BinOp("|", Box::new(Tuple(vec![])), Box::new(Var("abs"))),
)],
);
assert_eq!(
- super::parse("{{ (1)|abs }}", &syntax).unwrap(),
+ Ast::from_str("{{ (1)|abs }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
Filter("abs", vec![Group(Box::new(NumLit("1")))]),
)],
);
assert_eq!(
- super::parse("{{ (1) | abs }}", &syntax).unwrap(),
+ Ast::from_str("{{ (1) | abs }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
BinOp(
@@ -664,14 +690,14 @@ fn test_parse_tuple() {
)],
);
assert_eq!(
- super::parse("{{ (1,)|abs }}", &syntax).unwrap(),
+ Ast::from_str("{{ (1,)|abs }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
Filter("abs", vec![Tuple(vec![NumLit("1")])]),
)],
);
assert_eq!(
- super::parse("{{ (1,) | abs }}", &syntax).unwrap(),
+ Ast::from_str("{{ (1,) | abs }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
BinOp(
@@ -682,14 +708,14 @@ fn test_parse_tuple() {
)],
);
assert_eq!(
- super::parse("{{ (1, 2)|abs }}", &syntax).unwrap(),
+ Ast::from_str("{{ (1, 2)|abs }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
Filter("abs", vec![Tuple(vec![NumLit("1"), NumLit("2")])]),
)],
);
assert_eq!(
- super::parse("{{ (1, 2) | abs }}", &syntax).unwrap(),
+ Ast::from_str("{{ (1, 2) | abs }}", &syntax).unwrap().nodes,
vec![Node::Expr(
Ws(None, None),
BinOp(
@@ -704,7 +730,7 @@ fn test_parse_tuple() {
#[test]
fn test_missing_space_after_kw() {
let syntax = Syntax::default();
- let err = super::parse("{%leta=b%}", &syntax).unwrap_err();
+ let err = Ast::from_str("{%leta=b%}", &syntax).unwrap_err();
assert!(matches!(
&*err.to_string(),
"unable to parse template:\n\n\"{%leta=b%}\""