oni_comb_parser_rs/core/
parse_state.rs

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
/// 現在の解析状態を示す構造体。
#[derive(Clone)]
pub struct ParseState<'a, I> {
  input: &'a [I],
  offset: usize,
}

impl<'a, I> ParseState<'a, I> {
  pub fn new(input: &'a [I], offset: usize) -> Self {
    Self { input, offset }
  }

  pub fn last_offset(&self) -> Option<usize> {
    if self.offset > 0 {
      Some(self.offset - 1)
    } else {
      None
    }
  }

  pub fn next_offset(&self) -> usize {
    self.offset
  }

  pub fn add_offset(&self, num_chars: usize) -> ParseState<'a, I> {
    Self::new(self.input, self.offset + num_chars)
  }

  pub fn input(&self) -> &'a [I] {
    &self.input[self.offset..]
  }

  pub fn slice_with_len(&self, n: usize) -> &'a [I] {
    &self.input[self.offset..self.offset + n]
  }
}