Expand description
nom-recursive
is an extension of nom to handle left recursion.
§Examples
The following example show a quick example.
If #[recursive_parser]
is removed, stack overflow will occur because of infinite recursion.
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<T, RecursiveInfo> implements it.
type Span<'a> = LocatedSpan<&'a str, RecursiveInfo>;
pub fn expr(s: Span) -> IResult<Span, String> {
alt((expr_binary, term))(s)
}
// Apply recursive_parser by custom attribute
#[recursive_parser]
pub fn expr_binary(s: Span) -> IResult<Span, String> {
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<Span, String> {
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);
}
Structs§
- Recursive
Indexes - Recursive
Info - The type of payload used by recursive tracer
Constants§
Traits§
- HasRecursive
Info - Trait for recursive tracer
Attribute Macros§
- recursive_
parser - Custom attribute for recursive parser