Crate nom_locate[][src]

Expand description

nom_locate, a special input type to locate tokens

The source code is available on Github

Features

This crate exposes two cargo feature flags, generic-simd and runtime-dispatch-simd. These correspond to the features exposed by bytecount.

How to use it

The explanations are given in the README of the Github repository. You may also consult the FAQ.

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.location_offset(), 14);
    assert_eq!(position.location_line(), 2);
    assert_eq!(position.fragment(), &"");
    assert_eq!(position.get_column(), 2);
}

Extra information

You can also add arbitrary extra information using the extra property of LocatedSpan. This property is not used when comparing two LocatedSpans.

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

let input = Span::new("Lorem ipsum \n foobar", "filename");
let output = parse_foobar(input);
let extra = output.unwrap().1.extra;

Macros

impl_compareDeprecated
impl_input_iterDeprecated

Capture the position of the current fragment

Structs

A LocatedSpan is a set of meta information about the location of a token, including extra information.

Functions

Capture the position of the current fragment