1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/// Parser state tracking for error recovery
#[derive(Debug, Clone)]
pub struct ParseState {
/// Current index in token stream
pub index: usize,
/// Number of consecutive errors encountered
pub consecutive_errors: usize,
/// Number of iterations performed
pub iteration_count: usize,
/// Last index processed (for stuck detection)
last_index: usize,
}
impl ParseState {
/// Create a new parse state
pub fn new() -> Self {
ParseState {
index: 0,
consecutive_errors: 0,
iteration_count: 0,
last_index: usize::MAX, // Use MAX instead of -1 for unsigned
}
}
/// Check if parser is stuck (not making progress)
pub fn is_stuck(&self) -> bool {
self.index == self.last_index
}
/// Update the last index to current index
pub fn update_last_index(&mut self) {
self.last_index = self.index;
}
/// Reset error count
pub fn reset(&mut self) {
self.consecutive_errors = 0;
}
/// Increment error count
pub fn increment_error(&mut self) {
self.consecutive_errors += 1;
}
/// Increment iteration count
pub fn increment_iteration(&mut self) {
self.iteration_count += 1;
}
/// Check if parsing should terminate based on error/iteration limits
pub fn should_terminate(&self, max_errors: usize, max_iterations: usize) -> bool {
self.consecutive_errors >= max_errors || self.iteration_count >= max_iterations
}
/// Advance the index
pub fn advance(&mut self) {
self.index += 1;
}
/// Advance the index by n positions
pub fn advance_by(&mut self, n: usize) {
self.index += n;
}
/// Set the index to a specific position
pub fn set_index(&mut self, index: usize) {
self.index = index;
}
}
impl Default for ParseState {
fn default() -> Self {
Self::new()
}
}