Skip to content

A few doc improvments to /docs and others #1832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
6 changes: 3 additions & 3 deletions doc/choosing_a_combinator.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Those are used to recognize the lowest level elements of your grammar, like, "he

| combinator | usage | input | output | comment |
|---|---|---|---|---|
| [alt](https://docs.rs/nom/latest/nom/branch/fn.alt.html) | `alt((tag("ab"), tag("cd")))` | `"cdef"` | `Ok(("ef", "cd"))` |Try a list of parsers and return the result of the first successful one|
| [alt](https://docs.rs/nom/latest/nom/branch/fn.alt.html) | `alt((tag("ab"), tag("cd")))` | `"cdef"` | `Ok(("ef", "cd"))` |Try a list of parsers and return the result of the first successful one. All child parsers have the same `Output`.|
| [permutation](https://docs.rs/nom/latest/nom/branch/fn.permutation.html) | `permutation((tag("ab"), tag("cd"), tag("12")))` | `"cd12abc"` | `Ok(("c", ("ab", "cd", "12"))` |Succeeds when all its child parser have succeeded, whatever the order|

## Sequence combinators
Expand All @@ -38,7 +38,7 @@ Those are used to recognize the lowest level elements of your grammar, like, "he
| [terminated](https://docs.rs/nom/latest/nom/sequence/fn.terminated.html) | `terminated(tag("ab"), tag("XY"))` | `"abXYZ"` | `Ok(("Z", "ab"))` |Gets an object from the first parser, then matches an object from the second parser and discards it.|
| [pair](https://docs.rs/nom/latest/nom/sequence/fn.pair.html) | `pair(tag("ab"), tag("XY"))` | `"abXYZ"` | `Ok(("Z", ("ab", "XY")))` |Gets an object from the first parser, then gets another object from the second parser.|
| [separated_pair](https://docs.rs/nom/latest/nom/sequence/fn.separated_pair.html) | `separated_pair(tag("hello"), char(','), tag("world"))` | `"hello,world!"` | `Ok(("!", ("hello", "world")))` |Gets an object from the first parser, then matches an object from the sep_parser and discards it, then gets another object from the second parser.|
| [tuple](https://docs.rs/nom/latest/nom/sequence/fn.tuple.html) | `tuple((tag("ab"), tag("XY"), take(1)))` | `"abXYZ!"` | `Ok(("!", ("ab", "XY", "Z")))` | Chains parsers and assemble the sub results in a tuple. You can use as many child parsers as you can put elements in a tuple|
| tuples | `((tag("ab"), tag("XY"), take(1)))` | `"abXYZ!"` | `Ok(("!", ("ab", "XY", "Z")))` | `Parser` is implemented for tuples: it chains parsers and assemble the sub results in a tuple. You can use as many child parsers as you can put elements in a tuple|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove the doc link here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the thought was that the tuple combinator (which is linked to) is deprecated and the examples would just use tuples immediately


## Applying a parser multiple times

Expand Down Expand Up @@ -106,7 +106,7 @@ The following parsers could be found on [docs.rs number section](https://docs.rs

- [`escaped`](https://docs.rs/nom/latest/nom/bytes/complete/fn.escaped.html): Matches a byte string with escaped characters
- [`escaped_transform`](https://docs.rs/nom/latest/nom/bytes/complete/fn.escaped_transform.html): Matches a byte string with escaped characters, and returns a new string with the escaped characters replaced
- [`precedence`](https://docs.rs/nom/latest/nom/precedence/fn.precedence.html): Parses an expression with regards to operator precedence
- [`precedence`](https://docs.rs/nom-language/latest/nom_language/precedence/fn.precedence.html) (from `nom-language`): Parses an expression with regards to operator precedence

## Binary format parsing

Expand Down
2 changes: 2 additions & 0 deletions doc/custom_input_types.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
> **Note:** This document is for `nom7`. These traits were replaced in `nom8` by one `Input` trait, with the same principles.

# Custom input types

While historically, nom has worked mainly on `&[u8]` and `&str`, it can actually
Expand Down
8 changes: 4 additions & 4 deletions doc/error_management.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ println!(
);
```

### getting more information: nom::error::VerboseError
### getting more information: nom-language::error::VerboseError

The `VerboseError<I>` type accumulates more information about the chain of
The `VerboseError<I>` type (available from the companion `nom-language` crate) accumulates more information about the chain of
parsers that encountered an error:

```rust
Expand Down Expand Up @@ -187,7 +187,7 @@ println!("parsed verbose: {:#?}", json::<VerboseError<&str>>(data));
```

But by looking at the original input and the chain of errors, we can build
a more user friendly error message. The `nom::error::convert_error` function
a more user friendly error message. The `nom-language::error::convert_error` function
can build such a message.

```rust
Expand Down Expand Up @@ -232,7 +232,7 @@ information, like line and column.

#### nom-supreme

[nom-supreme](https://docs.rs/nom-supreme) provides the `ErrorTree<I>` error
[nom-supreme](https://docs.rs/nom-supreme) (not updated yet for `nom8`) provides the `ErrorTree<I>` error
type, that provides the same chain of parser errors as `VerboseError`, but also
accumulates errors from the various branches tried by `alt`.

Expand Down
4 changes: 2 additions & 2 deletions doc/making_a_new_parser_from_scratch.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use nom::bytes::complete::take;

pub fn length_value(input: &[u8]) -> IResult<&[u8],&[u8]> {
let (input, length) = be_u16(input)?;
take(length)(input)
take(length).parse_complete(input)
}
```

Expand Down Expand Up @@ -194,7 +194,7 @@ prints its hexdump if the child parser encountered an error:
use nom::{IResult, error::dbg_dmp, bytes::complete::tag};

fn f(i: &[u8]) -> IResult<&[u8], &[u8]> {
dbg_dmp(tag("abcd"), "tag")(i)
dbg_dmp(tag("abcd"), "tag").parse(i)
}

let a = &b"efghijkl"[..];
Expand Down
4 changes: 2 additions & 2 deletions doc/nom_recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ use std::str::FromStr;
// will recognize the name in "Hello, name!"
fn parse_name(input: &str) -> IResult<&str, &str> {
let (i, _) = tag("Hello, ").parse(input)?;
let (i, name) = take_while(|c:char| c.is_alphabetic())(i)?;
let (i, _) = tag("!")(i)?;
let (i, name) = take_while(|c:char| c.is_alphabetic()).parse(i)?;
let (i, _) = tag("!").parse(i)?;

Ok((i, name))
}
Expand Down
2 changes: 2 additions & 0 deletions src/branch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use crate::internal::{Err, Mode, Parser};
/// It takes as argument either a tuple or an array of parsers. If using a
/// tuple, there is a maximum of 21 parsers. If you need more, it is possible to
/// use an array.
///
/// Errors if all parsers do not output the same type.
///
/// ```rust
/// # use nom::error_position;
Expand Down
5 changes: 5 additions & 0 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,18 @@ pub trait Parser<Input> {

/// A parser takes in input type, and returns a `Result` containing
/// either the remaining input and the output value, or an error
///
/// Use this function for streaming parsers that recognize incomplete input.
#[inline]
fn parse(&mut self, input: Input) -> IResult<Input, Self::Output, Self::Error> {
self.process::<OutputM<Emit, Emit, Streaming>>(input)
}

/// A parser takes in input type, and returns a `Result` containing
/// either the remaining input and the output value, or an error
///
/// Use this function for complete parsers that fail on incomplete
/// input. This should be used in `nom7` style function parsers.
#[inline]
fn parse_complete(&mut self, input: Input) -> IResult<Input, Self::Output, Self::Error> {
self.process::<OutputM<Emit, Emit, Complete>>(input)
Expand Down
4 changes: 2 additions & 2 deletions src/sequence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ where
}
}

/// a
/// Parser wrapper for `preceded`
pub struct Preceded<F, G> {
f: F,
g: G,
Expand Down Expand Up @@ -129,7 +129,7 @@ where
}
}

/// a
/// Parser wrapper for `terminated`
pub struct Terminated<F, G> {
f: F,
g: G,
Expand Down