rust-bakery / nom

Rust parser combinator framework
MIT License
9.18k stars 792 forks source link

Suggestion: A character parser which eats any whitespace before and after a given parser #1653

Open LikeLakers2 opened 1 year ago

LikeLakers2 commented 1 year ago

Hi! I'm building parsers over a math-expression-like syntax (I'm trying out nom by building a calculator!), and I realized that a math expression cares not for whitespace.

I looked for solutions to ignoring whitespace before and after, and I came about nom's suggested solution, using nom::sequence::delimited with nom::character::complete::multispace0 as the first and third parameters.

However, while this solution works like a charm, I expect that many nom parsers over text will use this exact solution. So I suggest adding a new character parser, which I'll call trim_whitespace, which consumes any whitespace that's before and after a given parser. Using it, you could imagine shrinking this:

Old code ```rust use nom::{ IResult, character::complete::multispace0, combinator::map_res, number::complete::recognize_float, sequence::delimited, }; use rust_decimal::Decimal; pub fn scientific_number(input: &str) -> IResult<&str, Decimal> { delimited( multispace0, map_res(recognize_float, Decimal::from_scientific), multispace0, )(input) } ```

into this:

New code ```rust use nom::{ IResult, character::complete::trim_whitespace, combinator::map_res, number::complete::recognize_float, }; use rust_decimal::Decimal; pub fn scientific_number(input: &str) -> IResult<&str, Decimal> { trim_whitespace( map_res(recognize_float, Decimal::from_scientific), )(input) } ```

removing not just some lines of code, but additionally making it easier to build parsers for formats that don't care about whitespace. This also has the advantage of making it easier to see at a glance that "Oh, this parser ignores whitespace before and after what it's looking for."

If you accept my suggestion, thank you. :)

Prerequisites

Here are a few things you should provide to help me understand the issue: