Skip to content

Commit

Permalink
Added Parser::simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
zesterer committed Oct 28, 2024
1 parent c7acd96 commit f729942
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 4 additions & 3 deletions guide/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Let's write some tests for the parser we wrote in the last section.
fn test_parser() {
// Our parser expects empty strings, so this should parse successfully
assert_eq!(parser().parse("").into_result(), Ok(()));
// Anything other than an empty string should produce an error
assert!(parser().parse("123").has_errors());
}
Expand Down Expand Up @@ -158,7 +158,8 @@ error you're struggling to understand, you should:

2. Reduce the size of types. Thankfully Rust has recently taken steps to avoid printing extremely long type signatures
out to the terminal. Even so, parser types can still be rather large. You can reduce this problem by commenting out
unnecessary parts of your parser, or using `.boxed()` on parsers above the error to simplify their types.
unnecessary parts of your parser, or using `.simplify()` on parsers that contribute to the error to simplify their
types.

3. Complaints about types 'not implementing [`Parser`]' are more often than not a failure to fulfil the obligations that
come with implementing the trait. For example, [`recursive()`] requires that the inner parser implements `Clone`: a
Expand All @@ -185,4 +186,4 @@ Rust's trait solver.

### Debugging parsers

TODO
TODO
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2111,6 +2111,17 @@ pub trait Parser<'src, I: Input<'src>, O, E: ParserExtra<'src, I> = extra::Defau
}
}

/// Simplify the type of the parser using Rust's `impl Trait` syntax.
///
/// The only reason for using this function is to make Rust's compiler errors easier to debug: it does not change
/// the behaviour of the parser at all, and is in fact just a simple identity function.
fn simplify(self) -> impl Parser<'src, I, O, E>
where
Self: Sized + 'src,
{
self
}

/// Use [Pratt parsing](https://en.wikipedia.org/wiki/Operator-precedence_parser#Pratt_parsing) to ergonomically
/// parse this pattern separated by prefix, postfix, and infix operators of various associativites and precedence.
///
Expand Down

0 comments on commit f729942

Please sign in to comment.