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::combinator::{consumed, cut, fail, map, not, opt, peek, recognize, value};
use winnow::error::ErrorKind; use winnow::error::ErrorKind;
use winnow::multi::{fold_many0, many0, separated_list0, separated_list1}; 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 winnow::{Parser, error_position};
use crate::{ use crate::{
@ -21,7 +21,7 @@ macro_rules! expr_prec_layer {
let (_, level) = level.nest(i)?; let (_, level) = level.nest(i)?;
let start = i; let start = i;
let (i, left) = Self::$inner(i, level)?; 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) .map(|v: Vec<_>| v)
.parse_next(i)?; .parse_next(i)?;
Ok(( Ok((
@ -153,13 +153,13 @@ impl<'a> Expr<'a> {
let (_, level) = level.nest(i)?; let (_, level) = level.nest(i)?;
let start = i; let start = i;
let range_right = 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(( alt((
map(range_right, |(op, right)| { map(range_right, |(op, right)| {
WithSpan::new(Self::Range(op, None, right.map(Box::new)), start) WithSpan::new(Self::Range(op, None, right.map(Box::new)), start)
}), }),
map( map(
pair(move |i| Self::or(i, level), opt(range_right)), (move |i| Self::or(i, level), opt(range_right)),
|(left, right)| match right { |(left, right)| match right {
Some((op, right)) => WithSpan::new( Some((op, right)) => WithSpan::new(
Self::Range(op, Some(Box::new(left)), right.map(Box::new)), Self::Range(op, Some(Box::new(left)), right.map(Box::new)),
@ -259,7 +259,7 @@ impl<'a> Expr<'a> {
fn prefix(i: &'a str, mut level: Level) -> ParseResult<'a, WithSpan<'a, Self>> { fn prefix(i: &'a str, mut level: Level) -> ParseResult<'a, WithSpan<'a, Self>> {
let (_, nested) = level.nest(i)?; let (_, nested) = level.nest(i)?;
let start = i; let start = i;
let (i, (ops, mut expr)) = pair( let (i, (ops, mut expr)) = (
many0(ws(alt(("!", "-", "*", "&")))).map(|v: Vec<_>| v), many0(ws(alt(("!", "-", "*", "&")))).map(|v: Vec<_>| v),
|i| Suffix::parse(i, nested), |i| Suffix::parse(i, nested),
) )
@ -313,7 +313,7 @@ impl<'a> Expr<'a> {
}, },
) )
.parse_next(i)?; .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))) Ok((i, WithSpan::new(Self::Tuple(exprs), start)))
} }
@ -410,7 +410,7 @@ 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) = alt((
value(true, keyword("bitand")), value(true, keyword("bitand")),
value(false, pair('&', not('&'))), value(false, ('&', not('&'))),
)) ))
.parse_next(i)?; .parse_next(i)?;
if good { if good {
@ -519,7 +519,7 @@ impl<'a> Suffix<'a> {
} }
preceded( preceded(
pair(ws('!'), '('), (ws('!'), '('),
cut(terminated( cut(terminated(
map(recognize(nested_parenthesis), Self::MacroCall), map(recognize(nested_parenthesis), Self::MacroCall),
')', ')',
@ -530,7 +530,7 @@ impl<'a> Suffix<'a> {
fn attr(i: &'a str) -> ParseResult<'a, Self> { fn attr(i: &'a str) -> ParseResult<'a, Self> {
map( map(
preceded(ws(pair('.', not('.'))), cut(alt((digit1, identifier)))), preceded(ws(('.', not('.'))), cut(alt((digit1, identifier)))),
Self::Attr, Self::Attr,
) )
.parse_next(i) .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::combinator::{consumed, cut, fail, map, not, opt, recognize, value};
use winnow::error::{ErrorKind, FromExternalError}; use winnow::error::{ErrorKind, FromExternalError};
use winnow::multi::{many0_count, many1}; use winnow::multi::{many0_count, many1};
use winnow::sequence::{delimited, pair, preceded}; use winnow::sequence::{delimited, preceded};
use winnow::stream::AsChar; use winnow::stream::AsChar;
pub mod expr; 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) 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<'_> { 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>. // 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( let (i, (kind, base)) = consumed(preceded(
'0', '0',
alt((value(2, 'b'), value(8, 'o'), value(16, 'x'))), 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>: // 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 `+/-`. // no `_` directly after the decimal point `.`, or between `e` and `+/-`.
let float = |i: &'a str| -> ParseResult<'a, ()> { 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, 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)? { match opt(separated_digits(10, op.is_none())).parse_next(i)? {
(i, Some(_)) => Ok((i, ())), (i, Some(_)) => Ok((i, ())),
(_, None) => Err(winnow::Err::Cut(ErrorContext::new( (_, None) => Err(winnow::Err::Cut(ErrorContext::new(
@ -411,10 +411,8 @@ fn num_lit<'a>(start: &'a str) -> ParseResult<'a, Num<'a>> {
opt(|i| num_lit_suffix("integer", INTEGER_TYPES, start, i)).parse_next(i)?; opt(|i| num_lit_suffix("integer", INTEGER_TYPES, start, i)).parse_next(i)?;
(i, Num::Int(num, suffix)) (i, Num::Int(num, suffix))
} else { } else {
let (i, (num, float)) = consumed(preceded( let (i, (num, float)) =
pair(opt('-'), separated_digits(10, true)), consumed(preceded((opt('-'), separated_digits(10, true)), opt(float)))
opt(float),
))
.parse_next(start)?; .parse_next(start)?;
if float.is_some() { if float.is_some() {
let (i, suffix) = let (i, suffix) =
@ -883,15 +881,11 @@ fn filter<'a>(
) -> ParseResult<'a, (&'a str, Option<Vec<WithSpan<'a, Expr<'a>>>>)> { ) -> ParseResult<'a, (&'a str, Option<Vec<WithSpan<'a, Expr<'a>>>>)> {
let (j, _) = take_till(not_ws).parse_next(i)?; let (j, _) = take_till(not_ws).parse_next(i)?;
let had_spaces = i.len() != j.len(); let had_spaces = i.len() != j.len();
let (j, _) = pair('|', not('|')).parse_next(j)?; let (j, _) = ('|', not('|')).parse_next(j)?;
if !had_spaces { if !had_spaces {
*level = level.nest(i)?.1; *level = level.nest(i)?.1;
cut(pair( cut((ws(identifier), opt(|i| Expr::arguments(i, *level, false)))).parse_next(j)
ws(identifier),
opt(|i| Expr::arguments(i, *level, false)),
))
.parse_next(j)
} else { } else {
Err(winnow::Err::Cut(ErrorContext::new( Err(winnow::Err::Cut(ErrorContext::new(
"the filter operator `|` must not be preceded by any whitespace characters\n\ "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, complete, consumed, cut, eof, fail, map, map_opt, not, opt, peek, recognize, value,
}; };
use winnow::multi::{many0, separated_list0, separated_list1}; 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::memchr_splitter::{Splitter1, Splitter2, Splitter3};
use crate::{ use crate::{
@ -87,7 +87,7 @@ impl<'a> Node<'a> {
let (j, tag) = preceded( let (j, tag) = preceded(
|i| s.tag_block_start(i), |i| s.tag_block_start(i),
peek(preceded( peek(preceded(
pair(opt(Whitespace::parse), take_till(not_ws)), (opt(Whitespace::parse), take_till(not_ws)),
identifier, identifier,
)), )),
) )
@ -162,7 +162,7 @@ impl<'a> Node<'a> {
|i| s.tag_expr_start(i), |i| s.tag_expr_start(i),
cut_node( cut_node(
None, None,
pair( (
opt(Whitespace::parse), opt(Whitespace::parse),
ws(|i| Expr::parse(i, s.level.get())), ws(|i| Expr::parse(i, s.level.get())),
), ),
@ -172,7 +172,7 @@ impl<'a> Node<'a> {
let (i, (nws, closed)) = cut_node( let (i, (nws, closed)) = cut_node(
None, None,
pair( (
opt(Whitespace::parse), opt(Whitespace::parse),
alt((value(true, |i| s.tag_expr_end(i)), value(false, ws(eof)))), 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>> { fn when(i: &'a str, s: &State<'_>) -> ParseResult<'a, WithSpan<'a, Self>> {
let start = i; let start = i;
let endwhen = map( let endwhen = map(
consumed(ws(pair( consumed(ws((
delimited( delimited(
|i| s.tag_block_start(i), |i| s.tag_block_start(i),
opt(Whitespace::parse), opt(Whitespace::parse),
@ -410,7 +410,7 @@ impl<'a> CondTest<'a> {
} }
fn parse_cond(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> { fn parse_cond(i: &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
let (i, (target, expr)) = pair( let (i, (target, expr)) = (
opt(delimited( opt(delimited(
ws(alt((keyword("let"), keyword("set")))), ws(alt((keyword("let"), keyword("set")))),
ws(|i| Target::parse(i, s)), ws(|i| Target::parse(i, s)),
@ -761,7 +761,7 @@ impl<'a> Import<'a> {
( (
ws(str_lit_without_prefix), ws(str_lit_without_prefix),
ws(keyword("as")), 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")), ws(keyword("include")),
cut_node( cut_node(
Some("include"), 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)?; let (i, (pws, _, (path, nws))) = p.parse_next(i)?;
@ -1211,7 +1211,7 @@ impl<'a> Extends<'a> {
ws(keyword("extends")), ws(keyword("extends")),
cut_node( cut_node(
Some("extends"), Some("extends"),
pair(ws(str_lit_without_prefix), opt(Whitespace::parse)), (ws(str_lit_without_prefix), opt(Whitespace::parse)),
), ),
) )
.parse_next(i)?; .parse_next(i)?;

View File

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