https://github.com/dalance/nom-recursive
Extension of nom to handle left recursion
https://github.com/dalance/nom-recursive
Last synced: 10 months ago
JSON representation
Extension of nom to handle left recursion
- Host: GitHub
- URL: https://github.com/dalance/nom-recursive
- Owner: dalance
- License: apache-2.0
- Created: 2019-07-25T11:32:53.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2025-02-03T20:16:40.000Z (over 1 year ago)
- Last Synced: 2025-04-13T10:50:51.588Z (about 1 year ago)
- Language: Rust
- Homepage:
- Size: 31.3 KB
- Stars: 16
- Watchers: 3
- Forks: 5
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# nom-recursive
Extension of [nom](https://github.com/Geal/nom) to handle left recursion.
[](https://github.com/dalance/nom-recursive/actions)
[](https://crates.io/crates/nom-recursive)
[](https://docs.rs/nom-recursive)
## Requirement
nom must be 5.0.0 or later.
nom-recursive can be applied to function-style parser only.
The input type of nom parser must implement `HasRecursiveInfo` trait.
Therefore `&str` and `&[u8]` can't be used.
You can define a wrapper type of `&str` or `&[u8]` and implement `HasRecursiveInfo`.
Alternatively you can use `nom_locate::LocatedSpan`.
This implements `HasRecursiveInfo` in this crate.
## Usage
```Cargo.toml
[dependencies]
nom-recursive = "0.5.1"
```
## Example
```rust
use nom::branch::*;
use nom::character::complete::*;
use nom::IResult;
use nom_locate::LocatedSpan;
use nom_recursive::{recursive_parser, RecursiveInfo};
// Input type must implement trait HasRecursiveInfo
// nom_locate::LocatedSpan implements it.
type Span<'a> = LocatedSpan<&'a str, RecursiveInfo>;
pub fn expr(s: Span) -> IResult {
alt((expr_binary, term))(s)
}
// Apply recursive_parser by custom attribute
#[recursive_parser]
pub fn expr_binary(s: Span) -> IResult {
let (s, x) = expr(s)?;
let (s, y) = char('+')(s)?;
let (s, z) = expr(s)?;
let ret = format!("{}{}{}", x, y, z);
Ok((s, ret))
}
pub fn term(s: Span) -> IResult {
let (s, x) = char('1')(s)?;
Ok((s, x.to_string()))
}
fn main() {
let ret = expr(LocatedSpan::new_extra("1+1", RecursiveInfo::new()));
println!("{:?}", ret.unwrap().1);
}
```
## License
Licensed under either of
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.
### Contribution
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.