Crates.io | nom-packrat |
lib.rs | nom-packrat |
version | 0.7.0 |
source | src |
created_at | 2019-07-22 03:55:00.820184 |
updated_at | 2023-03-23 00:29:41.255883 |
description | Extension of nom to apply Packrat Parsing |
homepage | |
repository | https://github.com/dalance/nom-packrat |
max_upload_size | |
id | 150682 |
size | 30,188 |
Extension of nom to apply "Packrat Parsing".
nom must be 5.0.0 or later. nom-packrat can be applied to function-style parser only.
[dependencies]
nom-packrat = "0.7.0"
use nom::character::complete::char;
use nom::IResult;
use nom_packrat::{init, packrat_parser, storage};
// Declare storage used by packrat_parser
storage!(String);
// Apply packrat_parser by custom attribute
#[packrat_parser]
pub fn parser(s: &str) -> IResult<&str, String> {
let (s, x) = char('a')(s)?;
Ok((s, x.to_string()))
}
fn main() {
let input = "a";
// Initialize before parsing
init!();
let result = parser(input);
println!("{:?}", result);
}
<S> ::= <T> + <S> | <T> - <S> | <T>
<T> ::= ( <S> ) | a
The following 8 patterns. The first pattern is named as "pair 0" and the last is "pair 7".
a
(a)
((a))
(((a)))
((((a))))
(((((a)))))
((((((a))))))
(((((((a)))))))
#[packrat_parser]
<T>
with #[packrat_parser]
This is an edge case. The execution time of the original parser increases exponentially. By packrat pasring, the time becomes linear. Instead packrat parsers consume more memory than the original parser.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.