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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
use std::fmt;
use rowan::api::SyntaxNode;
use rnix::{
NixLanguage,
SyntaxKind,
types::{
AttrSet,
EntryHolder,
Ident,
Lambda,
LetIn,
Pattern,
TokenWrapper,
TypedNode,
},
};
use crate::binding::Binding;
/// AST subtree that declares variables
#[derive(Debug, Clone)]
pub enum Scope {
LambdaPattern(Pattern, SyntaxNode<NixLanguage>),
LambdaArg(Ident, SyntaxNode<NixLanguage>),
LetIn(LetIn),
RecAttrSet(AttrSet),
}
impl fmt::Display for Scope {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
Scope::LambdaPattern(_, _) =>
write!(fmt, "lambda pattern"),
Scope::LambdaArg(_, _) =>
write!(fmt, "lambda argument"),
Scope::LetIn(_) =>
write!(fmt, "let binding"),
Scope::RecAttrSet(_) =>
write!(fmt, "rec attrset"),
}
}
}
impl Scope {
/// Construct a new Scope *if* this is an AST node that opens a new scope
pub fn new(node: &SyntaxNode<NixLanguage>) -> Option<Self> {
match node.kind() {
SyntaxKind::NODE_LAMBDA => {
let lambda = Lambda::cast(node.clone())
.expect("Lambda::cast");
let arg = lambda.arg().expect("lambda.arg()");
let body = lambda.body()
.expect("lambda.body()");
match arg.kind() {
SyntaxKind::NODE_IDENT => {
let name = Ident::cast(arg.clone())
.expect("Ident::cast");
Some(Scope::LambdaArg(name, body))
}
SyntaxKind::NODE_PATTERN => {
let pattern = Pattern::cast(arg)
.expect("Pattern::cast");
Some(Scope::LambdaPattern(pattern, body))
}
_ => panic!("Unhandled arg kind: {:?}", arg.kind()),
}
}
SyntaxKind::NODE_LET_IN => {
let let_in = LetIn::cast(node.clone())
.expect("LetIn::cast");
Some(Scope::LetIn(let_in))
}
SyntaxKind::NODE_ATTR_SET => {
let attr_set = AttrSet::cast(node.clone())
.expect("AttrSet::cast");
if attr_set.recursive() {
Some(Scope::RecAttrSet(attr_set))
} else {
None
}
}
_ => None
}
}
pub fn is_lambda_arg(&self) -> bool {
match self {
Scope::LambdaArg(_, _) => true,
_ => false,
}
}
/// The Bindings this Scope introduces
pub fn bindings(&self) -> Box<dyn Iterator<Item = Binding>> {
match self {
Scope::LambdaPattern(pattern, _) => {
let mortal = pattern.ellipsis();
Box::new(
pattern.at()
.map(|name| {
let binding_node = name.node().clone();
Binding::new(name, binding_node, true)
})
.into_iter()
.chain(
pattern.entries()
.map(move |entry| {
let name = entry.name()
.expect("entry.name");
Binding::new(name, entry.node().clone(), mortal)
})
)
)
}
Scope::LambdaArg(name, _) => {
let mortal = ! name.as_str().starts_with("_");
Box::new(
Some(
Binding::new(name.clone(), name.node().clone(), mortal)
).into_iter()
)
}
Scope::LetIn(let_in) =>
Box::new(
let_in.inherits()
.flat_map(|inherit| {
let binding_node = inherit.node().clone();
inherit.idents()
.map(move |name| {
Binding::new(name, binding_node.clone(), true)
})
})
.chain(
let_in.entries()
.map(|entry| {
let key = entry.key()
.expect("entry.key")
.path().next()
.expect("key.path.next");
let name = Ident::cast(key)
.expect("Ident::cast");
Binding::new(name, entry.node().clone(), true)
})
)
),
Scope::RecAttrSet(attr_set) =>
Box::new(
attr_set.inherits()
.flat_map(|inherit| {
let binding_node = inherit.node().clone();
inherit.idents()
.map(move |name| {
Binding::new(name, binding_node.clone(), false)
})
})
.chain(
attr_set.entries()
.filter_map(|entry| {
let key = entry.key()
.expect("entry.key")
.path().next()
.expect("key.path.next");
if key.kind() == SyntaxKind::NODE_IDENT {
let name = Ident::cast(key)
.expect("Ident::cast");
Some(Binding::new(name, entry.node().clone(), false))
} else {
None
}
})
)
),
}
}
/// The code subtrees in which the introduced variables are available
/// TODO: return &SyntaxNode
pub fn bodies(&self) -> Box<dyn Iterator<Item = SyntaxNode<NixLanguage>>> {
match self {
Scope::LambdaPattern(pattern, body) =>
Box::new(
pattern.entries()
.map(|entry| entry.node().clone())
.chain(
Some(body.clone()).into_iter()
)
),
Scope::LambdaArg(_, body) =>
Box::new(
Some(body.clone()).into_iter()
),
Scope::LetIn(let_in) =>
Box::new(
let_in.inherits()
.map(|inherit| inherit.node().clone())
.chain(
let_in.entries()
.map(|entry| entry.node().clone())
)
.chain(let_in.body())
),
Scope::RecAttrSet(attr_set) =>
Box::new(
attr_set.inherits()
.map(|inherit| inherit.node().clone())
.chain(
attr_set.entries()
.map(|entry| entry.node().clone())
)
),
}
}
/// Check the `inherit (var) ...` and `inherit vars` clauses for a
/// given `name`.
///
/// Although a scope may shadow existing variable bindings, it can
/// `inherit` bindings from the outer scope.
pub fn inherits_from(&self, name: &Ident) -> bool {
match self {
Scope::LambdaPattern(_, _) | Scope::LambdaArg(_, _) =>
false,
Scope::LetIn(let_in) =>
let_in.inherits().any(|inherit|
inherit.from()
.map(|from|
crate::usage::find_usage(name, from.node().clone())
).unwrap_or_else(||
crate::usage::find_usage(name, inherit.node().clone())
)
),
Scope::RecAttrSet(attr_set) =>
attr_set.inherits().any(|inherit|
inherit.from()
.map(|from|
crate::usage::find_usage(name, from.node().clone())
).unwrap_or_else(||
crate::usage::find_usage(name, inherit.node().clone())
)
),
}
}
}