brush_parser/
error.rs

1use crate::Token;
2use crate::tokenizer;
3
4/// Represents an error that occurred while parsing tokens.
5#[derive(thiserror::Error, Debug)]
6pub enum ParseError {
7    /// A parsing error occurred near the given token.
8    #[error("parse error near token: {}", .0.location().start)]
9    ParsingNearToken(Token),
10
11    /// A parsing error occurred at the end of the input.
12    #[error("parse error at end of input")]
13    ParsingAtEndOfInput,
14
15    /// An error occurred while tokenizing the input stream.
16    #[error("failed to tokenize input")]
17    Tokenizing {
18        /// The inner error.
19        inner: tokenizer::TokenizerError,
20        /// Optionally provides the position of the error.
21        position: Option<tokenizer::SourcePosition>,
22    },
23}
24
25/// Represents a parsing error with its location information
26#[derive(Debug, thiserror::Error)]
27#[error(transparent)]
28pub struct ParseErrorLocation {
29    #[from]
30    inner: peg::error::ParseError<peg::str::LineCol>,
31}
32
33/// Represents an error that occurred while parsing a word.
34#[derive(Debug, thiserror::Error)]
35pub enum WordParseError {
36    /// An error occurred while parsing an arithmetic expression.
37    #[error("failed to parse arithmetic expression")]
38    ArithmeticExpression(ParseErrorLocation),
39
40    /// An error occurred while parsing a shell pattern.
41    #[error("failed to parse pattern")]
42    Pattern(ParseErrorLocation),
43
44    /// An error occurred while parsing a prompt string.
45    #[error("failed to parse prompt string")]
46    Prompt(ParseErrorLocation),
47
48    /// An error occurred while parsing a parameter.
49    #[error("failed to parse parameter '{0}'")]
50    Parameter(String, ParseErrorLocation),
51
52    /// An error occurred while parsing for brace expansion.
53    #[error("failed to parse for brace expansion: '{0}'")]
54    BraceExpansion(String, ParseErrorLocation),
55
56    /// An error occurred while parsing a word.
57    #[error("failed to parse word '{0}'")]
58    Word(String, ParseErrorLocation),
59}
60
61/// Represents an error that occurred while parsing a (non-extended) test command.
62#[derive(Debug, thiserror::Error)]
63#[error(transparent)]
64pub struct TestCommandParseError(#[from] peg::error::ParseError<usize>);
65
66/// Represents an error that occurred while parsing a key-binding specification.
67#[derive(Debug, thiserror::Error)]
68pub enum BindingParseError {
69    /// An unknown error occurred while parsing a key-binding specification.
70    #[error("unknown error while parsing key-binding: '{0}'")]
71    Unknown(String),
72
73    /// A key code was missing from the key-binding specification.
74    #[error("missing key code in key-binding")]
75    MissingKeyCode,
76}
77
78pub(crate) fn convert_peg_parse_error(
79    err: &peg::error::ParseError<usize>,
80    tokens: &[Token],
81) -> ParseError {
82    let approx_token_index = err.location;
83
84    if approx_token_index < tokens.len() {
85        ParseError::ParsingNearToken(tokens[approx_token_index].clone())
86    } else {
87        ParseError::ParsingAtEndOfInput
88    }
89}