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:
Ed Page 2024-07-17 14:34:15 -05:00
parent fbb04d3472
commit 42c941f41f
3 changed files with 30 additions and 28 deletions

View File

@ -3,9 +3,9 @@ use std::str;
use winnow::Parser;
use winnow::branch::alt;
use winnow::bytes::take_till0;
use winnow::bytes::{one_of, tag, take_till0};
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::multi::{fold_many0, many0, separated0, separated1};
use winnow::sequence::{preceded, terminated};
@ -172,7 +172,7 @@ impl<'a> Expr<'a> {
expr_prec_layer!(or, and, "||");
expr_prec_layer!(and, compare, "&&");
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!(band, shifts, token_bitand);
expr_prec_layer!(shifts, addsub, alt((">>", "<<")));
@ -398,7 +398,7 @@ impl<'a> Expr<'a> {
}
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 {
Ok((i, "^"))
} else {
@ -410,11 +410,8 @@ fn token_xor(i: &str) -> ParseResult<'_> {
}
fn token_bitand(i: &str) -> ParseResult<'_> {
let (i, good) = alt((
value(true, keyword("bitand")),
value(false, ('&', not('&'))),
))
.parse_next(i)?;
let (i, good) =
alt((keyword("bitand").value(true), ('&', not('&')).value(false))).parse_next(i)?;
if good {
Ok((i, "&"))
} else {

View File

@ -14,7 +14,7 @@ use winnow::Parser;
use winnow::branch::alt;
use winnow::bytes::{any, one_of, tag, take_till0, take_till1, take_while_m_n, take_while1};
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::multi::{many0, many1};
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>.
let int_with_base = (opt('-'), |i| {
let (i, (base, kind)) = preceded('0', alt((value(2, 'b'), value(8, 'o'), value(16, 'x'))))
.with_recognized()
.parse_next(i)?;
let (i, (base, kind)) = preceded(
'0',
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)? {
(i, Some(_)) => Ok((i, ())),
(_, None) => Err(winnow::error::ErrMode::Cut(ErrorContext::new(

View File

@ -3,7 +3,7 @@ use std::str;
use winnow::Parser;
use winnow::branch::alt;
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::sequence::{delimited, preceded};
@ -114,7 +114,7 @@ impl<'a> Node<'a> {
let (i, closed) = cut_node(
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)?;
match closed {
@ -173,7 +173,7 @@ impl<'a> Node<'a> {
None,
(
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)?;
@ -226,15 +226,13 @@ fn cut_node<'a, O>(
}
fn unexpected_tag<'a>(i: &'a str, s: &State<'_>) -> ParseResult<'a, ()> {
value(
(),
(
|i| s.tag_block_start(i),
opt(Whitespace::parse),
|i| unexpected_raw_tag(None, i),
),
(
|i| s.tag_block_start(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, ()> {
@ -303,7 +301,7 @@ impl<'a> When<'a> {
(
opt(Whitespace::parse),
|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(
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),
cut_node(
Some("match"),
@ -1241,8 +1239,8 @@ impl<'a> Comment<'a> {
fn tag<'a>(i: &'a str, s: &State<'_>) -> ParseResult<'a, Tag> {
alt((
value(Tag::Open, |i| s.tag_comment_start(i)),
value(Tag::Close, |i| s.tag_comment_end(i)),
(|i| s.tag_comment_start(i)).value(Tag::Open),
(|i| s.tag_comment_end(i)).value(Tag::Close),
))
.parse_next(i)
}