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
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Pos {
pub line: u32,
pub column: u32,
}
impl Pos {
#[inline]
pub fn new(line: u32, column: u32) -> Self {
Self { line: line, column: column }
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct ReprPosValue(pub u32);
impl std::fmt::Display for ReprPosValue {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self.0.checked_add(1) {
Some(value) => std::fmt::Display::fmt(&value, f),
None => f.write_str("4294967296"),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct PosTree {
pub pos: Pos,
pub children: Vec<PosTree>,
}
impl PosTree {
#[inline]
pub fn new(pos: Pos) -> Self {
Self {
pos: pos,
children: Vec::new(),
}
}
pub fn index_path(&self, path: &[usize]) -> Option<&Self> {
let mut current_node = self;
for &index in path {
if let Some(next_node) = self.children.get(index) {
current_node = next_node;
} else {
return None;
}
}
Some(current_node)
}
}