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
extern crate text_unit;
use std::{ops};
mod top_down_builder;
mod bottom_up_builder;
pub mod algo;
pub use text_unit::{TextRange, TextUnit};
pub use top_down_builder::TopDownBuilder;
pub use bottom_up_builder::BottomUpBuilder;
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct Symbol(pub u16);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Token {
pub symbol: Symbol,
pub len: TextUnit,
}
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct PtNodeId(u32);
#[derive(Debug, Clone)]
pub struct PtNode {
symbol: Symbol,
range: TextRange,
parent: Option<PtNodeId>,
first_child: Option<PtNodeId>,
next_sibling: Option<PtNodeId>,
}
#[derive(Debug, Clone)]
pub struct ParseTree {
root: PtNodeId,
nodes: Vec<PtNode>,
}
impl ParseTree {
pub fn root(&self) -> PtNodeId {
self.root
}
}
impl ops::Index<PtNodeId> for ParseTree {
type Output = PtNode;
fn index(&self, id: PtNodeId) -> &PtNode {
&self.nodes[id.0 as usize]
}
}
impl PtNode {
pub fn symbol(&self) -> Symbol {
self.symbol
}
pub fn range(&self) -> TextRange {
self.range
}
pub fn parent(&self) -> Option<PtNodeId> {
self.parent
}
pub fn first_child(&self) -> Option<PtNodeId> {
self.first_child
}
pub fn next_sibling(&self) -> Option<PtNodeId> {
self.next_sibling
}
}
impl ops::Index<PtNodeId> for Vec<PtNode> {
type Output = PtNode;
fn index(&self, PtNodeId(idx): PtNodeId) -> &PtNode {
&self[idx as usize]
}
}
impl ops::IndexMut<PtNodeId> for Vec<PtNode> {
fn index_mut(&mut self, PtNodeId(idx): PtNodeId) -> &mut PtNode {
&mut self[idx as usize]
}
}
fn fill<T>(slot: &mut Option<T>, value: T) {
assert!(slot.is_none());
*slot = Some(value);
}