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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
use ltk_meta::PropertyKind;
use crate::{
ast::{
builder::Builder,
diagnostics::{Diagnostic::*, MaybeSpanDiag},
Property, Value,
},
cst::Kind,
parse::Span,
Node, RitoType,
};
impl<'a> Builder<'a> {
/// Attempt to resolve a `Block`/`ListItemBlock` node to a value
pub(crate) fn resolve_block_value(
&mut self,
block: &Node,
hint: RitoType,
hint_span: Option<Span>,
) -> Result<Value, MaybeSpanDiag> {
use PropertyKind as K;
match hint.base {
K::Struct | K::Embedded => {
self.push(
MissingClassName {
span: block.open_brace_span(self.cst),
expected: hint,
}
.unwrap(),
);
Ok(Value::Unresolved {
span: block.span,
kind: hint.base,
})
}
K::Vector2 | K::Vector3 | K::Vector4 | K::Color | K::Matrix44 => {
Ok(self.resolve_listlike(block, hint.base, hint_span))
}
K::Map => {
let key_kind = hint.subtype(0);
let value_kind = hint.subtype(1);
let entries = self.resolve_body_map_entries(block, key_kind, value_kind, hint_span);
Ok(Value::Map {
key_kind,
value_kind,
entries,
span: block.span,
})
}
K::Container | K::UnorderedContainer => {
let item_kind = hint.subtype(0);
let items = self.resolve_body_items(block, item_kind, hint_span);
Ok(match hint.base {
K::Container => Value::Container {
item_kind,
items,
span: block.span,
},
K::UnorderedContainer => Value::UnorderedContainer {
item_kind,
items,
span: block.span,
},
_ => unreachable!(),
})
}
K::Optional => {
let item_kind = hint.subtype(0);
let item_hint = RitoType::simple(item_kind);
if matches!(
item_kind,
K::Vector2 | K::Vector3 | K::Vector4 | K::Color | K::Matrix44
) {
let content: Vec<&Node> = block
.children
.get(self.cst)
.iter()
.filter_map(|c| c.tree(self.cst))
.filter(|n| n.kind != Kind::Comment)
.collect();
// `option[vec3] = { { 0.5, 5.3, -0.2 } }` - the listlike wrapped in its own
// block, the same shape a listlike takes as a `list[vec3]` item.
if let [only] = content[..] {
if only.kind == Kind::ListItemBlock {
let inner = self.resolve_list_item_block(only, item_hint, hint_span)?;
return Ok(Value::Optional {
item_kind: Some(item_kind),
value: Some(Box::new(inner)),
span: block.span,
});
}
}
if content.is_empty() {
return Ok(Value::Optional {
item_kind: Some(item_kind),
value: None,
span: block.span,
});
}
// an optional listlike spells its components flat, same as a bare listlike
let inner = self.resolve_listlike(block, item_kind, hint_span);
return Ok(Value::Optional {
item_kind: Some(item_kind),
value: Some(Box::new(inner)),
span: block.span,
});
}
let mut value = None;
for child in block.children.get(self.cst).iter() {
let Some(node) = child.tree(self.cst) else {
continue;
};
match node.kind {
Kind::Comment => continue,
Kind::ListItem => {
match self.resolve_value(node, Some(item_hint), hint_span) {
Ok(v) => match v.try_coerce_to(item_kind) {
Ok(coerced) => value = Some(coerced),
Err(v) => self.push(
TypeMismatch {
span: v.span(),
expected: RitoType::simple(item_kind).into(),
expected_span: hint_span,
got: v.rito_type().into(),
}
.unwrap(),
),
},
Err(e) => self.push(e.default_span(node.span)),
}
}
Kind::ListItemBlock => {
match self.resolve_list_item_block(node, item_hint, hint_span) {
Ok(v) => value = Some(v),
Err(e) => self.push(e.fallback(node.span)),
}
}
_ => self.push(
UnexpectedItem {
span: node.trimmed_span(self.cst),
parent: hint,
expected: crate::ItemShape::Value,
}
.unwrap(),
),
}
}
Ok(Value::Optional {
item_kind: Some(item_kind),
value: value.map(Box::new),
span: block.span,
})
}
_ => Err(UnexpectedContainerItem {
span: block.span,
expected: hint,
expected_span: hint_span,
}
.into()),
}
}
pub(crate) fn resolve_body_properties(
&mut self,
block: &Node,
hint: RitoType,
) -> Vec<Property> {
let mut properties = Vec::new();
for child in block.children.get(self.cst).iter() {
let Some(node) = child.tree(self.cst) else {
continue;
};
match node.kind {
Kind::Comment => continue,
Kind::Entry => match self.resolve_entry(node, Some(hint), None) {
Ok(entry) => match entry.key.try_coerce_to(PropertyKind::Hash) {
Ok(Value::Hash(hash)) => properties.push(Property {
name: hash,
type_expr: entry.type_expr,
value: entry.value,
}),
Ok(value) | Err(value) => self.push(
TypeMismatch {
span: value.span(),
expected: RitoType::simple(PropertyKind::Hash).into(),
expected_span: None,
got: value.rito_type().into(),
}
.unwrap(),
),
},
Err(e) => self.push(e.fallback(node.span)),
},
Kind::ListItem | Kind::ListItemBlock => self.push(
UnexpectedItem {
span: node.trimmed_span(self.cst),
parent: hint,
expected: crate::ItemShape::Entry,
}
.unwrap(),
),
_ => {}
}
}
properties
}
fn resolve_body_map_entries(
&mut self,
block: &Node,
key_kind: PropertyKind,
value_kind: PropertyKind,
hint_span: Option<Span>,
) -> Vec<(Value, Option<Value>)> {
let hint = RitoType::map(key_kind, value_kind);
let mut entries = Vec::new();
for child in block.children.get(self.cst).iter() {
let Some(node) = child.tree(self.cst) else {
continue;
};
match node.kind {
Kind::Comment => continue,
Kind::Entry => match self.resolve_entry(node, Some(hint), hint_span) {
Ok(entry) => match entry.key.try_coerce_to(key_kind) {
Ok(key) => {
match entry.value.as_ref() {
Some(value) if value.kind().is_some_and(|k| k != value_kind) => {
self.push(
TypeMismatch {
span: value.span(),
expected: RitoType::simple(value_kind).into(),
expected_span: hint_span,
got: value.rito_type().into(),
}
.unwrap(),
);
}
_ => {
// reporting the error for not having a value should be handled already
}
}
entries.push((key, entry.value));
}
Err(key) => self.push(
TypeMismatch {
span: key.span(),
expected: RitoType::simple(key_kind).into(),
expected_span: hint_span,
got: key.rito_type().into(),
}
.unwrap(),
),
},
Err(e) => self.push(e.fallback(node.span)),
},
Kind::ListItem | Kind::ListItemBlock => self.push(
UnexpectedItem {
span: node.trimmed_span(self.cst),
parent: hint,
expected: crate::ItemShape::Entry,
}
.unwrap(),
),
_ => {}
}
}
entries
}
fn resolve_body_items(
&mut self,
block: &Node,
item_kind: PropertyKind,
hint_span: Option<Span>,
) -> Vec<Value> {
let item_hint = RitoType::simple(item_kind);
let mut items = Vec::new();
for child in block.children.get(self.cst).iter() {
let Some(node) = child.tree(self.cst) else {
continue;
};
match node.kind {
Kind::Comment => continue,
Kind::ListItem => {
match self
.resolve_value(node, Some(item_hint), hint_span)
.and_then(|value| {
value
.try_coerce_to(item_kind)
.map_err(|value| TypeMismatch {
span: value.span(),
expected: RitoType::simple(item_kind).into(),
expected_span: hint_span,
got: value.rito_type().into(),
})
}) {
Ok(value) => {
items.push(value);
}
Err(e) => self.push(e.default_span(node.span)),
}
}
Kind::ListItemBlock => {
match self.resolve_list_item_block(node, item_hint, hint_span) {
Ok(v) => items.push(v),
Err(e) => self.push(e.fallback(node.span)),
}
}
Kind::Entry => self.push(
UnexpectedItem {
span: node.trimmed_span(self.cst),
parent: RitoType::container(item_kind),
expected: crate::ItemShape::Value,
}
.unwrap(),
),
_ => {}
}
}
items
}
}