Skip to content

Commit

Permalink
update example in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
pontussjostedt committed Dec 22, 2023
1 parent 9a57067 commit 71f079f
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ The output structure of your parser may contain the position as a `Span` (which
````rust
struct Token<'a> {
pub position: Span<'a>,
pub foo: &'a str,
pub bar: &'a str,
pub _foo: &'a str,
pub _bar: &'a str,
}
````

Expand All @@ -96,8 +96,8 @@ fn parse_foobar(s: Span) -> IResult<Span, Token> {
s,
Token {
position: pos,
foo: foo.fragment,
bar: bar.fragment,
_foo: foo.fragment(),
_bar: bar.fragment(),
},
))
}
Expand All @@ -108,14 +108,17 @@ fn parse_foobar(s: Span) -> IResult<Span, Token> {
The parser returns a `nom::IResult<Token, _>` (hence the `unwrap().1`). The `position` property contains the `offset`, `line` and `column`.

````rust
fn main () {
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, unsafe {
Span::new_from_raw_offset(
14, // offset
2, // line
"", // fragment
(), // extra
)
});
assert_eq!(position.get_column(), 2);
}
Expand Down

0 comments on commit 71f079f

Please sign in to comment.