https://github.com/hatoo/stap
Stateful parser
https://github.com/hatoo/stap
async parse parser rust
Last synced: 11 months ago
JSON representation
Stateful parser
- Host: GitHub
- URL: https://github.com/hatoo/stap
- Owner: hatoo
- License: mit
- Created: 2024-05-18T07:59:05.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-06-03T05:41:20.000Z (about 2 years ago)
- Last Synced: 2025-07-05T07:44:17.723Z (12 months ago)
- Topics: async, parse, parser, rust
- Language: Rust
- Homepage:
- Size: 65.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# stap
[](https://crates.io/crates/stap)
`stap` (**sta**teful **p**arser) is a library of stateful parser for streaming input using `async` syntax.
When a input isn't enough for parsing, `stap` remembers the curent state and waits to more inputs and parses them when the input is supplied.
```rust
use futures::FutureExt;
use stap::{many1, Anchor, Cursor, Input, Parsing};
fn main() {
let mut input = Input::new(Cursor {
buf: Vec::::new(),
index: 0,
});
let mut parsing = Parsing::new(&mut input, |mut iref| {
async move {
// anchoring the current index
// restoring the index if parsing fails (= when dropped)
let mut anchor = Anchor::new(&mut iref);
let num = many1(&mut anchor, |b| b.is_ascii_digit()).await?;
// Since the parser holds state, this output is showed up only once.
dbg!(&num);
let alpha = many1(&mut anchor, |b| b.is_ascii_alphabetic()).await?;
dbg!(&alpha);
// parsing is successful, so we can forget the anchor in other words, current index of the input is valid.
anchor.forget();
Ok::<_, ()>((num, alpha))
}
// This makes the future `Unpin`, currently this is required but any workaround is welcome.
.boxed_local()
});
// There is no input to parse, so parsing should fail.
assert!(!parsing.poll());
parsing.cursor_mut().buf.extend_from_slice(b"123a");
// The parser recognizing the input as a number followed by an alphabet. But because of there may be more alphabets, it should fail.
assert!(!parsing.poll());
parsing.cursor_mut().buf.extend_from_slice(b"bc;");
// the parser should ends.
assert!(parsing.poll());
dbg!(parsing.into_result());
}
```