mirror of
https://github.com/askama-rs/askama.git
synced 2025-10-02 15:25:19 +00:00
refactor(parser): Switch 'value' from free to trait method
This has a regression in terms of adding uses of `tag` and `one_of`. This is due to a bug that gets addressed in a later release of winnow.
This commit is contained in:
parent
fbb04d3472
commit
42c941f41f
@ -3,9 +3,9 @@ use std::str;
|
|||||||
|
|
||||||
use winnow::Parser;
|
use winnow::Parser;
|
||||||
use winnow::branch::alt;
|
use winnow::branch::alt;
|
||||||
use winnow::bytes::take_till0;
|
use winnow::bytes::{one_of, tag, take_till0};
|
||||||
use winnow::character::digit1;
|
use winnow::character::digit1;
|
||||||
use winnow::combinator::{cut_err, fail, not, opt, peek, value};
|
use winnow::combinator::{cut_err, fail, not, opt, peek};
|
||||||
use winnow::error::{ErrorKind, ParseError as _};
|
use winnow::error::{ErrorKind, ParseError as _};
|
||||||
use winnow::multi::{fold_many0, many0, separated0, separated1};
|
use winnow::multi::{fold_many0, many0, separated0, separated1};
|
||||||
use winnow::sequence::{preceded, terminated};
|
use winnow::sequence::{preceded, terminated};
|
||||||
@ -172,7 +172,7 @@ impl<'a> Expr<'a> {
|
|||||||
expr_prec_layer!(or, and, "||");
|
expr_prec_layer!(or, and, "||");
|
||||||
expr_prec_layer!(and, compare, "&&");
|
expr_prec_layer!(and, compare, "&&");
|
||||||
expr_prec_layer!(compare, bor, alt(("==", "!=", ">=", ">", "<=", "<",)));
|
expr_prec_layer!(compare, bor, alt(("==", "!=", ">=", ">", "<=", "<",)));
|
||||||
expr_prec_layer!(bor, bxor, value("|", "bitor"));
|
expr_prec_layer!(bor, bxor, tag("bitor").value("|"));
|
||||||
expr_prec_layer!(bxor, band, token_xor);
|
expr_prec_layer!(bxor, band, token_xor);
|
||||||
expr_prec_layer!(band, shifts, token_bitand);
|
expr_prec_layer!(band, shifts, token_bitand);
|
||||||
expr_prec_layer!(shifts, addsub, alt((">>", "<<")));
|
expr_prec_layer!(shifts, addsub, alt((">>", "<<")));
|
||||||
@ -398,7 +398,7 @@ impl<'a> Expr<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn token_xor(i: &str) -> ParseResult<'_> {
|
fn token_xor(i: &str) -> ParseResult<'_> {
|
||||||
let (i, good) = alt((value(true, keyword("xor")), value(false, '^'))).parse_next(i)?;
|
let (i, good) = alt((keyword("xor").value(true), one_of('^').value(false))).parse_next(i)?;
|
||||||
if good {
|
if good {
|
||||||
Ok((i, "^"))
|
Ok((i, "^"))
|
||||||
} else {
|
} else {
|
||||||
@ -410,11 +410,8 @@ fn token_xor(i: &str) -> ParseResult<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn token_bitand(i: &str) -> ParseResult<'_> {
|
fn token_bitand(i: &str) -> ParseResult<'_> {
|
||||||
let (i, good) = alt((
|
let (i, good) =
|
||||||
value(true, keyword("bitand")),
|
alt((keyword("bitand").value(true), ('&', not('&')).value(false))).parse_next(i)?;
|
||||||
value(false, ('&', not('&'))),
|
|
||||||
))
|
|
||||||
.parse_next(i)?;
|
|
||||||
if good {
|
if good {
|
||||||
Ok((i, "&"))
|
Ok((i, "&"))
|
||||||
} else {
|
} else {
|
||||||
|
@ -14,7 +14,7 @@ use winnow::Parser;
|
|||||||
use winnow::branch::alt;
|
use winnow::branch::alt;
|
||||||
use winnow::bytes::{any, one_of, tag, take_till0, take_till1, take_while_m_n, take_while1};
|
use winnow::bytes::{any, one_of, tag, take_till0, take_till1, take_while_m_n, take_while1};
|
||||||
use winnow::character::escaped;
|
use winnow::character::escaped;
|
||||||
use winnow::combinator::{cut_err, fail, not, opt, value};
|
use winnow::combinator::{cut_err, fail, not, opt};
|
||||||
use winnow::error::{ErrorKind, FromExternalError};
|
use winnow::error::{ErrorKind, FromExternalError};
|
||||||
use winnow::multi::{many0, many1};
|
use winnow::multi::{many0, many1};
|
||||||
use winnow::sequence::{delimited, preceded};
|
use winnow::sequence::{delimited, preceded};
|
||||||
@ -371,9 +371,16 @@ fn num_lit<'a>(start: &'a str) -> ParseResult<'a, Num<'a>> {
|
|||||||
|
|
||||||
// Equivalent to <https://github.com/rust-lang/rust/blob/e3f909b2bbd0b10db6f164d466db237c582d3045/compiler/rustc_lexer/src/lib.rs#L587-L620>.
|
// Equivalent to <https://github.com/rust-lang/rust/blob/e3f909b2bbd0b10db6f164d466db237c582d3045/compiler/rustc_lexer/src/lib.rs#L587-L620>.
|
||||||
let int_with_base = (opt('-'), |i| {
|
let int_with_base = (opt('-'), |i| {
|
||||||
let (i, (base, kind)) = preceded('0', alt((value(2, 'b'), value(8, 'o'), value(16, 'x'))))
|
let (i, (base, kind)) = preceded(
|
||||||
.with_recognized()
|
'0',
|
||||||
.parse_next(i)?;
|
alt((
|
||||||
|
one_of('b').value(2),
|
||||||
|
one_of('o').value(8),
|
||||||
|
one_of('x').value(16),
|
||||||
|
)),
|
||||||
|
)
|
||||||
|
.with_recognized()
|
||||||
|
.parse_next(i)?;
|
||||||
match opt(separated_digits(base, false)).parse_next(i)? {
|
match opt(separated_digits(base, false)).parse_next(i)? {
|
||||||
(i, Some(_)) => Ok((i, ())),
|
(i, Some(_)) => Ok((i, ())),
|
||||||
(_, None) => Err(winnow::error::ErrMode::Cut(ErrorContext::new(
|
(_, None) => Err(winnow::error::ErrMode::Cut(ErrorContext::new(
|
||||||
|
@ -3,7 +3,7 @@ use std::str;
|
|||||||
use winnow::Parser;
|
use winnow::Parser;
|
||||||
use winnow::branch::alt;
|
use winnow::branch::alt;
|
||||||
use winnow::bytes::{any, tag, take_till0};
|
use winnow::bytes::{any, tag, take_till0};
|
||||||
use winnow::combinator::{cut_err, eof, fail, map_opt, not, opt, peek, value};
|
use winnow::combinator::{cut_err, eof, fail, map_opt, not, opt, peek};
|
||||||
use winnow::multi::{many0, separated0, separated1};
|
use winnow::multi::{many0, separated0, separated1};
|
||||||
use winnow::sequence::{delimited, preceded};
|
use winnow::sequence::{delimited, preceded};
|
||||||
|
|
||||||
@ -114,7 +114,7 @@ impl<'a> Node<'a> {
|
|||||||
|
|
||||||
let (i, closed) = cut_node(
|
let (i, closed) = cut_node(
|
||||||
None,
|
None,
|
||||||
alt((value(true, |i| s.tag_block_end(i)), value(false, ws(eof)))),
|
alt(((|i| s.tag_block_end(i)).value(true), ws(eof).value(false))),
|
||||||
)
|
)
|
||||||
.parse_next(i)?;
|
.parse_next(i)?;
|
||||||
match closed {
|
match closed {
|
||||||
@ -173,7 +173,7 @@ impl<'a> Node<'a> {
|
|||||||
None,
|
None,
|
||||||
(
|
(
|
||||||
opt(Whitespace::parse),
|
opt(Whitespace::parse),
|
||||||
alt((value(true, |i| s.tag_expr_end(i)), value(false, ws(eof)))),
|
alt(((|i| s.tag_expr_end(i)).value(true), ws(eof).value(false))),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.parse_next(i)?;
|
.parse_next(i)?;
|
||||||
@ -226,15 +226,13 @@ fn cut_node<'a, O>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn unexpected_tag<'a>(i: &'a str, s: &State<'_>) -> ParseResult<'a, ()> {
|
fn unexpected_tag<'a>(i: &'a str, s: &State<'_>) -> ParseResult<'a, ()> {
|
||||||
value(
|
(
|
||||||
(),
|
|i| s.tag_block_start(i),
|
||||||
(
|
opt(Whitespace::parse),
|
||||||
|i| s.tag_block_start(i),
|
|i| unexpected_raw_tag(None, i),
|
||||||
opt(Whitespace::parse),
|
|
||||||
|i| unexpected_raw_tag(None, i),
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
.parse_next(i)
|
.void()
|
||||||
|
.parse_next(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unexpected_raw_tag<'a>(kind: Option<&'static str>, i: &'a str) -> ParseResult<'a, ()> {
|
fn unexpected_raw_tag<'a>(kind: Option<&'static str>, i: &'a str) -> ParseResult<'a, ()> {
|
||||||
@ -303,7 +301,7 @@ impl<'a> When<'a> {
|
|||||||
(
|
(
|
||||||
opt(Whitespace::parse),
|
opt(Whitespace::parse),
|
||||||
|i| s.tag_block_end(i),
|
|i| s.tag_block_end(i),
|
||||||
many0(value((), ws(|i| Comment::parse(i, s)))).map(|()| ()),
|
many0(ws(|i| Comment::parse(i, s))).map(|()| ()),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
))
|
))
|
||||||
@ -845,7 +843,7 @@ impl<'a> Match<'a> {
|
|||||||
cut_node(
|
cut_node(
|
||||||
Some("match"),
|
Some("match"),
|
||||||
(
|
(
|
||||||
ws(many0(ws(value((), |i| Comment::parse(i, s))))).map(|()| ()),
|
ws(many0(ws(|i| Comment::parse(i, s)))).map(|()| ()),
|
||||||
many0(|i| When::when(i, s)).map(|v: Vec<_>| v),
|
many0(|i| When::when(i, s)).map(|v: Vec<_>| v),
|
||||||
cut_node(
|
cut_node(
|
||||||
Some("match"),
|
Some("match"),
|
||||||
@ -1241,8 +1239,8 @@ impl<'a> Comment<'a> {
|
|||||||
|
|
||||||
fn tag<'a>(i: &'a str, s: &State<'_>) -> ParseResult<'a, Tag> {
|
fn tag<'a>(i: &'a str, s: &State<'_>) -> ParseResult<'a, Tag> {
|
||||||
alt((
|
alt((
|
||||||
value(Tag::Open, |i| s.tag_comment_start(i)),
|
(|i| s.tag_comment_start(i)).value(Tag::Open),
|
||||||
value(Tag::Close, |i| s.tag_comment_end(i)),
|
(|i| s.tag_comment_end(i)).value(Tag::Close),
|
||||||
))
|
))
|
||||||
.parse_next(i)
|
.parse_next(i)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user