Crate nom_locate

source ·
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.

#[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);
}
fn main() {}

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
Implement nom::ExtendInto for a specific fragment type.
Implement nom::InputIter for a specific fragment type
Implement nom::Slice for a specific fragment type and range type.
Implement nom::Slice for a specific fragment type and for these types of range:
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