-
Hello, I'm porting ANTRL based parser to winnow Stateful parser, and so far so good, but now I'm stumbled into case which I can't figure out how to make this work. This is minimized example, based on winnow The Stateful The identifier works and e.g. passes unit tests, but it can not be used with any combinator. use winnow::prelude::*;
use winnow::{stream::AsChar, token::take_while, token::one_of, seq, Stateful};
#[derive(Debug)]
struct State {}
pub type InputState<'is> = Stateful<&'is str, &'is mut State>;
pub fn identifier<'is>(is: &'is mut InputState) -> PResult<&'is str> {
(
one_of(|c: char| c.is_alpha() || c == '_'),
take_while(0.., |c: char| c.is_alphanum() || c == '_')
)
.take()
.parse_next(is)
}
fn use_id_parser<'is>(is: &'is mut InputState) -> PResult<&'is str> {
let m = seq!(
identifier,
).parse_next(is)?;
Ok(m.0)
}
fn main() {
let mut input = &mut "hello";
let mut state = State {};
let mut is = InputState { input: &mut input, state: &mut state };
use_id_parser(&mut is).unwrap();
}
Used environment is:
This is for Tackler-NG, and there are about ~40 other parser functions implemented at the moment (in private repo). Those other parsers work as expected, but can't figure out this one. Is it something to do with character matching? Any help would truly appreciated! // Update: I found a work-around: For 'id' parsing, there is no need for State, so I dropped the state away from 'id' parser. This works now, but in any case it would be nice to understand what is going on here. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
pub fn identifier<'s>(input: &mut &'s str) -> PResult<&'s str> {
// vs
pub fn identifier<'is>(is: &'is mut InputState) -> PResult<&'is str> { A key difference between these is of lifetimes. The signature should be pub fn identifier<'is>(is: &mut InputState<'is>) -> PResult<&'is str> { I'd recommend having the following in your [package.lints.rust]
rust_2018_idioms = { level = "warn", priority = -1 } As it would highlight that |
Beta Was this translation helpful? Give feedback.
-
Update how it went (TL:DR: winnow rocks!) The parser benchmark results are like this:
Overall processing speed increased 2-3 times (from 40_000 txn/s to 120_000 txn/s on slow machine and 120_000 to 250_000 txn/s on fast system). If someone likes to take look, the parser implementation is here. Thanks again for all help and making winnow! |
Beta Was this translation helpful? Give feedback.
A key difference between these is of lifetimes. The signature should be
I'd recommend having the following in your
Cargo.toml
file:As it would highlight that
InputState
did not have a lifetime specified (even if it can usually be<'_>
). This likely would have made the difference in lifetimes more obvious.