Skip to content

Commit adda27e

Browse files
committed
parser: let State::tag_block_{start,end} return Parser
1 parent 6bf8918 commit adda27e

File tree

2 files changed

+48
-58
lines changed

2 files changed

+48
-58
lines changed

rinja_parser/src/lib.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use winnow::Parser;
1414
use winnow::ascii::take_escaped;
1515
use winnow::combinator::{alt, cut_err, delimited, fail, not, opt, peek, preceded, repeat};
1616
use winnow::error::{ErrorKind, FromExternalError};
17-
use winnow::stream::{AsChar, Stream as _};
17+
use winnow::stream::{AsChar, Stream};
1818
use winnow::token::{any, one_of, take_till, take_while};
1919

2020
pub mod expr;
@@ -318,12 +318,7 @@ impl<'a> winnow::error::ParserError<&'a str> for ErrorContext<'a> {
318318
}
319319
}
320320

321-
fn append(
322-
self,
323-
_: &&'a str,
324-
_: &<&str as winnow::stream::Stream>::Checkpoint,
325-
_: ErrorKind,
326-
) -> Self {
321+
fn append(self, _: &&'a str, _: &<&str as Stream>::Checkpoint, _: ErrorKind) -> Self {
327322
self
328323
}
329324
}
@@ -740,26 +735,30 @@ struct State<'a, 'l> {
740735
level: Level<'l>,
741736
}
742737

743-
impl State<'_, '_> {
744-
fn tag_block_start<'i>(&self, i: &mut &'i str) -> ParseResult<'i, ()> {
745-
self.syntax.block_start.value(()).parse_next(i)
738+
impl<'a> State<'a, '_> {
739+
fn tag_block_start<'i>(&self) -> impl Parser<&'i str, (), ErrorContext<'i>> + 'a {
740+
let block_start = self.syntax.block_start;
741+
|i: &mut _| block_start.value(()).parse_next(i)
746742
}
747743

748-
fn tag_block_end<'i>(&self, i: &mut &'i str) -> ParseResult<'i, ()> {
749-
let control = alt((
750-
self.syntax.block_end.value(None),
751-
peek(delimited('%', alt(('-', '~', '+')).map(Some), '}')),
752-
fail, // rollback on partial matches in the previous line
753-
))
754-
.parse_next(i)?;
755-
if let Some(control) = control {
756-
let message = format!(
757-
"unclosed block, you likely meant to apply whitespace control: {:?}",
758-
format!("{control}{}", self.syntax.block_end),
759-
);
760-
Err(ParseErr::backtrack(ErrorContext::new(message, *i).into()))
761-
} else {
762-
Ok(())
744+
fn tag_block_end<'i>(&self) -> impl Parser<&'i str, (), ErrorContext<'i>> + 'a {
745+
let block_end = self.syntax.block_end;
746+
move |i: &mut _| {
747+
let control = alt((
748+
block_end.value(None),
749+
peek(delimited('%', alt(('-', '~', '+')).map(Some), '}')),
750+
fail, // rollback on partial matches in the previous line
751+
))
752+
.parse_next(i)?;
753+
if let Some(control) = control {
754+
let message = format!(
755+
"unclosed block, you likely meant to apply whitespace control: {:?}",
756+
format!("{control}{}", block_end),
757+
);
758+
Err(ParseErr::backtrack(ErrorContext::new(message, *i).into()))
759+
} else {
760+
Ok(())
761+
}
763762
}
764763
}
765764

@@ -1268,7 +1267,7 @@ impl<'a, F, I, O, A> UnboundParser<'a, I, O, A> for F
12681267
where
12691268
F: FnMut(&mut I, A) -> ParseResult<'a, O>,
12701269
A: Clone,
1271-
I: winnow::stream::Stream + 'a,
1270+
I: Stream + 'a,
12721271
{
12731272
fn bind(mut self, arg: A) -> impl Parser<I, O, ErrorContext<'a>> {
12741273
move |i: &mut _| self(i, arg.clone())
@@ -1285,7 +1284,7 @@ where
12851284
F: FnMut(&mut I, A0, A1) -> ParseResult<'a, O>,
12861285
A0: Clone,
12871286
A1: Clone,
1288-
I: winnow::stream::Stream + 'a,
1287+
I: Stream + 'a,
12891288
{
12901289
fn bind(mut self, a0: A0, a1: A1) -> impl Parser<I, O, ErrorContext<'a>> {
12911290
move |i: &mut _| self(i, a0.clone(), a1.clone())

rinja_parser/src/node.rs

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl<'a> Node<'a> {
8686
fn parse(i: &mut &'a str, s: &State<'_, '_>) -> ParseResult<'a, Self> {
8787
let mut start = *i;
8888
let tag = preceded(
89-
|i: &mut _| s.tag_block_start(i),
89+
s.tag_block_start(),
9090
peek(preceded((opt(Whitespace::parse), skip_ws0), identifier)),
9191
)
9292
.parse_next(i)?;
@@ -113,10 +113,7 @@ impl<'a> Node<'a> {
113113
let node = func(i, s)?;
114114
let closed = cut_node(
115115
None,
116-
alt((
117-
ws(eof).value(false),
118-
(|i: &mut _| s.tag_block_end(i)).value(true),
119-
)),
116+
alt((ws(eof).value(false), s.tag_block_end().value(true))),
120117
)
121118
.parse_next(i)?;
122119
match closed {
@@ -238,7 +235,7 @@ fn cut_node<'a, O>(
238235

239236
fn unexpected_tag<'a>(i: &mut &'a str, s: &State<'_, '_>) -> ParseResult<'a, ()> {
240237
(
241-
|i: &mut _| s.tag_block_start(i),
238+
s.tag_block_start(),
242239
opt(Whitespace::parse),
243240
unexpected_raw_tag.bind(None),
244241
)
@@ -271,14 +268,14 @@ pub struct When<'a> {
271268
impl<'a> When<'a> {
272269
fn r#else(i: &mut &'a str, s: &State<'_, '_>) -> ParseResult<'a, WithSpan<'a, Self>> {
273270
let mut p = (
274-
|i: &mut _| s.tag_block_start(i),
271+
s.tag_block_start(),
275272
opt(Whitespace::parse),
276273
ws(keyword("else")),
277274
cut_node(
278275
Some("match-else"),
279276
(
280277
opt(Whitespace::parse),
281-
|i: &mut _| s.tag_block_end(i),
278+
s.tag_block_end(),
282279
cut_node(Some("match-else"), Node::many.bind(s)),
283280
),
284281
),
@@ -301,15 +298,15 @@ impl<'a> When<'a> {
301298
let start = *i;
302299
let endwhen = ws((
303300
delimited(
304-
|i: &mut _| s.tag_block_start(i),
301+
s.tag_block_start(),
305302
opt(Whitespace::parse),
306303
ws(keyword("endwhen")),
307304
),
308305
cut_node(
309306
Some("match-endwhen"),
310307
(
311308
opt(Whitespace::parse),
312-
|i: &mut _| s.tag_block_end(i),
309+
s.tag_block_end(),
313310
repeat(0.., ws(Comment::parse.bind(s))).map(|()| ()),
314311
),
315312
),
@@ -329,15 +326,15 @@ impl<'a> When<'a> {
329326
))
330327
});
331328
let mut p = (
332-
|i: &mut _| s.tag_block_start(i),
329+
s.tag_block_start(),
333330
opt(Whitespace::parse),
334331
ws(keyword("when")),
335332
cut_node(
336333
Some("match-when"),
337334
(
338335
separated(1.., ws(Target::parse.bind(s)), '|'),
339336
opt(Whitespace::parse),
340-
|i: &mut _| s.tag_block_end(i),
337+
s.tag_block_end(),
341338
cut_node(Some("match-when"), Node::many.bind(s)),
342339
opt(endwhen),
343340
),
@@ -369,7 +366,7 @@ impl<'a> Cond<'a> {
369366
fn parse(i: &mut &'a str, s: &State<'_, '_>) -> ParseResult<'a, WithSpan<'a, Self>> {
370367
let start = *i;
371368
let (_, pws, cond, nws, _, nodes) = (
372-
|i: &mut _| s.tag_block_start(i),
369+
s.tag_block_start(),
373370
opt(Whitespace::parse),
374371
alt((
375372
preceded(ws(keyword("else")), opt(CondTest::parse.bind(s))),
@@ -379,7 +376,7 @@ impl<'a> Cond<'a> {
379376
),
380377
)),
381378
opt(Whitespace::parse),
382-
cut_node(Some("if"), |i: &mut _| s.tag_block_end(i)),
379+
cut_node(Some("if"), s.tag_block_end()),
383380
cut_node(Some("if"), Node::many.bind(s)),
384381
)
385382
.parse_next(i)?;
@@ -492,7 +489,7 @@ fn check_block_start<'a>(
492489
start,
493490
)));
494491
}
495-
(|i: &mut _| s.tag_block_start(i)).parse_next(i)
492+
s.tag_block_start().parse_next(i)
496493
}
497494

498495
#[derive(Debug, PartialEq)]
@@ -529,11 +526,7 @@ impl<'a> Loop<'a> {
529526
Some("for-else"),
530527
(
531528
opt(Whitespace::parse),
532-
delimited(
533-
|i: &mut _| s.tag_block_end(i),
534-
Node::many.bind(s),
535-
|i: &mut _| s.tag_block_start(i),
536-
),
529+
delimited(s.tag_block_end(), Node::many.bind(s), s.tag_block_start()),
537530
opt(Whitespace::parse),
538531
),
539532
),
@@ -577,7 +570,7 @@ impl<'a> Loop<'a> {
577570
ws(Expr::parse.bind(s.level, true)),
578571
opt(if_cond),
579572
opt(Whitespace::parse),
580-
|i: &mut _| s.tag_block_end(i),
573+
s.tag_block_end(),
581574
body_and_end,
582575
),
583576
),
@@ -670,7 +663,7 @@ impl<'a> Macro<'a> {
670663
ws(identifier),
671664
parameters,
672665
opt(Whitespace::parse),
673-
|i: &mut _| s.tag_block_end(i),
666+
s.tag_block_end(),
674667
),
675668
),
676669
);
@@ -771,7 +764,7 @@ impl<'a> FilterBlock<'a> {
771764
.map(|v: Vec<_>| v),
772765
ws(empty),
773766
opt(Whitespace::parse),
774-
|i: &mut _| s.tag_block_end(i),
767+
s.tag_block_end(),
775768
),
776769
),
777770
);
@@ -915,7 +908,7 @@ impl<'a> Match<'a> {
915908
(
916909
ws(Expr::parse.bind(s.level, false)),
917910
opt(Whitespace::parse),
918-
|i: &mut _| s.tag_block_end(i),
911+
s.tag_block_end(),
919912
cut_node(
920913
Some("match"),
921914
(
@@ -984,9 +977,7 @@ impl<'a> BlockDef<'a> {
984977
ws(keyword("block")),
985978
cut_node(
986979
Some("block"),
987-
(ws(identifier), opt(Whitespace::parse), |i: &mut _| {
988-
s.tag_block_end(i)
989-
}),
980+
(ws(identifier), opt(Whitespace::parse), s.tag_block_end()),
990981
),
991982
);
992983
let (pws1, _, (name, nws1, _)) = start.parse_next(i)?;
@@ -1107,11 +1098,11 @@ impl<'a> Raw<'a> {
11071098
fn parse(i: &mut &'a str, s: &State<'_, '_>) -> ParseResult<'a, WithSpan<'a, Self>> {
11081099
let start = *i;
11091100
let endraw = (
1110-
|i: &mut _| s.tag_block_start(i),
1101+
s.tag_block_start(),
11111102
opt(Whitespace::parse),
11121103
ws(keyword("endraw")), // sic: ignore `{% end %}` in raw blocks
11131104
opt(Whitespace::parse),
1114-
peek(|i: &mut _| s.tag_block_end(i)),
1105+
peek(s.tag_block_end()),
11151106
);
11161107

11171108
let mut p = (
@@ -1121,7 +1112,7 @@ impl<'a> Raw<'a> {
11211112
Some("raw"),
11221113
(
11231114
opt(Whitespace::parse),
1124-
|i: &mut _| s.tag_block_end(i),
1115+
s.tag_block_end(),
11251116
skip_till(Splitter1::new(s.syntax.block_start), endraw).with_taken(),
11261117
),
11271118
),
@@ -1212,7 +1203,7 @@ impl<'a> If<'a> {
12121203
Some("if"),
12131204
(
12141205
opt(Whitespace::parse),
1215-
|i: &mut _| s.tag_block_end(i),
1206+
s.tag_block_end(),
12161207
cut_node(
12171208
Some("if"),
12181209
(

0 commit comments

Comments
 (0)