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
use crate::Key;
use std::collections::HashMap;
pub struct Context<'a> {
map_stack: Vec<HashMap<Key, Key>>,
pos: usize,
bytes: &'a [u8],
}
impl<'a> Context<'a> {
pub fn new(bytes: &'a [u8]) -> Context<'a> {
Context {
map_stack: vec![HashMap::new()],
pos: 0,
bytes,
}
}
pub fn peek(&self, n: usize) -> Result<&'a [u8], String> {
match self.bytes.get(self.pos..self.pos + n) {
Some(s) => Ok(s),
None => Err("Tried to peek beyond end".to_owned()),
}
}
pub fn read(&mut self, n: usize) -> Result<&'a [u8], String> {
self.pos += n;
match self.bytes.get(self.pos - n..self.pos) {
Some(s) => Ok(s),
None => Err("Tried to read beyond end".to_owned()),
}
}
pub fn push_stack(&mut self, map: HashMap<Key, Key>) {
self.map_stack.push(map);
}
pub fn pop_stack(&mut self) {
self.map_stack.pop().unwrap();
}
pub fn getvar<K: Into<Key>>(&self, k: K) -> Option<&Key> {
self.map_stack.last().unwrap().get(&k.into())
}
pub fn store(&self) -> usize {
self.pos
}
pub fn restore(&mut self, pos: usize) {
self.pos = pos;
}
pub fn setvar<K: Into<Key>, V: Into<Key>>(&mut self, k: K, v: V) {
self.map_stack
.last_mut()
.unwrap()
.insert(k.into(), v.into());
}
}