Skip to content

Commit

Permalink
chore: Upgrade to Winnow 0.7
Browse files Browse the repository at this point in the history
I am not thrilled with the fact that annotations of some kind (I used
no-op `map_err`s here) are needed for the errors and will be digging
into this to better understand why.  The code in `toml_edit` is very
similar and yet it doesn't need them.
  • Loading branch information
epage committed Jan 30, 2025
1 parent 9338fd3 commit 53d2ab0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
2 changes: 1 addition & 1 deletion rinja_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ harness = false
[dependencies]
memchr = "2"
serde = { version = "1.0", optional = true, features = ["derive"] }
winnow = "0.6.26"
winnow = "0.7.0"

[dev-dependencies]
criterion = "0.5"
Expand Down
4 changes: 2 additions & 2 deletions rinja_parser/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,13 @@ impl<'a> Expr<'a> {
let start = *i;
let expr = preceded(ws('('), opt(|i: &mut _| Self::parse(i, level, true))).parse_next(i)?;
let Some(expr) = expr else {
let _ = ')'.parse_next(i)?;
let _ = ')'.parse_next(i).map_err(|e: ParseErr<'_>| e)?;
return Ok(WithSpan::new(Self::Tuple(vec![]), start));
};

let comma = ws(opt(peek(','))).parse_next(i)?;
if comma.is_none() {
let _ = ')'.parse_next(i)?;
let _ = ')'.parse_next(i).map_err(|e: ParseErr<'_>| e)?;
return Ok(WithSpan::new(Self::Group(Box::new(expr)), start));
}

Expand Down
35 changes: 18 additions & 17 deletions rinja_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,28 +311,23 @@ impl<'a> ErrorContext<'a> {
}

impl<'a> winnow::error::ParserError<&'a str> for ErrorContext<'a> {
#[allow(deprecated)]
fn from_error_kind(input: &&'a str, _code: winnow::error::ErrorKind) -> Self {
type Inner = Self;

fn from_input(input: &&'a str) -> Self {
Self {
span: (*input).into(),
message: None,
}
}

#[allow(deprecated)]
fn append(
self,
_: &&'a str,
_: &<&str as winnow::stream::Stream>::Checkpoint,
_: winnow::error::ErrorKind,
) -> Self {
self
#[inline(always)]
fn into_inner(self) -> Result<Self::Inner, Self> {
Ok(self)
}
}

impl<'a, E: std::fmt::Display> FromExternalError<&'a str, E> for ErrorContext<'a> {
#[allow(deprecated)]
fn from_external_error(input: &&'a str, _kind: winnow::error::ErrorKind, e: E) -> Self {
fn from_external_error(input: &&'a str, e: E) -> Self {
Self {
span: (*input).into(),
message: Some(Cow::Owned(e.to_string())),
Expand Down Expand Up @@ -447,7 +442,8 @@ fn num_lit<'a>(i: &mut &'a str) -> ParseResult<'a, Num<'a>> {
let int_with_base = (opt('-'), |i: &mut _| {
let (base, kind) = preceded('0', alt(('b'.value(2), 'o'.value(8), 'x'.value(16))))
.with_taken()
.parse_next(i)?;
.parse_next(i)
.map_err(|e: ParseErr<'_>| e)?;
match opt(separated_digits(base, false)).parse_next(i)? {
Some(_) => Ok(()),
None => Err(winnow::error::ErrMode::Cut(ErrorContext::new(
Expand All @@ -462,7 +458,9 @@ fn num_lit<'a>(i: &mut &'a str) -> ParseResult<'a, Num<'a>> {
let float = |i: &mut &'a str| -> ParseResult<'a, ()> {
let has_dot = opt(('.', separated_digits(10, true))).parse_next(i)?;
let has_exp = opt(|i: &mut _| {
let (kind, op) = (one_of(['e', 'E']), opt(one_of(['+', '-']))).parse_next(i)?;
let (kind, op) = (one_of(['e', 'E']), opt(one_of(['+', '-'])))
.parse_next(i)
.map_err(|e: ParseErr<'_>| e)?;
match opt(separated_digits(10, op.is_none())).parse_next(i)? {
Some(_) => Ok(()),
None => Err(winnow::error::ErrMode::Cut(ErrorContext::new(
Expand Down Expand Up @@ -559,7 +557,8 @@ fn str_lit_without_prefix<'a>(i: &mut &'a str) -> ParseResult<'a> {
opt(take_escaped(take_till(1.., ['\\', '"']), '\\', any)),
'"',
)
.parse_next(i)?;
.parse_next(i)
.map_err(|e: ParseErr<'_>| e)?;
Ok(s.unwrap_or_default())
}

Expand Down Expand Up @@ -596,7 +595,8 @@ fn char_lit<'a>(i: &mut &'a str) -> ParseResult<'a, CharLit<'a>> {
'\'',
),
)
.parse_next(i)?;
.parse_next(i)
.map_err(|e: ParseErr<'_>| e)?;

let Some(s) = s else {
i.reset(&start);
Expand Down Expand Up @@ -754,7 +754,8 @@ impl State<'_, '_> {
peek(delimited('%', alt(('-', '~', '+')).map(Some), '}')),
fail, // rollback on partial matches in the previous line
))
.parse_next(i)?;
.parse_next(i)
.map_err(|e: ParseErr<'_>| e)?;
if let Some(control) = control {
let message = format!(
"unclosed block, you likely meant to apply whitespace control: \"{}{}\"",
Expand Down
10 changes: 5 additions & 5 deletions rinja_parser/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use winnow::{ModalParser, Parser};

use crate::memchr_splitter::{Splitter1, Splitter2, Splitter3};
use crate::{
ErrorContext, Expr, Filter, ParseResult, Span, State, Target, WithSpan, filter, identifier,
is_rust_keyword, keyword, skip_till, skip_ws0, str_lit_without_prefix, ws,
ErrorContext, Expr, Filter, ParseErr, ParseResult, Span, State, Target, WithSpan, filter,
identifier, is_rust_keyword, keyword, skip_till, skip_ws0, str_lit_without_prefix, ws,
};

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -56,7 +56,7 @@ impl<'a> Node<'a> {
}
};
opt(|i: &mut _| unexpected_tag(i, s)).parse_next(i)?;
let is_eof = opt(eof).parse_next(i)?;
let is_eof = opt(eof).parse_next(i).map_err(|e: ParseErr<'_>| e)?;
if is_eof.is_none() {
return Err(winnow::error::ErrMode::Cut(ErrorContext::new(
"cannot parse entire template\n\
Expand Down Expand Up @@ -1076,7 +1076,7 @@ pub struct Lit<'a> {
impl<'a> Lit<'a> {
fn parse(i: &mut &'a str, s: &State<'_, '_>) -> ParseResult<'a, WithSpan<'a, Self>> {
let start = *i;
not(eof).parse_next(i)?;
not(eof).parse_next(i).map_err(|e: ParseErr<'_>| e)?;

let candidate_finder = Splitter3::new(
s.syntax.block_start,
Expand All @@ -1096,7 +1096,7 @@ impl<'a> Lit<'a> {
return fail.parse_next(i);
}
Some(content) => content,
None => rest.parse_next(i)?, // there is no {block,comment,expr}_start: take everything
None => rest.parse_next(i).map_err(|e: ParseErr<'_>| e)?, /* there is no {block,comment,expr}_start: take everything */
};
Ok(WithSpan::new(Self::split_ws_parts(content), start))
}
Expand Down

0 comments on commit 53d2ab0

Please sign in to comment.