Skip to content

Commit

Permalink
docs: add ignore empty lines example
Browse files Browse the repository at this point in the history
  • Loading branch information
guissalustiano committed Aug 14, 2022
1 parent 761ab0a commit 56a3cdc
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions doc/nom_recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ These are short recipes for accomplishing common tasks with nom.

* [Whitespace](#whitespace)
+ [Wrapper combinators that eat whitespace before and after a parser](#wrapper-combinators-that-eat-whitespace-before-and-after-a-parser)
+ [Ignore empty lines](#ignore-empty-lines)
* [Comments](#comments)
+ [`// C++/EOL-style comments`](#-ceol-style-comments)
+ [`/* C-style comments */`](#-c-style-comments-)
Expand Down Expand Up @@ -52,6 +53,41 @@ Likewise, the eat only leading whitespace, replace `delimited(...)` with `preced
&inner)`. You can use your own parser instead of `multispace0` if you want to skip a different set
of lexemes.

### Ignore empty lines
```rust
use nom::{
InputLength,
error::ParseError,
combinator::map,
multi::separated_list1,
branch::alt,
IResult, Parser
};

pub fn separated_lines_ignore_empty<I, O, O2, O3, F, E>(
sep: impl FnMut(I) -> IResult<I, O2>,
f: impl FnMut(I) -> IResult<I, O>,
ignore: impl FnMut(I) -> IResult<I, O3>,
) -> impl FnMut(I) -> IResult<I, Vec<O>>
where
I: Clone + InputLength,
F: Parser<I, O, E>,
E: ParseError<I>,
{
map(
separated_list1(
sep,
alt((
map(f, |l| Some(l)),
map(ignore, |_| None)
))
),
|ls| ls.into_iter().flatten().collect()
)
}

```

## Comments

### `// C++/EOL-style comments`
Expand Down

0 comments on commit 56a3cdc

Please sign in to comment.