15 releases (9 stable)

4.2.0 Aug 22, 2023
4.1.0 Jan 17, 2023
4.0.0 Aug 29, 2021
3.0.2 Jul 14, 2021
0.1.0 Jun 24, 2017

#41 in Parser implementations

Download history 36401/week @ 2024-07-17 44875/week @ 2024-07-24 42522/week @ 2024-07-31 42356/week @ 2024-08-07 39297/week @ 2024-08-14 37776/week @ 2024-08-21 36939/week @ 2024-08-28 44529/week @ 2024-09-04 40310/week @ 2024-09-11 42571/week @ 2024-09-18 40562/week @ 2024-09-25 44427/week @ 2024-10-02 37224/week @ 2024-10-09 43423/week @ 2024-10-16 44745/week @ 2024-10-23 155471/week @ 2024-10-30

289,065 downloads per month
Used in 257 crates (100 directly)

MIT license

47KB
977 lines

nom_locate

Coverage Status

A special input type for nom to locate tokens

Documentation

The documentation of the crate is available here.

How to use it

The crate provide the LocatedSpan struct that encapsulates the data. Look at the below example and the explanations:

#[macro_use]
extern crate nom;
#[macro_use]
extern crate nom_locate;

use nom_locate::LocatedSpan;
type Span<'a> = LocatedSpan<&'a str>;

struct Token<'a> {
    pub position: Span<'a>,
    pub foo: String,
    pub bar: String,
}

named!(parse_foobar( Span ) -> Token, do_parse!(
    take_until!("foo") >>
    position: position!() >>
    foo: tag!("foo") >>
    bar: tag!("bar") >>
    (Token {
        position: position,
        foo: foo.to_string(),
        bar: bar.to_string()
    })
));

fn main () {
    let input = Span::new("Lorem ipsum \n foobar");
    let output = parse_foobar(input);
    let position = output.unwrap().1.position;
    assert_eq!(position.location_offset(), 14);
    assert_eq!(position.location_line(), 2);
    assert_eq!(position.fragment(), &"");
    assert_eq!(position.get_column(), 2);
}

Import

Import nom and nom_locate.

extern crate nom;
extern crate nom_locate;

use nom::bytes::complete::{tag, take_until};
use nom::IResult;
use nom_locate::{position, LocatedSpan};

Also you'd probably create type alias for convenience so you don't have to specify the fragment type every time:

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

Define the output structure

The output structure of your parser may contain the position as a Span (which provides the index, line and column information to locate your token).

struct Token<'a> {
    pub position: Span<'a>,
    pub foo: &'a str,
    pub bar: &'a str,
}

Create the parser

The parser has to accept a Span as an input. You may use position() in your nom parser, in order to capture the location of your token:

fn parse_foobar(s: Span) -> IResult<Span, Token> {
    let (s, _) = take_until("foo")(s)?;
    let (s, pos) = position(s)?;
    let (s, foo) = tag("foo")(s)?;
    let (s, bar) = tag("bar")(s)?;

    Ok((
        s,
        Token {
            position: pos,
            foo: foo.fragment,
            bar: bar.fragment,
        },
    ))
}

Call the parser

The parser returns a nom::IResult<Token, _> (hence the unwrap().1). The position property contains the offset, line and column.

fn main () {
    let input = Span::new("Lorem ipsum \n foobar");
    let output = parse_foobar(input);
    let position = output.unwrap().1.position;
    assert_eq!(position, Span {
        offset: 14,
        line: 2,
        fragment: ""
    });
    assert_eq!(position.get_column(), 2);
}

Dependencies

~1MB
~20K SLoC