parsey/ast.rs
1use crate::{parser::Parser, token_stream::TokenStream};
2
3/// A trait representing a component of an abstract syntax tree (AST).
4///
5/// This trait defines the interface for parsing a specific node or component of the AST
6/// from a sequence of tokens.
7///
8/// # Type Parameters
9/// - `Token`: The type of tokens being parsed.
10/// - `Error`: The type of errors that may occur during parsing.
11pub trait Ast<Token, Error>: Sized {
12 /// Parses an AST node from a peekable token stream.
13 ///
14 /// # Parameters
15 /// - `token_stream`: A peekable iterator implementing the `Parser` trait.
16 ///
17 /// # Returns
18 /// Returns the parsed AST node or an error if parsing fails.
19 ///
20 /// # Errors
21 /// Returns an error of type `Error` if the token sequence does not match the expected structure.
22 fn parse<P>(token_stream: &mut TokenStream<P, Token, Error>) -> Result<Self, Error>
23 where
24 P: Parser<Token, Error>;
25}