mini_builder_rs/parser/
parser_error.rs

1use crate::tokenizer::token::TokenType;
2
3/// When an invalid sequence of tokens is passed to the [super::Parser], it will
4/// return a [ParserError].
5#[derive(Debug)]
6pub enum ParserError {
7	/// Expected one thing but got something else. The type of thing that is
8	/// being expected can be anything (not necessarily a token).
9	ExpectedButFound(String, String),
10	/// Expected something but reached the end of the tokens.
11	ExpectedButReachedEnd(String),
12	/// A different token type was expected.
13	UnexpectedToken(usize, TokenType),
14	// A directive was opened, but not closed (For example: `{{# if` ).
15	UnclosedDirective,
16}
17
18impl std::fmt::Display for ParserError {
19	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20		match self {
21			Self::ExpectedButFound(s1, s2) => write!(f, "expected: {s1}, but found: {s2}"),
22			Self::ExpectedButReachedEnd(s) => write!(f, "expected: {s}, but reached the end"),
23			Self::UnexpectedToken(token_location, token_type) => write!(
24				f,
25				"unexpected token at character {token_location}, got: {:?}",
26				token_type
27			),
28			Self::UnclosedDirective => write!(f, "directive was not closed"),
29		}
30	}
31}
32
33impl std::error::Error for ParserError {}