| 1 | # nom_locate |
| 2 | |
| 3 | [](https://coveralls.io/github/fflorent/nom_locate?branch=master) |
| 4 | [](https://crates.io/crates/nom_locate) |
| 5 | |
| 6 | A special input type for [nom](https://github.com/geal/nom) to locate tokens |
| 7 | |
| 8 | ## Documentation |
| 9 | |
| 10 | The documentation of the crate is available [here](https://docs.rs/nom_locate/). |
| 11 | |
| 12 | ## How to use it |
| 13 | The crate provide the [`LocatedSpan` struct](https://docs.rs/nom_locate/latest/nom_locate/struct.LocatedSpan.html) that encapsulates the data. Look at the below example and the explanations: |
| 14 | |
| 15 | ````rust |
| 16 | extern crate nom; |
| 17 | extern crate nom_locate; |
| 18 | |
| 19 | use nom::bytes::complete::{tag, take_until}; |
| 20 | use nom::IResult; |
| 21 | use nom_locate::{position, LocatedSpan}; |
| 22 | |
| 23 | type Span<'a> = LocatedSpan<&'a str>; |
| 24 | struct Token<'a> { |
| 25 | pub position: Span<'a>, |
| 26 | pub _foo: &'a str, |
| 27 | pub _bar: &'a str, |
| 28 | } |
| 29 | |
| 30 | fn parse_foobar(s: Span) -> IResult<Span, Token> { |
| 31 | let (s, _) = take_until("foo")(s)?; |
| 32 | let (s, pos) = position(s)?; |
| 33 | let (s, foo) = tag("foo")(s)?; |
| 34 | let (s, bar) = tag("bar")(s)?; |
| 35 | |
| 36 | Ok(( |
| 37 | s, |
| 38 | Token { |
| 39 | position: pos, |
| 40 | _foo: foo.fragment(), |
| 41 | _bar: bar.fragment(), |
| 42 | }, |
| 43 | )) |
| 44 | } |
| 45 | |
| 46 | fn main() { |
| 47 | let input = Span::new("Lorem ipsum \n foobar"); |
| 48 | let output = parse_foobar(input); |
| 49 | let position = output.unwrap().1.position; |
| 50 | assert_eq!(position, unsafe { |
| 51 | Span::new_from_raw_offset( |
| 52 | 14, // offset |
| 53 | 2, // line |
| 54 | "", // fragment |
| 55 | (), // extra |
| 56 | ) |
| 57 | }); |
| 58 | assert_eq!(position.get_column(), 2); |
| 59 | } |
| 60 | ```` |
| 61 | |
| 62 | ### Import |
| 63 | |
| 64 | Import [nom](https://github.com/geal/nom) and nom_locate. |
| 65 | |
| 66 | ````rust |
| 67 | extern crate nom; |
| 68 | extern crate nom_locate; |
| 69 | |
| 70 | use nom::bytes::complete::{tag, take_until}; |
| 71 | use nom::IResult; |
| 72 | use nom_locate::{position, LocatedSpan}; |
| 73 | ```` |
| 74 | |
| 75 | Also you'd probably create [type alias](https://doc.rust-lang.org/book/type-aliases.html) for convenience so you don't have to specify the `fragment` type every time: |
| 76 | |
| 77 | ````rust |
| 78 | type Span<'a> = LocatedSpan<&'a str>; |
| 79 | ```` |
| 80 | |
| 81 | ### Define the output structure |
| 82 | |
| 83 | 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). |
| 84 | |
| 85 | ````rust |
| 86 | struct Token<'a> { |
| 87 | pub position: Span<'a>, |
| 88 | pub _foo: &'a str, |
| 89 | pub _bar: &'a str, |
| 90 | } |
| 91 | ```` |
| 92 | |
| 93 | ### Create the parser |
| 94 | |
| 95 | 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: |
| 96 | |
| 97 | ````rust |
| 98 | fn parse_foobar(s: Span) -> IResult<Span, Token> { |
| 99 | let (s, _) = take_until("foo")(s)?; |
| 100 | let (s, pos) = position(s)?; |
| 101 | let (s, foo) = tag("foo")(s)?; |
| 102 | let (s, bar) = tag("bar")(s)?; |
| 103 | |
| 104 | Ok(( |
| 105 | s, |
| 106 | Token { |
| 107 | position: pos, |
| 108 | _foo: foo.fragment(), |
| 109 | _bar: bar.fragment(), |
| 110 | }, |
| 111 | )) |
| 112 | } |
| 113 | ```` |
| 114 | |
| 115 | ### Call the parser |
| 116 | |
| 117 | The parser returns a `nom::IResult<Token, _>` (hence the `unwrap().1`). The `position` property contains the `offset`, `line` and `column`. |
| 118 | |
| 119 | ````rust |
| 120 | fn main() { |
| 121 | let input = Span::new("Lorem ipsum \n foobar"); |
| 122 | let output = parse_foobar(input); |
| 123 | let position = output.unwrap().1.position; |
| 124 | assert_eq!(position, unsafe { |
| 125 | Span::new_from_raw_offset( |
| 126 | 14, // offset |
| 127 | 2, // line |
| 128 | "", // fragment |
| 129 | (), // extra |
| 130 | ) |
| 131 | }); |
| 132 | assert_eq!(position.get_column(), 2); |
| 133 | } |
| 134 | ```` |