The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Get AST Style Locations During Text Parsing With nom_locate

TODO: Pull subtitle into page object
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(),
        ),
    ))
}

Notes

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Get AST Style Locations During Text Parsing With nom_locate

-- code
-- 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(),
        ),
    ))
}


-- notes

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


-- categories
-- Rust 
-- nom 

-- metadata
-- date: 2023-09-27 23:47:07
-- id: 2w0cehwu
-- site: aws
-- type: snippet 
-- status: scratch