Skip to main content

oni_comb_parser/
byte_input.rs

1use crate::input::Input;
2
3pub struct ByteInput<'a> {
4  src: &'a [u8],
5  offset: usize,
6}
7
8impl<'a> ByteInput<'a> {
9  pub fn new(src: &'a [u8]) -> Self {
10    Self { src, offset: 0 }
11  }
12
13  #[inline]
14  pub fn peek_byte(&self) -> Option<u8> {
15    self.src.get(self.offset).copied()
16  }
17}
18
19impl<'a> Input for ByteInput<'a> {
20  type Checkpoint = usize;
21  type Slice = &'a [u8];
22  type Token = u8;
23
24  #[inline]
25  fn next_token(&mut self) -> Option<u8> {
26    let b = self.src.get(self.offset).copied()?;
27    self.offset += 1;
28    Some(b)
29  }
30
31  #[inline]
32  fn peek_token(&self) -> Option<u8> {
33    self.src.get(self.offset).copied()
34  }
35
36  #[inline]
37  fn slice_since(&self, cp: usize) -> &'a [u8] {
38    &self.src[cp..self.offset]
39  }
40
41  fn checkpoint(&self) -> usize {
42    self.offset
43  }
44
45  fn reset(&mut self, cp: usize) {
46    self.offset = cp;
47  }
48
49  fn offset(&self) -> usize {
50    self.offset
51  }
52
53  fn remaining(&self) -> &'a [u8] {
54    &self.src[self.offset..]
55  }
56
57  fn is_eof(&self) -> bool {
58    self.offset >= self.src.len()
59  }
60}