Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Get AST Style Locations During Text Parsing With nom _ locate

rust
#!/usr/bin/env cargo +nightly -Zscript

//! ```cargo
//! [package]
//! edition = "2021"
//! [dependencies]
//! nom = { version = "7.1.3"}
//! nom_locate = { version = "4.2"}
//! ```

use nom::bytes::complete::is_not;
use nom::bytes::complete::tag;
use nom::character::complete::none_of;
use nom::character::complete::one_of;
use nom::multi::many1;
use nom::multi::separated_list1;
use nom::IResult;
use nom_locate::{position, LocatedSpan};

type Span<'a> = LocatedSpan<&'a str>;

#[derive(Debug)]
enum Token {
    Word(String, usize, usize),
}

fn main() {
    let source = Span::new("alfa\nbravo alfa");
    let result = parse(source);
    dbg!(result);
}

fn parse(s: Span) -> IResult<Span, Vec<Token>> {
    let (s, t) = separated_list1(one_of(" \n"), alfa)(s)?;
    Ok((s, t))
}

fn alfa(s: Span) -> IResult<Span, Token> {
    let (s, start) = position(s)?;
    let (s, val) = is_not(" \n")(s)?;
    let (s, end) = position(s)?;
    Ok((
        s,
        Token::Word(
            val.to_string(),
            start.location_offset(),
            end.location_offset(),
        ),
    ))
}

- not all those includes are required. I haven't gone thru the sample to clean them up yet