refactor(parser): Remove deprecated 'pair' uses

This commit is contained in:
Ed Page 2024-07-17 12:23:31 -05:00
parent 9758df6e4b
commit 6504bb62d3
4 changed files with 33 additions and 39 deletions

View File

@ -7,7 +7,7 @@ use winnow::character::complete::digit1;
use winnow::combinator::{consumed, cut, fail, map, not, opt, peek, recognize, value};
use winnow::error::ErrorKind;
use winnow::multi::{fold_many0, many0, separated_list0, separated_list1};
use winnow::sequence::{pair, preceded, terminated};
use winnow::sequence::{preceded, terminated};
use winnow::{Parser, error_position};
use crate::{
@ -21,7 +21,7 @@ macro_rules! expr_prec_layer {
let (_, level) = level.nest(i)?;
let start = i;
let (i, left) = Self::$inner(i, level)?;
let (i, right) = many0(pair(ws($op), |i| Self::$inner(i, level)))
let (i, right) = many0((ws($op), |i| Self::$inner(i, level)))
.map(|v: Vec<_>| v)
.parse_next(i)?;
Ok((
@ -153,13 +153,13 @@ impl<'a> Expr<'a> {
let (_, level) = level.nest(i)?;
let start = i;
let range_right =
move |i| pair(ws(alt(("..=", ".."))), opt(move |i| Self::or(i, level))).parse_next(i);
move |i| (ws(alt(("..=", ".."))), opt(move |i| Self::or(i, level))).parse_next(i);
alt((
map(range_right, |(op, right)| {
WithSpan::new(Self::Range(op, None, right.map(Box::new)), start)
}),
map(
pair(move |i| Self::or(i, level), opt(range_right)),
(move |i| Self::or(i, level), opt(range_right)),
|(left, right)| match right {
Some((op, right)) => WithSpan::new(
Self::Range(op, Some(Box::new(left)), right.map(Box::new)),
@ -259,11 +259,11 @@ impl<'a> Expr<'a> {
fn prefix(i: &'a str, mut level: Level) -> ParseResult<'a, WithSpan<'a, Self>> {
let (_, nested) = level.nest(i)?;
let start = i;
let (i, (ops, mut expr)) = pair(
let (i, (ops, mut expr)) = (
many0(ws(alt(("!", "-", "*", "&")))).map(|v: Vec<_>| v),
|i| Suffix::parse(i, nested),
)
.parse_next(i)?;
.parse_next(i)?;
for op in ops.iter().rev() {
// This is a rare place where we create recursion in the parsed AST
@ -313,7 +313,7 @@ impl<'a> Expr<'a> {
},
)
.parse_next(i)?;
let (i, _) = pair(ws(opt(',')), ')').parse_next(i)?;
let (i, _) = (ws(opt(',')), ')').parse_next(i)?;
Ok((i, WithSpan::new(Self::Tuple(exprs), start)))
}
@ -410,7 +410,7 @@ fn token_xor(i: &str) -> ParseResult<'_> {
fn token_bitand(i: &str) -> ParseResult<'_> {
let (i, good) = alt((
value(true, keyword("bitand")),
value(false, pair('&', not('&'))),
value(false, ('&', not('&'))),
))
.parse_next(i)?;
if good {
@ -519,7 +519,7 @@ impl<'a> Suffix<'a> {
}
preceded(
pair(ws('!'), '('),
(ws('!'), '('),
cut(terminated(
map(recognize(nested_parenthesis), Self::MacroCall),
')',
@ -530,7 +530,7 @@ impl<'a> Suffix<'a> {
fn attr(i: &'a str) -> ParseResult<'a, Self> {
map(
preceded(ws(pair('.', not('.'))), cut(alt((digit1, identifier)))),
preceded(ws(('.', not('.'))), cut(alt((digit1, identifier)))),
Self::Attr,
)
.parse_next(i)

View File

@ -17,7 +17,7 @@ use winnow::character::complete::{anychar, one_of, satisfy};
use winnow::combinator::{consumed, cut, fail, map, not, opt, recognize, value};
use winnow::error::{ErrorKind, FromExternalError};
use winnow::multi::{many0_count, many1};
use winnow::sequence::{delimited, pair, preceded};
use winnow::sequence::{delimited, preceded};
use winnow::stream::AsChar;
pub mod expr;
@ -334,7 +334,7 @@ fn identifier(input: &str) -> ParseResult<'_> {
take_while1(|c: char| c.is_alphanum() || c == '_' || c >= '\u{0080}').parse_next(s)
}
recognize(pair(start, opt(tail))).parse_next(input)
recognize((start, opt(tail))).parse_next(input)
}
fn bool_lit(i: &str) -> ParseResult<'_> {
@ -370,7 +370,7 @@ 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 = pair(opt('-'), |i| {
let int_with_base = (opt('-'), |i| {
let (i, (kind, base)) = consumed(preceded(
'0',
alt((value(2, 'b'), value(8, 'o'), value(16, 'x'))),
@ -388,9 +388,9 @@ 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#L626-L653>:
// no `_` directly after the decimal point `.`, or between `e` and `+/-`.
let float = |i: &'a str| -> ParseResult<'a, ()> {
let (i, has_dot) = opt(pair('.', separated_digits(10, true))).parse_next(i)?;
let (i, has_dot) = opt(('.', separated_digits(10, true))).parse_next(i)?;
let (i, has_exp) = opt(|i| {
let (i, (kind, op)) = pair(one_of("eE"), opt(one_of("+-"))).parse_next(i)?;
let (i, (kind, op)) = (one_of("eE"), opt(one_of("+-"))).parse_next(i)?;
match opt(separated_digits(10, op.is_none())).parse_next(i)? {
(i, Some(_)) => Ok((i, ())),
(_, None) => Err(winnow::Err::Cut(ErrorContext::new(
@ -411,11 +411,9 @@ fn num_lit<'a>(start: &'a str) -> ParseResult<'a, Num<'a>> {
opt(|i| num_lit_suffix("integer", INTEGER_TYPES, start, i)).parse_next(i)?;
(i, Num::Int(num, suffix))
} else {
let (i, (num, float)) = consumed(preceded(
pair(opt('-'), separated_digits(10, true)),
opt(float),
))
.parse_next(start)?;
let (i, (num, float)) =
consumed(preceded((opt('-'), separated_digits(10, true)), opt(float)))
.parse_next(start)?;
if float.is_some() {
let (i, suffix) =
opt(|i| num_lit_suffix("float", FLOAT_TYPES, start, i)).parse_next(i)?;
@ -883,15 +881,11 @@ fn filter<'a>(
) -> ParseResult<'a, (&'a str, Option<Vec<WithSpan<'a, Expr<'a>>>>)> {
let (j, _) = take_till(not_ws).parse_next(i)?;
let had_spaces = i.len() != j.len();
let (j, _) = pair('|', not('|')).parse_next(j)?;
let (j, _) = ('|', not('|')).parse_next(j)?;
if !had_spaces {
*level = level.nest(i)?.1;
cut(pair(
ws(identifier),
opt(|i| Expr::arguments(i, *level, false)),
))
.parse_next(j)
cut((ws(identifier), opt(|i| Expr::arguments(i, *level, false)))).parse_next(j)
} else {
Err(winnow::Err::Cut(ErrorContext::new(
"the filter operator `|` must not be preceded by any whitespace characters\n\

View File

@ -8,7 +8,7 @@ use winnow::combinator::{
complete, consumed, cut, eof, fail, map, map_opt, not, opt, peek, recognize, value,
};
use winnow::multi::{many0, separated_list0, separated_list1};
use winnow::sequence::{delimited, pair, preceded};
use winnow::sequence::{delimited, preceded};
use crate::memchr_splitter::{Splitter1, Splitter2, Splitter3};
use crate::{
@ -87,7 +87,7 @@ impl<'a> Node<'a> {
let (j, tag) = preceded(
|i| s.tag_block_start(i),
peek(preceded(
pair(opt(Whitespace::parse), take_till(not_ws)),
(opt(Whitespace::parse), take_till(not_ws)),
identifier,
)),
)
@ -162,7 +162,7 @@ impl<'a> Node<'a> {
|i| s.tag_expr_start(i),
cut_node(
None,
pair(
(
opt(Whitespace::parse),
ws(|i| Expr::parse(i, s.level.get())),
),
@ -172,7 +172,7 @@ impl<'a> Node<'a> {
let (i, (nws, closed)) = cut_node(
None,
pair(
(
opt(Whitespace::parse),
alt((value(true, |i| s.tag_expr_end(i)), value(false, ws(eof)))),
),
@ -292,7 +292,7 @@ impl<'a> When<'a> {
fn when(i: &'a str, s: &State<'_>) -> ParseResult<'a, WithSpan<'a, Self>> {
let start = i;
let endwhen = map(
consumed(ws(pair(
consumed(ws((
delimited(
|i| s.tag_block_start(i),
opt(Whitespace::parse),
@ -410,7 +410,7 @@ impl<'a> CondTest<'a> {
}
fn parse_cond(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
let (i, (target, expr)) = pair(
let (i, (target, expr)) = (
opt(delimited(
ws(alt((keyword("let"), keyword("set")))),
ws(|i| Target::parse(i, s)),
@ -418,7 +418,7 @@ impl<'a> CondTest<'a> {
)),
ws(|i| Expr::parse(i, s.level.get())),
)
.parse_next(i)?;
.parse_next(i)?;
let contains_bool_lit_or_is_defined = expr.contains_bool_lit_or_is_defined();
Ok((i, Self {
target,
@ -761,7 +761,7 @@ impl<'a> Import<'a> {
(
ws(str_lit_without_prefix),
ws(keyword("as")),
cut_node(Some("import"), pair(ws(identifier), opt(Whitespace::parse))),
cut_node(Some("import"), (ws(identifier), opt(Whitespace::parse))),
),
),
);
@ -1180,7 +1180,7 @@ impl<'a> Include<'a> {
ws(keyword("include")),
cut_node(
Some("include"),
pair(ws(str_lit_without_prefix), opt(Whitespace::parse)),
(ws(str_lit_without_prefix), opt(Whitespace::parse)),
),
);
let (i, (pws, _, (path, nws))) = p.parse_next(i)?;
@ -1211,7 +1211,7 @@ impl<'a> Extends<'a> {
ws(keyword("extends")),
cut_node(
Some("extends"),
pair(ws(str_lit_without_prefix), opt(Whitespace::parse)),
(ws(str_lit_without_prefix), opt(Whitespace::parse)),
),
)
.parse_next(i)?;

View File

@ -3,7 +3,7 @@ use winnow::branch::alt;
use winnow::character::complete::{char, one_of};
use winnow::combinator::{consumed, map, map_res, opt};
use winnow::multi::separated_list1;
use winnow::sequence::{pair, preceded};
use winnow::sequence::preceded;
use crate::{
CharLit, ErrorContext, Num, ParseErr, ParseResult, PathOrIdentifier, State, StrLit, WithSpan,
@ -155,7 +155,7 @@ impl<'a> Target<'a> {
}
let (i, (src, target)) =
pair(identifier, opt(preceded(ws(':'), |i| Self::parse(i, s))))(init_i)?;
(identifier, opt(preceded(ws(':'), |i| Self::parse(i, s)))).parse_next(init_i)?;
if src == "_" {
return Err(winnow::Err::Cut(ErrorContext::new(
@ -216,7 +216,7 @@ fn collect_targets<'a, T>(
)));
};
let (i, (has_comma, has_end)) = pair(opt_comma, opt_end).parse_next(i)?;
let (i, (has_comma, has_end)) = (opt_comma, opt_end).parse_next(i)?;
if !has_end {
let msg = match has_comma {
true => format!("expected member, or `{delim}` as terminator"),