Skip to main content

antlr4_runtime/
errors.rs

1use crate::recognizer::Recognizer;
2use thiserror::Error;
3
4#[derive(Debug, Error, Clone, Eq, PartialEq)]
5pub enum AntlrError {
6    #[error("mismatched input: expected {expected}, found {found}")]
7    MismatchedInput { expected: String, found: String },
8    #[error("no viable alternative at input {input}")]
9    NoViableAlternative { input: String },
10    #[error("lexer error at {line}:{column}: {message}")]
11    LexerError {
12        line: usize,
13        column: usize,
14        message: String,
15    },
16    #[error("parser error at {line}:{column}: {message}")]
17    ParserError {
18        line: usize,
19        column: usize,
20        message: String,
21    },
22    #[error("unsupported runtime feature: {0}")]
23    Unsupported(String),
24}
25
26pub trait ErrorListener<R: Recognizer> {
27    fn syntax_error(
28        &mut self,
29        recognizer: &R,
30        line: usize,
31        column: usize,
32        message: &str,
33        error: Option<&AntlrError>,
34    );
35}
36
37#[derive(Debug, Default)]
38pub struct ConsoleErrorListener;
39
40impl<R: Recognizer> ErrorListener<R> for ConsoleErrorListener {
41    #[allow(clippy::print_stderr)]
42    fn syntax_error(
43        &mut self,
44        _recognizer: &R,
45        line: usize,
46        column: usize,
47        message: &str,
48        _error: Option<&AntlrError>,
49    ) {
50        eprintln!("line {line}:{column} {message}");
51    }
52}