@@ -86,6 +86,17 @@ pub trait Error<'a, I: Input<'a>>:
8686
8787/// A ZST error type that tracks only whether a parse error occurred at all. This type is for when
8888/// you want maximum parse speed, at the cost of all error reporting.
89+ ///
90+ /// # Examples
91+ ///
92+ /// ```
93+ /// use chumsky::prelude::*;
94+ ///
95+ /// let parser = just::<_, _, extra::Err<EmptyErr>>("valid");
96+ /// let error = parser.parse("invalid").into_errors()[0];
97+ ///
98+ /// assert_eq!(error, EmptyErr::default());
99+ /// ```
89100#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
90101#[ derive( PartialEq , Eq , PartialOrd , Ord , Debug , Copy , Clone , Default ) ]
91102pub struct EmptyErr ( ( ) ) ;
@@ -109,8 +120,19 @@ impl fmt::Display for EmptyErr {
109120 }
110121}
111122
112- /// A very cheap error type that tracks only the error span. This type is most useful when you want fast parsing but do
113- /// not particularly care about the quality of error messages.
123+ /// A very cheap error type that tracks only the error span ([`SimpleSpan`] by default).
124+ /// This type is most useful when you want fast parsing but do not particularly care about the quality of error messages.
125+ ///
126+ /// # Examples
127+ ///
128+ /// ```
129+ /// use chumsky::prelude::*;
130+ ///
131+ /// let parser = just::<_, _, extra::Err<Cheap>>("+");
132+ /// let error = parser.parse("-").into_errors()[0];
133+ ///
134+ /// assert_eq!(error.span(), &SimpleSpan::new(0,1));
135+ /// ```
114136#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
115137#[ derive( Copy , Clone , PartialEq , Eq , Hash , PartialOrd , Ord ) ]
116138pub struct Cheap < S = SimpleSpan < usize > > {
@@ -119,6 +141,8 @@ pub struct Cheap<S = SimpleSpan<usize>> {
119141
120142impl < S > Cheap < S > {
121143 /// Get the span than that error related to.
144+ ///
145+ /// If the span type is unspecified, it is [`SimpleSpan`].
122146 pub fn span ( & self ) -> & S {
123147 & self . span
124148 }
@@ -156,8 +180,20 @@ where
156180 }
157181}
158182
159- /// A simple error type that tracks the error span and found token. This type is most useful when you want fast parsing
183+ /// A simple error type that tracks the error span ([`SimpleSpan`] by default) and found token. This type is most useful when you want fast parsing
160184/// but do not particularly care about the quality of error messages.
185+ ///
186+ /// # Examples
187+ ///
188+ /// ```
189+ /// use chumsky::prelude::*;
190+ ///
191+ /// let parser = just::<_, _, extra::Err<Simple<char>>>("+");
192+ /// let error = parser.parse("-").into_errors()[0];
193+ ///
194+ /// assert_eq!(error.span(), &SimpleSpan::new(0,1));
195+ /// assert_eq!(error.found(), Some(&'-'));
196+ /// ```
161197#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
162198#[ derive( Copy , Clone , PartialEq , Eq , Hash , PartialOrd , Ord ) ]
163199pub struct Simple < ' a , T , S = SimpleSpan < usize > > {
@@ -167,11 +203,13 @@ pub struct Simple<'a, T, S = SimpleSpan<usize>> {
167203
168204impl < T , S > Simple < ' _ , T , S > {
169205 /// Get the span than that error related to.
206+ ///
207+ /// If the span type is unspecified, it is [`SimpleSpan`].
170208 pub fn span ( & self ) -> & S {
171209 & self . span
172210 }
173211
174- /// Get the token, if any, that was found at the error location .
212+ /// Get the token found by this error when parsing. `None` implies that the error expected the end of input .
175213 pub fn found ( & self ) -> Option < & T > {
176214 self . found . as_deref ( )
177215 }
@@ -537,6 +575,23 @@ where
537575///
538576/// Please note that it uses a [`Vec`] to remember expected symbols. If you find this to be too slow, you can
539577/// implement [`Error`] for your own error type or use [`Simple`] instead.
578+ ///
579+ /// This error type stores a span ([`SimpleSpan`] by default), a [`RichReason`], and a list of expected [`RichPattern`] with their spans.
580+ ///
581+ /// # Examples
582+ ///
583+ /// ```
584+ /// use chumsky::prelude::*;
585+ /// use chumsky::error::{RichReason, RichPattern};
586+ ///
587+ /// let parser = one_of::<_, _, extra::Err<Rich<char>>>("1234");
588+ /// let error = parser.parse("5").into_errors()[0].clone();
589+ ///
590+ /// assert_eq!(error.span(), &SimpleSpan::new(0,1));
591+ /// assert!(matches!(error.reason(), &RichReason::ExpectedFound {..}));
592+ /// assert_eq!(error.found(), Some(&'5'));
593+ ///
594+ /// ```
540595#[ derive( Clone , PartialEq , Eq , Hash ) ]
541596pub struct Rich < ' a , T , S = SimpleSpan < usize > > {
542597 span : S ,
@@ -574,6 +629,8 @@ impl<'a, T, S> Rich<'a, T, S> {
574629 }
575630
576631 /// Get the span associated with this error.
632+ ///
633+ /// If the span type is unspecified, it is [`SimpleSpan`].
577634 pub fn span ( & self ) -> & S {
578635 & self . span
579636 }
0 commit comments