Docs.rs
  • nom-recursive-0.5.1
    • nom-recursive 0.5.1
    • Permalink
    • Docs.rs crate page
    • MIT OR Apache-2.0
    • Links
    • Repository
    • crates.io
    • Source
    • Owners
    • dalance
    • Dependencies
      • nom-recursive-macros ^0.5.1 normal
      • nom_locate ^4 normal
      • nom ^7 dev
    • Versions
    • 20% of the crate is documented
  • Platform
    • i686-pc-windows-msvc
    • i686-unknown-linux-gnu
    • x86_64-apple-darwin
    • x86_64-pc-windows-msvc
    • x86_64-unknown-linux-gnu
  • Feature flags
  • docs.rs
    • About docs.rs
    • Privacy policy
  • Rust
    • Rust website
    • The Book
    • Standard Library API Reference
    • Rust by Example
    • The Cargo Guide
    • Clippy Documentation

Crate nom_recursive

nom_recursive0.5.1

  • All Items

Sections

  • Examples

Crate Items

  • Structs
  • Constants
  • Traits
  • Attribute Macros

Crates

  • nom_recursive

Crate nom_recursive

Source
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§

RecursiveIndexes
RecursiveInfo
The type of payload used by recursive tracer

Constants§

RECURSIVE_STORAGE

Traits§

HasRecursiveInfo
Trait for recursive tracer

Attribute Macros§

recursive_parser
Custom attribute for recursive parser

Results

Settings
Help

Query parser error: "Unexpected - (did you mean ->?)".