Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Get AST Style Locations During Text Parsing With nom_locate

Code

#!/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(),
        ),
    ))
}