do_not_use_antlr_rust/
errors.rs1use std::borrow::Borrow;
3use std::error::Error;
4use std::fmt;
5use std::fmt::Formatter;
6use std::fmt::{Debug, Display};
7use std::ops::Deref;
8use std::rc::Rc;
9
10use crate::atn_simulator::IATNSimulator;
11use crate::interval_set::IntervalSet;
12use crate::parser::{Parser, ParserNodeType};
13use crate::rule_context::states_stack;
14use crate::token::{OwningToken, Token};
15use crate::transition::PredicateTransition;
16use crate::transition::TransitionType::TRANSITION_PREDICATE;
17
18#[derive(Debug, Clone)]
20pub enum ANTLRError {
21 LexerNoAltError {
29 start_index: isize,
31 },
32
33 NoAltError(NoViableAltError),
38
39 InputMismatchError(InputMisMatchError),
42
43 PredicateError(FailedPredicateError),
48
49 IllegalStateError(String),
52
53 FallThrough(Rc<dyn Error>),
56
57 OtherError(Rc<dyn Error>),
61}
62
63impl Display for ANTLRError {
80 fn fmt(&self, _f: &mut Formatter<'_>) -> fmt::Result { <Self as Debug>::fmt(self, _f) }
81}
82
83impl Error for ANTLRError {
84 fn source(&self) -> Option<&(dyn Error + 'static)> {
85 match self {
86 ANTLRError::FallThrough(x) => Some(x.as_ref()),
87 ANTLRError::OtherError(x) => Some(x.as_ref()),
88 _ => None,
89 }
90 }
91}
92
93impl ANTLRError {
94 pub fn get_offending_token(&self) -> Option<&OwningToken> {
96 Some(match self {
97 ANTLRError::NoAltError(e) => &e.base.offending_token,
98 ANTLRError::InputMismatchError(e) => &e.base.offending_token,
99 ANTLRError::PredicateError(e) => &e.base.offending_token,
100 _ => return None,
101 })
102 }
103}
104
105#[derive(Debug, Clone)]
114#[allow(missing_docs)]
115pub struct BaseRecognitionError {
116 pub message: String,
117 pub offending_token: OwningToken,
119 pub offending_state: isize,
120 states_stack: Vec<isize>, }
123
124impl BaseRecognitionError {
125 pub fn get_expected_tokens<'a, T: Parser<'a>>(&self, recognizer: &T) -> IntervalSet {
127 recognizer
128 .get_interpreter()
129 .atn()
130 .get_expected_tokens(self.offending_state, self.states_stack.iter().copied())
131 }
132
133 fn new<'a, T: Parser<'a>>(recog: &mut T) -> BaseRecognitionError {
134 BaseRecognitionError {
135 message: "".to_string(),
136 offending_token: recog.get_current_token().borrow().to_owned(),
137 offending_state: recog.get_state(),
138 states_stack: states_stack(recog.get_parser_rule_context().clone()).collect(),
140 }
141 }
142}
143
144#[derive(Debug, Clone)]
146#[allow(missing_docs)]
147pub struct NoViableAltError {
148 pub base: BaseRecognitionError,
149 pub start_token: OwningToken,
150 }
153
154#[allow(missing_docs)]
155impl NoViableAltError {
156 pub fn new<'a, T: Parser<'a>>(recog: &mut T) -> NoViableAltError {
157 Self {
158 base: BaseRecognitionError {
159 message: "".to_string(),
160 offending_token: recog.get_current_token().borrow().to_owned(),
161 offending_state: recog.get_state(),
162 states_stack: states_stack(recog.get_parser_rule_context().clone()).collect(),
164 },
165 start_token: recog.get_current_token().borrow().to_owned(),
166 }
168 }
169 pub fn new_full<'a, T: Parser<'a>>(
170 recog: &mut T,
171 start_token: OwningToken,
172 offending_token: OwningToken,
173 ) -> NoViableAltError {
174 Self {
175 base: BaseRecognitionError {
176 message: "".to_string(),
177 offending_token,
178 offending_state: recog.get_state(),
179 states_stack: states_stack(recog.get_parser_rule_context().clone()).collect(), },
181 start_token,
182 }
184 }
185}
186
187#[derive(Debug, Clone)]
189#[allow(missing_docs)]
190pub struct InputMisMatchError {
191 pub base: BaseRecognitionError,
192}
193
194#[allow(missing_docs)]
195impl InputMisMatchError {
196 pub fn new<'a, T: Parser<'a>>(recognizer: &mut T) -> InputMisMatchError {
197 InputMisMatchError {
198 base: BaseRecognitionError::new(recognizer),
199 }
200 }
201
202 pub fn with_state<'a, T: Parser<'a>>(
203 recognizer: &mut T,
204 offending_state: isize,
205 ctx: Rc<<T::Node as ParserNodeType<'a>>::Type>,
206 ) -> InputMisMatchError {
207 let mut a = Self::new(recognizer);
208 a.base.offending_state = offending_state;
210 a.base.states_stack = states_stack(ctx).collect();
211 a
212 }
213}
214
215#[derive(Debug, Clone)]
219#[allow(missing_docs)]
220pub struct FailedPredicateError {
221 pub base: BaseRecognitionError,
222 pub rule_index: isize,
223 predicate_index: isize,
224 pub predicate: String,
225}
226
227#[allow(missing_docs)]
228impl FailedPredicateError {
229 pub fn new<'a, T: Parser<'a>>(
230 recog: &mut T,
231 predicate: Option<String>,
232 msg: Option<String>,
233 ) -> ANTLRError {
234 let tr = recog.get_interpreter().atn().states[recog.get_state() as usize]
235 .get_transitions()
236 .first()
237 .unwrap();
238 let (rule_index, predicate_index) = if tr.get_serialization_type() == TRANSITION_PREDICATE {
239 let pr = tr.deref().cast::<PredicateTransition>();
240 (pr.rule_index, pr.pred_index)
241 } else {
242 (0, 0)
243 };
244
245 ANTLRError::PredicateError(FailedPredicateError {
246 base: BaseRecognitionError {
247 message: msg.unwrap_or_else(|| {
248 format!(
249 "failed predicate: {}",
250 predicate.as_deref().unwrap_or("None")
251 )
252 }),
253 offending_token: recog.get_current_token().borrow().to_owned(),
254 offending_state: recog.get_state(),
255 states_stack: states_stack(recog.get_parser_rule_context().clone()).collect(), },
257 rule_index,
258 predicate_index,
259 predicate: predicate.unwrap_or_default(),
260 })
261 }
262}