-
Notifications
You must be signed in to change notification settings - Fork 771
Expand file tree
/
Copy pathscope.rs
More file actions
76 lines (65 loc) · 1.65 KB
/
scope.rs
File metadata and controls
76 lines (65 loc) · 1.65 KB
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
use super::*;
#[derive(Debug)]
pub(crate) struct Scope<'src: 'run, 'run> {
bindings: Table<'src, Binding<'src, String>>,
parent: Option<&'run Self>,
}
impl<'src, 'run> Scope<'src, 'run> {
pub(crate) fn child(&'run self) -> Self {
Self {
parent: Some(self),
bindings: Table::new(),
}
}
pub(crate) fn root() -> Self {
let mut root = Self {
parent: None,
bindings: Table::new(),
};
for (i, (key, value)) in constants().iter().enumerate() {
root.bind(Binding {
eager: false,
export: false,
file_depth: 0,
name: Name {
token: Token {
column: 0,
kind: TokenKind::Identifier,
length: key.len(),
line: 0,
offset: 0,
path: Path::new("PRELUDE"),
src: key,
},
},
number: Numerator::constant(i),
prelude: true,
private: false,
value: (*value).into(),
});
}
root
}
pub(crate) fn bind(&mut self, binding: Binding<'src>) {
self.bindings.insert(binding);
}
pub(crate) fn bound(&self, name: &str) -> bool {
self.bindings.contains_key(name)
}
pub(crate) fn value(&self, name: &str) -> Option<&str> {
if let Some(binding) = self.bindings.get(name) {
Some(binding.value.as_ref())
} else {
self.parent?.value(name)
}
}
pub(crate) fn bindings(&self) -> impl Iterator<Item = &Binding<String>> {
self.bindings.values()
}
pub(crate) fn names(&self) -> impl Iterator<Item = &str> {
self.bindings.keys().copied()
}
pub(crate) fn parent(&self) -> Option<&'run Self> {
self.parent
}
}