Skip to content

Fix some holes in documentation#720

Merged
zesterer merged 7 commits intozesterer:mainfrom
benjamin-cates:docs_updates
Feb 1, 2025
Merged

Fix some holes in documentation#720
zesterer merged 7 commits intozesterer:mainfrom
benjamin-cates:docs_updates

Conversation

@benjamin-cates
Copy link
Contributor

Hello!

Added some missing documentation, mostly examples and filling in holes in the error module.

Change list:

  • Added examples to error::EmptyError, error::Cheap, error::Simple, Error::Rich
  • Added examples to Parser::into_iter, Parser::to_slice
  • Added note comparing IterParser::Collect and Parser::to_slice on including/excluding ignored patterns.
  • Added links to relevant attribute types in errors
    • It wasn't really clear what error::Rich was storing, especially that the links to RichPattern and RichReason were buried in method headers below, so I thought it would be good to add links to those.
    • It also wasn't clear what the return type of each of the "span" methods was, since they all said they returned "&S", but for some reason, cargo doc linked to https://doc.rust-lang.org/std/primitive.reference.html 😭
  • Fixed inconsistency between docs on Simple::found and Rich::found

Let me know if I should make any changes

src/lib.rs Outdated
Comment on lines +2074 to +2079
/// let list = letter
/// .repeated()
/// .to_slice()
/// .map(|string: &str| string.chars())
/// .into_iter()
/// .collect_exactly::<[char; 4]>();
Copy link
Owner

@zesterer zesterer Jan 26, 2025

Choose a reason for hiding this comment

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

I feel like this is a somewhat contrived example since the code could be expressed equivalently but more simply like so:

let list = letter.repeated().collect_exactly::<[char; 4]>();

A better example might be something like:

// Parses whole integers
let num = text::int().padded().map(|x| x.parse::<u64>().unwrap());

// Parses a range like `0..4` into a vector like `[0, 1, 2, 3]`
let range = num.ignore_then(just("..")).then(num)
    .map(|(x, y)| x..y)
    .into_iter()
    .collect::<Vec<_>>();

// Parses a list of numbers into a vector
let list = num.separated_by(just(',')).collect::<Vec<_>>();

let set = range.or(list);

assert_eq!(set.parse("0, 1, 2, 3").unwrap(), [0, 1, 2, 3]);
assert_eq!(set.parse("0..4").unwrap(), [0, 1, 2, 3]);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh okay, thank you! I was struggling to think of a useful example lol

Copy link
Owner

Choose a reason for hiding this comment

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

It's difficult! A lot of chumsky's parsers only become useful at scale, so coming up with short examples that demonstrate their use it tough.

Copy link
Owner

@zesterer zesterer left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! I think these changes are great. I've just one suggestion regarding one of the examples.

@zesterer zesterer merged commit 266f404 into zesterer:main Feb 1, 2025
4 checks passed
@zesterer
Copy link
Owner

zesterer commented Feb 1, 2025

Thanks for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants