makepad_live_compiler/
live_parser.rs

1use {
2    std::{
3        rc::Rc,
4        iter::Cloned,
5        slice::Iter
6    },
7    crate::{
8        makepad_live_tokenizer::{LiveId, Delim},
9        makepad_live_tokenizer::{LiveErrorOrigin, live_error_origin},
10        makepad_live_id::*,
11        makepad_math::{
12            Vec2,
13            Vec3,
14            Vec4
15        },
16        live_token::{LiveToken, TokenWithSpan, LiveTokenId},
17        live_ptr::{LiveFileId, LiveModuleId},
18        span::{TextSpan, TextPos},
19        live_error::{LiveError},
20        live_document::LiveOriginal,
21        live_node::{LiveImport, LivePropType, LiveNode, LiveValue, LiveTypeInfo, LiveBinOp, LiveUnOp, LiveNodeOrigin, LiveEditInfo},
22    }
23};
24
25pub struct LiveParser<'a> {
26    pub token_index: usize,
27    pub file_id: LiveFileId,
28    pub live_type_info_counter: usize,
29    pub live_type_infos: &'a [LiveTypeInfo],
30    pub tokens_with_span: Cloned<Iter<'a, TokenWithSpan >>,
31    pub token_with_span: TokenWithSpan,
32    pub end: TextPos,
33}
34
35impl<'a> LiveParser<'a> {
36    pub fn new(tokens: &'a [TokenWithSpan], live_type_infos: &'a [LiveTypeInfo], file_id: LiveFileId) -> Self {
37        let mut tokens_with_span = tokens.iter().cloned();
38        let token_with_span = tokens_with_span.next().unwrap();
39        LiveParser {
40            live_type_info_counter: 0,
41            file_id,
42            tokens_with_span,
43            live_type_infos,
44            token_with_span,
45            token_index: 0,
46            end: TextPos::default(),
47        }
48    }
49}
50
51impl<'a> LiveParser<'a> {
52    
53    fn peek_span(&self) -> TextSpan {
54        self.token_with_span.span
55    }
56    
57    fn peek_token(&self) -> LiveToken {
58        self.token_with_span.token.clone()
59    }
60    
61    fn eat_token(&mut self) -> LiveToken {
62        let token = self.peek_token();
63        self.skip_token();
64        token
65    }
66    
67    fn skip_token(&mut self) {
68        self.end = self.token_with_span.span.end;
69        if let Some(next) = self.tokens_with_span.next(){
70            self.token_with_span = next;
71        }
72        else {
73            self.token_with_span.token = LiveToken::Eof
74        }
75        self.token_index += 1;
76    }
77    
78    fn error(&mut self, message: String, origin: LiveErrorOrigin) -> LiveError {
79        LiveError {
80            origin,
81            span: self.token_with_span.span.into(),
82            message,
83        }
84    }
85    
86    
87    fn end(&self) -> TextPos {
88        self.end
89    }
90    
91    fn token_end(&self) -> TextPos {
92        self.token_with_span.span.end
93    }
94    
95    fn accept_ident(&mut self) -> Option<LiveId> {
96        if let LiveToken::Ident(id) = self.peek_token() {
97            self.skip_token();
98            Some(id)
99        }
100        else {
101            None
102        }
103    }
104    
105    fn accept_token(&mut self, token: LiveToken) -> bool {
106        if self.peek_token() != token {
107            return false;
108        }
109        self.skip_token();
110        true
111    }
112    
113    fn expect_ident(&mut self) -> Result<LiveId, LiveError> {
114        match self.peek_token() {
115            LiveToken::Ident(ident) => {
116                self.skip_token();
117                Ok(ident)
118            }
119            token => Err(self.error(format!("expected ident, unexpected token `{}`", token), live_error_origin!())),
120        }
121    }
122    
123    fn expect_string(&mut self) -> Result<Rc<String>, LiveError> {
124        match self.peek_token() {
125            LiveToken::String(v) => {
126                self.skip_token();
127                Ok(v)
128            }
129            token => Err(self.error(format!("expected ident, unexpected token `{}`", token), live_error_origin!())),
130        }
131    }
132    
133    fn expect_int2(&mut self) -> Result<i64, LiveError> {
134        match self.peek_token() {
135            LiveToken::Int(v) => {
136                self.skip_token();
137                Ok(v)
138            }
139            token => Err(self.error(format!("expected int, unexpected token `{}`", token), live_error_origin!())),
140        }
141    }
142    
143    fn expect_float(&mut self) -> Result<f64, LiveError> {
144        let sign = if let LiveToken::Punct(live_id!(-)) = self.peek_token(){
145            self.skip_token();
146            -1.0
147        }
148        else{
149            1.0
150        };
151        match self.peek_token() {
152            LiveToken::Float(v) => {
153                self.skip_token();
154                Ok(v*sign)
155            }
156            LiveToken::Int(v) => {
157                self.skip_token();
158                Ok(v as f64 * sign)
159            }
160            token => Err(self.error(format!("expected float, unexpected token `{}`", token), live_error_origin!())),
161        }
162    }
163    
164    fn expect_token(&mut self, expected: LiveToken) -> Result<(), LiveError> {
165        let actual = self.peek_token();
166        if actual != expected {
167            return Err(self.error(format!("expected {} unexpected token `{}`", expected, actual), live_error_origin!()));
168        }
169        self.skip_token();
170        Ok(())
171    }
172    
173    fn expect_import(&mut self, ld: &mut LiveOriginal) -> Result<(), LiveError> {
174        let token_id = self.get_token_id();
175        let crate_id = self.expect_ident() ?;
176        // if crate_id is capitalized, its a component.
177        // so we should make a LiveValue::UseComponent
178        self.expect_token(LiveToken::Punct(live_id!(::))) ?;
179        
180        // ok so. we need to collect everything 'upto' the last id
181        let first_module_id = self.expect_ident() ?;
182        self.expect_token(LiveToken::Punct(live_id!(::))) ?;
183        let mut module = String::new();
184        let mut last_id = LiveId(0);
185        
186        module.push_str(&format!("{}", first_module_id));
187        loop {
188            match self.peek_token() {
189                LiveToken::Ident(id) => {
190                    self.skip_token();
191                    last_id = id;
192                    if !self.accept_token(LiveToken::Punct(live_id!(::))) {
193                        break;
194                    }
195                    module.push_str(&format!("::{}", id));
196                },
197                LiveToken::Punct(live_id!(*)) => {
198                    self.skip_token();
199                    last_id = LiveId(0);
200                    break;
201                }
202                _ => {
203                    break;
204                }
205            }
206        }
207        let import_id = last_id;
208        if let LiveToken::Ident(live_id!(as)) = self.peek_token(){
209            self.skip_token();
210            last_id = self.expect_ident() ?;
211        }
212        
213        ld.nodes.push(LiveNode {
214            origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Instance),
215            id: last_id,
216            value: LiveValue::Import(Box::new(LiveImport{
217                module_id: LiveModuleId(crate_id, LiveId::from_str_with_lut(&module).unwrap()),
218                import_id,
219            }))
220        });
221        
222        Ok(())
223    }
224    /*
225   fn expect_registry(&mut self, ld: &mut LiveOriginal) -> Result<(), LiveError> {
226        let token_id = self.get_token_id();
227        let crate_id = self.expect_ident() ?;
228        // if crate_id is capitalized, its a component.
229        // so we should make a LiveValue::UseComponent
230        self.expect_token(LiveToken::Punct(live_id!(::))) ?;
231    
232        let component_id = if self.accept_token(LiveToken::Punct(live_id!(*))) {
233            LiveId(0)
234        }
235        else {
236            self.expect_ident() ?
237        };
238        
239        ld.nodes.push(LiveNode {
240            origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Instance),
241            id: component_id,
242            value: LiveValue::Registry(crate_id)
243        });
244        Ok(())
245    }*/
246    
247    fn expect_fn(&mut self, ld: &mut LiveOriginal) -> Result<(), LiveError> {
248        let token_start = self.token_index - 1;
249        let token_id = self.get_token_id();
250        let prop_id = self.expect_ident() ?;
251        //let token_start = self.token_index;
252        let token_index = self.scan_to_token(LiveToken::Close(Delim::Brace)) ?;
253        
254        ld.nodes.push(LiveNode {
255            origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Instance),
256            id: prop_id,
257            value: LiveValue::DSL {
258                token_start: token_start as u32,
259                token_count: (token_index - token_start) as u32,
260                expand_index: None
261            }
262        });
263        
264        Ok(())
265    }
266    
267    
268    fn possible_edit_info(&mut self, ld: &mut LiveOriginal) -> Result<Option<LiveEditInfo>, LiveError> {
269        // metadata is .{key:value}
270        // lets align to the next index
271        if self.accept_token(LiveToken::Punct(live_id!(.))) {
272            self.expect_token(LiveToken::Open(Delim::Brace)) ?;
273            
274            let fill_index = ld.edit_info.len() & 0xf;
275            if fill_index != 0 {
276                for _ in 0..(16 - fill_index) {
277                    ld.edit_info.push(LiveNode::empty())
278                }
279            }
280            let edit_info_index = ld.edit_info.len();
281            
282            if edit_info_index > 0x3e0 {
283                return Err(self.error("Used more than 64 .{..} edit info fields in a file, we dont have the bitspace for that in our u64.".to_string(), live_error_origin!()))
284            }
285            
286            ld.edit_info.push(LiveNode {
287                origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
288                id: LiveId::empty(),
289                value: LiveValue::Object
290            });
291            
292            
293            while self.peek_token() != LiveToken::Eof {
294                match self.peek_token() {
295                    LiveToken::Close(Delim::Brace) => {
296                        self.skip_token();
297                        ld.edit_info.push(LiveNode {
298                            origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
299                            id: LiveId::empty(),
300                            value: LiveValue::Close
301                        });
302                        return Ok(Some(LiveEditInfo::new(edit_info_index)));
303                    }
304                    LiveToken::Ident(prop_id) => {
305                        self.skip_token();
306                        self.expect_token(LiveToken::Punct(live_id!(:))) ?;
307                        
308                        match self.peek_token() {
309                            LiveToken::Bool(val) => {
310                                self.skip_token();
311                                ld.edit_info.push(LiveNode {
312                                    origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
313                                    id: prop_id,
314                                    value: LiveValue::Bool(val)
315                                });
316                            },
317                            LiveToken::Int(val) => {
318                                self.skip_token();
319                                ld.edit_info.push(LiveNode {
320                                    origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
321                                    id: prop_id,
322                                    value: LiveValue::Int64(val)
323                                });
324                            },
325                            LiveToken::Float(val) => {
326                                self.skip_token();
327                                ld.edit_info.push(LiveNode {
328                                    origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
329                                    id: prop_id,
330                                    value: LiveValue::Float64(val)
331                                });
332                            },
333                            LiveToken::Color(val) => {
334                                self.skip_token();
335                                ld.edit_info.push(LiveNode {
336                                    origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
337                                    id: prop_id,
338                                    value: LiveValue::Color(val)
339                                });
340                            },
341                            LiveToken::String(rcstring) => {
342                                self.skip_token();
343                                ld.edit_info.push(LiveNode {
344                                    origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
345                                    id: prop_id,
346                                    value: LiveValue::String(rcstring)
347                                });
348                            },
349                            other => return Err(self.error(format!("Unexpected token {} in edit_info", other), live_error_origin!()))
350                        }
351                        self.accept_optional_delim();
352                    },
353                    other => return Err(self.error(format!("Unexpected token {} in edit_info", other), live_error_origin!()))
354                }
355            }
356            return Err(self.error("Eof in edit info".to_string(), live_error_origin!()))
357        }
358        
359        Ok(None)
360    }
361    
362    fn expect_node_with_prefix(&mut self, ld: &mut LiveOriginal) -> Result<(), LiveError> {
363        let token_id = self.get_token_id();
364        let real_prop_id = self.expect_ident() ?;
365        let edit_info = self.possible_edit_info(ld) ?;
366        let origin = LiveNodeOrigin::from_token_id(token_id).with_edit_info(edit_info).with_node_has_prefix(true);
367        
368        if self.accept_token(LiveToken::Punct(live_id!(:))) {
369            self.expect_live_value(real_prop_id, origin.with_prop_type(LivePropType::Field), ld) ?;
370        }
371        else if self.accept_token(LiveToken::Punct(live_id!(=))) {
372            self.expect_live_value(real_prop_id, origin.with_prop_type(LivePropType::Instance), ld) ?;
373        }
374        
375        Ok(())
376    }
377    
378    fn expect_array(&mut self, prop_id: LiveId, origin: LiveNodeOrigin, ld: &mut LiveOriginal) -> Result<(), LiveError> {
379        self.expect_token(LiveToken::Open(Delim::Bracket)) ?;
380        ld.nodes.push(LiveNode {
381            origin,
382            id: prop_id,
383            value: LiveValue::Array
384        });
385        let mut counter = 1;
386        while self.peek_token() != LiveToken::Eof {
387            if self.accept_token(LiveToken::Close(Delim::Bracket)) {
388                ld.nodes.push(LiveNode {
389                    origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
390                    id: LiveId::empty(),
391                    value: LiveValue::Close
392                });
393                return Ok(())
394            }
395            self.expect_live_value(
396                LiveId(counter),
397                LiveNodeOrigin::from_token_id(self.get_token_id()).with_prop_type(LivePropType::Nameless),
398                ld
399            ) ?;
400            self.accept_token(LiveToken::Punct(live_id!(,)));
401            counter += 1;
402        }
403        Err(self.error("Eof in array body".to_string(), live_error_origin!()))
404    }
405    
406    
407    fn expect_tuple_enum(&mut self, prop_id: LiveId, origin: LiveNodeOrigin, variant: LiveId, ld: &mut LiveOriginal) -> Result<(), LiveError> {
408        self.expect_token(LiveToken::Open(Delim::Paren)) ?;
409        ld.nodes.push(LiveNode {
410            origin,
411            id: prop_id,
412            value: LiveValue::TupleEnum(variant)
413        });
414        while self.peek_token() != LiveToken::Eof {
415            if self.accept_token(LiveToken::Close(Delim::Paren)) {
416                ld.nodes.push(LiveNode {
417                    origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
418                    id: prop_id,
419                    value: LiveValue::Close
420                });
421                return Ok(())
422            }
423            //let span = self.begin_span();
424            self.expect_live_value(LiveId::empty(), LiveNodeOrigin::from_token_id(self.get_token_id()).with_prop_type(LivePropType::Nameless), ld) ?;
425            self.accept_token(LiveToken::Punct(live_id!(,)));
426        }
427        Err(self.error("Eof in object body".to_string(), live_error_origin!()))
428    }
429    
430    
431    fn expect_named_enum(&mut self, prop_id: LiveId, origin: LiveNodeOrigin,variant: LiveId, ld: &mut LiveOriginal) -> Result<(), LiveError> {
432        self.expect_token(LiveToken::Open(Delim::Brace)) ?;
433        
434        ld.nodes.push(LiveNode {
435            origin,
436            id: prop_id,
437            value: LiveValue::NamedEnum(variant)
438        });
439        
440        while self.peek_token() != LiveToken::Eof {
441            if self.accept_token(LiveToken::Close(Delim::Brace)) {
442                ld.nodes.push(LiveNode {
443                    origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
444                    id: prop_id,
445                    value: LiveValue::Close
446                });
447                return Ok(())
448            }
449            let token_id = self.get_token_id();
450            let prop_id = self.expect_ident() ?;
451            let edit_info = self.possible_edit_info(ld) ?;
452            self.expect_token(LiveToken::Punct(live_id!(:))) ?;
453            self.expect_live_value(prop_id, LiveNodeOrigin::from_token_id(token_id).with_edit_info(edit_info), ld) ?;
454            self.accept_token(LiveToken::Punct(live_id!(,)));
455        }
456        Err(self.error("Eof in named enum".to_string(), live_error_origin!()))
457    }
458    
459    fn get_token_id(&self) -> LiveTokenId {
460        LiveTokenId::new(self.file_id, self.token_index)
461    }
462    
463    fn expect_live_value(&mut self, prop_id: LiveId, origin: LiveNodeOrigin, ld: &mut LiveOriginal) -> Result<(), LiveError> {
464        // now we can have an array or a class instance
465        match self.peek_token() {
466            LiveToken::Punct(live_id!(<))=>{ // class instance
467                self.skip_token();
468                
469                let ident = self.expect_ident()?;
470                self.expect_token(LiveToken::Punct(live_id!(>))) ?;
471                self.expect_token(LiveToken::Open(Delim::Brace))?;
472                ld.nodes.push(LiveNode {
473                    origin,
474                    id: prop_id,
475                    value: LiveValue::Clone(ident)
476                });
477                self.expect_live_class(false, prop_id, ld) ?;
478            }
479            LiveToken::Ident(live_id!(struct)) => { // key/value map
480                self.skip_token();
481                self.expect_token(LiveToken::Open(Delim::Brace)) ?;
482                ld.nodes.push(LiveNode {
483                    origin,
484                    id: prop_id,
485                    value: LiveValue::Clone(live_id!(struct))
486                });
487                self.expect_live_class(false, prop_id, ld) ?;
488            },
489            
490            LiveToken::Open(Delim::Brace) => { // key/value map
491                self.skip_token();
492                
493                // if we get an OpenBrace immediately after, we are a rust_type
494                if self.peek_token() == LiveToken::Open(Delim::Brace) {
495                    self.skip_token();
496                    
497                    let val = self.live_type_info_counter;
498                    self.live_type_info_counter += 1;
499                    
500                    self.accept_ident();
501                    
502                    if val >= self.live_type_infos.len() {
503                        return Err(self.error(format!("live_type index out of range {}", val), live_error_origin!()));
504                    }
505                    ld.nodes.push(LiveNode {
506                        origin,
507                        id: prop_id,
508                        value: LiveValue::Class {
509                            live_type: self.live_type_infos[val].live_type,
510                            class_parent: None,
511                        }
512                    });
513                    self.expect_token(LiveToken::Close(Delim::Brace)) ?;
514                    self.expect_token(LiveToken::Close(Delim::Brace)) ?;
515                    
516                    self.expect_token(LiveToken::Open(Delim::Brace)) ?;
517                    self.expect_live_class(false, prop_id, ld) ?;
518                    
519                    return Ok(());
520                }
521                
522                ld.nodes.push(LiveNode {
523                    origin,
524                    id: prop_id,
525                    value: LiveValue::Object
526                });
527                self.expect_live_class(false, prop_id, ld) ?;
528            },
529            LiveToken::Open(Delim::Paren) => { // expression
530                self.expect_expression(prop_id, origin, ld) ?;
531            },
532            LiveToken::Open(Delim::Bracket) => { // array
533                self.expect_array(prop_id, origin, ld) ?;
534            },
535            LiveToken::Bool(val) => {
536                self.skip_token();
537                ld.nodes.push(LiveNode {
538                    origin,
539                    id: prop_id,
540                    value: LiveValue::Bool(val)
541                });
542            },
543            LiveToken::Punct(live_id!(-))=>{
544                self.skip_token();
545                match self.peek_token() {
546                    LiveToken::Int(val) => {
547                        self.skip_token();
548                        ld.nodes.push(LiveNode {
549                            origin,
550                            id: prop_id,
551                            value: LiveValue::Int64(-val)
552                        });
553                    },
554                    LiveToken::Float(val) => {
555                        self.skip_token();
556                        ld.nodes.push(LiveNode {
557                            origin,
558                            id: prop_id,
559                            value: LiveValue::Float64(-val)
560                        });
561                    },
562                    _=>return Err(self.error("Expected int or float after -".to_string(), live_error_origin!()))
563                }
564            }
565            LiveToken::Int(val) => {
566                self.skip_token();
567                ld.nodes.push(LiveNode {
568                    origin,
569                    id: prop_id,
570                    value: LiveValue::Int64(val)
571                });
572            },
573            LiveToken::Float(val) => {
574                self.skip_token();
575                ld.nodes.push(LiveNode {
576                    origin,
577                    id: prop_id,
578                    value: LiveValue::Float64(val)
579                });
580            },
581            LiveToken::Color(val) => {
582                self.skip_token();
583                ld.nodes.push(LiveNode {
584                    origin,
585                    id: prop_id,
586                    value: LiveValue::Color(val)
587                });
588            },
589            LiveToken::String(v) => {
590                self.skip_token();
591                ld.nodes.push(LiveNode {
592                    origin,
593                    id: prop_id,
594                    value: LiveValue::String(v)
595                });
596            },
597            LiveToken::Ident(live_id!(dep)) => {
598                self.skip_token();
599                if self.accept_token(LiveToken::Open(Delim::Paren)) {
600                    let x = self.expect_string() ?;
601                    ld.nodes.push(LiveNode {
602                        origin,
603                        id: prop_id,
604                        value: LiveValue::Dependency(x)
605                    });
606                    self.expect_token(LiveToken::Close(Delim::Paren)) ?;
607                }
608                else {
609                    ld.nodes.push(LiveNode {
610                        origin,
611                        id: prop_id,
612                        value: LiveValue::Id(live_id!(dep))
613                    });
614                }
615            }
616            LiveToken::Ident(live_id!(vec2)) => {
617                self.skip_token();
618                if self.accept_token(LiveToken::Open(Delim::Paren)) {
619                    let x = self.expect_float() ?;
620                    self.expect_token(LiveToken::Punct(live_id!(,))) ?;
621                    let y = self.expect_float() ?;
622                    self.expect_token(LiveToken::Close(Delim::Paren)) ?;
623                    ld.nodes.push(LiveNode {
624                        origin,
625                        id: prop_id,
626                        value: LiveValue::Vec2(Vec2 {x: x as f32, y: y as f32})
627                    });
628                }
629                else {
630                    ld.nodes.push(LiveNode {
631                        origin,
632                        id: prop_id,
633                        value: LiveValue::Id(live_id!(vec2))
634                    });
635                }
636            },
637            LiveToken::Ident(live_id!(vec3)) => {
638                self.skip_token();
639                if self.accept_token(LiveToken::Open(Delim::Paren)) {
640                    let x = self.expect_float() ?;
641                    self.expect_token(LiveToken::Punct(live_id!(,))) ?;
642                    let y = self.expect_float() ?;
643                    self.expect_token(LiveToken::Punct(live_id!(,))) ?;
644                    let z = self.expect_float() ?;
645                    self.expect_token(LiveToken::Close(Delim::Paren)) ?;
646                    ld.nodes.push(LiveNode {
647                        origin,
648                        id: prop_id,
649                        value: LiveValue::Vec3(Vec3 {x: x as f32, y: y as f32, z: z as f32})
650                    });
651                }
652                else {
653                    ld.nodes.push(LiveNode {
654                        origin,
655                        id: prop_id,
656                        value: LiveValue::Id(live_id!(vec3))
657                    });
658                }
659            },
660            LiveToken::Ident(live_id!(vec4)) => {
661                self.skip_token();
662                if self.accept_token(LiveToken::Open(Delim::Paren)) {
663                    let x = self.expect_float() ?;
664                    self.expect_token(LiveToken::Punct(live_id!(,))) ?;
665                    let y = self.expect_float() ?;
666                    self.expect_token(LiveToken::Punct(live_id!(,))) ?;
667                    let z = self.expect_float() ?;
668                    self.expect_token(LiveToken::Punct(live_id!(,))) ?;
669                    let w = self.expect_float() ?;
670                    self.expect_token(LiveToken::Close(Delim::Paren)) ?;
671                    ld.nodes.push(LiveNode {
672                        origin,
673                        id: prop_id,
674                        value: LiveValue::Vec4(Vec4 {x: x as f32, y: y as f32, z: z as f32, w: w as f32})
675                    });
676                }
677                else {
678                    ld.nodes.push(LiveNode {
679                        origin,
680                        id: prop_id,
681                        value: LiveValue::Id(live_id!(vec4))
682                    });
683                }
684            },
685            LiveToken::Ident(variant) => { // parse enum
686                self.skip_token();
687                match self.peek_token() {
688                    LiveToken::Punct(live_id!(.))=>{ // IdPath
689                        let mut id_path = Vec::new();
690                        id_path.push(variant);
691                        while self.accept_token(LiveToken::Punct(live_id!(.))){
692                            let ident = self.expect_ident()?;
693                            id_path.push(ident);
694                        }
695                        ld.nodes.push(LiveNode {
696                            origin,
697                            id: prop_id,
698                            value: LiveValue::IdPath(Rc::new(id_path))
699                        })
700                        // loop till not id or =>
701                    }
702                    LiveToken::Open(Delim::Brace) => {
703                        self.expect_named_enum(prop_id, origin, variant, ld) ?;
704                    }
705                    LiveToken::Open(Delim::Paren) => {
706                        self.expect_tuple_enum(prop_id, origin, variant, ld) ?;
707                    }
708                    _ => {
709                        if variant.as_string(|v| v.unwrap().chars().next().unwrap().is_uppercase()){
710                            ld.nodes.push(LiveNode {
711                                origin,
712                                id: prop_id,
713                                value: LiveValue::BareEnum(variant)
714                            })
715                        }
716                        else{
717                            ld.nodes.push(LiveNode {
718                                origin,
719                                id: prop_id,
720                                value: LiveValue::Id(variant)
721                            })
722                        }
723                    }
724                }
725            },
726            other => return Err(self.error(format!("Unexpected token {} in property value", other), live_error_origin!()))
727        }
728        Ok(())
729    }
730    
731    fn scan_to_token(&mut self, scan_token: LiveToken) -> Result<usize, LiveError> {
732        // ok we are going to scan to token, keeping in mind our levels.
733        let mut stack_depth = 0;
734        
735        while self.peek_token() != LiveToken::Eof {
736            match self.peek_token() {
737                LiveToken::Open(_) => {
738                    stack_depth += 1;
739                }
740                LiveToken::Close(_) => {
741                    if stack_depth == 0 {
742                        return Err(self.error(format!("Found closing )}}] whilst scanning for {}", scan_token), live_error_origin!()));
743                    }
744                    stack_depth -= 1;
745                }
746                _ => ()
747            }
748            if stack_depth == 0 && self.peek_token() == scan_token {
749                self.skip_token();
750                return Ok(self.token_index)
751            }
752            self.skip_token();
753        }
754        Err(self.error(format!("Could not find ending token {} whilst scanning", scan_token), live_error_origin!()))
755    }
756    /*
757    fn expect_var_def_type(&mut self) -> Result<(), LiveError> {
758        self.expect_ident() ?;
759        if self.accept_token(LiveToken::Ident(live_id!(in))) {
760            self.expect_ident() ?;
761        }
762        Ok(())
763    }*/
764    
765    fn expect_value_literal(&mut self) -> Result<(), LiveError> {
766        match self.peek_token() {
767            LiveToken::Bool(_)
768                | LiveToken::Int(_)
769                | LiveToken::Float(_)
770                | LiveToken::Color(_) => {
771                self.skip_token();
772                return Ok(())
773            }
774            LiveToken::Ident(live_id!(vec2)) => {todo!()}
775            LiveToken::Ident(live_id!(vec3)) => {todo!()}
776            _ => ()
777        }
778        Err(self.error("Expected value literal".to_string(), live_error_origin!()))
779    }
780    
781    fn expect_live_class(&mut self, root: bool, prop_id: LiveId, ld: &mut LiveOriginal) -> Result<(), LiveError> {
782        let mut nameless_id = 0;
783        while self.peek_token() != LiveToken::Eof {
784            match self.peek_token() {
785                LiveToken::Close(Delim::Brace) => {
786                    if root {
787                        return Err(self.error("Unexpected token } in root".to_string(), live_error_origin!()))
788                    }
789                    let token_id = self.get_token_id();
790                    self.skip_token();
791                    ld.nodes.push(LiveNode {
792                        origin: LiveNodeOrigin::from_token_id(token_id),
793                        id: prop_id,
794                        value: LiveValue::Close
795                    });
796                    return Ok(());
797                }
798                LiveToken::Punct(live_id!(<))=>{ // class instance
799                    self.skip_token();
800                    let token_id = self.get_token_id();
801                    let ident = self.expect_ident()?;
802                    self.expect_token(LiveToken::Punct(live_id!(>))) ?;
803                    self.expect_token(LiveToken::Open(Delim::Brace))?;
804                    ld.nodes.push(LiveNode {
805                        origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Instance),
806                        id: LiveId::from_str(&format!("nameless_{}", nameless_id)),
807                        value: LiveValue::Clone(ident)
808                    });
809                    nameless_id += 1;
810                    self.expect_live_class(false, prop_id, ld) ?;
811                }
812                LiveToken::Ident(prop_id) => {
813                    let token_id = self.get_token_id();
814                    self.skip_token();
815                    
816                    //let span = self.begin_span();
817                    // next
818                    // there is another token coming
819                    if let LiveToken::Ident(_) = self.peek_token() {
820                        match prop_id {
821                            live_id!(fn) => {
822                                self.expect_fn(ld) ?;
823                                self.accept_optional_delim();
824                            }
825                            live_id!(import) => {
826                                self.expect_import(ld) ?;
827                                self.accept_optional_delim();
828                            }
829                            _ => {
830                                let token_id = self.get_token_id();
831                                let real_prop_id = self.expect_ident() ?;
832                                let edit_info = self.possible_edit_info(ld) ?;
833                                let prop_type = self.expect_prop_type()?;
834                                let origin = LiveNodeOrigin::from_token_id(token_id)
835                                    .with_edit_info(edit_info)
836                                    .with_node_has_prefix(true)
837                                    .with_prop_type(prop_type);
838                                
839                                self.expect_live_value(real_prop_id, origin, ld) ?;
840                                //self.expect_node_with_prefix(ld) ?;
841                                self.accept_optional_delim();
842                            }
843                        }
844                    }
845                    else { // has to be key:value
846                        // if we get a . metadata follows
847                        let edit_info = self.possible_edit_info(ld) ?;
848                  
849                        let assign_type = self.expect_prop_type()?;
850                        let origin = LiveNodeOrigin::from_token_id(token_id)
851                            .with_edit_info(edit_info)
852                            .with_prop_type(assign_type);
853                        self.expect_live_value(prop_id, origin, ld) ?;
854
855                        self.accept_optional_delim();
856                    }
857                },
858                other => return Err(self.error(format!("Unexpected token {} in class body of {}", other, prop_id), live_error_origin!()))
859            }
860        }
861        if root {
862            return Ok(())
863        }
864        Err(self.error("Eof in class body".to_string(), live_error_origin!()))
865    }
866    
867    pub fn expect_prop_type(&mut self)->Result<LivePropType, LiveError>{
868        Ok(if self.accept_token(LiveToken::Punct(live_id!(:))){
869            LivePropType::Field
870        }
871        else if self.accept_token(LiveToken::Punct(live_id!(=))){
872            /*if self.accept_token(LiveToken::Punct(live_id!( ?))) {
873                LivePropType::Template
874            }
875            else{*/
876                LivePropType::Instance
877            //}
878        }
879        else{
880            return Err(self.error("Unexpected assign_type, expected = or :".to_string(), live_error_origin!()))
881        })
882    }
883    
884    pub fn accept_optional_delim(&mut self) {
885        if !self.accept_token(LiveToken::Punct(live_id!(,))) {
886            self.accept_token(LiveToken::Punct(live_id!(;)));
887        }
888    }
889    
890    pub fn parse_live_document(&mut self) -> Result<LiveOriginal, LiveError> {
891        let mut ld = LiveOriginal::new();
892        ld.nodes.push(LiveNode {
893            origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
894            id: LiveId::empty(),
895            value: LiveValue::Object
896        });
897        self.expect_live_class(true, LiveId::empty(), &mut ld) ?;
898        ld.nodes.push(LiveNode {
899            origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
900            id: LiveId::empty(),
901            value: LiveValue::Close
902        });
903        // we should s
904        Ok(ld)
905    }
906    
907    
908    
909    
910    
911    // EXPRESSION PARSER
912    
913    
914    
915    
916    fn expect_expression(&mut self, prop_id: LiveId, origin: LiveNodeOrigin, ld: &mut LiveOriginal) -> Result<(), LiveError> {
917        
918        let expr = self.expect_prim_expr() ?;
919        
920        ld.nodes.push(LiveNode {
921            origin,
922            id: prop_id,
923            value: LiveValue::Expr {expand_index: None}
924        });
925        
926        fn recur_walk(expr: Expr, ld: &mut LiveOriginal) {
927            match expr {
928                Expr::Bin {token_id, op, left_expr, right_expr} => {
929                    ld.nodes.push(LiveNode {
930                        origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Nameless),
931                        id: LiveId::empty(),
932                        value: LiveValue::ExprBinOp(op)
933                    });
934                    recur_walk(*left_expr, ld);
935                    recur_walk(*right_expr, ld);
936                }
937                Expr::Un {token_id, op, expr} => {
938                    ld.nodes.push(LiveNode {
939                        origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Nameless),
940                        id: LiveId::empty(),
941                        value: LiveValue::ExprUnOp(op)
942                    });
943                    recur_walk(*expr, ld);
944                }
945                Expr::Call {token_id, ident, arg_exprs} => {
946                    ld.nodes.push(LiveNode {
947                        origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Nameless),
948                        id: LiveId::empty(),
949                        value: LiveValue::ExprCall {ident, args: arg_exprs.len()}
950                    });
951                    for arg in arg_exprs {
952                        recur_walk(arg, ld);
953                    }
954                }
955                Expr::Member {token_id, ident, expr} => {
956                    ld.nodes.push(LiveNode {
957                        origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Nameless),
958                        id: LiveId::empty(),
959                        value: LiveValue::ExprMember(ident)
960                    });
961                    recur_walk(*expr, ld);
962                }
963                Expr::Var {token_id, ident} => {
964                    ld.nodes.push(LiveNode {
965                        origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Nameless),
966                        id: LiveId::empty(),
967                        value: LiveValue::Id(ident)
968                    });
969                }
970                Expr::Bool {token_id, v} => {
971                    ld.nodes.push(LiveNode {
972                        origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Nameless),
973                        id: LiveId::empty(),
974                        value: LiveValue::Bool(v)
975                    });
976                }
977                Expr::Int {token_id, v} => {
978                    ld.nodes.push(LiveNode {
979                        origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Nameless),
980                        id: LiveId::empty(),
981                        value: LiveValue::Int64(v)
982                    });
983                }
984                Expr::Float {token_id, v} => {
985                    ld.nodes.push(LiveNode {
986                        origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Nameless),
987                        id: LiveId::empty(),
988                        value: LiveValue::Float64(v)
989                    });
990                }
991                Expr::Color {token_id, v} => {
992                    ld.nodes.push(LiveNode {
993                        origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Nameless),
994                        id: LiveId::empty(),
995                        value: LiveValue::Color(v)
996                    });
997                }
998            }
999        }
1000        
1001        recur_walk(expr, ld);
1002        
1003        ld.nodes.push(LiveNode {
1004            origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
1005            id: prop_id,
1006            value: LiveValue::Close
1007        });
1008        
1009        
1010        Ok(())
1011        /*
1012        ld.nodes.push(LiveNode {
1013            token_id: Some(self.get_token_id()),
1014            id: prop_id,
1015            value: LiveValue::Expression
1016        });
1017        
1018        let mut stack_depth = 0;
1019        
1020        while self.peek_token() != Token::Eof {
1021            if self.accept_token(Token::OpenParen){
1022                stack_depth += 1;             
1023            }
1024            else if self.accept_token(Token::CloseParen) {
1025                stack_depth -= 1;
1026                // terminate
1027                if stack_depth == 0{
1028                    ld.nodes.push(LiveNode {
1029                        token_id: Some(self.get_token_id()),
1030                        id: LiveId::empty(),
1031                        value: LiveValue::Close
1032                    });
1033                    return Ok(())
1034                }
1035            }
1036            // ok so what do we do here.
1037            // we accept 
1038            self.expect_live_value(LiveId::empty(), ld) ?;
1039            self.accept_token(Token::Punct(live_id!(,)));
1040        }
1041        return Err(self.error(format!("Eof in array body")))*/
1042    }
1043    
1044    fn expect_expr(&mut self) -> Result<Expr, LiveError> {
1045        self.expect_cond_expr()
1046    }
1047    
1048    fn expect_cond_expr(&mut self) -> Result<Expr, LiveError> {
1049        let expr = self.expect_or_expr() ?;
1050        Ok(if self.accept_token(LiveToken::Punct(live_id!( ?))) {
1051            let token_id = self.get_token_id();
1052            let expr_if_true = self.expect_expr() ?;
1053            self.expect_token(LiveToken::Punct(live_id!(:))) ?;
1054            let expr_if_false = self.expect_cond_expr() ?;
1055            Expr ::Call {token_id, ident: live_id!(cond), arg_exprs: vec![expr, expr_if_true, expr_if_false]}
1056        } else {
1057            expr
1058        })
1059    }
1060    
1061    fn expect_or_expr(&mut self) -> Result<Expr, LiveError> {
1062        let mut acc = self.expect_and_expr() ?;
1063        while let Some(op) = LiveBinOp::from_or_op(self.peek_token()) {
1064            self.skip_token();
1065            let token_id = self.get_token_id();
1066            let left_expr = Box::new(acc);
1067            let right_expr = Box::new(self.expect_and_expr() ?);
1068            acc = Expr::Bin {token_id, op, left_expr, right_expr,};
1069        }
1070        Ok(acc)
1071    }
1072    
1073    fn expect_and_expr(&mut self) -> Result<Expr, LiveError> {
1074        let mut acc = self.expect_eq_expr() ?;
1075        while let Some(op) = LiveBinOp::from_and_op(self.peek_token()) {
1076            self.skip_token();
1077            let token_id = self.get_token_id();
1078            let left_expr = Box::new(acc);
1079            let right_expr = Box::new(self.expect_eq_expr() ?);
1080            acc = Expr::Bin {token_id, op, left_expr, right_expr,};
1081        }
1082        Ok(acc)
1083    }
1084    
1085    fn expect_eq_expr(&mut self) -> Result<Expr, LiveError> {
1086        let mut acc = self.expect_rel_expr() ?;
1087        while let Some(op) = LiveBinOp::from_eq_op(self.peek_token()) {
1088            self.skip_token();
1089            let token_id = self.get_token_id();
1090            let left_expr = Box::new(acc);
1091            let right_expr = Box::new(self.expect_rel_expr() ?);
1092            acc = Expr::Bin {token_id, op, left_expr, right_expr,};
1093        }
1094        Ok(acc)
1095    }
1096    
1097    fn expect_rel_expr(&mut self) -> Result<Expr, LiveError> {
1098        let mut acc = self.expect_add_expr() ?;
1099        while let Some(op) = LiveBinOp::from_rel_op(self.peek_token()) {
1100            self.skip_token();
1101            let token_id = self.get_token_id();
1102            let left_expr = Box::new(acc);
1103            let right_expr = Box::new(self.expect_add_expr() ?);
1104            acc = Expr::Bin {token_id, op, left_expr, right_expr,};
1105        }
1106        Ok(acc)
1107    }
1108    
1109    fn expect_add_expr(&mut self) -> Result<Expr, LiveError> {
1110        let mut acc = self.expect_mul_expr() ?;
1111        while let Some(op) = LiveBinOp::from_add_op(self.peek_token()) {
1112            self.skip_token();
1113            let token_id = self.get_token_id();
1114            let left_expr = Box::new(acc);
1115            let right_expr = Box::new(self.expect_mul_expr() ?);
1116            acc = Expr::Bin {token_id, op, left_expr, right_expr,};
1117        }
1118        Ok(acc)
1119    }
1120    
1121    fn expect_mul_expr(&mut self) -> Result<Expr, LiveError> {
1122        let mut acc = self.expect_un_expr() ?;
1123        while let Some(op) = LiveBinOp::from_mul_op(self.peek_token()) {
1124            self.skip_token();
1125            let token_id = self.get_token_id();
1126            let left_expr = Box::new(acc);
1127            let right_expr = Box::new(self.expect_un_expr() ?);
1128            acc = Expr::Bin {token_id, op, left_expr, right_expr,};
1129        }
1130        Ok(acc)
1131    }
1132    
1133    fn expect_un_expr(&mut self) -> Result<Expr, LiveError> {
1134        Ok(if let Some(op) = LiveUnOp::from_un_op(self.peek_token()) {
1135            self.skip_token();
1136            let token_id = self.get_token_id();
1137            let expr = Box::new(self.expect_un_expr() ?);
1138            Expr::Un {token_id, op, expr,}
1139        } else {
1140            self.expect_member_expr() ?
1141        })
1142    }
1143    
1144    fn expect_member_expr(&mut self) -> Result<Expr, LiveError> {
1145        let mut acc = self.expect_prim_expr()?;
1146        while let LiveToken::Punct(live_id!(.)) = self.peek_token() {
1147            self.skip_token();
1148            let token_id = self.get_token_id();
1149            let ident = self.expect_ident()?;
1150            acc = Expr::Member {token_id, ident, expr: Box::new(acc)}
1151        }
1152        Ok(acc)
1153    }
1154    
1155    fn expect_prim_expr(&mut self) -> Result<Expr, LiveError> {
1156        match self.peek_token() {
1157            LiveToken::Ident(ident) => {
1158                let token_id = self.get_token_id();
1159                self.skip_token();
1160                match self.peek_token() {
1161                    LiveToken::Open(Delim::Paren) => {
1162                        let arg_exprs = self.expect_arg_exprs() ?;
1163                        Ok(Expr::Call {token_id, ident, arg_exprs})
1164                    }
1165                    _ => Ok(Expr::Var {token_id, ident}),
1166                }
1167            }
1168            LiveToken::Bool(v) => {
1169                self.skip_token();
1170                let token_id = self.get_token_id();
1171                Ok(Expr::Bool {token_id, v})
1172            }
1173            LiveToken::Int(v) => {
1174                self.skip_token();
1175                let token_id = self.get_token_id();
1176                Ok(Expr::Int {token_id, v})
1177            }
1178            LiveToken::Float(v) => {
1179                self.skip_token();
1180                let token_id = self.get_token_id();
1181                Ok(Expr::Float {token_id, v})
1182            }
1183            LiveToken::Color(v) => {
1184                self.skip_token();
1185                let token_id = self.get_token_id();
1186                Ok(Expr::Color {token_id, v})
1187            }
1188            LiveToken::Open(Delim::Paren) => {
1189                self.skip_token();
1190                let expr = self.expect_expr() ?;
1191                self.expect_token(LiveToken::Close(Delim::Paren)) ?;
1192                Ok(expr)
1193            }
1194            token => Err(self.error(format!("Unexpected token {} in class expression", token), live_error_origin!()))
1195        }
1196    }
1197    
1198    fn expect_arg_exprs(&mut self) -> Result<Vec<Expr>, LiveError> {
1199        self.expect_token(LiveToken::Open(Delim::Paren)) ?;
1200        let mut arg_exprs = Vec::new();
1201        if !self.accept_token(LiveToken::Close(Delim::Paren)) {
1202            loop {
1203                arg_exprs.push(self.expect_expr() ?);
1204                if !self.accept_token(LiveToken::Punct(live_id!(,))) {
1205                    break;
1206                }
1207            }
1208            self.expect_token(LiveToken::Close(Delim::Paren)) ?;
1209        }
1210        Ok(arg_exprs)
1211    }
1212    
1213    
1214}
1215
1216#[derive(Debug)]
1217enum Expr {
1218    Bin {
1219        token_id: LiveTokenId,
1220        op: LiveBinOp,
1221        left_expr: Box<Expr>,
1222        right_expr: Box<Expr>,
1223    },
1224    Un {
1225        token_id: LiveTokenId,
1226        op: LiveUnOp,
1227        expr: Box<Expr>,
1228    },
1229    Call {
1230        token_id: LiveTokenId,
1231        ident: LiveId,
1232        arg_exprs: Vec<Expr>,
1233    },
1234    Member {
1235        token_id: LiveTokenId,
1236        ident: LiveId,
1237        expr: Box<Expr>
1238    },
1239    Var {
1240        token_id: LiveTokenId,
1241        ident: LiveId,
1242    },
1243    Bool {
1244        token_id: LiveTokenId,
1245        v: bool
1246    },
1247    Int {
1248        token_id: LiveTokenId,
1249        v: i64
1250    },
1251    Float {
1252        token_id: LiveTokenId,
1253        v: f64
1254    },
1255    Color {
1256        token_id: LiveTokenId,
1257        v: u32
1258    }
1259}
1260
1261
1262impl LiveBinOp {
1263    fn from_or_op(token: LiveToken) -> Option<Self> {
1264        match token {
1265            LiveToken::Punct(live_id!( ||)) => Some(Self::Or),
1266            _ => None,
1267        }
1268    }
1269    
1270    fn from_and_op(token: LiveToken) -> Option<Self> {
1271        match token {
1272            LiveToken::Punct(live_id!( &&)) => Some(Self::And),
1273            _ => None,
1274        }
1275    }
1276    
1277    fn from_eq_op(token: LiveToken) -> Option<Self> {
1278        match token {
1279            LiveToken::Punct(live_id!( ==)) => Some(Self::Eq),
1280            LiveToken::Punct(live_id!( !=)) => Some(Self::Ne),
1281            _ => None,
1282        }
1283    }
1284    
1285    fn from_rel_op(token: LiveToken) -> Option<Self> {
1286        match token {
1287            LiveToken::Punct(live_id!(<)) => Some(Self::Lt),
1288            LiveToken::Punct(live_id!( <=)) => Some(Self::Le),
1289            LiveToken::Punct(live_id!(>)) => Some(Self::Gt),
1290            LiveToken::Punct(live_id!( >=)) => Some(Self::Ge),
1291            _ => None,
1292        }
1293    }
1294    
1295    fn from_add_op(token: LiveToken) -> Option<Self> {
1296        match token {
1297            LiveToken::Punct(live_id!( +)) => Some(Self::Add),
1298            LiveToken::Punct(live_id!(-)) => Some(Self::Sub),
1299            _ => None,
1300        }
1301    }
1302    
1303    fn from_mul_op(token: LiveToken) -> Option<Self> {
1304        match token {
1305            LiveToken::Punct(live_id!(*)) => Some(Self::Mul),
1306            LiveToken::Punct(live_id!( /)) => Some(Self::Div),
1307            _ => None,
1308        }
1309    }
1310}
1311
1312impl LiveUnOp {
1313    pub fn from_un_op(token: LiveToken) -> Option<Self> {
1314        match token {
1315            LiveToken::Punct(live_id!(!)) => Some(Self::Not),
1316            LiveToken::Punct(live_id!(-)) => Some(Self::Neg),
1317            _ => None,
1318        }
1319    }
1320}
1321