diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2023-07-02 11:18:27 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2023-07-31 10:27:15 +0200 |
commit | 1677fd6f57bcbfe1c810b3839bf155b3a0a02b76 (patch) | |
tree | 3f98c03924de513b82004e36301ad6f0fa1a3bea /askama_parser/src/lib.rs | |
parent | 17ee42cb6a49e4bf4f7a61359445d46b1fc3d74c (diff) | |
download | askama-1677fd6f57bcbfe1c810b3839bf155b3a0a02b76.tar.gz askama-1677fd6f57bcbfe1c810b3839bf155b3a0a02b76.tar.bz2 askama-1677fd6f57bcbfe1c810b3839bf155b3a0a02b76.zip |
parser: move helper functions into State impl
Diffstat (limited to 'askama_parser/src/lib.rs')
-rw-r--r-- | askama_parser/src/lib.rs | 122 |
1 files changed, 61 insertions, 61 deletions
diff --git a/askama_parser/src/lib.rs b/askama_parser/src/lib.rs index bebf020..ac4ebb7 100644 --- a/askama_parser/src/lib.rs +++ b/askama_parser/src/lib.rs @@ -22,32 +22,6 @@ mod node; #[cfg(test)] mod tests; -struct State<'a> { - syntax: &'a Syntax<'a>, - loop_depth: Cell<usize>, -} - -impl<'a> State<'a> { - fn new(syntax: &'a Syntax<'a>) -> State<'a> { - State { - syntax, - loop_depth: Cell::new(0), - } - } - - fn enter_loop(&self) { - self.loop_depth.set(self.loop_depth.get() + 1); - } - - fn leave_loop(&self) { - self.loop_depth.set(self.loop_depth.get() - 1); - } - - fn is_in_loop(&self) -> bool { - self.loop_depth.get() > 0 - } -} - mod _parsed { use std::mem; @@ -305,48 +279,74 @@ fn path(i: &str) -> IResult<&str, Vec<&str>> { } } -fn take_content<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, Node<'a>> { - let p_start = alt(( - tag(s.syntax.block_start), - tag(s.syntax.comment_start), - tag(s.syntax.expr_start), - )); - - let (i, _) = not(eof)(i)?; - let (i, content) = opt(recognize(skip_till(p_start)))(i)?; - let (i, content) = match content { - Some("") => { - // {block,comment,expr}_start follows immediately. - return Err(nom::Err::Error(error_position!(i, ErrorKind::TakeUntil))); - } - Some(content) => (i, content), - None => ("", i), // there is no {block,comment,expr}_start: take everything - }; - Ok((i, split_ws_parts(content))) +struct State<'a> { + syntax: &'a Syntax<'a>, + loop_depth: Cell<usize>, } -fn tag_block_start<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> { - tag(s.syntax.block_start)(i) -} +impl<'a> State<'a> { + fn new(syntax: &'a Syntax<'a>) -> State<'a> { + State { + syntax, + loop_depth: Cell::new(0), + } + } -fn tag_block_end<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> { - tag(s.syntax.block_end)(i) -} + fn take_content<'i>(&self, i: &'i str) -> IResult<&'i str, Node<'i>> { + let p_start = alt(( + tag(self.syntax.block_start), + tag(self.syntax.comment_start), + tag(self.syntax.expr_start), + )); + + let (i, _) = not(eof)(i)?; + let (i, content) = opt(recognize(skip_till(p_start)))(i)?; + let (i, content) = match content { + Some("") => { + // {block,comment,expr}_start follows immediately. + return Err(nom::Err::Error(error_position!(i, ErrorKind::TakeUntil))); + } + Some(content) => (i, content), + None => ("", i), // there is no {block,comment,expr}_start: take everything + }; + Ok((i, split_ws_parts(content))) + } -fn tag_comment_start<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> { - tag(s.syntax.comment_start)(i) -} + fn tag_block_start<'i>(&self, i: &'i str) -> IResult<&'i str, &'i str> { + tag(self.syntax.block_start)(i) + } -fn tag_comment_end<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> { - tag(s.syntax.comment_end)(i) -} + fn tag_block_end<'i>(&self, i: &'i str) -> IResult<&'i str, &'i str> { + tag(self.syntax.block_end)(i) + } -fn tag_expr_start<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> { - tag(s.syntax.expr_start)(i) -} + fn tag_comment_start<'i>(&self, i: &'i str) -> IResult<&'i str, &'i str> { + tag(self.syntax.comment_start)(i) + } + + fn tag_comment_end<'i>(&self, i: &'i str) -> IResult<&'i str, &'i str> { + tag(self.syntax.comment_end)(i) + } + + fn tag_expr_start<'i>(&self, i: &'i str) -> IResult<&'i str, &'i str> { + tag(self.syntax.expr_start)(i) + } + + fn tag_expr_end<'i>(&self, i: &'i str) -> IResult<&'i str, &'i str> { + tag(self.syntax.expr_end)(i) + } + + fn enter_loop(&self) { + self.loop_depth.set(self.loop_depth.get() + 1); + } + + fn leave_loop(&self) { + self.loop_depth.set(self.loop_depth.get() - 1); + } -fn tag_expr_end<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> { - tag(s.syntax.expr_end)(i) + fn is_in_loop(&self) -> bool { + self.loop_depth.get() > 0 + } } #[derive(Debug)] |