10 releases (4 stable)

3.0.0 Nov 25, 2020
2.1.0 Oct 26, 2020
2.0.0 Feb 12, 2020
1.0.0 Aug 14, 2019
0.1.0 Jun 24, 2017

#22 in Parser tooling

Download history 1276/week @ 2020-08-13 1605/week @ 2020-08-20 1774/week @ 2020-08-27 1056/week @ 2020-09-03 1144/week @ 2020-09-10 871/week @ 2020-09-17 815/week @ 2020-09-24 1167/week @ 2020-10-01 1547/week @ 2020-10-08 1414/week @ 2020-10-15 1710/week @ 2020-10-22 2137/week @ 2020-10-29 1456/week @ 2020-11-05 1267/week @ 2020-11-12 1985/week @ 2020-11-19 1354/week @ 2020-11-26

6,017 downloads per month
Used in 39 crates (21 directly)

MIT license

46KB
910 lines

nom_locate

Build Status 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:

extern crate nom;
extern crate nom_locate;

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

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

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

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

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: "",
            extra: (),
        }
    );
    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
~18K SLoC