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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
use {
crate::{
makepad_live_id::*,
makepad_live_tokenizer::{live_error_origin, LiveErrorOrigin},
live_ptr::{LiveFileId, LivePtr, LiveFileGeneration},
live_error::{LiveError},
live_document::{LiveOriginal, LiveExpanded},
live_node::{LiveValue, LiveNode, LiveIdAsProp, LivePropType},
live_node_vec::{LiveNodeSliceApi, LiveNodeVecApi},
live_registry::{LiveRegistry, LiveScopeTarget},
}
};
pub struct LiveExpander<'a> {
pub live_registry: &'a LiveRegistry,
pub in_crate: LiveId,
pub in_file_id: LiveFileId,
pub errors: &'a mut Vec<LiveError>,
}
impl<'a> LiveExpander<'a> {
pub fn is_baseclass(id: LiveId) -> bool {
id == live_id!(struct)
}
pub fn shift_parent_stack(&self, parents: &mut Vec<(LiveId, usize)>, nodes: &[LiveNode], after_point: usize, old_size: usize, new_size: usize) {
for (live_id, item) in parents {
if *item > after_point {
if old_size > new_size {
*item -= old_size - new_size;
}
else if old_size < new_size {
*item += new_size - old_size;
}
if nodes[*item].id != *live_id {
panic!()
}
if !nodes[*item].is_open() {
panic!()
}
}
}
}
pub fn expand(&mut self, in_doc: &LiveOriginal, out_doc: &mut LiveExpanded, generation: LiveFileGeneration) {
out_doc.nodes.push(in_doc.nodes[0].clone());
let mut current_parent = vec![(LiveId(0), 0usize)];
let mut in_index = 1;
loop {
if in_index >= in_doc.nodes.len() - 1 {
break;
}
let in_node = &in_doc.nodes[in_index];
let in_value = &in_node.value;
match in_value {
LiveValue::Close => {
current_parent.pop();
in_index += 1;
continue;
}
LiveValue::Registry(component_type) => {
let registries = self.live_registry.components.0.borrow();
if let Some(registry) = registries.values().find( | v | v.component_type() == *component_type) {
if in_node.id != LiveId(0) && registry.get_component_info(in_node.id).is_none() {
self.errors.push(LiveError {
origin: live_error_origin!(),
span: in_node.origin.token_id().unwrap().into(),
message: format!("Use statement component not found {}::{}", component_type, in_node.id)
});
}
}
else {
self.errors.push(LiveError {
origin: live_error_origin!(),
span: in_node.origin.token_id().unwrap().into(),
message: format!("Use statement invalid component type {}::{}", component_type, in_node.id)
});
}
let index = out_doc.nodes.append_child_index(current_parent.last().unwrap().1);
let old_len = out_doc.nodes.len();
out_doc.nodes.insert(index, in_node.clone());
self.shift_parent_stack(&mut current_parent, &out_doc.nodes, index, old_len, out_doc.nodes.len());
in_index += 1;
continue;
}
LiveValue::Import(module_id) => {
if self.live_registry.module_id_and_name_to_doc(*module_id, in_node.id).is_none() {
self.errors.push(LiveError {
origin: live_error_origin!(),
span: in_node.origin.token_id().unwrap().into(),
message: format!("Import statement invalid target {}::{}", module_id, in_node.id)
});
}
let index = out_doc.nodes.append_child_index(current_parent.last().unwrap().1);
let old_len = out_doc.nodes.len();
out_doc.nodes.insert(index, in_node.clone());
self.shift_parent_stack(&mut current_parent, &out_doc.nodes, index, old_len, out_doc.nodes.len());
in_index += 1;
continue;
}
_ => ()
}
let out_index = match out_doc.nodes.child_or_append_index_by_name(current_parent.last().unwrap().1, in_node.prop()) {
Ok(overwrite) => {
let out_value = &out_doc.nodes[overwrite].value;
let out_origin = out_doc.nodes[overwrite].origin;
if in_node.origin.edit_info().is_some() {
self.errors.push(LiveError {
origin: live_error_origin!(),
span: in_doc.token_id_to_span(in_node.origin.token_id().unwrap()).into(),
message: format!("Cannot define edit info after first prop def of {}", in_node.id)
});
}
if in_value.is_object() && (out_value.is_clone() || out_value.is_class() || out_value.is_object()) { }
else if out_value.is_expr() || in_value.is_expr() && out_value.is_value_type() {
let next_index = out_doc.nodes.skip_node(overwrite);
let old_len = out_doc.nodes.len();
out_doc.nodes.splice(overwrite..next_index, in_doc.nodes.node_slice(in_index).iter().cloned());
self.shift_parent_stack(&mut current_parent, &out_doc.nodes, overwrite, old_len, out_doc.nodes.len());
in_index = in_doc.nodes.skip_node(in_index);
out_doc.nodes[overwrite].origin.inherit_origin(out_origin);
continue;
}
else if out_value.is_open() && in_value.is_open(){ let next_index = out_doc.nodes.skip_node(overwrite);
let old_len = out_doc.nodes.len();
out_doc.nodes.drain(overwrite+1..next_index-1);
self.shift_parent_stack(&mut current_parent, &out_doc.nodes, overwrite, old_len, out_doc.nodes.len());
out_doc.nodes[overwrite] = in_node.clone();
}
else if out_value.is_open(){
let next_index = out_doc.nodes.skip_node(overwrite);
let old_len = out_doc.nodes.len();
out_doc.nodes.drain(overwrite+1..next_index);
self.shift_parent_stack(&mut current_parent, &out_doc.nodes, overwrite, old_len, out_doc.nodes.len());
out_doc.nodes[overwrite] = in_node.clone();
}
else if in_value.is_open(){
let old_len = out_doc.nodes.len();
out_doc.nodes[overwrite] = in_node.clone();
out_doc.nodes.insert(overwrite+1, in_node.clone());
out_doc.nodes[overwrite+1].value = LiveValue::Close;
self.shift_parent_stack(&mut current_parent, &out_doc.nodes, overwrite, old_len, out_doc.nodes.len());
}
else{
out_doc.nodes[overwrite] = in_node.clone();
};
out_doc.nodes[overwrite].origin.inherit_origin(out_origin);
overwrite
}
Err(insert_point) => {
if in_node.is_expr() {
let old_len = out_doc.nodes.len();
out_doc.nodes.splice(insert_point..insert_point, in_doc.nodes.node_slice(in_index).iter().cloned());
self.shift_parent_stack(&mut current_parent, &out_doc.nodes, insert_point - 1, old_len, out_doc.nodes.len());
in_index = in_doc.nodes.skip_node(in_index);
continue;
}
let old_len = out_doc.nodes.len();
out_doc.nodes.insert(insert_point, in_node.clone());
if in_node.is_open() {
out_doc.nodes.insert(insert_point+1, in_node.clone());
out_doc.nodes[insert_point+1].value = LiveValue::Close;
}
self.shift_parent_stack(&mut current_parent, &out_doc.nodes, insert_point - 1, old_len, out_doc.nodes.len());
insert_point
}
};
match in_value {
LiveValue::Clone(clone) => {
if let Some(target) = self.live_registry.find_scope_target_via_start(*clone, out_index, &out_doc.nodes) {
match target {
LiveScopeTarget::LocalPtr(local_ptr) => {
let old_len = out_doc.nodes.len();
out_doc.nodes.insert_children_from_self(local_ptr, out_index + 1);
self.shift_parent_stack(&mut current_parent, &out_doc.nodes, out_index, old_len, out_doc.nodes.len());
if let LiveValue::Class {live_type: old_live_type, ..} = &out_doc.nodes[out_index].value {
if let LiveValue::Class {live_type, ..} = &out_doc.nodes[local_ptr].value {
if live_type != old_live_type {
self.errors.push(LiveError {
origin: live_error_origin!(),
span: in_doc.token_id_to_span(in_node.origin.token_id().unwrap()).into(),
message: format!("Class override with wrong type {}", in_node.id)
});
}
}
}
out_doc.nodes[out_index].value = out_doc.nodes[local_ptr].value.clone();
if let LiveValue::Class {class_parent, ..} = &mut out_doc.nodes[out_index].value {
*class_parent = Some(LivePtr {file_id: self.in_file_id, index: out_index as u32, generation});
}
}
LiveScopeTarget::LivePtr(live_ptr) => {
let doc = &self.live_registry.live_files[live_ptr.file_id.to_index()].expanded;
let old_len = out_doc.nodes.len();
out_doc.nodes.insert_children_from_other(live_ptr.node_index(), out_index + 1, &doc.nodes);
self.shift_parent_stack(&mut current_parent, &out_doc.nodes, out_index, old_len, out_doc.nodes.len());
out_doc.nodes[out_index].value = doc.nodes[live_ptr.node_index()].value.clone();
if let LiveValue::Class {class_parent, ..} = &mut out_doc.nodes[out_index].value {
*class_parent = Some(LivePtr {file_id: self.in_file_id, index: out_index as u32, generation});
}
}
};
}
else if !Self::is_baseclass(*clone){self.errors.push(LiveError {
origin: live_error_origin!(),
span: in_doc.token_id_to_span(in_node.origin.token_id().unwrap()).into(),
message: format!("Can't find live definition of {} did you forget to call live_design for it?", clone)
});
}
current_parent.push((out_doc.nodes[out_index].id, out_index));
},
LiveValue::Class {live_type, ..} => {
if let LiveValue::Class {class_parent, ..} = &mut out_doc.nodes[out_index].value {
*class_parent = Some(LivePtr {file_id: self.in_file_id, index: out_index as u32, generation});
}
let mut insert_point = out_index + 1;
let mut live_type_info = self.live_registry.live_type_infos.get(live_type).unwrap();
let mut has_deref_hop = false;
while let Some(field) = live_type_info.fields.iter().find( | f | f.id == live_id!(draw_super)) {
has_deref_hop = true;
live_type_info = &field.live_type_info;
}
if has_deref_hop {
if let Some(file_id) = self.live_registry.module_id_to_file_id.get(&live_type_info.module_id) {
let doc = &self.live_registry.live_files[file_id.to_index()].expanded;
if let Some(index) = doc.nodes.child_by_name(0, live_type_info.type_name.as_instance()) {
let old_len = out_doc.nodes.len();
out_doc.nodes.insert_children_from_other(index, out_index + 1, &doc.nodes);
self.shift_parent_stack(&mut current_parent, &out_doc.nodes, out_index, old_len, out_doc.nodes.len());
}
}
}
else {
for field in &live_type_info.fields {
let lti = &field.live_type_info;
if let Some(file_id) = self.live_registry.module_id_to_file_id.get(<i.module_id) {
if *file_id == self.in_file_id { if let Some(index) = out_doc.nodes.child_by_name(0, lti.type_name.as_instance()) {
let node_insert_point = insert_point;
let old_len = out_doc.nodes.len();
insert_point = out_doc.nodes.insert_node_from_self(index, insert_point);
self.shift_parent_stack(&mut current_parent, &out_doc.nodes, node_insert_point - 1, old_len, out_doc.nodes.len());
out_doc.nodes[node_insert_point].id = field.id;
out_doc.nodes[node_insert_point].origin.set_prop_type(LivePropType::Field);
}
else if !lti.live_ignore {
self.errors.push(LiveError {
origin: live_error_origin!(),
span: in_doc.token_id_to_span(in_node.origin.token_id().unwrap()).into(),
message: format!("Can't find live definition of {} did you forget to call live_design for it?", lti.type_name)
});
}
}
else {
let other_nodes = &self.live_registry.live_files[file_id.to_index()].expanded.nodes;
if other_nodes.len() == 0 {
panic!(
"Dependency order bug finding {}, file {} not registered before {}",
lti.type_name,
self.live_registry.file_id_to_file_name(*file_id),
self.live_registry.file_id_to_file_name(self.in_file_id),
);
}
if let Some(index) = other_nodes.child_by_name(0, lti.type_name.as_instance()) {
let node_insert_point = insert_point;
let old_len = out_doc.nodes.len();
insert_point = out_doc.nodes.insert_node_from_other(index, insert_point, other_nodes);
self.shift_parent_stack(&mut current_parent, &out_doc.nodes, node_insert_point - 1, old_len, out_doc.nodes.len());
out_doc.nodes[node_insert_point].id = field.id;
out_doc.nodes[node_insert_point].origin.set_prop_type(LivePropType::Field);
}
else if lti.type_name != LiveId(0) {
self.errors.push(LiveError {
origin: live_error_origin!(),
span: in_doc.token_id_to_span(in_node.origin.token_id().unwrap()).into(),
message: format!("Typename {}, not defined in file where it was expected", lti.type_name)
});
}
}
}
else if !lti.live_ignore {
self.errors.push(LiveError {
origin: live_error_origin!(),
span: in_doc.token_id_to_span(in_node.origin.token_id().unwrap()).into(),
message: format!("Can't find live definition of {} did you forget to call live_design for it?", lti.type_name)
});
}
}
}
current_parent.push((out_doc.nodes[out_index].id, out_index));
}
LiveValue::Expr {..} => {panic!()},
LiveValue::Array |
LiveValue::TupleEnum {..} |
LiveValue::NamedEnum {..} |
LiveValue::Object => { current_parent.push((out_doc.nodes[out_index].id, out_index));
},
LiveValue::DSL {..} => {
},
_ => {}
}
in_index += 1;
}
out_doc.nodes.push(in_doc.nodes.last().unwrap().clone());
for i in 1..out_doc.nodes.len() {
if out_doc.nodes[i].value.is_dsl() {
out_doc.nodes[i].value.set_dsl_expand_index_if_none(i);
}
if out_doc.nodes[i].value.is_expr() {
out_doc.nodes[i].value.set_expr_expand_index_if_none(i);
}
}
}
}