diff --git a/doc/nom_recipes.md b/doc/nom_recipes.md index 1a767734a..122f49d59 100644 --- a/doc/nom_recipes.md +++ b/doc/nom_recipes.md @@ -1,4 +1,4 @@ -# Nom Recipes +# Common recipes to build nom parsers These are short recipes for accomplishing common tasks with nom. @@ -35,9 +35,9 @@ use nom::{ /// A combinator that takes a parser `inner` and produces a parser that also consumes both leading and /// trailing whitespace, returning the output of `inner`. -fn ws<'a, F, O, E: ParseError<&'a str>>(inner: F) -> impl FnMut(&'a str) -> IResult<&'a str, O, E> +fn ws<'a, F, O, E: ParseError<&'a str>>(inner: F) -> impl Parser<&'a str> where - F: Parser<&'a str, O, E>, + F: Parser<&'a str>, { delimited( multispace0, @@ -62,6 +62,7 @@ output of `()`. ```rust use nom::{ IResult, + Parser, error::ParseError, combinator::value, sequence::pair, @@ -74,7 +75,7 @@ pub fn peol_comment<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, value( (), // Output is thrown away. pair(char('%'), is_not("\n\r")) - )(i) + ).parse(i) } ``` @@ -86,6 +87,7 @@ and does not handle nested comments. ```rust use nom::{ IResult, + Parser, error::ParseError, combinator::value, bytes::complete::{tag, take_until}, @@ -99,7 +101,7 @@ pub fn pinline_comment<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a st take_until("*)"), tag("*)") ) - )(i) + ).parse(i) } ``` @@ -113,6 +115,7 @@ letters and numbers may be parsed like this: ```rust use nom::{ IResult, + Parser, branch::alt, multi::many0_count, combinator::recognize, @@ -127,7 +130,7 @@ pub fn identifier(input: &str) -> IResult<&str, &str> { alt((alpha1, tag("_"))), many0_count(alt((alphanumeric1, tag("_")))) ) - )(input) + ).parse(input) } ``` @@ -164,6 +167,7 @@ The parser outputs the string slice of the digits without the leading `0x`/`0X`. ```rust use nom::{ IResult, + Parser, branch::alt, multi::{many0, many1}, combinator::recognize, @@ -180,7 +184,7 @@ fn hexadecimal(input: &str) -> IResult<&str, &str> { // <'a, E: ParseError<&'a s terminated(one_of("0123456789abcdefABCDEF"), many0(char('_'))) ) ) - )(input) + ).parse(input) } ``` @@ -189,6 +193,7 @@ If you want it to return the integer value instead, use map: ```rust use nom::{ IResult, + Parser, branch::alt, multi::{many0, many1}, combinator::{map_res, recognize}, @@ -208,7 +213,7 @@ fn hexadecimal_value(input: &str) -> IResult<&str, i64> { ) ), |out: &str| i64::from_str_radix(&str::replace(&out, "_", ""), 16) - )(input) + ).parse(input) } ``` @@ -217,6 +222,7 @@ fn hexadecimal_value(input: &str) -> IResult<&str, i64> { ```rust use nom::{ IResult, + Parser, branch::alt, multi::{many0, many1}, combinator::recognize, @@ -233,7 +239,7 @@ fn octal(input: &str) -> IResult<&str, &str> { terminated(one_of("01234567"), many0(char('_'))) ) ) - )(input) + ).parse(input) } ``` @@ -242,6 +248,7 @@ fn octal(input: &str) -> IResult<&str, &str> { ```rust use nom::{ IResult, + Parser, branch::alt, multi::{many0, many1}, combinator::recognize, @@ -258,7 +265,7 @@ fn binary(input: &str) -> IResult<&str, &str> { terminated(one_of("01"), many0(char('_'))) ) ) - )(input) + ).parse(input) } ``` @@ -267,6 +274,7 @@ fn binary(input: &str) -> IResult<&str, &str> { ```rust use nom::{ IResult, + Parser, multi::{many0, many1}, combinator::recognize, sequence::terminated, @@ -278,7 +286,7 @@ fn decimal(input: &str) -> IResult<&str, &str> { many1( terminated(one_of("0123456789"), many0(char('_'))) ) - )(input) + ).parse(input) } ``` @@ -289,6 +297,7 @@ The following is adapted from [the Python parser by Valentin Lorentz (ProgVal)]( ```rust use nom::{ IResult, + Parser, branch::alt, multi::{many0, many1}, combinator::{opt, recognize}, @@ -331,7 +340,7 @@ fn float(input: &str) -> IResult<&str, &str> { opt(decimal) ) ) - ))(input) + )).parse(input) } fn decimal(input: &str) -> IResult<&str, &str> { @@ -339,7 +348,7 @@ fn decimal(input: &str) -> IResult<&str, &str> { many1( terminated(one_of("0123456789"), many0(char('_'))) ) - )(input) + ).parse(input) } ``` @@ -350,14 +359,14 @@ a common interface to parse from a string. ```rust use nom::{ - IResult, Finish, error::Error, + IResult, Parser, Finish, error::Error, bytes::complete::{tag, take_while}, }; use std::str::FromStr; // will recognize the name in "Hello, name!" fn parse_name(input: &str) -> IResult<&str, &str> { - let (i, _) = tag("Hello, ")(input)?; + let (i, _) = tag("Hello, ").parse(input)?; let (i, name) = take_while(|c:char| c.is_alphabetic())(i)?; let (i, _) = tag("!")(i)?; diff --git a/src/lib.rs b/src/lib.rs index a1c1ac890..295710192 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -377,7 +377,6 @@ //! check out the [recipes]! #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(feature = "docsrs", feature(doc_cfg))] -#![cfg_attr(feature = "docsrs", feature(extended_key_value_attributes))] #![allow(clippy::doc_markdown)] #![deny(missing_docs)] #[cfg_attr(nightly, warn(rustdoc::missing_doc_code_examples))] @@ -463,6 +462,6 @@ mod str; pub mod number; -#[cfg(feature = "docsrs")] -#[cfg_attr(feature = "docsrs", cfg_attr(feature = "docsrs", doc = include_str!("../doc/nom_recipes.md")))] +#[cfg(all(feature = "std", any(doc, doctest, feature = "docsrs")))] +#[cfg_attr(any(doc, doctest, feature = "docsrs"), doc = include_str!("../doc/nom_recipes.md"))] pub mod recipes {}