This trait allows AST nodes to construct themselves from a mutable [Parser] instance. Nodes that implement this trait are entitled to consume any number of [Cursors][crate::Cursor] from [Parser] in order to construct themselves. They may also consume some amount of tokens and still return an [Err] - there is no need to try and reset the [Parser] state on failure ([Parser::try_parse()] exists for
| 17 | /// If a Node can construct itself from a single [Cursor][crate::Cursor] it should implement |
| 18 | /// [Peek][crate::Peek] and [Parse], where [Parse::parse()] calls [Parser::next()] and constructs from the cursor. |
| 19 | pub trait Parse<'a>: Sized { |
| 20 | fn parse<I>(p: &mut Parser<'a, I>) -> Result<Self> |
| 21 | where |
| 22 | I: Iterator<Item = Cursor> + Clone; |
| 23 | |
| 24 | fn try_parse<I>(p: &mut Parser<'a, I>) -> Result<Self> |
| 25 | where |
| 26 | I: Iterator<Item = Cursor> + Clone, |
| 27 | { |
| 28 | let checkpoint = p.checkpoint(); |
| 29 | Self::parse(p).inspect_err(|_| p.rewind(checkpoint)) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | impl<'a, T> Parse<'a> for Option<T> |
| 34 | where |
nothing calls this directly
no outgoing calls
no test coverage detected