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 + ?Sized> {
33 fn syntax_error(
34 &mut self,
35 recognizer: &R,
36 line: usize,
37 column: usize,
38 message: &str,
39 error: Option<&AntlrError>,
40 );
41}
42
43#[derive(Debug, Default)]
44pub struct ConsoleErrorListener;
45
46impl<R: Recognizer + ?Sized> ErrorListener<R> for ConsoleErrorListener {
47 #[allow(clippy::print_stderr)]
48 fn syntax_error(
49 &mut self,
50 _recognizer: &R,
51 line: usize,
52 column: usize,
53 message: &str,
54 _error: Option<&AntlrError>,
55 ) {
56 eprintln!("line {line}:{column} {message}");
57 }
58}