/rust/registry/src/index.crates.io-1949cf8c6b5b557f/antlr4rust-0.6.0/src/errors.rs
Line | Count | Source |
1 | | //! Error types |
2 | | use crate::atn_simulator::IATNSimulator; |
3 | | use crate::interval_set::IntervalSet; |
4 | | use crate::parser::{Parser, ParserNodeType}; |
5 | | use crate::rule_context::states_stack; |
6 | | use crate::token::{OwningToken, Token}; |
7 | | use crate::transition::PredicateTransition; |
8 | | use crate::transition::TransitionType::TRANSITION_PREDICATE; |
9 | | use std::borrow::Borrow; |
10 | | use std::error::Error; |
11 | | use std::fmt; |
12 | | use std::fmt::Formatter; |
13 | | use std::fmt::{Debug, Display}; |
14 | | use std::ops::Deref; |
15 | | use std::rc::Rc; |
16 | | use std::sync::Arc; |
17 | | |
18 | | /// Main ANTLR4 Rust runtime error |
19 | | #[derive(Debug, Clone)] |
20 | | pub enum ANTLRError { |
21 | | /// Returned from Lexer when it fails to find matching token type for current input |
22 | | /// |
23 | | /// Usually Lexers contain last rule that captures all invalid tokens like: |
24 | | /// ```text |
25 | | /// ERROR_TOKEN: . ; |
26 | | /// ``` |
27 | | /// to prevent lexer from throwing errors and have all error handling in parser. |
28 | | LexerNoAltError { |
29 | | /// Index at which error has happened |
30 | | start_index: isize, |
31 | | }, |
32 | | |
33 | | /// Indicates that the parser could not decide which of two or more paths |
34 | | /// to take based upon the remaining input. It tracks the starting token |
35 | | /// of the offending input and also knows where the parser was |
36 | | /// in the various paths when the error. Reported by reportNoViableAlternative() |
37 | | NoAltError(NoViableAltError), |
38 | | |
39 | | /// This signifies any kind of mismatched input exceptions such as |
40 | | /// when the current input does not match the expected token. |
41 | | InputMismatchError(InputMisMatchError), |
42 | | |
43 | | /// A semantic predicate failed during validation. Validation of predicates |
44 | | /// occurs when normally parsing the alternative just like matching a token. |
45 | | /// Disambiguating predicate evaluation occurs when we test a predicate during |
46 | | /// prediction. |
47 | | PredicateError(FailedPredicateError), |
48 | | |
49 | | /// Internal error. Or user provided type returned data that is |
50 | | /// incompatible with current parser state |
51 | | IllegalStateError(String), |
52 | | |
53 | | /// Unrecoverable error. Indicates that error should not be processed by parser/error strategy |
54 | | /// and it should abort parsing and immediately return to caller. |
55 | | FallThrough(Arc<dyn Error + Send + Sync + 'static>), |
56 | | |
57 | | /// Potentially recoverable error. |
58 | | /// Used to allow user to emit his own errors from parser actions or from custom error strategy. |
59 | | /// Parser will try to recover with provided `ErrorStrategy` |
60 | | OtherError(Arc<dyn Error + Send + Sync + 'static>), |
61 | | } |
62 | | |
63 | | // impl Clone for ANTLRError { |
64 | | // fn clone(&self) -> Self { |
65 | | // match self { |
66 | | // ANTLRError::LexerNoAltError { start_index } => ANTLRError::LexerNoAltError { |
67 | | // start_index: *start_index, |
68 | | // }, |
69 | | // ANTLRError::NoAltError(e) => ANTLRError::NoAltError(e.clone()), |
70 | | // ANTLRError::InputMismatchError(e) => ANTLRError::InputMismatchError(e.clone()), |
71 | | // ANTLRError::PredicateError(e) => ANTLRError::PredicateError(e.clone()), |
72 | | // ANTLRError::IllegalStateError(e) => ANTLRError::IllegalStateError(e.clone()), |
73 | | // ANTLRError::FallThrough(_) => panic!("clone not supported"), |
74 | | // ANTLRError::OtherError(_) => panic!("clone not supported"), |
75 | | // } |
76 | | // } |
77 | | // } |
78 | | |
79 | | impl Display for ANTLRError { |
80 | 1.39k | fn fmt(&self, _f: &mut Formatter<'_>) -> fmt::Result { |
81 | 1.39k | <Self as Debug>::fmt(self, _f) |
82 | 1.39k | } |
83 | | } |
84 | | |
85 | | impl Error for ANTLRError { |
86 | 0 | fn source(&self) -> Option<&(dyn Error + 'static)> { |
87 | 0 | match self { |
88 | 0 | ANTLRError::FallThrough(x) => Some(x.as_ref()), |
89 | 0 | ANTLRError::OtherError(x) => Some(x.as_ref()), |
90 | 0 | _ => None, |
91 | | } |
92 | 0 | } |
93 | | } |
94 | | |
95 | | impl ANTLRError { |
96 | | /// Returns first token that caused parser to fail. |
97 | 47.6k | pub fn get_offending_token(&self) -> Option<&OwningToken> { |
98 | 47.6k | Some(match self { |
99 | 6.86k | ANTLRError::NoAltError(e) => &e.base.offending_token, |
100 | 39.4k | ANTLRError::InputMismatchError(e) => &e.base.offending_token, |
101 | 0 | ANTLRError::PredicateError(e) => &e.base.offending_token, |
102 | 1.39k | _ => return None, |
103 | | }) |
104 | 47.6k | } |
105 | | } |
106 | | |
107 | | //impl ANTLRError { |
108 | | // fn get_expected_tokens(&self, _atn: &ATN) -> IntervalSet { |
109 | | // atn.get_expected_tokens(se) |
110 | | // unimplemented!() |
111 | | // } |
112 | | //} |
113 | | |
114 | | /// Common part of ANTLR parser errors |
115 | | #[derive(Debug, Clone)] |
116 | | #[allow(missing_docs)] |
117 | | pub struct BaseRecognitionError { |
118 | | pub message: String, |
119 | | // recognizer: Box<Recognizer>, |
120 | | pub offending_token: OwningToken, |
121 | | pub offending_state: i32, |
122 | | states_stack: Vec<i32>, // ctx: Rc<dyn ParserRuleContext> |
123 | | // input: Box<IntStream> |
124 | | } |
125 | | |
126 | | impl BaseRecognitionError { |
127 | | /// Returns tokens that were expected by parser in error place |
128 | 39.4k | pub fn get_expected_tokens<'a, T: Parser<'a>>(&self, recognizer: &T) -> IntervalSet { |
129 | 39.4k | recognizer |
130 | 39.4k | .get_interpreter() |
131 | 39.4k | .atn() |
132 | 39.4k | .get_expected_tokens(self.offending_state, self.states_stack.iter().copied()) |
133 | 39.4k | } <antlr4rust::errors::BaseRecognitionError>::get_expected_tokens::<antlr4rust::parser::BaseParser<cel::parser::gen::celparser::CELParserExt, antlr4rust::common_token_stream::CommonTokenStream<cel::parser::gen::cellexer::CELLexer<antlr4rust::input_stream::InputStream<&str>>>, cel::parser::gen::celparser::CELParserContextType, dyn cel::parser::gen::cellistener::CELListener>> Line | Count | Source | 128 | 39.4k | pub fn get_expected_tokens<'a, T: Parser<'a>>(&self, recognizer: &T) -> IntervalSet { | 129 | 39.4k | recognizer | 130 | 39.4k | .get_interpreter() | 131 | 39.4k | .atn() | 132 | 39.4k | .get_expected_tokens(self.offending_state, self.states_stack.iter().copied()) | 133 | 39.4k | } |
Unexecuted instantiation: <antlr4rust::errors::BaseRecognitionError>::get_expected_tokens::<_> |
134 | | |
135 | 59.6k | fn new<'a, T: Parser<'a>>(recog: &mut T) -> BaseRecognitionError { |
136 | 59.6k | BaseRecognitionError { |
137 | 59.6k | message: "".to_string(), |
138 | 59.6k | offending_token: recog.get_current_token().borrow().to_owned(), |
139 | 59.6k | offending_state: recog.get_state(), |
140 | 59.6k | // ctx: recog.get_parser_rule_context().clone(), |
141 | 59.6k | states_stack: states_stack(recog.get_parser_rule_context().clone()).collect(), |
142 | 59.6k | } |
143 | 59.6k | } <antlr4rust::errors::BaseRecognitionError>::new::<antlr4rust::parser::BaseParser<cel::parser::gen::celparser::CELParserExt, antlr4rust::common_token_stream::CommonTokenStream<cel::parser::gen::cellexer::CELLexer<antlr4rust::input_stream::InputStream<&str>>>, cel::parser::gen::celparser::CELParserContextType, dyn cel::parser::gen::cellistener::CELListener>> Line | Count | Source | 135 | 59.6k | fn new<'a, T: Parser<'a>>(recog: &mut T) -> BaseRecognitionError { | 136 | 59.6k | BaseRecognitionError { | 137 | 59.6k | message: "".to_string(), | 138 | 59.6k | offending_token: recog.get_current_token().borrow().to_owned(), | 139 | 59.6k | offending_state: recog.get_state(), | 140 | 59.6k | // ctx: recog.get_parser_rule_context().clone(), | 141 | 59.6k | states_stack: states_stack(recog.get_parser_rule_context().clone()).collect(), | 142 | 59.6k | } | 143 | 59.6k | } |
Unexecuted instantiation: <antlr4rust::errors::BaseRecognitionError>::new::<_> |
144 | | } |
145 | | |
146 | | /// See `ANTLRError::NoAltError` |
147 | | #[derive(Debug, Clone)] |
148 | | #[allow(missing_docs)] |
149 | | pub struct NoViableAltError { |
150 | | pub base: BaseRecognitionError, |
151 | | pub start_token: OwningToken, |
152 | | // ctx: Rc<dyn ParserRuleContext>, |
153 | | // dead_end_configs: BaseATNConfigSet, |
154 | | } |
155 | | |
156 | | #[allow(missing_docs)] |
157 | | impl NoViableAltError { |
158 | 0 | pub fn new<'a, T: Parser<'a>>(recog: &mut T) -> NoViableAltError { |
159 | 0 | Self { |
160 | 0 | base: BaseRecognitionError { |
161 | 0 | message: "".to_string(), |
162 | 0 | offending_token: recog.get_current_token().borrow().to_owned(), |
163 | 0 | offending_state: recog.get_state(), |
164 | 0 | // ctx: recog.get_parser_rule_context().clone(), |
165 | 0 | states_stack: states_stack(recog.get_parser_rule_context().clone()).collect(), |
166 | 0 | }, |
167 | 0 | start_token: recog.get_current_token().borrow().to_owned(), |
168 | 0 | // ctx: recog.get_parser_rule_context().clone() |
169 | 0 | } |
170 | 0 | } Unexecuted instantiation: <antlr4rust::errors::NoViableAltError>::new::<antlr4rust::parser::BaseParser<cel::parser::gen::celparser::CELParserExt, antlr4rust::common_token_stream::CommonTokenStream<cel::parser::gen::cellexer::CELLexer<antlr4rust::input_stream::InputStream<&str>>>, cel::parser::gen::celparser::CELParserContextType, dyn cel::parser::gen::cellistener::CELListener>> Unexecuted instantiation: <antlr4rust::errors::NoViableAltError>::new::<_> |
171 | 81.4k | pub fn new_full<'a, T: Parser<'a>>( |
172 | 81.4k | recog: &mut T, |
173 | 81.4k | start_token: OwningToken, |
174 | 81.4k | offending_token: OwningToken, |
175 | 81.4k | ) -> NoViableAltError { |
176 | 81.4k | Self { |
177 | 81.4k | base: BaseRecognitionError { |
178 | 81.4k | message: "".to_string(), |
179 | 81.4k | offending_token, |
180 | 81.4k | offending_state: recog.get_state(), |
181 | 81.4k | states_stack: states_stack(recog.get_parser_rule_context().clone()).collect(), // ctx: recog.get_parser_rule_context().clone(), |
182 | 81.4k | }, |
183 | 81.4k | start_token, |
184 | 81.4k | // ctx |
185 | 81.4k | } |
186 | 81.4k | } <antlr4rust::errors::NoViableAltError>::new_full::<antlr4rust::parser::BaseParser<cel::parser::gen::celparser::CELParserExt, antlr4rust::common_token_stream::CommonTokenStream<cel::parser::gen::cellexer::CELLexer<antlr4rust::input_stream::InputStream<&str>>>, cel::parser::gen::celparser::CELParserContextType, dyn cel::parser::gen::cellistener::CELListener>> Line | Count | Source | 171 | 81.4k | pub fn new_full<'a, T: Parser<'a>>( | 172 | 81.4k | recog: &mut T, | 173 | 81.4k | start_token: OwningToken, | 174 | 81.4k | offending_token: OwningToken, | 175 | 81.4k | ) -> NoViableAltError { | 176 | 81.4k | Self { | 177 | 81.4k | base: BaseRecognitionError { | 178 | 81.4k | message: "".to_string(), | 179 | 81.4k | offending_token, | 180 | 81.4k | offending_state: recog.get_state(), | 181 | 81.4k | states_stack: states_stack(recog.get_parser_rule_context().clone()).collect(), // ctx: recog.get_parser_rule_context().clone(), | 182 | 81.4k | }, | 183 | 81.4k | start_token, | 184 | 81.4k | // ctx | 185 | 81.4k | } | 186 | 81.4k | } |
Unexecuted instantiation: <antlr4rust::errors::NoViableAltError>::new_full::<_> |
187 | | } |
188 | | |
189 | | /// See `ANTLRError::InputMismatchError` |
190 | | #[derive(Debug, Clone)] |
191 | | #[allow(missing_docs)] |
192 | | pub struct InputMisMatchError { |
193 | | pub base: BaseRecognitionError, |
194 | | } |
195 | | |
196 | | #[allow(missing_docs)] |
197 | | impl InputMisMatchError { |
198 | 59.6k | pub fn new<'a, T: Parser<'a>>(recognizer: &mut T) -> InputMisMatchError { |
199 | 59.6k | InputMisMatchError { |
200 | 59.6k | base: BaseRecognitionError::new(recognizer), |
201 | 59.6k | } |
202 | 59.6k | } <antlr4rust::errors::InputMisMatchError>::new::<antlr4rust::parser::BaseParser<cel::parser::gen::celparser::CELParserExt, antlr4rust::common_token_stream::CommonTokenStream<cel::parser::gen::cellexer::CELLexer<antlr4rust::input_stream::InputStream<&str>>>, cel::parser::gen::celparser::CELParserContextType, dyn cel::parser::gen::cellistener::CELListener>> Line | Count | Source | 198 | 59.6k | pub fn new<'a, T: Parser<'a>>(recognizer: &mut T) -> InputMisMatchError { | 199 | 59.6k | InputMisMatchError { | 200 | 59.6k | base: BaseRecognitionError::new(recognizer), | 201 | 59.6k | } | 202 | 59.6k | } |
Unexecuted instantiation: <antlr4rust::errors::InputMisMatchError>::new::<_> |
203 | | |
204 | 15.5k | pub fn with_state<'a, T: Parser<'a>>( |
205 | 15.5k | recognizer: &mut T, |
206 | 15.5k | offending_state: i32, |
207 | 15.5k | ctx: Rc<<T::Node as ParserNodeType<'a>>::Type>, |
208 | 15.5k | ) -> InputMisMatchError { |
209 | 15.5k | let mut a = Self::new(recognizer); |
210 | | // a.base.ctx = ctx; |
211 | 15.5k | a.base.offending_state = offending_state; |
212 | 15.5k | a.base.states_stack = states_stack(ctx).collect(); |
213 | 15.5k | a |
214 | 15.5k | } <antlr4rust::errors::InputMisMatchError>::with_state::<antlr4rust::parser::BaseParser<cel::parser::gen::celparser::CELParserExt, antlr4rust::common_token_stream::CommonTokenStream<cel::parser::gen::cellexer::CELLexer<antlr4rust::input_stream::InputStream<&str>>>, cel::parser::gen::celparser::CELParserContextType, dyn cel::parser::gen::cellistener::CELListener>> Line | Count | Source | 204 | 15.5k | pub fn with_state<'a, T: Parser<'a>>( | 205 | 15.5k | recognizer: &mut T, | 206 | 15.5k | offending_state: i32, | 207 | 15.5k | ctx: Rc<<T::Node as ParserNodeType<'a>>::Type>, | 208 | 15.5k | ) -> InputMisMatchError { | 209 | 15.5k | let mut a = Self::new(recognizer); | 210 | | // a.base.ctx = ctx; | 211 | 15.5k | a.base.offending_state = offending_state; | 212 | 15.5k | a.base.states_stack = states_stack(ctx).collect(); | 213 | 15.5k | a | 214 | 15.5k | } |
Unexecuted instantiation: <antlr4rust::errors::InputMisMatchError>::with_state::<_> |
215 | | } |
216 | | |
217 | | //fn new_input_mis_match_exception(recognizer: Parser) -> InputMisMatchError { unimplemented!() } |
218 | | |
219 | | /// See `ANTLRError::PredicateError` |
220 | | #[derive(Debug, Clone)] |
221 | | #[allow(missing_docs)] |
222 | | pub struct FailedPredicateError { |
223 | | pub base: BaseRecognitionError, |
224 | | pub rule_index: i32, |
225 | | pub predicate: String, |
226 | | } |
227 | | |
228 | | #[allow(missing_docs)] |
229 | | impl FailedPredicateError { |
230 | 0 | pub fn new<'a, T: Parser<'a>>( |
231 | 0 | recog: &mut T, |
232 | 0 | predicate: Option<String>, |
233 | 0 | msg: Option<String>, |
234 | 0 | ) -> ANTLRError { |
235 | 0 | let tr = recog.get_interpreter().atn().states[recog.get_state() as usize] |
236 | 0 | .get_transitions() |
237 | 0 | .first() |
238 | 0 | .unwrap(); |
239 | 0 | let (rule_index, _) = if tr.get_serialization_type() == TRANSITION_PREDICATE { |
240 | 0 | let pr = tr.deref().cast::<PredicateTransition>(); |
241 | 0 | (pr.rule_index, pr.pred_index) |
242 | | } else { |
243 | 0 | (0, 0) |
244 | | }; |
245 | | |
246 | | ANTLRError::PredicateError(FailedPredicateError { |
247 | | base: BaseRecognitionError { |
248 | 0 | message: msg.unwrap_or_else(|| { |
249 | 0 | format!( |
250 | 0 | "failed predicate: {}", |
251 | 0 | predicate.as_deref().unwrap_or("None") |
252 | | ) |
253 | 0 | }), Unexecuted instantiation: <antlr4rust::errors::FailedPredicateError>::new::<antlr4rust::parser::BaseParser<cel::parser::gen::celparser::CELParserExt, antlr4rust::common_token_stream::CommonTokenStream<cel::parser::gen::cellexer::CELLexer<antlr4rust::input_stream::InputStream<&str>>>, cel::parser::gen::celparser::CELParserContextType, dyn cel::parser::gen::cellistener::CELListener>>::{closure#0}Unexecuted instantiation: <antlr4rust::errors::FailedPredicateError>::new::<_>::{closure#0} |
254 | 0 | offending_token: recog.get_current_token().borrow().to_owned(), |
255 | 0 | offending_state: recog.get_state(), |
256 | 0 | states_stack: states_stack(recog.get_parser_rule_context().clone()).collect(), // ctx: recog.get_parser_rule_context().clone() |
257 | | }, |
258 | 0 | rule_index, |
259 | 0 | predicate: predicate.unwrap_or_default(), |
260 | | }) |
261 | 0 | } Unexecuted instantiation: <antlr4rust::errors::FailedPredicateError>::new::<antlr4rust::parser::BaseParser<cel::parser::gen::celparser::CELParserExt, antlr4rust::common_token_stream::CommonTokenStream<cel::parser::gen::cellexer::CELLexer<antlr4rust::input_stream::InputStream<&str>>>, cel::parser::gen::celparser::CELParserContextType, dyn cel::parser::gen::cellistener::CELListener>> Unexecuted instantiation: <antlr4rust::errors::FailedPredicateError>::new::<_> |
262 | | } |