Skip to content

Commit

Permalink
refactor(parser): Upgrade to Winnow 0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Dec 20, 2024
1 parent 47a7334 commit 64b4893
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
2 changes: 1 addition & 1 deletion rinja_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ harness = false
[dependencies]
memchr = "2"
serde = { version = "1.0", optional = true, features = ["derive"] }
winnow = "0.5"
winnow = "0.6"

[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 @@ -469,7 +469,7 @@ impl<'a> Expr<'a> {

fn num(i: &mut &'a str) -> ParseResult<'a, WithSpan<'a, Self>> {
let start = *i;
let (num, full) = num_lit.with_recognized().parse_next(i)?;
let (num, full) = num_lit.with_taken().parse_next(i)?;
Ok(WithSpan::new(Expr::NumLit(full, num), start))
}

Expand Down Expand Up @@ -647,7 +647,7 @@ impl<'a> Suffix<'a> {
preceded(
(ws('!'), '('),
cut_err(terminated(
nested_parenthesis.recognize().map(Self::MacroCall),
nested_parenthesis.take().map(Self::MacroCall),
')',
)),
)
Expand Down
31 changes: 18 additions & 13 deletions rinja_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::sync::Arc;
use std::{fmt, str};

use winnow::Parser;
use winnow::ascii::escaped;
use winnow::ascii::take_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 _};
Expand Down Expand Up @@ -312,7 +312,12 @@ impl<'a> winnow::error::ParserError<&'a str> for ErrorContext<'a> {
}
}

fn append(self, _: &&'a str, _: ErrorKind) -> Self {
fn append(
self,
_: &&'a str,
_: &<&str as winnow::stream::Stream>::Checkpoint,
_: ErrorKind,
) -> Self {
self
}
}
Expand Down Expand Up @@ -394,7 +399,7 @@ fn identifier<'i>(input: &mut &'i str) -> ParseResult<'i> {
c.is_alphanum() || c == '_' || c >= '\u{0080}'
});

(start, opt(tail)).recognize().parse_next(input)
(start, opt(tail)).take().parse_next(input)
}

fn bool_lit<'i>(i: &mut &'i str) -> ParseResult<'i> {
Expand Down Expand Up @@ -434,7 +439,7 @@ fn num_lit<'a>(i: &mut &'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: &mut _| {
let (base, kind) = preceded('0', alt(('b'.value(2), 'o'.value(8), 'x'.value(16))))
.with_recognized()
.with_taken()
.parse_next(i)?;
match opt(separated_digits(base, false)).parse_next(i)? {
Some(_) => Ok(()),
Expand Down Expand Up @@ -469,13 +474,13 @@ fn num_lit<'a>(i: &mut &'a str) -> ParseResult<'a, Num<'a>> {
}
};

let num = if let Ok(Some(num)) = opt(int_with_base.recognize()).parse_next(i) {
let num = if let Ok(Some(num)) = opt(int_with_base.take()).parse_next(i) {
let suffix =
opt(|i: &mut _| num_lit_suffix("integer", INTEGER_TYPES, start, i)).parse_next(i)?;
Num::Int(num, suffix)
} else {
let (float, num) = preceded((opt('-'), separated_digits(10, true)), opt(float))
.with_recognized()
.with_taken()
.parse_next(i)?;
if float.is_some() {
let suffix =
Expand Down Expand Up @@ -508,7 +513,7 @@ fn separated_digits<'a>(
one_of(move |ch: char| ch.is_digit(radix)),
repeat(0.., one_of(move |ch: char| ch == '_' || ch.is_digit(radix))).map(|()| ()),
)
.recognize()
.take()
}

#[derive(Clone, Copy, Debug, PartialEq)]
Expand Down Expand Up @@ -544,7 +549,7 @@ pub struct StrLit<'a> {
fn str_lit_without_prefix<'a>(i: &mut &'a str) -> ParseResult<'a> {
let s = delimited(
'"',
opt(escaped(take_till(1.., ['\\', '"']), '\\', any)),
opt(take_escaped(take_till(1.., ['\\', '"']), '\\', any)),
'"',
)
.parse_next(i)?;
Expand Down Expand Up @@ -580,22 +585,22 @@ fn char_lit<'a>(i: &mut &'a str) -> ParseResult<'a, CharLit<'a>> {
opt('b'),
delimited(
'\'',
opt(escaped(take_till(1.., ['\\', '\'']), '\\', any)),
opt(take_escaped(take_till(1.., ['\\', '\'']), '\\', any)),
'\'',
),
)
.parse_next(i)?;

let Some(s) = s else {
i.reset(start);
i.reset(&start);
return Err(winnow::error::ErrMode::Cut(ErrorContext::new(
"empty character literal",
*i,
)));
};
let mut is = s;
let Ok(c) = Char::parse(&mut is) else {
i.reset(start);
i.reset(&start);
return Err(winnow::error::ErrMode::Cut(ErrorContext::new(
"invalid character",
*i,
Expand Down Expand Up @@ -626,11 +631,11 @@ fn char_lit<'a>(i: &mut &'a str) -> ParseResult<'a, CharLit<'a>> {
};

let Ok(nb) = u32::from_str_radix(nb, 16) else {
i.reset(start);
i.reset(&start);
return Err(winnow::error::ErrMode::Cut(ErrorContext::new(err1, *i)));
};
if nb > max_value {
i.reset(start);
i.reset(&start);
return Err(winnow::error::ErrMode::Cut(ErrorContext::new(err2, *i)));
}

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 @@ -315,7 +315,7 @@ impl<'a> When<'a> {
),
),
))
.with_recognized()
.with_taken()
.map(|((pws, _), span)| {
// A comment node is used to pass the whitespace suppressing information to the
// generator. This way we don't have to fix up the next `when` node or the closing
Expand Down Expand Up @@ -1071,7 +1071,7 @@ impl<'a> Lit<'a> {
literal(s.syntax.expr_start),
));

let content = opt(skip_till(candidate_finder, p_start).recognize()).parse_next(i)?;
let content = opt(skip_till(candidate_finder, p_start).take()).parse_next(i)?;
let content = match content {
Some("") => {
// {block,comment,expr}_start follows immediately.
Expand Down Expand Up @@ -1121,7 +1121,7 @@ impl<'a> Raw<'a> {
(
opt(Whitespace::parse),
|i: &mut _| s.tag_block_end(i),
skip_till(Splitter1::new(s.syntax.block_start), endraw).with_recognized(),
skip_till(Splitter1::new(s.syntax.block_start), endraw).with_taken(),
),
),
);
Expand Down Expand Up @@ -1378,13 +1378,13 @@ fn end_node<'a, 'g: 'a>(
if actual == expected {
Ok(actual)
} else if actual.starts_with("end") {
i.reset(start);
i.reset(&start);
Err(winnow::error::ErrMode::Cut(ErrorContext::new(
format!("expected `{expected}` to terminate `{node}` node, found `{actual}`"),
*i,
)))
} else {
i.reset(start);
i.reset(&start);
fail.parse_next(i)
}
}
Expand Down
4 changes: 2 additions & 2 deletions rinja_parser/src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl<'a> Target<'a> {
str_lit.map(Self::StrLit),
char_lit.map(Self::CharLit),
num_lit
.with_recognized()
.with_taken()
.map(|(num, full)| Target::NumLit(full, num)),
bool_lit.map(Self::BoolLit),
))
Expand All @@ -135,7 +135,7 @@ impl<'a> Target<'a> {

fn named(i: &mut &'a str, s: &State<'_>) -> ParseResult<'a, (&'a str, Self)> {
let start = *i;
let rest = opt(Self::rest.with_recognized()).parse_next(i)?;
let rest = opt(Self::rest.with_taken()).parse_next(i)?;
if let Some(rest) = rest {
let chr = peek(ws(opt(one_of([',', ':'])))).parse_next(i)?;
if let Some(chr) = chr {
Expand Down

0 comments on commit 64b4893

Please sign in to comment.