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 15, 2022
1 parent 761ab0a commit f61f26c
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions doc/nom_recipes.md
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,35 @@ 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::{
branch::alt,
combinator::map,
multi::{separated_list1, many1},
IResult,
InputLength,
};

pub fn separated_lines_ignore<I: Clone + InputLength, O, O2, O3>(
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>>
{
map(
separated_list1(
many1(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 f61f26c

Please sign in to comment.