diff --git a/guide/getting_started.md b/guide/getting_started.md index 0431ffaf..a9ab8f0d 100644 --- a/guide/getting_started.md +++ b/guide/getting_started.md @@ -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()); } @@ -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 @@ -185,4 +186,4 @@ Rust's trait solver. ### Debugging parsers -TODO \ No newline at end of file +TODO diff --git a/src/lib.rs b/src/lib.rs index b45f5bf0..4eaf5c30 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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. ///