Skip to content

Commit

Permalink
refactor(parser): Resolve remaining deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Dec 20, 2024
1 parent 8b1817e commit 2a92482
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
12 changes: 7 additions & 5 deletions rinja_parser/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use std::str;
use winnow::Parser;
use winnow::ascii::digit1;
use winnow::combinator::{
alt, cut_err, fail, fold_repeat, not, opt, peek, preceded, repeat, separated0, separated1,
terminated,
alt, cut_err, fail, not, opt, peek, preceded, repeat, separated, terminated,
};
use winnow::error::{ErrorKind, ParserError as _};
use winnow::stream::Stream as _;
Expand Down Expand Up @@ -143,7 +142,8 @@ impl<'a> Expr<'a> {
preceded(
ws('('),
cut_err(terminated(
separated0(
separated(
0..,
ws(move |i: &mut _| {
// Needed to prevent borrowing it twice between this closure and the one
// calling `Self::named_arguments`.
Expand Down Expand Up @@ -415,9 +415,11 @@ impl<'a> Expr<'a> {
}

let mut exprs = vec![expr];
fold_repeat(
repeat(
0..,
preceded(',', ws(|i: &mut _| Self::parse(i, level, true))),
)
.fold(
|| (),
|(), expr| {
exprs.push(expr);
Expand All @@ -435,7 +437,7 @@ impl<'a> Expr<'a> {
ws('['),
cut_err(terminated(
opt(terminated(
separated1(ws(move |i: &mut _| Self::parse(i, level, true)), ','),
separated(1.., ws(move |i: &mut _| Self::parse(i, level, true)), ','),
ws(opt(',')),
)),
']',
Expand Down
11 changes: 8 additions & 3 deletions rinja_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use winnow::ascii::escaped;
use winnow::combinator::{alt, cut_err, delimited, fail, not, opt, peek, preceded, repeat};
use winnow::error::{ErrorKind, FromExternalError};
use winnow::stream::{AsChar, Stream as _};
use winnow::token::{any, one_of, take_till1, take_while};
use winnow::token::{any, one_of, take_till, take_while};

pub mod expr;
pub use expr::{Expr, Filter};
Expand Down Expand Up @@ -545,7 +545,12 @@ pub struct StrLit<'a> {
}

fn str_lit_without_prefix<'a>(i: &mut &'a str) -> ParseResult<'a> {
let s = delimited('"', opt(escaped(take_till1(['\\', '"']), '\\', any)), '"').parse_next(i)?;
let s = delimited(
'"',
opt(escaped(take_till(1.., ['\\', '"']), '\\', any)),
'"',
)
.parse_next(i)?;
Ok(s.unwrap_or_default())
}

Expand Down Expand Up @@ -578,7 +583,7 @@ fn char_lit<'a>(i: &mut &'a str) -> ParseResult<'a, CharLit<'a>> {
opt('b'),
delimited(
'\'',
opt(escaped(take_till1(['\\', '\'']), '\\', any)),
opt(escaped(take_till(1.., ['\\', '\'']), '\\', any)),
'\'',
),
)
Expand Down
15 changes: 8 additions & 7 deletions rinja_parser/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use std::str::{self, FromStr};

use winnow::Parser;
use winnow::combinator::{
alt, cut_err, delimited, empty, eof, fail, not, opt, peek, preceded, repeat, rest, separated1,
alt, cut_err, delimited, empty, eof, fail, not, opt, peek, preceded, repeat, rest, separated,
terminated,
};
use winnow::stream::Stream as _;
use winnow::token::{any, tag};
use winnow::token::{any, literal};

use crate::memchr_splitter::{Splitter1, Splitter2, Splitter3};
use crate::{
Expand Down Expand Up @@ -336,7 +336,7 @@ impl<'a> When<'a> {
cut_node(
Some("match-when"),
(
separated1(ws(|i: &mut _| Target::parse(i, s)), '|'),
separated(1.., ws(|i: &mut _| Target::parse(i, s)), '|'),
opt(Whitespace::parse),
|i: &mut _| s.tag_block_end(i),
cut_node(Some("match-when"), |i: &mut _| Node::many(i, s)),
Expand Down Expand Up @@ -632,7 +632,8 @@ impl<'a> Macro<'a> {
'(',
(
opt(terminated(
separated1(
separated(
1..,
(
ws(identifier),
opt(preceded('=', ws(|i: &mut _| Expr::parse(i, level, false)))),
Expand Down Expand Up @@ -1065,9 +1066,9 @@ impl<'a> Lit<'a> {
s.syntax.expr_start,
);
let p_start = alt((
tag(s.syntax.block_start),
tag(s.syntax.comment_start),
tag(s.syntax.expr_start),
literal(s.syntax.block_start),
literal(s.syntax.comment_start),
literal(s.syntax.expr_start),
));

let content = opt(skip_till(candidate_finder, p_start).recognize()).parse_next(i)?;
Expand Down
7 changes: 4 additions & 3 deletions rinja_parser/src/target.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use winnow::Parser;
use winnow::combinator::{alt, opt, peek, preceded, separated1};
use winnow::combinator::{alt, opt, peek, preceded, separated};
use winnow::token::one_of;

use crate::{
Expand Down Expand Up @@ -27,7 +27,8 @@ pub enum Target<'a> {
impl<'a> Target<'a> {
/// Parses multiple targets with `or` separating them
pub(super) fn parse(i: &mut &'a str, s: &State<'_>) -> ParseResult<'a, Self> {
separated1(
separated(
1..,
|i: &mut _| s.nest(i, |i: &mut _| Self::parse_one(i, s)),
ws("or"),
)
Expand Down Expand Up @@ -215,7 +216,7 @@ fn collect_targets<'a, T>(
return Ok((false, Vec::new()));
}

let targets = opt(separated1(one, ws(',')).map(|v: Vec<_>| v)).parse_next(i)?;
let targets = opt(separated(1.., one, ws(',')).map(|v: Vec<_>| v)).parse_next(i)?;
let Some(targets) = targets else {
return Err(winnow::error::ErrMode::Cut(ErrorContext::new(
"expected comma separated list of members",
Expand Down

0 comments on commit 2a92482

Please sign in to comment.