diff options
author | René Kijewski <rene.kijewski@fu-berlin.de> | 2023-08-01 11:30:06 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2023-08-01 13:04:41 +0200 |
commit | 4315773676527c94646315ac2a3475fd6eb2925a (patch) | |
tree | f9be7d81305bfc0210249db28c822b6df6ce25f8 /askama_parser/src/node.rs | |
parent | 32cd2825c4e5be9d0f160f9d112a98dbba24b29d (diff) | |
download | askama-4315773676527c94646315ac2a3475fd6eb2925a.tar.gz askama-4315773676527c94646315ac2a3475fd6eb2925a.tar.bz2 askama-4315773676527c94646315ac2a3475fd6eb2925a.zip |
parser: add type for `Node::Comment`
Diffstat (limited to '')
-rw-r--r-- | askama_parser/src/node.rs | 100 |
1 files changed, 57 insertions, 43 deletions
diff --git a/askama_parser/src/node.rs b/askama_parser/src/node.rs index 0d7552f..a45cc89 100644 --- a/askama_parser/src/node.rs +++ b/askama_parser/src/node.rs @@ -17,7 +17,7 @@ use super::{ #[derive(Debug, PartialEq)] pub enum Node<'a> { Lit(Lit<'a>), - Comment(Ws), + Comment(Comment<'a>), Expr(Ws, Expr<'a>), Call(Call<'a>), Let(Let<'a>), @@ -38,7 +38,7 @@ impl<'a> Node<'a> { pub(super) fn many(i: &'a str, s: &State<'_>) -> IResult<&'a str, Vec<Self>> { many0(alt(( map(complete(|i| Lit::parse(i, s)), Self::Lit), - complete(|i| Self::comment(i, s)), + map(complete(|i| Comment::parse(i, s)), Self::Comment), complete(|i| Self::expr(i, s)), complete(|i| Self::parse(i, s)), )))(i) @@ -107,46 +107,6 @@ impl<'a> Node<'a> { let (i, (_, (pws, expr, nws, _))) = p(i)?; Ok((i, Self::Expr(Ws(pws, nws), expr))) } - - fn comment(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { - fn body<'a>(mut i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> { - let mut level = 0; - loop { - let (end, tail) = take_until(s.syntax.comment_end)(i)?; - match take_until::<_, _, Error<_>>(s.syntax.comment_start)(i) { - Ok((start, _)) if start.as_ptr() < end.as_ptr() => { - level += 1; - i = &start[2..]; - } - _ if level > 0 => { - level -= 1; - i = &end[2..]; - } - _ => return Ok((end, tail)), - } - } - } - - let mut p = tuple(( - |i| s.tag_comment_start(i), - cut(tuple(( - opt(Whitespace::parse), - |i| body(i, s), - |i| s.tag_comment_end(i), - ))), - )); - let (i, (_, (pws, tail, _))) = p(i)?; - let nws = if tail.ends_with('-') { - Some(Whitespace::Suppress) - } else if tail.ends_with('+') { - Some(Whitespace::Preserve) - } else if tail.ends_with('~') { - Some(Whitespace::Minimize) - } else { - None - }; - Ok((i, Self::Comment(Ws(pws, nws)))) - } } #[derive(Debug, PartialEq)] @@ -604,7 +564,7 @@ impl<'a> Match<'a> { opt(Whitespace::parse), |i| s.tag_block_end(i), cut(tuple(( - ws(many0(ws(value((), |i| Node::comment(i, s))))), + ws(many0(ws(value((), |i| Comment::parse(i, s))))), many1(|i| When::when(i, s)), cut(tuple(( opt(|i| When::r#match(i, s)), @@ -866,6 +826,60 @@ impl<'a> Extends<'a> { } } +#[derive(Debug, PartialEq)] +pub struct Comment<'a> { + pub ws: Ws, + pub content: &'a str, +} + +impl<'a> Comment<'a> { + fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { + fn body<'a>(mut i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> { + let mut level = 0; + loop { + let (end, tail) = take_until(s.syntax.comment_end)(i)?; + match take_until::<_, _, Error<_>>(s.syntax.comment_start)(i) { + Ok((start, _)) if start.as_ptr() < end.as_ptr() => { + level += 1; + i = &start[2..]; + } + _ if level > 0 => { + level -= 1; + i = &end[2..]; + } + _ => return Ok((end, tail)), + } + } + } + + let mut p = tuple(( + |i| s.tag_comment_start(i), + cut(tuple(( + opt(Whitespace::parse), + |i| body(i, s), + |i| s.tag_comment_end(i), + ))), + )); + let (i, (content, (pws, tail, _))) = p(i)?; + let nws = if tail.ends_with('-') { + Some(Whitespace::Suppress) + } else if tail.ends_with('+') { + Some(Whitespace::Preserve) + } else if tail.ends_with('~') { + Some(Whitespace::Minimize) + } else { + None + }; + Ok(( + i, + Self { + ws: Ws(pws, nws), + content, + }, + )) + } +} + /// First field is "minus/plus sign was used on the left part of the item". /// /// Second field is "minus/plus sign was used on the right part of the item". |