| 1 |
extern crate nom; |
| 2 |
extern crate nom_locate; |
| 3 |
|
| 4 |
use nom::bytes::complete::{tag, take_until}; |
| 5 |
use nom::IResult; |
| 6 |
use nom_locate::{position, LocatedSpan}; |
| 7 |
|
| 8 |
type Span<'a> = LocatedSpan<&'a str>; |
| 9 |
|
| 10 |
struct Token<'a> { |
| 11 |
pub position: Span<'a>, |
| 12 |
pub _foo: &'a str, |
| 13 |
pub _bar: &'a str, |
| 14 |
} |
| 15 |
|
| 16 |
fn parse_foobar(s: Span) -> IResult<Span, Token> { |
| 17 |
let (s, _) = take_until("foo")(s)?; |
| 18 |
let (s, pos) = position(s)?; |
| 19 |
let (s, foo) = tag("foo")(s)?; |
| 20 |
let (s, bar) = tag("bar")(s)?; |
| 21 |
|
| 22 |
Ok(( |
| 23 |
s, |
| 24 |
Token { |
| 25 |
position: pos, |
| 26 |
_foo: foo.fragment(), |
| 27 |
_bar: bar.fragment(), |
| 28 |
}, |
| 29 |
)) |
| 30 |
} |
| 31 |
|
| 32 |
fn main() { |
| 33 |
let input = Span::new("Lorem ipsum \n foobar"); |
| 34 |
let output = parse_foobar(input); |
| 35 |
let position = output.unwrap().1.position; |
| 36 |
assert_eq!(position, unsafe { |
| 37 |
Span::new_from_raw_offset( |
| 38 |
14, // offset |
| 39 |
2, // line |
| 40 |
"", // fragment |
| 41 |
(), // extra |
| 42 |
) |
| 43 |
}); |
| 44 |
assert_eq!(position.get_column(), 2); |
| 45 |
} |