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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/// Trait for a collection of tokens.
pub trait TokenCollection<'a> {
  /// The position type the token uses.
  type TPos: PartialOrd + Copy;
  /// The token type.
  type TToken: 'a;

  /// Gets the start position at the specified collection index.
  fn get_start_at_index(&self, index: usize) -> Self::TPos;
  /// Gets the end position at the specified collection index.
  fn get_end_at_index(&self, index: usize) -> Self::TPos;
  /// Gets the token at the specified collection index.
  fn get_token_at_index(&self, index: usize) -> &'a Self::TToken;
  /// Gets the length of the collection.
  fn len(&self) -> usize;
  /// Gets if the collection is empty.
  fn is_empty(&self) -> bool;
}

/// Searches a token collection for
pub struct TokenFinder<TTokenCollection> {
  tokens: TTokenCollection,
  token_index: usize,
}

impl<'a, TTokenCollection> TokenFinder<TTokenCollection>
where
  TTokenCollection: TokenCollection<'a>,
{
  pub fn new(tokens: TTokenCollection) -> TokenFinder<TTokenCollection> {
    TokenFinder { tokens, token_index: 0 }
  }

  #[inline]
  pub fn get_next_token_if(
    &mut self,
    end: TTokenCollection::TPos,
    is_match: impl FnOnce(&'a TTokenCollection::TToken) -> bool,
  ) -> Option<&'a TTokenCollection::TToken> {
    let next_token = self.get_next_token(end)?;
    if is_match(next_token) {
      Some(next_token)
    } else {
      None
    }
  }

  #[inline]
  pub fn get_next_token(&mut self, end: TTokenCollection::TPos) -> Option<&'a TTokenCollection::TToken> {
    self.get_first_token_after(end, |_| true)
  }

  #[inline]
  pub fn get_previous_token_if(
    &mut self,
    start: TTokenCollection::TPos,
    is_match: impl FnOnce(&'a TTokenCollection::TToken) -> bool,
  ) -> Option<&'a TTokenCollection::TToken> {
    let previous_token = self.get_previous_token(start)?;
    if is_match(previous_token) {
      Some(previous_token)
    } else {
      None
    }
  }

  #[inline]
  pub fn get_previous_token(&mut self, start: TTokenCollection::TPos) -> Option<&'a TTokenCollection::TToken> {
    self.get_first_token_before(start, |_| true)
  }

  pub fn get_first_token_within(
    &mut self,
    start: TTokenCollection::TPos,
    end: TTokenCollection::TPos,
    is_match: impl Fn(&'a TTokenCollection::TToken) -> bool,
  ) -> Option<&'a TTokenCollection::TToken> {
    if self.tokens.is_empty() {
      return None;
    }
    self.move_to_node_start(start);

    loop {
      let token_start = self.tokens.get_start_at_index(self.token_index);
      if token_start >= end {
        break;
      } else {
        let current_token = self.tokens.get_token_at_index(self.token_index);
        if is_match(current_token) {
          return Some(current_token);
        }
      }

      if !self.try_increment_index() {
        break;
      }
    }

    None
  }

  pub fn get_last_token_within(
    &mut self,
    start: TTokenCollection::TPos,
    end: TTokenCollection::TPos,
    is_match: impl Fn(&'a TTokenCollection::TToken) -> bool,
  ) -> Option<&'a TTokenCollection::TToken> {
    if self.tokens.is_empty() {
      return None;
    }

    self.move_to_node_end(end);

    loop {
      let token_start = self.tokens.get_start_at_index(self.token_index);
      if token_start >= end || token_start < start {
        break;
      } else {
        let current_token = self.tokens.get_token_at_index(self.token_index);
        if is_match(current_token) {
          return Some(current_token);
        }
      }

      if !self.try_decrement_index() {
        break;
      }
    }

    None
  }

  pub fn get_first_token_before(
    &mut self,
    start: TTokenCollection::TPos,
    is_match: impl Fn(&'a TTokenCollection::TToken) -> bool,
  ) -> Option<&'a TTokenCollection::TToken> {
    if self.tokens.is_empty() {
      return None;
    }
    self.move_to_node_start(start);

    if self.tokens.get_start_at_index(self.token_index) < start {
      let current_token = self.tokens.get_token_at_index(self.token_index);
      if is_match(current_token) {
        return Some(current_token);
      }
    }

    while self.try_decrement_index() {
      let current_token = self.tokens.get_token_at_index(self.token_index);
      if is_match(current_token) {
        return Some(current_token);
      }
    }

    None
  }

  pub fn get_first_token_after(
    &mut self,
    end: TTokenCollection::TPos,
    is_match: impl Fn(&'a TTokenCollection::TToken) -> bool,
  ) -> Option<&'a TTokenCollection::TToken> {
    if self.tokens.is_empty() {
      return None;
    }
    self.move_to_node_end(end);

    while self.try_increment_index() {
      let current_token = self.tokens.get_token_at_index(self.token_index);
      if is_match(current_token) {
        return Some(current_token);
      }
    }

    None
  }

  fn move_to_node_start(&mut self, start: TTokenCollection::TPos) {
    while self.tokens.get_start_at_index(self.token_index) < start {
      if !self.try_increment_index() {
        break;
      }
    }

    while self.tokens.get_start_at_index(self.token_index) > start {
      if !self.try_decrement_index() {
        break;
      }
    }
  }

  fn move_to_node_end(&mut self, end: TTokenCollection::TPos) {
    while self.tokens.get_end_at_index(self.token_index) < end {
      if !self.try_increment_index() {
        break;
      }
    }

    while self.tokens.get_end_at_index(self.token_index) > end {
      if !self.try_decrement_index() {
        break;
      }
    }
  }

  fn try_increment_index(&mut self) -> bool {
    if self.token_index == self.tokens.len() - 1 {
      false
    } else {
      self.token_index += 1;
      true
    }
  }

  fn try_decrement_index(&mut self) -> bool {
    if self.token_index == 0 {
      false
    } else {
      self.token_index -= 1;
      true
    }
  }
}