aboutsummaryrefslogtreecommitdiffstats
path: root/askama_parser/src/node.rs
diff options
context:
space:
mode:
authorLibravatar Guillaume Gomez <guillaume1.gomez@gmail.com>2023-10-25 15:22:32 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-10-25 16:26:36 +0200
commit76462255ea6ce20cc13552164c24b34f729f9d0f (patch)
tree1751ddfcc74787e49314c68c51111dd2b586ac3d /askama_parser/src/node.rs
parent77d5d28b20162c522483ed009afba2ca9a6ee390 (diff)
downloadaskama-76462255ea6ce20cc13552164c24b34f729f9d0f.tar.gz
askama-76462255ea6ce20cc13552164c24b34f729f9d0f.tar.bz2
askama-76462255ea6ce20cc13552164c24b34f729f9d0f.zip
Create a type alias for all `IResult` to simplify code reading
Diffstat (limited to 'askama_parser/src/node.rs')
-rw-r--r--askama_parser/src/node.rs75
1 files changed, 33 insertions, 42 deletions
diff --git a/askama_parser/src/node.rs b/askama_parser/src/node.rs
index dc05f48..69703e6 100644
--- a/askama_parser/src/node.rs
+++ b/askama_parser/src/node.rs
@@ -8,11 +8,11 @@ use nom::combinator::{
complete, consumed, cut, eof, map, map_res, not, opt, peek, recognize, value,
};
use nom::error::{Error, ErrorKind};
+use nom::error_position;
use nom::multi::{fold_many0, many0, many1, separated_list0, separated_list1};
use nom::sequence::{delimited, pair, preceded, terminated, tuple};
-use nom::{error_position, IResult};
-use crate::ErrorContext;
+use crate::{ErrorContext, ParseResult};
use super::{
bool_lit, char_lit, identifier, is_ws, keyword, num_lit, path_or_identifier, skip_till,
@@ -40,10 +40,7 @@ pub enum Node<'a> {
}
impl<'a> Node<'a> {
- pub(super) fn many(
- i: &'a str,
- s: &State<'_>,
- ) -> IResult<&'a str, Vec<Self>, ErrorContext<&'a str>> {
+ pub(super) fn many(i: &'a str, s: &State<'_>) -> ParseResult<'a, Vec<Self>> {
complete(many0(alt((
map(|i| Lit::parse(i, s), Self::Lit),
map(|i| Comment::parse(i, s), Self::Comment),
@@ -52,7 +49,7 @@ impl<'a> Node<'a> {
))))(i)
}
- fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
+ fn parse(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
let mut p = delimited(
|i| s.tag_block_start(i),
alt((
@@ -80,7 +77,7 @@ impl<'a> Node<'a> {
result
}
- fn r#break(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
+ fn r#break(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
let mut p = tuple((
opt(Whitespace::parse),
ws(keyword("break")),
@@ -93,7 +90,7 @@ impl<'a> Node<'a> {
Ok((j, Self::Break(Ws(pws, nws))))
}
- fn r#continue(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
+ fn r#continue(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
let mut p = tuple((
opt(Whitespace::parse),
ws(keyword("continue")),
@@ -106,7 +103,7 @@ impl<'a> Node<'a> {
Ok((j, Self::Continue(Ws(pws, nws))))
}
- fn expr(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
+ fn expr(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
let mut p = tuple((
|i| s.tag_expr_start(i),
cut(tuple((
@@ -134,7 +131,7 @@ pub enum Target<'a> {
}
impl<'a> Target<'a> {
- pub(super) fn parse(i: &'a str) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
+ pub(super) fn parse(i: &'a str) -> ParseResult<'a, Self> {
let mut opt_opening_paren = map(opt(ws(char('('))), |o| o.is_some());
let mut opt_closing_paren = map(opt(ws(char(')'))), |o| o.is_some());
let mut opt_opening_brace = map(opt(ws(char('{'))), |o| o.is_some());
@@ -217,7 +214,7 @@ impl<'a> Target<'a> {
map(identifier, Self::Name)(i)
}
- fn lit(i: &'a str) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
+ fn lit(i: &'a str) -> ParseResult<'a, Self> {
alt((
map(str_lit, Self::StrLit),
map(char_lit, Self::CharLit),
@@ -226,7 +223,7 @@ impl<'a> Target<'a> {
))(i)
}
- fn named(i: &'a str) -> IResult<&str, (&str, Self), ErrorContext<&'a str>> {
+ fn named(i: &'a str) -> ParseResult<'a, (&str, Self)> {
let (i, (src, target)) = pair(identifier, opt(preceded(ws(char(':')), Self::parse)))(i)?;
Ok((i, (src, target.unwrap_or(Self::Name(src)))))
}
@@ -240,7 +237,7 @@ pub struct When<'a> {
}
impl<'a> When<'a> {
- fn r#match(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
+ fn r#match(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
let mut p = tuple((
|i| s.tag_block_start(i),
opt(Whitespace::parse),
@@ -263,7 +260,7 @@ impl<'a> When<'a> {
}
#[allow(clippy::self_named_constructors)]
- fn when(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
+ fn when(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
let mut p = tuple((
|i| s.tag_block_start(i),
opt(Whitespace::parse),
@@ -295,7 +292,7 @@ pub struct Cond<'a> {
}
impl<'a> Cond<'a> {
- fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
+ fn parse(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
let mut p = tuple((
|i| s.tag_block_start(i),
opt(Whitespace::parse),
@@ -334,7 +331,7 @@ pub struct CondTest<'a> {
}
impl<'a> CondTest<'a> {
- fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
+ fn parse(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
let mut p = preceded(
ws(keyword("if")),
cut(tuple((
@@ -359,7 +356,7 @@ pub enum Whitespace {
}
impl Whitespace {
- fn parse(i: &str) -> IResult<&str, Self, ErrorContext<&str>> {
+ fn parse(i: &str) -> ParseResult<'_, Self> {
alt((
value(Self::Preserve, char('+')),
value(Self::Suppress, char('-')),
@@ -381,11 +378,8 @@ pub struct Loop<'a> {
}
impl<'a> Loop<'a> {
- fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
- fn content<'a>(
- i: &'a str,
- s: &State<'_>,
- ) -> IResult<&'a str, Vec<Node<'a>>, ErrorContext<&'a str>> {
+ fn parse(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
+ fn content<'a>(i: &'a str, s: &State<'_>) -> ParseResult<'a, Vec<Node<'a>>> {
s.enter_loop();
let result = Node::many(i, s);
s.leave_loop();
@@ -465,8 +459,8 @@ pub struct Macro<'a> {
}
impl<'a> Macro<'a> {
- fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
- fn parameters(i: &str) -> IResult<&str, Vec<&str>, ErrorContext<&str>> {
+ fn parse(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
+ fn parameters(i: &str) -> ParseResult<'_, Vec<&str>> {
delimited(
ws(char('(')),
separated_list0(char(','), ws(identifier)),
@@ -532,7 +526,7 @@ pub struct Import<'a> {
}
impl<'a> Import<'a> {
- fn parse(i: &'a str) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
+ fn parse(i: &'a str) -> ParseResult<'a, Self> {
let mut p = tuple((
opt(Whitespace::parse),
ws(keyword("import")),
@@ -563,7 +557,7 @@ pub struct Call<'a> {
}
impl<'a> Call<'a> {
- fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
+ fn parse(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
let mut p = tuple((
opt(Whitespace::parse),
ws(keyword("call")),
@@ -598,7 +592,7 @@ pub struct Match<'a> {
}
impl<'a> Match<'a> {
- fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
+ fn parse(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
let mut p = tuple((
opt(Whitespace::parse),
ws(keyword("match")),
@@ -649,7 +643,7 @@ pub struct BlockDef<'a> {
}
impl<'a> BlockDef<'a> {
- fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
+ fn parse(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
let mut start = tuple((
opt(Whitespace::parse),
ws(keyword("block")),
@@ -694,7 +688,7 @@ fn check_end_name<'a>(
name: &'a str,
end_name: &'a str,
kind: &str,
-) -> IResult<&'a str, &'a str, ErrorContext<&'a str>> {
+) -> ParseResult<'a> {
if name == end_name {
return Ok((after, end_name));
}
@@ -717,7 +711,7 @@ pub struct Lit<'a> {
}
impl<'a> Lit<'a> {
- fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
+ fn parse(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
let p_start = alt((
tag(s.syntax.block_start),
tag(s.syntax.comment_start),
@@ -757,7 +751,7 @@ pub struct Raw<'a> {
}
impl<'a> Raw<'a> {
- fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
+ fn parse(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
let endraw = tuple((
|i| s.tag_block_start(i),
opt(Whitespace::parse),
@@ -792,7 +786,7 @@ pub struct Let<'a> {
}
impl<'a> Let<'a> {
- fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
+ fn parse(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
let mut p = tuple((
opt(Whitespace::parse),
ws(alt((keyword("let"), keyword("set")))),
@@ -825,7 +819,7 @@ pub struct If<'a> {
}
impl<'a> If<'a> {
- fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
+ fn parse(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
let mut p = tuple((
opt(Whitespace::parse),
|i| CondTest::parse(i, s),
@@ -870,7 +864,7 @@ pub struct Include<'a> {
}
impl<'a> Include<'a> {
- fn parse(i: &'a str) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
+ fn parse(i: &'a str) -> ParseResult<'a, Self> {
let mut p = tuple((
opt(Whitespace::parse),
ws(keyword("include")),
@@ -893,7 +887,7 @@ pub struct Extends<'a> {
}
impl<'a> Extends<'a> {
- fn parse(i: &'a str) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
+ fn parse(i: &'a str) -> ParseResult<'a, Self> {
let (i, path) = preceded(ws(keyword("extends")), cut(ws(str_lit)))(i)?;
Ok((i, Self { path }))
}
@@ -906,15 +900,12 @@ pub struct Comment<'a> {
}
impl<'a> Comment<'a> {
- fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self, ErrorContext<&'a str>> {
- fn body<'a>(
- mut i: &'a str,
- s: &State<'_>,
- ) -> IResult<&'a str, &'a str, ErrorContext<&'a str>> {
+ fn parse(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
+ fn body<'a>(mut i: &'a str, s: &State<'_>) -> ParseResult<'a> {
let mut level = 0;
loop {
let (end, tail) = take_until(s.syntax.comment_end)(i)?;
- match take_until::<_, _, ErrorContext<_>>(s.syntax.comment_start)(i) {
+ match take_until::<_, _, ErrorContext<'_>>(s.syntax.comment_start)(i) {
Ok((start, _)) if start.as_ptr() < end.as_ptr() => {
level += 1;
i = &start[2..];