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
use crate::{
ast::{
diagnostics::Diagnostic as D,
node::{
root::{KnownRoot, Root, RootKind, RootValue},
roots::Roots,
TypeExpr,
},
RootEntry, Value,
},
cst::Kind,
parse::Span,
Node, Spanned, SpannedExt,
};
use super::*;
use ltk_meta::PropertyKind::{self};
#[derive(Debug, Clone)]
pub struct RawRootProperty {
pub key: Spanned<RootKind>,
pub type_expr: Spanned<Option<TypeExpr>>,
pub value: Option<Value>,
}
impl<'a> Builder<'a> {
pub(crate) fn build(mut self) -> Ast {
let root_node = self.cst.root();
let mut file_type = None;
let mut version = None;
let mut linked = None;
let mut entries = None;
let mut idx = 0;
let mut roots = Roots::new(root_node.children.get(self.cst).iter().filter_map(|child| {
let node = child.tree(self.cst)?;
let root = self.resolve_root(node, idx)?;
idx += 1;
Some(root)
}));
// we don't need to coerce here since we collected these roots via Self::resolve_entry, who
// handles coercion already
for (idx, root) in roots.iter_mut().enumerate() {
if let Some(expected_type) = root.name.expected_type() {
match root.type_expr.value {
Some(type_expr) => {
if type_expr != expected_type {
self.push(
D::InvalidRootEntryType {
root_kind: *root.name,
key_span: root.name.span,
type_span: root.type_expr.span,
got: type_expr.into(),
expected: expected_type,
}
.unwrap(),
);
}
}
None => {
self.push(
D::MissingEntryType {
key_span: root.name.span,
}
.unwrap(),
);
}
}
if let Some(RootValue::Value(value)) = root.value.as_ref() {
if let Some(got) = value.rito_type() {
if got != expected_type {
self.push(
D::TypeMismatch {
span: value.span(),
expected: expected_type.into(),
expected_span: None,
got: got.into(),
}
.unwrap(),
);
continue;
}
}
}
}
match *root.name {
RootKind::Unknown => {
self.push(
D::MissingEntryValue {
key_span: root.name.span,
expected: root
.name
.expected_type()
.map(|t| t.with_span(root.type_expr.span)),
}
.unwrap(),
);
}
RootKind::Version => {
if let Some(RootValue::Value(Value::U32(v))) = &root.value {
if let Some(existing) = version.replace(KnownRoot {
idx,
value: v.value,
}) {
self.push(
D::ShadowedRoot {
shadower: idx,
shadowee: existing.idx,
}
.default_span(Span::empty(0)),
);
}
}
}
RootKind::Type => {
if let Some(v) = root
.value
.as_ref()
.and_then(|v| v.as_value())
.and_then(|v| v.as_string())
{
if let Some(existing) = file_type.replace(KnownRoot {
idx,
value: v.parse().unwrap(),
}) {
self.push(
D::ShadowedRoot {
shadower: idx,
shadowee: existing.idx,
}
.default_span(Span::empty(0)),
);
}
}
}
RootKind::Linked => {
let Some(value) = &root.value else {
continue;
};
match value {
RootValue::Value(Value::Container { items, .. }) => {
if let Some(existing) = linked.replace(KnownRoot {
idx,
value: items
.iter()
.filter_map(|v| {
v.clone()
.try_coerce_to(PropertyKind::String)
.ok()
.and_then(|v| v.into_string())
})
.collect(),
}) {
self.push(
D::ShadowedRoot {
shadower: idx,
shadowee: existing.idx,
}
.default_span(Span::empty(0)),
);
}
}
_ => {
continue;
}
}
}
RootKind::Entries => match root.value.take() {
Some(RootValue::Value(Value::Map {
entries: map, span, ..
})) => {
let items = map
.into_iter()
.filter_map(|(k, v)| match (k, v) {
(Value::Hash(path_hash), Some(Value::Embedded(object))) => {
Some(RootEntry { path_hash, object })
}
_ => None,
})
.collect();
root.value = Some(RootValue::Entries(Spanned::new(span, items)));
if let Some(shadowee) = entries.replace(idx) {
self.push(
D::ShadowedRoot {
shadower: idx,
shadowee,
}
.default_span(Span::empty(0)),
);
}
}
// not a well-formed map: leave the raw value in place to navigate/diagnose
other => root.value = other,
},
}
}
roots.file_type = file_type;
roots.version = version;
roots.linked = linked;
roots.entries = entries;
for kind in roots.missing() {
self.push(D::MissingRootEntry { root_kind: kind }.default_span(Span::empty(0)));
}
Ast {
roots,
diagnostics: self.diagnostics,
}
}
fn resolve_root(&mut self, node: &Node, idx: usize) -> Option<Root> {
match node.kind {
Kind::Comment | Kind::ErrorTree => return None,
Kind::Entry => match self.resolve_entry(node, None, None) {
Ok(entry) => {
let kind = RootKind::from_value(&entry.key);
return Some(Root {
idx,
name: kind.with_span(entry.key.span()),
type_expr: entry.type_expr,
value: entry.value.map(RootValue::Value),
});
}
Err(e) => {
self.push(e.fallback(node.span));
}
},
_ => {
self.push(D::RootNonEntry.default_span(node.span));
}
}
None
}
}