1use crate::Token;
2use crate::tokenizer;
3
4#[derive(thiserror::Error, Debug)]
6pub enum ParseError {
7 #[error("parse error near token: {}", .0.location().start)]
9 ParsingNearToken(Token),
10
11 #[error("parse error at end of input")]
13 ParsingAtEndOfInput,
14
15 #[error("failed to tokenize input")]
17 Tokenizing {
18 inner: tokenizer::TokenizerError,
20 position: Option<tokenizer::SourcePosition>,
22 },
23}
24
25#[derive(Debug, thiserror::Error)]
27#[error(transparent)]
28pub struct ParseErrorLocation {
29 #[from]
30 inner: peg::error::ParseError<peg::str::LineCol>,
31}
32
33#[derive(Debug, thiserror::Error)]
35pub enum WordParseError {
36 #[error("failed to parse arithmetic expression")]
38 ArithmeticExpression(ParseErrorLocation),
39
40 #[error("failed to parse pattern")]
42 Pattern(ParseErrorLocation),
43
44 #[error("failed to parse prompt string")]
46 Prompt(ParseErrorLocation),
47
48 #[error("failed to parse parameter '{0}'")]
50 Parameter(String, ParseErrorLocation),
51
52 #[error("failed to parse for brace expansion: '{0}'")]
54 BraceExpansion(String, ParseErrorLocation),
55
56 #[error("failed to parse word '{0}'")]
58 Word(String, ParseErrorLocation),
59}
60
61#[derive(Debug, thiserror::Error)]
63#[error(transparent)]
64pub struct TestCommandParseError(#[from] peg::error::ParseError<usize>);
65
66#[derive(Debug, thiserror::Error)]
68pub enum BindingParseError {
69 #[error("unknown error while parsing key-binding: '{0}'")]
71 Unknown(String),
72
73 #[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}