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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
//use crate::id::Id;
use {
    std::collections::{HashMap, BTreeSet},
    crate::{
        makepad_live_id::*,
        makepad_error_log::*,
        makepad_live_tokenizer::{TokenWithLen, Delim, FullToken, LiveId, State, Cursor, live_error_origin, LiveErrorOrigin},
        live_error::{LiveError, LiveErrorSpan, LiveFileError},
        live_parser::LiveParser,
        live_document::{LiveOriginal, LiveExpanded},
        live_node::{LiveNodeOrigin, LiveNode, LiveValue, LiveType, LiveTypeInfo, LiveIdAsProp},
        /*live_node_reader::{LiveNodeMutReader},*/
        live_node_vec::{LiveNodeSliceApi, /*LiveNodeVecApi*/},
        live_ptr::{LiveFileId, LivePtr, LiveModuleId, LiveFileGeneration},
        live_token::{LiveToken, LiveTokenId, TokenWithSpan},
        span::{TextSpan, TextPos},
        live_expander::{LiveExpander},
        live_component::{LiveComponentRegistries}
    }
};

#[derive(Default)]
pub struct LiveFile {
    pub (crate) reexpand: bool,
    
    pub module_id: LiveModuleId,
    pub (crate) start_pos: TextPos,
    pub file_name: String,
    pub cargo_manifest_path: String,
    pub (crate) source: String,
    pub (crate) deps: BTreeSet<LiveModuleId>,
    
    pub generation: LiveFileGeneration,
    pub original: LiveOriginal,
    pub next_original: Option<LiveOriginal>,
    pub expanded: LiveExpanded,
    
    pub live_type_infos: Vec<LiveTypeInfo>,
}

pub struct LiveRegistry {
    pub (crate) file_ids: HashMap<String, LiveFileId>,
    pub module_id_to_file_id: HashMap<LiveModuleId, LiveFileId>,
    pub live_files: Vec<LiveFile>,
    pub live_type_infos: HashMap<LiveType, LiveTypeInfo>,
    //pub ignore_no_dsl: HashSet<LiveId>,
    pub main_module: Option<(LiveModuleId, LiveId)>,
    pub components: LiveComponentRegistries,
    pub package_root: Option<String>
}

impl Default for LiveRegistry {
    fn default() -> Self {
        Self {
            main_module: None,
            file_ids: HashMap::new(),
            module_id_to_file_id: HashMap::new(),
            live_files: Vec::new(),
            live_type_infos: HashMap::new(),
            components: LiveComponentRegistries::default(),
            package_root: None
        }
    }
}
/*
pub struct LiveDocNodes<'a> {
    pub nodes: &'a [LiveNode],
    pub file_id: LiveFileId,
    pub index: usize
}*/

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum LiveScopeTarget {
    LocalPtr(usize),
    LivePtr(LivePtr)
}


#[derive(Clone, Debug, PartialEq)]
pub struct LiveFileChange {
    pub file_name: String,
    pub content: String
}

impl LiveRegistry {
    
    pub fn generation_valid(&self, live_ptr: LivePtr) -> bool {
        let doc = &self.live_files[live_ptr.file_id.to_index()];
        doc.generation == live_ptr.generation
    }
    
    pub fn ptr_to_node(&self, live_ptr: LivePtr) -> &LiveNode {
        let doc = &self.live_files[live_ptr.file_id.to_index()];
        if doc.generation != live_ptr.generation {
            panic!("ptr_to_node generation invalid for file {} gen:{} ptr:{}", doc.file_name, doc.generation, live_ptr.generation);
        }
        doc.expanded.resolve_ptr(live_ptr.index as usize)
    }
    
    pub fn file_name_to_file_id(&self, file_name: &str) -> Option<LiveFileId> {
        for (index, file) in self.live_files.iter().enumerate() {
            if file.file_name == file_name {
                return Some(LiveFileId::new(index))
            }
        }
        None
    }
    
    pub fn file_id_to_file_name(&self, file_id: LiveFileId) -> &str {
        &self.live_files[file_id.to_index()].file_name
    }
    
    pub fn file_id_to_cargo_manifest_path(&self, file_id: LiveFileId) -> String {
        let file = &self.live_files[file_id.to_index()];
        let manifest_path = &file.cargo_manifest_path;
        if let Some(package_root) = &self.package_root {
            if file.module_id.0.0 == 0 {
                return package_root.to_string();
            }
            return format!("{}/{}", package_root, file.module_id.0);
        }
        manifest_path.to_string()
    }
    
    pub fn crate_name_to_cargo_manifest_path(&self, crate_name: &str) -> Option<String> {
        let crate_name = crate_name.replace('-', "_");
        let base_crate = LiveId::from_str_with_lut(&crate_name).unwrap();
        for file in &self.live_files {
            if file.module_id.0 == base_crate {
                if let Some(package_root) = &self.package_root {
                    return Some(format!("{}/{}", package_root, crate_name));
                }
                return Some(file.cargo_manifest_path.to_string())
            }
        }
        None
    }
    
    pub fn ptr_to_doc_node(&self, live_ptr: LivePtr) -> (&LiveExpanded, &LiveNode) {
        let doc = &self.live_files[live_ptr.file_id.to_index()];
        if doc.generation != live_ptr.generation {
            panic!("ptr_to_doc_node generation invalid for file {} gen:{} ptr:{}", doc.file_name, doc.generation, live_ptr.generation);
        }
        (&doc.expanded, doc.expanded.resolve_ptr(live_ptr.index as usize))
    }
    
    pub fn ptr_to_doc(&self, live_ptr: LivePtr) -> &LiveExpanded {
        let doc = &self.live_files[live_ptr.file_id.to_index()];
        if doc.generation != live_ptr.generation {
            panic!("ptr_to_doc generation invalid for file {} gen:{} ptr:{}", doc.file_name, doc.generation, live_ptr.generation);
        }
        &doc.expanded
    }
    
    pub fn file_id_to_file(&self, file_id: LiveFileId) -> &LiveFile {
        &self.live_files[file_id.to_index()]
    }
    
    pub fn file_id_to_file_mut(&mut self, file_id: LiveFileId) -> &mut LiveFile {
        &mut self.live_files[file_id.to_index()]
    }
    
    pub fn file_id_index_to_live_ptr(&self, file_id: LiveFileId, index: usize) -> LivePtr {
        LivePtr {
            file_id,
            index: index as u32,
            generation: self.live_files[file_id.to_index()].generation
        }
    }
    
    pub fn ptr_to_nodes_index(&self, live_ptr: LivePtr) -> (&[LiveNode], usize) {
        let doc = &self.live_files[live_ptr.file_id.to_index()];
        if doc.generation != live_ptr.generation {
            panic!("ptr_to_nodes_index generation invalid for file {} gen:{} ptr:{}", doc.file_name, doc.generation, live_ptr.generation);
        }
        (&doc.expanded.nodes, live_ptr.index as usize)
    }
    
    pub fn path_str_to_file_id(&self, path: &str) -> Option<LiveFileId> {
        for (index, file) in self.live_files.iter().enumerate() {
            if file.file_name == path {
                return Some(LiveFileId(index as u16))
            }
        }
        None
    }
    
    
    pub fn token_id_to_origin_doc(&self, token_id: LiveTokenId) -> &LiveOriginal {
        &self.live_files[token_id.file_id().unwrap().to_index()].original
    }
    
    pub fn token_id_to_token(&self, token_id: LiveTokenId) -> &TokenWithSpan {
        &self.live_files[token_id.file_id().unwrap().to_index()].original.tokens[token_id.token_index()]
    }
    
    pub fn token_id_to_expanded_doc(&self, token_id: LiveTokenId) -> &LiveExpanded {
        &self.live_files[token_id.file_id().unwrap().to_index()].expanded
    }
    
    pub fn module_id_to_file_id(&self, module_id: LiveModuleId) -> Option<LiveFileId> {
        self.module_id_to_file_id.get(&module_id).cloned()
    }
    
    pub fn file_id_to_module_id(&self, file_id: LiveFileId) -> Option<LiveModuleId> {
        if let Some((k,_v)) = self.module_id_to_file_id.iter().find(|(_k,v)| **v == file_id){
            return Some(*k)
        }
        None
    }
    
    pub fn live_node_as_string(&self, node: &LiveNode) -> Option<String> {
        match &node.value {
            LiveValue::Str(v) => {
                Some(v.to_string())
            }
            LiveValue::String(v) => {
                Some(v.as_str().to_string())
            }
            LiveValue::InlineString(v) => {
                Some(v.as_str().to_string())
            }
            LiveValue::Dependency (v) => {
                Some(v.as_str().to_string())
            }
            _ => None
        }
    }
    
    // this looks at the 'id' before the live token id
    pub fn get_node_prefix(&self, origin: LiveNodeOrigin) -> Option<LiveId> {
        if !origin.node_has_prefix() {
            return None
        }
        let first_def = origin.first_def().unwrap();
        let token_index = first_def.token_index();
        if token_index == 0 {
            return None;
        }
        let doc = &self.live_files[first_def.file_id().unwrap().to_index()].original;
        let token = &doc.tokens[token_index - 1];
        if let LiveToken::Ident(id) = token.token {
            return Some(id)
        }
        None
    }
    
    pub fn module_id_to_expanded_nodes(&self, module_id: LiveModuleId) -> Option<&[LiveNode]> {
        if let Some(file_id) = self.module_id_to_file_id.get(&module_id) {
            let doc = &self.live_files[file_id.to_index()].expanded;
            return Some(&doc.nodes)
        }
        None
    }
    
    pub fn module_id_and_name_to_ptr(&self, module_id: LiveModuleId, name: LiveId) -> Option<LivePtr> {
        if let Some(file_id) = self.module_id_to_file_id.get(&module_id) {
            let live = &self.live_files[file_id.to_index()];
            let doc = &live.expanded;
            if name != LiveId::empty() {
                if doc.nodes.is_empty() {
                    error!("module_path_id_to_doc zero nodelen {}", self.file_id_to_file_name(*file_id));
                    return None
                }
                if let Some(index) = doc.nodes.child_by_name(0, name.as_instance()) {
                    return Some(LivePtr {file_id: *file_id, index: index as u32, generation: live.generation});
                }
                else {
                    return None
                }
            }
            else {
                return Some(LivePtr {file_id: *file_id, index: 0, generation: live.generation});
            }
        }
        None
    }
    /*
    pub fn find_scope_item_via_class_parent(&self, start_ptr: LivePtr, item: LiveId) -> Option<(&[LiveNode], usize)> {
        let (nodes, index) = self.ptr_to_nodes_index(start_ptr);
        if let LiveValue::Class {class_parent, ..} = &nodes[index].value {
            // ok its a class so now first scan up from here.
            
            if let Some(index) = nodes.scope_up_down_by_name(index, item) {
                // item can be a 'use' as well.
                // if its a use we need to resolve it, otherwise w found it
                if let LiveValue::Use(module_id) = &nodes[index].value {
                    if let Some(ldn) = self.module_id_and_name_to_doc(*module_id, nodes[index].id) {
                        return Some((ldn.nodes, ldn.index))
                    }
                }
                else {
                    return Some((nodes, index))
                }
            }
            else {
                if let Some(class_parent) = class_parent {
                    if class_parent.file_id != start_ptr.file_id {
                        return self.find_scope_item_via_class_parent(*class_parent, item)
                    }
                }
                
            }
        }
        else {
            println!("WRONG TYPE  {:?}", nodes[index].value);
        }
        None
    }*/
    
    pub fn find_module_id_name(&self, item: LiveId, module_id: LiveModuleId) -> Option<LiveScopeTarget> {
        // ok lets find it in that other doc
        if let Some(file_id) = self.module_id_to_file_id(module_id) {
            let file = self.file_id_to_file(file_id);
            if file.expanded.nodes.is_empty() {
                log!("Looking for {} but its not expanded yet, dependency order bug", file.file_name);
                return None
            }
            if let Some(index) = file.expanded.nodes.child_by_name(0, item.as_instance()) {
                return Some(LiveScopeTarget::LivePtr(
                    LivePtr {file_id, index: index as u32, generation: file.generation}
                ))
            }
        }
        None
    }
    
    pub fn find_scope_target(&self, item: LiveId, nodes: &[LiveNode]) -> Option<LiveScopeTarget> {
        if let LiveValue::Root {id_resolve} = &nodes[0].value {
            id_resolve.get(&item).cloned()
        }
        else {
            log!("Can't find scope target on rootnode without id_resolve");
            None
        }
    }
    
    
    pub fn find_scope_target_one_level_or_global(&self, item: LiveId, index: usize, nodes: &[LiveNode]) -> Option<LiveScopeTarget> {
        if let Some(index) = nodes.scope_up_down_by_name(index, item.as_instance(), 1) {
            Some(LiveScopeTarget::LocalPtr(index))
        } else {
            self.find_scope_target(item, nodes)
        }
    }
    
    pub fn find_scope_ptr_via_expand_index(&self, file_id: LiveFileId, index: usize, item: LiveId) -> Option<LivePtr> {
        // ok lets start
        // let token_id = origin.token_id().unwrap();
        //let index = origin.node_index().unwrap();
        //let file_id = token_id.file_id();
        let file = self.file_id_to_file(file_id);
        match self.find_scope_target_one_level_or_global(item, index, &file.expanded.nodes) {
            Some(LiveScopeTarget::LocalPtr(index)) => Some(LivePtr {file_id, index: index as u32, generation: file.generation}),
            Some(LiveScopeTarget::LivePtr(ptr)) => Some(ptr),
            None => None
        }
    }
    
    pub fn live_error_to_live_file_error(&self, live_error: LiveError) -> LiveFileError {
        match live_error.span {
            LiveErrorSpan::Text(text_span) => {
                let live_file = &self.live_files[text_span.file_id.to_index()];
                LiveFileError {
                    origin: live_error.origin,
                    file: live_file.file_name.clone(),
                    span: text_span,
                    message: live_error.message
                }
            }
            LiveErrorSpan::Token(token_span) => {
                let (file_name, span) = if let Some(file_id) = token_span.token_id.file_id() {
                    let live_file = &self.live_files[file_id.to_index()];
                    (live_file.file_name.as_str(), live_file.original.tokens[token_span.token_id.token_index()].span)
                }
                else {
                    ("<file id is not defined>", TextSpan::default())
                };
                LiveFileError {
                    origin: live_error.origin,
                    file: file_name.to_string(),
                    span,
                    message: live_error.message
                }
            }
        }
    }
    
    pub fn token_id_to_span(&self, token_id: LiveTokenId) -> TextSpan {
        self.live_files[token_id.file_id().unwrap().to_index()].original.token_id_to_span(token_id)
    }
    
    pub fn tokenize_from_str(source: &str, start_pos: TextPos, file_id: LiveFileId) -> Result<Vec<TokenWithSpan>, LiveError> {
        let mut line_chars = Vec::new();
        let mut state = State::default();
        let mut scratch = String::new();
        let mut tokens = Vec::new();
        let mut line_count = start_pos.line;
        for line_str in source.lines() {
            line_chars.clear();
            line_chars.extend(line_str.chars());
            let mut cursor = Cursor::new(&line_chars, &mut scratch);
            let mut last_index = 0usize;
            loop {
                let (next_state, full_token) = state.next(&mut cursor);
                if let Some(full_token) = full_token {
                    let span = TextSpan {
                        file_id,
                        start: TextPos {column: last_index  as u32, line: line_count},
                        end: TextPos {column: last_index as u32 + full_token.len  as u32, line: line_count}
                    };
                    match full_token.token {
                        FullToken::Unknown | FullToken::OtherNumber | FullToken::Lifetime => {
                            return Err(LiveError {
                                origin: live_error_origin!(),
                                span: span.into(),
                                message: "Error tokenizing".to_string()
                            })
                        },
                        _ => if let Some(live_token) = LiveToken::from_full_token(&full_token.token) {
                            // lets build up the span info
                            tokens.push(TokenWithSpan {span, token: live_token})
                        },
                    }
                }
                else {
                    break;
                }
                state = next_state;
                last_index = cursor.index();
            }
            line_count += 1;
        }
        tokens.push(TokenWithSpan {span: TextSpan::default(), token: LiveToken::Eof});
        Ok(tokens)
    }
    
    pub fn tokenize_from_str_live_design(source: &str, start_pos: TextPos, file_id: LiveFileId, mut negative:Option<&mut Vec<TokenWithLen>>) -> Result<Vec<TokenWithSpan>, LiveError> {
        let mut line_chars = Vec::new();
        let mut state = State::default();
        let mut scratch = String::new();
        let mut tokens = Vec::new();
        let mut line_count = start_pos.line;
        #[derive(Debug)]
        enum Parse{
            Before,
            After,
            Bang,
            Brace,
            Body(usize),
        }
        let mut parse = Parse::Before;
        
        'outer: for line_str in source.lines() {
            line_chars.clear();
            line_chars.extend(line_str.chars());
            let mut cursor = Cursor::new(&line_chars, &mut scratch);
            let mut last_index = 0usize;
            loop {
                let (next_state, full_token) = state.next(&mut cursor);
                if let Some(full_token) = full_token {
                    //log!("PARSE STATE {:?} {:?}", parse, full_token);
                    match parse{
                        Parse::Before=>{
                            if let FullToken::Ident(live_id!(live_design)) = &full_token.token{
                                parse = Parse::Bang;
                            }
                            else if let Some(negative) = &mut negative{
                                negative.push(full_token);
                            }
                        }
                        Parse::Bang=> if let FullToken::Punct(live_id!(!)) = &full_token.token{
                            parse = Parse::Brace;
                        }
                        else if let FullToken::Whitespace = &full_token.token{
                        }
                        else{
                            parse = Parse::Before;
                        }
                        Parse::Brace=> if let FullToken::Open(Delim::Brace) = &full_token.token{
                            parse = Parse::Body(0);
                        }
                        else if let FullToken::Whitespace = &full_token.token{
                        }
                        else{
                            parse = Parse::Before;
                        }
                        Parse::Body(depth)=>{
                             if let FullToken::Open(Delim::Brace) = &full_token.token{
                                 parse = Parse::Body(depth + 1)
                            }
                            if let FullToken::Close(Delim::Brace) = &full_token.token{
                                if depth == 0{
                                    last_index = cursor.index();
                                    parse = Parse::After;
                                    continue;
                                }
                                parse = Parse::Body(depth - 1);
                            }
                            let span = TextSpan {
                                file_id,
                                start: TextPos {column: last_index  as u32, line: line_count},
                                end: TextPos {column: last_index as u32 + full_token.len  as u32, line: line_count}
                            };
                            if let Some(live_token) = LiveToken::from_full_token(&full_token.token) {
                                tokens.push(TokenWithSpan {span, token: live_token})
                            }
                        }
                        Parse::After=>{
                            if let Some(negative) = &mut negative{
                                negative.push(full_token);
                            }
                            else{
                                break 'outer;
                            }
                        }
                    }
                }
                else {
                    break;
                }
                state = next_state;
                last_index = cursor.index();
            }
            line_count += 1;
        }
        tokens.push(TokenWithSpan {span: TextSpan::default(), token: LiveToken::Eof});
        Ok(tokens)
    }
    
    pub fn process_file_changes(&mut self, changes: Vec<LiveFileChange>, errors:&mut Vec<LiveError >){
        let mut any_changes = false;
        for change in changes {
            if let Some(file_id) = self.file_name_to_file_id(&change.file_name){
                let module_id = self.file_id_to_module_id(file_id).unwrap();
                let live_file = self.file_id_to_file_mut(file_id);
                match Self::tokenize_from_str_live_design(&change.content, TextPos::default(), file_id, None) {
                    Err(msg) => errors.push(msg), //panic!("Lex error {}", msg),
                    Ok(new_tokens) => {
                        let mut parser = LiveParser::new(&new_tokens, &live_file.live_type_infos, file_id);
                        match parser.parse_live_document() {
                            Err(msg) => {
                                errors.push(msg);
                            },
                            Ok(mut ld) => { // only swap it out when it parses
                                for node in &mut ld.nodes {
                                    match &mut node.value {
                                        LiveValue::Import(live_import) => {
                                            if live_import.module_id.0 == live_id!(crate) { // patch up crate refs
                                               live_import.module_id.0 = module_id.0
                                            };
                                        }
                                        _=>()
                                    }
                                }
                                any_changes = true;
                                ld.tokens = new_tokens;
                                live_file.original = ld;
                                live_file.reexpand = true;
                                live_file.generation.next_gen();
                            }
                        };
                    }
                }
            }
        }
        if any_changes{
            // try to re-expand
            self.expand_all_documents(errors);
        }
    }

    pub fn register_live_file(
        &mut self,
        file_name: &str,
        cargo_manifest_path: &str,
        own_module_id: LiveModuleId,
        source: String,
        live_type_infos: Vec<LiveTypeInfo>,
        start_pos: TextPos,
    ) -> Result<LiveFileId, LiveFileError> {
        
        // lets register our live_type_infos
        if self.file_ids.get(file_name).is_some() {
            panic!("cant register same file twice {}", file_name);
        }
        let file_id = LiveFileId::new(self.live_files.len());
        
        let tokens = match Self::tokenize_from_str(&source, start_pos, file_id) {
            Err(msg) => return Err(msg.into_live_file_error(file_name)), //panic!("Lex error {}", msg),
            Ok(lex_result) => lex_result
        };
        
        let mut parser = LiveParser::new(&tokens, &live_type_infos, file_id);
        
        let mut original = match parser.parse_live_document() {
            Err(msg) => return Err(msg.into_live_file_error(file_name)), //panic!("Parse error {}", msg.to_live_file_error(file, &source)),
            Ok(ld) => ld
        };
        
        original.tokens = tokens;
        
        // update our live type info
        for live_type_info in &live_type_infos {
            if let Some(info) = self.live_type_infos.get(&live_type_info.live_type) {
                if info.module_id != live_type_info.module_id
                    || info.live_type != live_type_info.live_type {
                    panic!()
                }
            };
            self.live_type_infos.insert(live_type_info.live_type, live_type_info.clone());
        }
        
        let mut deps = BTreeSet::new();
        
        for node in &mut original.nodes {
            match &mut node.value {
                LiveValue::Import(live_import) => {
                    if live_import.module_id.0 == live_id!(crate) { // patch up crate refs
                       live_import.module_id.0 = own_module_id.0
                    };
                    deps.insert(live_import.module_id);
                }, // import
                /*LiveValue::Registry(component_id) => {
                    let reg = self.components.0.borrow();
                    if let Some(entry) = reg.values().find(|entry| entry.component_type() == *component_id){
                        entry.get_module_set(&mut deps);
                    }
                }, */
                LiveValue::Class {live_type, ..} => { // hold up. this is always own_module_path
                    let infos = self.live_type_infos.get(live_type).unwrap();
                    for sub_type in infos.fields.clone() {
                        let sub_module_id = sub_type.live_type_info.module_id;
                        if sub_module_id != own_module_id {
                            deps.insert(sub_module_id);
                        }
                    }
                }
                _ => {
                }
            }
        }
        
        let live_file = LiveFile {
            cargo_manifest_path: cargo_manifest_path.to_string(),
            reexpand: true,
            module_id: own_module_id,
            file_name: file_name.to_string(),
            start_pos,
            deps,
            source,
            generation: LiveFileGeneration::default(),
            live_type_infos,
            original,
            next_original: None,
            expanded: LiveExpanded::new()
        };
        self.module_id_to_file_id.insert(own_module_id, file_id);
        
        self.file_ids.insert(file_name.to_string(), file_id);
        self.live_files.push(live_file);
        
        Ok(file_id)
    }
    
    pub fn expand_all_documents(&mut self, errors: &mut Vec<LiveError>) {
        // lets build up all dependencies here
        
        // alright so. we iterate
        let mut dep_order = Vec::new();
        
        fn recur_insert_dep(parent_index: usize, dep_order: &mut Vec<LiveModuleId>, current: LiveModuleId, files: &Vec<LiveFile>) {
            let file = if let Some(file) = files.iter().find( | v | v.module_id == current) {
                file
            }
            else {
                return
            };
            let final_index = if let Some(index) = dep_order.iter().position( | v | *v == current) {
                if index > parent_index { // insert before
                    dep_order.remove(index);
                    dep_order.insert(parent_index, current);
                    parent_index
                }
                else {
                    index
                }
            }
            else {
                dep_order.insert(parent_index, current);
                parent_index
            };
            
            for dep in &file.deps {
                recur_insert_dep(final_index, dep_order, *dep, files);
            }
        }
        
        for file in &self.live_files {
            recur_insert_dep(dep_order.len(), &mut dep_order, file.module_id, &self.live_files);
        }
        
        // now lets do the recursive recompile parsing.
        fn recur_check_reexpand(current: LiveModuleId, files: &Vec<LiveFile>) -> bool {
            let file = if let Some(file) = files.iter().find( | v | v.module_id == current) {
                file
            }
            else {
                return false
            };
            
            if file.reexpand {
                return true;
            }
            
            for dep in &file.deps {
                if recur_check_reexpand(*dep, files) {
                    return true
                }
            }
            false
        }
        
        for i in 0..self.live_files.len() {
            if recur_check_reexpand(self.live_files[i].module_id, &self.live_files) {
                self.live_files[i].reexpand = true;
            }
        }
        
        for module_id in dep_order {
            let file_id = if let Some(file_id) = self.module_id_to_file_id.get(&module_id) {
                file_id
            }
            else {
                continue
            };
            
            if !self.live_files[file_id.to_index()].reexpand {
                continue;
            }
            
            let mut out_doc = LiveExpanded::new();
            std::mem::swap(&mut out_doc, &mut self.live_files[file_id.to_index()].expanded);
            
            out_doc.nodes.clear();
            
            let in_doc = &self.live_files[file_id.to_index()].original;
            
            let mut live_document_expander = LiveExpander {
                live_registry: self,
                in_crate: module_id.0,
                in_file_id: *file_id,
                errors
            };
            live_document_expander.expand(in_doc, &mut out_doc, self.live_files[file_id.to_index()].generation);
            
            self.live_files[file_id.to_index()].reexpand = false;
            std::mem::swap(&mut out_doc, &mut self.live_files[file_id.to_index()].expanded);
        }
    }
}

struct FileDepIter {
    files_todo: Vec<LiveFileId>,
    files_done: Vec<LiveFileId>
}

impl FileDepIter {
    pub fn new(start: LiveFileId) -> Self {
        Self {
            files_todo: vec![start],
            files_done: Vec::new()
        }
    }
    
    pub fn pop_todo(&mut self) -> Option<LiveFileId> {
        if let Some(file_id) = self.files_todo.pop() {
            self.files_done.push(file_id);
            Some(file_id)
        }
        else {
            None
        }
    }
    
    pub fn scan_next(&mut self, live_files: &[LiveFile]) {
        let last_file_id = self.files_done.last().unwrap();
        let module_id = live_files[last_file_id.to_index()].module_id;
        
        for (file_index, live_file) in live_files.iter().enumerate() {
            if live_file.deps.contains(&module_id) {
                let dep_id = LiveFileId::new(file_index);
                if !self.files_done.iter().any( | v | *v == dep_id) {
                    self.files_todo.push(dep_id);
                }
            }
        }
    }
}