1use {
2 std::{
3 sync::Arc,
4 iter::Cloned,
5 slice::Iter
6 },
7 crate::{
8 makepad_live_tokenizer::{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, LivePtr},
18 span::{TextSpan, TextPos},
19 live_error::{LiveError},
20 live_document::LiveOriginal,
21 live_node::{LiveFont, 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 ld: LiveOriginal,
33 pub end: TextPos,
34}
35
36impl<'a> LiveParser<'a> {
37
38 fn peek_span(&self) -> TextSpan {
39 self.token_with_span.span
40 }
41
42 fn peek_token(&self) -> LiveToken {
43 self.token_with_span.token.clone()
44 }
45
46 fn eat_token(&mut self) -> LiveToken {
47 let token = self.peek_token();
48 self.skip_token();
49 token
50 }
51
52 fn skip_token(&mut self) {
53 self.end = self.token_with_span.span.end;
54 if let Some(next) = self.tokens_with_span.next(){
55 self.token_with_span = next;
56 }
57 else {
58 self.token_with_span.token = LiveToken::Eof
59 }
60 self.token_index += 1;
61 }
62
63 fn error(&mut self, message: String, origin: LiveErrorOrigin) -> LiveError {
64 LiveError {
65 origin,
66 span: self.token_with_span.span.into(),
67 message,
68 }
69 }
70
71
72 fn end(&self) -> TextPos {
73 self.end
74 }
75
76 fn token_end(&self) -> TextPos {
77 self.token_with_span.span.end
78 }
79
80 fn accept_ident(&mut self) -> Option<LiveId> {
81 if let LiveToken::Ident(id) = self.peek_token() {
82 self.skip_token();
83 Some(id)
84 }
85 else {
86 None
87 }
88 }
89
90 fn accept_token(&mut self, token: LiveToken) -> bool {
91 if self.peek_token() != token {
92 return false;
93 }
94 self.skip_token();
95 true
96 }
97
98 fn expect_ident(&mut self) -> Result<LiveId, LiveError> {
99 match self.peek_token() {
100 LiveToken::Ident(ident) => {
101 self.ld.identifiers.insert(ident);
102 self.skip_token();
103 Ok(ident)
104 }
105 token => Err(self.error(format!("expected ident, unexpected token `{}`", token), live_error_origin!())),
106 }
107 }
108
109 fn expect_string(&mut self) -> Result<Arc<String>, LiveError> {
110 match self.peek_token() {
111 LiveToken::String(v) => {
112 self.skip_token();
113 Ok(v)
114 }
115 token => Err(self.error(format!("expected ident, unexpected token `{}`", token), live_error_origin!())),
116 }
117 }
118
119 fn expect_int2(&mut self) -> Result<i64, LiveError> {
120 match self.peek_token() {
121 LiveToken::Int(v) => {
122 self.skip_token();
123 Ok(v)
124 }
125 token => Err(self.error(format!("expected int, unexpected token `{}`", token), live_error_origin!())),
126 }
127 }
128
129 fn expect_float(&mut self) -> Result<f64, LiveError> {
130 let sign = if let LiveToken::Punct(live_id!(-)) = self.peek_token(){
131 self.skip_token();
132 -1.0
133 }
134 else{
135 1.0
136 };
137 match self.peek_token() {
138 LiveToken::Float(v) => {
139 self.skip_token();
140 Ok(v*sign)
141 }
142 LiveToken::Int(v) => {
143 self.skip_token();
144 Ok(v as f64 * sign)
145 }
146 token => Err(self.error(format!("expected float, unexpected token `{}`", token), live_error_origin!())),
147 }
148 }
149
150 fn expect_token(&mut self, expected: LiveToken) -> Result<(), LiveError> {
151 let actual = self.peek_token();
152 if actual != expected {
153 return Err(self.error(format!("expected {} unexpected token `{}`", expected, actual), live_error_origin!()));
154 }
155 self.skip_token();
156 Ok(())
157 }
158
159 fn expect_use(&mut self) -> Result<(), LiveError> {
160 let token_id = self.get_token_id();
161 let crate_id = self.expect_ident() ?;
162 self.expect_token(LiveToken::Punct(live_id!(::))) ?;
165
166 let first_module_id = self.expect_ident() ?;
168 self.expect_token(LiveToken::Punct(live_id!(::))) ?;
169 let mut module = String::new();
170 let mut last_id = LiveId(0);
171
172 module.push_str(&format!("{}", first_module_id));
173 loop {
174 match self.peek_token() {
175 LiveToken::Ident(id) => {
176 self.skip_token();
177 last_id = id;
178 if !self.accept_token(LiveToken::Punct(live_id!(::))) {
179 break;
180 }
181 module.push_str(&format!("::{}", id));
182 },
183 LiveToken::Punct(live_id!(*)) => {
184 self.skip_token();
185 last_id = LiveId(0);
186 break;
187 }
188 _ => {
189 break;
190 }
191 }
192 }
193 let import_id = last_id;
194 if let LiveToken::Ident(live_id!(as)) = self.peek_token(){
195 self.skip_token();
196 last_id = self.expect_ident() ?;
197 }
198
199 self.ld.nodes.push(LiveNode {
200 origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Instance),
201 id: last_id,
202 value: LiveValue::Import(Box::new(LiveImport{
203 module_id: LiveModuleId(crate_id, LiveId::from_str_with_lut(&module).unwrap()),
204 import_id,
205 }))
206 });
207
208 Ok(())
209 }
210
211 fn expect_link(&mut self) -> Result<(), LiveError> {
212 let link_id = self.expect_ident() ?;
213 self.ld.link = Some(link_id);
214 Ok(())
215 }
216 fn expect_fn(&mut self) -> Result<(), LiveError> {
240 let token_start = self.token_index - 1;
241 let token_id = self.get_token_id();
242 let prop_id = self.expect_ident() ?;
243 let token_index = self.scan_to_token(LiveToken::Close(Delim::Brace)) ?;
245
246 self.ld.nodes.push(LiveNode {
247 origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Instance),
248 id: prop_id,
249 value: LiveValue::DSL {
250 token_start: token_start as u32,
251 token_count: (token_index - token_start) as u32,
252 expand_index: None
253 }
254 });
255
256 Ok(())
257 }
258
259
260 fn possible_edit_info(&mut self) -> Result<Option<LiveEditInfo>, LiveError> {
261 if self.accept_token(LiveToken::Punct(live_id!(.))) {
264 self.expect_token(LiveToken::Open(Delim::Brace)) ?;
265
266 let fill_index = self.ld.edit_info.len() & 0xf;
267 if fill_index != 0 {
268 for _ in 0..(16 - fill_index) {
269 self.ld.edit_info.push(LiveNode::empty())
270 }
271 }
272 let edit_info_index = self.ld.edit_info.len();
273
274 if edit_info_index > 0x3e0 {
275 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!()))
276 }
277
278 self.ld.edit_info.push(LiveNode {
279 origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
280 id: LiveId::empty(),
281 value: LiveValue::Object
282 });
283
284
285 while self.peek_token() != LiveToken::Eof {
286 match self.peek_token() {
287 LiveToken::Close(Delim::Brace) => {
288 self.skip_token();
289 self.ld.edit_info.push(LiveNode {
290 origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
291 id: LiveId::empty(),
292 value: LiveValue::Close
293 });
294 return Ok(Some(LiveEditInfo::new(edit_info_index)));
295 }
296 LiveToken::Ident(prop_id) => {
297 self.skip_token();
298 self.expect_token(LiveToken::Punct(live_id!(:))) ?;
299
300 match self.peek_token() {
301 LiveToken::Bool(val) => {
302 self.skip_token();
303 self.ld.edit_info.push(LiveNode {
304 origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
305 id: prop_id,
306 value: LiveValue::Bool(val)
307 });
308 },
309 LiveToken::Int(val) => {
310 self.skip_token();
311 self.ld.edit_info.push(LiveNode {
312 origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
313 id: prop_id,
314 value: LiveValue::Int64(val)
315 });
316 },
317 LiveToken::Float(val) => {
318 self.skip_token();
319 self.ld.edit_info.push(LiveNode {
320 origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
321 id: prop_id,
322 value: LiveValue::Float64(val)
323 });
324 },
325 LiveToken::Color(val) => {
326 self.skip_token();
327 self.ld.edit_info.push(LiveNode {
328 origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
329 id: prop_id,
330 value: LiveValue::Color(val)
331 });
332 },
333 LiveToken::String(rcstring) => {
334 self.skip_token();
335 self.ld.edit_info.push(LiveNode {
336 origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
337 id: prop_id,
338 value: LiveValue::String(rcstring)
339 });
340 },
341 other => return Err(self.error(format!("Unexpected token {} in edit_info", other), live_error_origin!()))
342 }
343 self.accept_optional_delim();
344 },
345 other => return Err(self.error(format!("Unexpected token {} in edit_info", other), live_error_origin!()))
346 }
347 }
348 return Err(self.error("Eof in edit info".to_string(), live_error_origin!()))
349 }
350
351 Ok(None)
352 }
353
354 fn expect_node_with_prefix(&mut self) -> Result<(), LiveError> {
355 let token_id = self.get_token_id();
356 let real_prop_id = self.expect_ident() ?;
357 let edit_info = self.possible_edit_info() ?;
358 let origin = LiveNodeOrigin::from_token_id(token_id).with_edit_info(edit_info).with_node_has_prefix(true);
359
360 if self.accept_token(LiveToken::Punct(live_id!(:))) {
361 self.expect_live_value(real_prop_id, origin.with_prop_type(LivePropType::Field)) ?;
362 }
363 else if self.accept_token(LiveToken::Punct(live_id!(=))) {
364 self.expect_live_value(real_prop_id, origin.with_prop_type(LivePropType::Instance)) ?;
365 }
366
367 Ok(())
368 }
369
370 fn expect_array(&mut self, prop_id: LiveId, origin: LiveNodeOrigin) -> Result<(), LiveError> {
371 self.expect_token(LiveToken::Open(Delim::Bracket)) ?;
372 self.ld.nodes.push(LiveNode {
373 origin,
374 id: prop_id,
375 value: LiveValue::Array
376 });
377 let mut counter = 1;
378 while self.peek_token() != LiveToken::Eof {
379 if self.accept_token(LiveToken::Close(Delim::Bracket)) {
380 self.ld.nodes.push(LiveNode {
381 origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
382 id: LiveId::empty(),
383 value: LiveValue::Close
384 });
385 return Ok(())
386 }
387 self.expect_live_value(
388 LiveId(counter),
389 LiveNodeOrigin::from_token_id(self.get_token_id()).with_prop_type(LivePropType::Nameless)
390 ) ?;
391 self.accept_token(LiveToken::Punct(live_id!(,)));
392 counter += 1;
393 }
394 Err(self.error("Eof in array body".to_string(), live_error_origin!()))
395 }
396
397
398 fn expect_tuple_enum(&mut self, prop_id: LiveId, origin: LiveNodeOrigin, variant: LiveId) -> Result<(), LiveError> {
399 self.expect_token(LiveToken::Open(Delim::Paren)) ?;
400 self.ld.nodes.push(LiveNode {
401 origin,
402 id: prop_id,
403 value: LiveValue::TupleEnum(variant)
404 });
405 while self.peek_token() != LiveToken::Eof {
406 if self.accept_token(LiveToken::Close(Delim::Paren)) {
407 self.ld.nodes.push(LiveNode {
408 origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
409 id: prop_id,
410 value: LiveValue::Close
411 });
412 return Ok(())
413 }
414 self.expect_live_value(LiveId::empty(), LiveNodeOrigin::from_token_id(self.get_token_id()).with_prop_type(LivePropType::Nameless)) ?;
416 self.accept_token(LiveToken::Punct(live_id!(,)));
417 }
418 Err(self.error("Eof in object body".to_string(), live_error_origin!()))
419 }
420
421
422 fn expect_named_enum(&mut self, prop_id: LiveId, origin: LiveNodeOrigin,variant: LiveId) -> Result<(), LiveError> {
423 self.expect_token(LiveToken::Open(Delim::Brace)) ?;
424
425 self.ld.nodes.push(LiveNode {
426 origin,
427 id: prop_id,
428 value: LiveValue::NamedEnum(variant)
429 });
430
431 while self.peek_token() != LiveToken::Eof {
432 if self.accept_token(LiveToken::Close(Delim::Brace)) {
433 self.ld.nodes.push(LiveNode {
434 origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
435 id: prop_id,
436 value: LiveValue::Close
437 });
438 return Ok(())
439 }
440 let token_id = self.get_token_id();
441 let prop_id = self.expect_ident() ?;
442 let edit_info = self.possible_edit_info() ?;
443 self.expect_token(LiveToken::Punct(live_id!(:))) ?;
444 self.expect_live_value(prop_id, LiveNodeOrigin::from_token_id(token_id).with_edit_info(edit_info)) ?;
445 self.accept_token(LiveToken::Punct(live_id!(,)));
446 }
447 Err(self.error("Eof in named enum".to_string(), live_error_origin!()))
448 }
449
450 fn get_token_id(&self) -> LiveTokenId {
451 LiveTokenId::new(self.file_id, self.token_index)
452 }
453 fn expect_live_value(&mut self, prop_id: LiveId, origin: LiveNodeOrigin) -> Result<(), LiveError> {
510 match self.peek_token() {
512 LiveToken::Punct(live_id!(<))=>{ self.skip_token();
514
515 let ident = self.expect_ident()?;
516 self.expect_token(LiveToken::Punct(live_id!(>))) ?;
517 self.expect_token(LiveToken::Open(Delim::Brace))?;
518 self.ld.nodes.push(LiveNode {
519 origin,
520 id: prop_id,
521 value: LiveValue::Clone{clone:ident}
522 });
523 self.expect_live_class(false, prop_id) ?;
524 }
525 LiveToken::Ident(live_id!(struct)) => { self.skip_token();
527 self.expect_token(LiveToken::Open(Delim::Brace)) ?;
528 self.ld.nodes.push(LiveNode {
529 origin,
530 id: prop_id,
531 value: LiveValue::Clone{clone:live_id!(struct)}
532 });
533 self.expect_live_class(false, prop_id) ?;
534 },
535
536 LiveToken::Open(Delim::Brace) => { self.skip_token();
538
539 if self.peek_token() == LiveToken::Open(Delim::Brace) {
541 self.skip_token();
542
543 let val = self.live_type_info_counter;
544 self.live_type_info_counter += 1;
545
546 self.accept_ident();
547
548 if val >= self.live_type_infos.len() {
549 return Err(self.error(format!("live_type index out of range {}", val), live_error_origin!()));
550 }
551
552 self.expect_token(LiveToken::Close(Delim::Brace)) ?;
553 self.expect_token(LiveToken::Close(Delim::Brace)) ?;
554
555 if self.peek_token() == LiveToken::Punct(live_id!(<)) {
557 self.skip_token();
558 let ident = self.expect_ident()?;
559 self.expect_token(LiveToken::Punct(live_id!(>))) ?;
560 self.ld.nodes.push(LiveNode {
561 origin,
562 id: prop_id,
563 value: LiveValue::Deref{
564 live_type: self.live_type_infos[val].live_type,
565 clone: ident,
566 }
567 });
568 }
569 else{
570 self.ld.nodes.push(LiveNode {
571 origin,
572 id: prop_id,
573 value: LiveValue::Class {
574 live_type: self.live_type_infos[val].live_type,
575 class_parent: LivePtr::invalid(),
576 }
577 });
578 }
579
580 self.expect_token(LiveToken::Open(Delim::Brace)) ?;
581 self.expect_live_class(false, prop_id) ?;
582
583 return Ok(());
584 }
585
586 self.ld.nodes.push(LiveNode {
587 origin,
588 id: prop_id,
589 value: LiveValue::Object
590 });
591 self.expect_live_class(false, prop_id) ?;
592 },
593 LiveToken::Open(Delim::Paren) => { self.expect_expression(prop_id, origin) ?;
595 },
596 LiveToken::Open(Delim::Bracket) => { self.expect_array(prop_id, origin) ?;
598 },
599 LiveToken::Bool(val) => {
600 self.skip_token();
601 self.ld.nodes.push(LiveNode {
602 origin,
603 id: prop_id,
604 value: LiveValue::Bool(val)
605 });
606 },
607 LiveToken::Punct(live_id!(-))=>{
608 self.skip_token();
609 match self.peek_token() {
610 LiveToken::Int(val) => {
611 self.skip_token();
612 self.ld.nodes.push(LiveNode {
613 origin,
614 id: prop_id,
615 value: LiveValue::Int64(-val)
616 });
617 },
618 LiveToken::Float(val) => {
619 self.skip_token();
620 self.ld.nodes.push(LiveNode {
621 origin,
622 id: prop_id,
623 value: LiveValue::Float64(-val)
624 });
625 },
626 _=>return Err(self.error("Expected int or float after -".to_string(), live_error_origin!()))
627 }
628 }
629 LiveToken::Int(val) => {
630 self.skip_token();
631 self.ld.nodes.push(LiveNode {
632 origin,
633 id: prop_id,
634 value: LiveValue::Int64(val)
635 });
636 },
637 LiveToken::Float(val) => {
638 self.skip_token();
639 self.ld.nodes.push(LiveNode {
640 origin,
641 id: prop_id,
642 value: LiveValue::Float64(val)
643 });
644 },
645 LiveToken::Color(val) => {
646 self.skip_token();
647 self.ld.nodes.push(LiveNode {
648 origin,
649 id: prop_id,
650 value: LiveValue::Color(val)
651 });
652 },
653 LiveToken::String(v) => {
654 self.skip_token();
655 self.ld.nodes.push(LiveNode {
656 origin,
657 id: prop_id,
658 value: LiveValue::String(v)
659 });
660 },
661 LiveToken::Ident(live_id!(dep)) => {
662 self.skip_token();
663 if self.accept_token(LiveToken::Open(Delim::Paren)) {
664 let x = self.expect_string() ?;
665 self.ld.nodes.push(LiveNode {
666 origin,
667 id: prop_id,
668 value: LiveValue::Dependency(x)
669 });
670 self.expect_token(LiveToken::Close(Delim::Paren)) ?;
671 }
672 else {
673 self.ld.nodes.push(LiveNode {
674 origin,
675 id: prop_id,
676 value: LiveValue::Id(live_id!(dep))
677 });
678 }
679 }
680 LiveToken::Ident(live_id!(font)) => {
681 self.skip_token();
682 if self.accept_token(LiveToken::Open(Delim::Paren)) {
683 let path = self.expect_string() ?;
684
685 let mut ascender_fudge = 0.0;
686 let mut descender_fudge = 0.0;
687 if self.accept_token(LiveToken::Punct(live_id!(,))){
689 ascender_fudge = self.expect_float() ? as f32;
690 self.expect_token(LiveToken::Punct(live_id!(,))) ?;
691 descender_fudge = self.expect_float() ? as f32;
692 }
693 self.expect_token(LiveToken::Close(Delim::Paren)) ?;
694
695 self.ld.nodes.push(LiveNode {
696 origin,
697 id: prop_id,
698 value: LiveValue::Font(LiveFont{
699 path,
700 ascender_fudge,
701 descender_fudge
702 })
703 });
704 }
705 else {
706 self.ld.nodes.push(LiveNode {
707 origin,
708 id: prop_id,
709 value: LiveValue::Id(live_id!(font))
710 });
711 }
712 }
713 LiveToken::Ident(live_id!(vec2)) => {
714 self.skip_token();
715 if self.accept_token(LiveToken::Open(Delim::Paren)) {
716 let x = self.expect_float() ?;
717 self.expect_token(LiveToken::Punct(live_id!(,))) ?;
718 let y = self.expect_float() ?;
719 self.expect_token(LiveToken::Close(Delim::Paren)) ?;
720 self.ld.nodes.push(LiveNode {
721 origin,
722 id: prop_id,
723 value: LiveValue::Vec2(Vec2 {x: x as f32, y: y as f32})
724 });
725 }
726 else {
727 self.ld.nodes.push(LiveNode {
728 origin,
729 id: prop_id,
730 value: LiveValue::Id(live_id!(vec2))
731 });
732 }
733 },
734 LiveToken::Ident(live_id!(vec3)) => {
735 self.skip_token();
736 if self.accept_token(LiveToken::Open(Delim::Paren)) {
737 let x = self.expect_float() ?;
738 self.expect_token(LiveToken::Punct(live_id!(,))) ?;
739 let y = self.expect_float() ?;
740 self.expect_token(LiveToken::Punct(live_id!(,))) ?;
741 let z = self.expect_float() ?;
742 self.expect_token(LiveToken::Close(Delim::Paren)) ?;
743 self.ld.nodes.push(LiveNode {
744 origin,
745 id: prop_id,
746 value: LiveValue::Vec3(Vec3 {x: x as f32, y: y as f32, z: z as f32})
747 });
748 }
749 else {
750 self.ld.nodes.push(LiveNode {
751 origin,
752 id: prop_id,
753 value: LiveValue::Id(live_id!(vec3))
754 });
755 }
756 },
757 LiveToken::Ident(live_id!(vec4)) => {
758 self.skip_token();
759 if self.accept_token(LiveToken::Open(Delim::Paren)) {
760 let x = self.expect_float() ?;
761 self.expect_token(LiveToken::Punct(live_id!(,))) ?;
762 let y = self.expect_float() ?;
763 self.expect_token(LiveToken::Punct(live_id!(,))) ?;
764 let z = self.expect_float() ?;
765 self.expect_token(LiveToken::Punct(live_id!(,))) ?;
766 let w = self.expect_float() ?;
767 self.expect_token(LiveToken::Close(Delim::Paren)) ?;
768 self.ld.nodes.push(LiveNode {
769 origin,
770 id: prop_id,
771 value: LiveValue::Vec4(Vec4 {x: x as f32, y: y as f32, z: z as f32, w: w as f32})
772 });
773 }
774 else {
775 self.ld.nodes.push(LiveNode {
776 origin,
777 id: prop_id,
778 value: LiveValue::Id(live_id!(vec4))
779 });
780 }
781 },
782 LiveToken::Ident(variant) => { self.skip_token();
784 match self.peek_token() {
785 LiveToken::Punct(live_id!(.))=>{ let mut id_path = Vec::new();
787 id_path.push(variant);
788 while self.accept_token(LiveToken::Punct(live_id!(.))){
789 let ident = self.expect_ident()?;
790 id_path.push(ident);
791 }
792 self.ld.nodes.push(LiveNode {
793 origin,
794 id: prop_id,
795 value: LiveValue::IdPath(Arc::new(id_path))
796 })
797 }
799 LiveToken::Open(Delim::Brace) => {
800 self.expect_named_enum(prop_id, origin, variant) ?;
801 }
802 LiveToken::Open(Delim::Paren) => {
803 self.expect_tuple_enum(prop_id, origin, variant) ?;
804 }
805 _ => {
806 if variant.as_string(|v| v.unwrap().chars().next().unwrap().is_uppercase()){
807 self.ld.nodes.push(LiveNode {
808 origin,
809 id: prop_id,
810 value: LiveValue::BareEnum(variant)
811 })
812 }
813 else{
814 self.ld.nodes.push(LiveNode {
815 origin,
816 id: prop_id,
817 value: LiveValue::Id(variant)
818 })
819 }
820 }
821 }
822 },
823 other => return Err(self.error(format!("Unexpected token {} in property value", other), live_error_origin!()))
824 }
825 Ok(())
826 }
827
828 fn scan_to_token(&mut self, scan_token: LiveToken) -> Result<usize, LiveError> {
829 let mut stack_depth = 0;
831
832 while self.peek_token() != LiveToken::Eof {
833 match self.peek_token() {
834 LiveToken::Open(_) => {
835 stack_depth += 1;
836 }
837 LiveToken::Ident(id)=>{
838 self.ld.identifiers.insert(id);
839 }
840 LiveToken::Close(_) => {
841 if stack_depth == 0 {
842 return Err(self.error(format!("Found closing )}}] whilst scanning for {}", scan_token), live_error_origin!()));
843 }
844 stack_depth -= 1;
845 }
846 _ => ()
847 }
848 if stack_depth == 0 && self.peek_token() == scan_token {
849 self.skip_token();
850 return Ok(self.token_index)
851 }
852 self.skip_token();
853 }
854 Err(self.error(format!("Could not find ending token {} whilst scanning", scan_token), live_error_origin!()))
855 }
856 fn expect_value_literal(&mut self) -> Result<(), LiveError> {
866 match self.peek_token() {
867 LiveToken::Bool(_)
868 | LiveToken::Int(_)
869 | LiveToken::Float(_)
870 | LiveToken::Color(_) => {
871 self.skip_token();
872 return Ok(())
873 }
874 LiveToken::Ident(live_id!(vec2)) => {todo!()}
875 LiveToken::Ident(live_id!(vec3)) => {todo!()}
876 _ => ()
877 }
878 Err(self.error("Expected value literal".to_string(), live_error_origin!()))
879 }
880
881 fn expect_live_class(&mut self, root: bool, prop_id: LiveId) -> Result<(), LiveError> {
882 let mut nameless_id = 1;
883 while self.peek_token() != LiveToken::Eof {
884 match self.peek_token() {
885 LiveToken::Close(Delim::Brace) => {
886 if root {
887 return Err(self.error("Unexpected token } in root".to_string(), live_error_origin!()))
888 }
889 let token_id = self.get_token_id();
890 self.skip_token();
891 self.ld.nodes.push(LiveNode {
892 origin: LiveNodeOrigin::from_token_id(token_id),
893 id: prop_id,
894 value: LiveValue::Close
895 });
896 return Ok(());
897 }
898 LiveToken::Punct(live_id!(<))=>{ let token_id = self.get_token_id();
900 self.skip_token();
901 let ident = self.expect_ident()?;
902 self.expect_token(LiveToken::Punct(live_id!(>))) ?;
903 self.expect_token(LiveToken::Open(Delim::Brace))?;
904 self.ld.nodes.push(LiveNode {
905 origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Instance),
906 id: LiveId(nameless_id),
907 value: LiveValue::Clone{clone:ident}
908 });
909 nameless_id += 1;
910 self.expect_live_class(false, prop_id) ?;
911 }
912 LiveToken::Ident(prop_id) => {
913 let token_id = self.get_token_id();
914 self.skip_token();
915
916 if let LiveToken::Ident(_) = self.peek_token() {
920 match prop_id {
921 live_id!(fn) => {
922 self.expect_fn() ?;
923 self.accept_optional_delim();
924 }
925 live_id!(use) => {
926 self.expect_use() ?;
927 self.accept_optional_delim();
928 }
929 live_id!(link) => {
930 self.expect_link() ?;
931 self.accept_optional_delim();
932 }
933 _ => {
934 let token_id = self.get_token_id();
935 let real_prop_id = self.expect_ident() ?;
936 let edit_info = self.possible_edit_info() ?;
937 let prop_type = self.expect_prop_type()?;
938 let origin = LiveNodeOrigin::from_token_id(token_id)
939 .with_edit_info(edit_info)
940 .with_node_has_prefix(true)
941 .with_prop_type(prop_type);
942
943 self.expect_live_value(real_prop_id, origin) ?;
944 self.accept_optional_delim();
946 }
947 }
948 }
949 else { let edit_info = self.possible_edit_info() ?;
952
953 let assign_type = self.expect_prop_type()?;
954 let origin = LiveNodeOrigin::from_token_id(token_id)
955 .with_edit_info(edit_info)
956 .with_prop_type(assign_type);
957 self.expect_live_value(prop_id, origin) ?;
958
959 self.accept_optional_delim();
960 }
961 },
962 other => return Err(self.error(format!("Unexpected token {} in class body of {}", other, prop_id), live_error_origin!()))
963 }
964 }
965 if root {
966 return Ok(())
967 }
968 Err(self.error("Eof in class body".to_string(), live_error_origin!()))
969 }
970
971 pub fn expect_prop_type(&mut self)->Result<LivePropType, LiveError>{
972 Ok(if self.accept_token(LiveToken::Punct(live_id!(:))){
973 LivePropType::Field
974 }
975 else if self.accept_token(LiveToken::Punct(live_id!(=))){
976 LivePropType::Instance
981 }
983 else{
984 return Err(self.error("Unexpected assign_type, expected = or :".to_string(), live_error_origin!()))
985 })
986 }
987
988 pub fn accept_optional_delim(&mut self) {
989 if !self.accept_token(LiveToken::Punct(live_id!(,))) {
990 self.accept_token(LiveToken::Punct(live_id!(;)));
991 }
992 }
993
994 pub fn parse_live_document(tokens: &'a [TokenWithSpan], live_type_infos: &'a [LiveTypeInfo], file_id: LiveFileId) -> Result<LiveOriginal, LiveError> {
995 let mut tokens_with_span = tokens.iter().cloned();
996 let token_with_span = tokens_with_span.next().unwrap();
997 let mut lp = LiveParser {
998 live_type_info_counter: 0,
999 file_id,
1000 ld: LiveOriginal::default(),
1001 tokens_with_span,
1002 live_type_infos,
1003 token_with_span,
1004 token_index: 0,
1005 end: TextPos::default(),
1006 };
1007 lp.ld.nodes.push(LiveNode {
1009 origin: LiveNodeOrigin::from_token_id(lp.get_token_id()),
1010 id: LiveId::empty(),
1011 value: LiveValue::Object
1012 });
1013 lp.expect_live_class(true, LiveId::empty()) ?;
1014 lp.ld.nodes.push(LiveNode {
1015 origin: LiveNodeOrigin::from_token_id(lp.get_token_id()),
1016 id: LiveId::empty(),
1017 value: LiveValue::Close
1018 });
1019 Ok(lp.ld)
1021 }
1022
1023
1024
1025
1026
1027 fn expect_expression(&mut self, prop_id: LiveId, origin: LiveNodeOrigin) -> Result<(), LiveError> {
1033
1034 let expr = self.expect_prim_expr() ?;
1035
1036 self.ld.nodes.push(LiveNode {
1037 origin,
1038 id: prop_id,
1039 value: LiveValue::Expr
1040 });
1041
1042 fn recur_walk(expr: Expr, ld: &mut LiveOriginal) {
1043 match expr {
1044 Expr::Bin {token_id, op, left_expr, right_expr} => {
1045 ld.nodes.push(LiveNode {
1046 origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Nameless),
1047 id: LiveId::empty(),
1048 value: LiveValue::ExprBinOp(op)
1049 });
1050 recur_walk(*left_expr, ld);
1051 recur_walk(*right_expr, ld);
1052 }
1053 Expr::Un {token_id, op, expr} => {
1054 ld.nodes.push(LiveNode {
1055 origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Nameless),
1056 id: LiveId::empty(),
1057 value: LiveValue::ExprUnOp(op)
1058 });
1059 recur_walk(*expr, ld);
1060 }
1061 Expr::Call {token_id, ident, arg_exprs} => {
1062 ld.nodes.push(LiveNode {
1063 origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Nameless),
1064 id: LiveId::empty(),
1065 value: LiveValue::ExprCall {ident, args: arg_exprs.len()}
1066 });
1067 for arg in arg_exprs {
1068 recur_walk(arg, ld);
1069 }
1070 }
1071 Expr::Member {token_id, ident, expr} => {
1072 ld.nodes.push(LiveNode {
1073 origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Nameless),
1074 id: LiveId::empty(),
1075 value: LiveValue::ExprMember(ident)
1076 });
1077 recur_walk(*expr, ld);
1078 }
1079 Expr::Var {token_id, ident} => {
1080 ld.nodes.push(LiveNode {
1081 origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Nameless),
1082 id: LiveId::empty(),
1083 value: LiveValue::Id(ident)
1084 });
1085 }
1086 Expr::Bool {token_id, v} => {
1087 ld.nodes.push(LiveNode {
1088 origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Nameless),
1089 id: LiveId::empty(),
1090 value: LiveValue::Bool(v)
1091 });
1092 }
1093 Expr::Int {token_id, v} => {
1094 ld.nodes.push(LiveNode {
1095 origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Nameless),
1096 id: LiveId::empty(),
1097 value: LiveValue::Int64(v)
1098 });
1099 }
1100 Expr::Float {token_id, v} => {
1101 ld.nodes.push(LiveNode {
1102 origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Nameless),
1103 id: LiveId::empty(),
1104 value: LiveValue::Float64(v)
1105 });
1106 }
1107 Expr::Color {token_id, v} => {
1108 ld.nodes.push(LiveNode {
1109 origin: LiveNodeOrigin::from_token_id(token_id).with_prop_type(LivePropType::Nameless),
1110 id: LiveId::empty(),
1111 value: LiveValue::Color(v)
1112 });
1113 }
1114 }
1115 }
1116
1117 recur_walk(expr, &mut self.ld);
1118
1119 self.ld.nodes.push(LiveNode {
1120 origin: LiveNodeOrigin::from_token_id(self.get_token_id()),
1121 id: prop_id,
1122 value: LiveValue::Close
1123 });
1124
1125
1126 Ok(())
1127 }
1159
1160 fn expect_expr(&mut self) -> Result<Expr, LiveError> {
1161 self.expect_cond_expr()
1162 }
1163
1164 fn expect_cond_expr(&mut self) -> Result<Expr, LiveError> {
1165 let expr = self.expect_or_expr() ?;
1166 Ok(if self.accept_token(LiveToken::Punct(live_id!( ?))) {
1167 let token_id = self.get_token_id();
1168 let expr_if_true = self.expect_expr() ?;
1169 self.expect_token(LiveToken::Punct(live_id!(:))) ?;
1170 let expr_if_false = self.expect_cond_expr() ?;
1171 Expr ::Call {token_id, ident: live_id!(cond), arg_exprs: vec![expr, expr_if_true, expr_if_false]}
1172 } else {
1173 expr
1174 })
1175 }
1176
1177 fn expect_or_expr(&mut self) -> Result<Expr, LiveError> {
1178 let mut acc = self.expect_and_expr() ?;
1179 while let Some(op) = LiveBinOp::from_or_op(self.peek_token()) {
1180 self.skip_token();
1181 let token_id = self.get_token_id();
1182 let left_expr = Box::new(acc);
1183 let right_expr = Box::new(self.expect_and_expr() ?);
1184 acc = Expr::Bin {token_id, op, left_expr, right_expr,};
1185 }
1186 Ok(acc)
1187 }
1188
1189 fn expect_and_expr(&mut self) -> Result<Expr, LiveError> {
1190 let mut acc = self.expect_eq_expr() ?;
1191 while let Some(op) = LiveBinOp::from_and_op(self.peek_token()) {
1192 self.skip_token();
1193 let token_id = self.get_token_id();
1194 let left_expr = Box::new(acc);
1195 let right_expr = Box::new(self.expect_eq_expr() ?);
1196 acc = Expr::Bin {token_id, op, left_expr, right_expr,};
1197 }
1198 Ok(acc)
1199 }
1200
1201 fn expect_eq_expr(&mut self) -> Result<Expr, LiveError> {
1202 let mut acc = self.expect_rel_expr() ?;
1203 while let Some(op) = LiveBinOp::from_eq_op(self.peek_token()) {
1204 self.skip_token();
1205 let token_id = self.get_token_id();
1206 let left_expr = Box::new(acc);
1207 let right_expr = Box::new(self.expect_rel_expr() ?);
1208 acc = Expr::Bin {token_id, op, left_expr, right_expr,};
1209 }
1210 Ok(acc)
1211 }
1212
1213 fn expect_rel_expr(&mut self) -> Result<Expr, LiveError> {
1214 let mut acc = self.expect_add_expr() ?;
1215 while let Some(op) = LiveBinOp::from_rel_op(self.peek_token()) {
1216 self.skip_token();
1217 let token_id = self.get_token_id();
1218 let left_expr = Box::new(acc);
1219 let right_expr = Box::new(self.expect_add_expr() ?);
1220 acc = Expr::Bin {token_id, op, left_expr, right_expr,};
1221 }
1222 Ok(acc)
1223 }
1224
1225 fn expect_add_expr(&mut self) -> Result<Expr, LiveError> {
1226 let mut acc = self.expect_mul_expr() ?;
1227 while let Some(op) = LiveBinOp::from_add_op(self.peek_token()) {
1228 self.skip_token();
1229 let token_id = self.get_token_id();
1230 let left_expr = Box::new(acc);
1231 let right_expr = Box::new(self.expect_mul_expr() ?);
1232 acc = Expr::Bin {token_id, op, left_expr, right_expr,};
1233 }
1234 Ok(acc)
1235 }
1236
1237 fn expect_mul_expr(&mut self) -> Result<Expr, LiveError> {
1238 let mut acc = self.expect_un_expr() ?;
1239 while let Some(op) = LiveBinOp::from_mul_op(self.peek_token()) {
1240 self.skip_token();
1241 let token_id = self.get_token_id();
1242 let left_expr = Box::new(acc);
1243 let right_expr = Box::new(self.expect_un_expr() ?);
1244 acc = Expr::Bin {token_id, op, left_expr, right_expr,};
1245 }
1246 Ok(acc)
1247 }
1248
1249 fn expect_un_expr(&mut self) -> Result<Expr, LiveError> {
1250 Ok(if let Some(op) = LiveUnOp::from_un_op(self.peek_token()) {
1251 self.skip_token();
1252 let token_id = self.get_token_id();
1253 let expr = Box::new(self.expect_un_expr() ?);
1254 Expr::Un {token_id, op, expr,}
1255 } else {
1256 self.expect_member_expr() ?
1257 })
1258 }
1259
1260 fn expect_member_expr(&mut self) -> Result<Expr, LiveError> {
1261 let mut acc = self.expect_prim_expr()?;
1262 while let LiveToken::Punct(live_id!(.)) = self.peek_token() {
1263 self.skip_token();
1264 let token_id = self.get_token_id();
1265 let ident = self.expect_ident()?;
1266 acc = Expr::Member {token_id, ident, expr: Box::new(acc)}
1267 }
1268 Ok(acc)
1269 }
1270
1271 fn expect_prim_expr(&mut self) -> Result<Expr, LiveError> {
1272 match self.peek_token() {
1273 LiveToken::Ident(ident) => {
1274 let token_id = self.get_token_id();
1275 self.skip_token();
1276 match self.peek_token() {
1277 LiveToken::Open(Delim::Paren) => {
1278 let arg_exprs = self.expect_arg_exprs() ?;
1279 Ok(Expr::Call {token_id, ident, arg_exprs})
1280 }
1281 _ => {
1282 self.ld.identifiers.insert(ident);
1283 Ok(Expr::Var {token_id, ident})
1284 }
1285 }
1286 }
1287 LiveToken::Bool(v) => {
1288 self.skip_token();
1289 let token_id = self.get_token_id();
1290 Ok(Expr::Bool {token_id, v})
1291 }
1292 LiveToken::Int(v) => {
1293 self.skip_token();
1294 let token_id = self.get_token_id();
1295 Ok(Expr::Int {token_id, v})
1296 }
1297 LiveToken::Float(v) => {
1298 self.skip_token();
1299 let token_id = self.get_token_id();
1300 Ok(Expr::Float {token_id, v})
1301 }
1302 LiveToken::Color(v) => {
1303 self.skip_token();
1304 let token_id = self.get_token_id();
1305 Ok(Expr::Color {token_id, v})
1306 }
1307 LiveToken::Open(Delim::Paren) => {
1308 self.skip_token();
1309 let expr = self.expect_expr() ?;
1310 self.expect_token(LiveToken::Close(Delim::Paren)) ?;
1311 Ok(expr)
1312 }
1313 token => Err(self.error(format!("Unexpected token {} in class expression", token), live_error_origin!()))
1314 }
1315 }
1316
1317 fn expect_arg_exprs(&mut self) -> Result<Vec<Expr>, LiveError> {
1318 self.expect_token(LiveToken::Open(Delim::Paren)) ?;
1319 let mut arg_exprs = Vec::new();
1320 if !self.accept_token(LiveToken::Close(Delim::Paren)) {
1321 loop {
1322 arg_exprs.push(self.expect_expr() ?);
1323 if !self.accept_token(LiveToken::Punct(live_id!(,))) {
1324 break;
1325 }
1326 }
1327 self.expect_token(LiveToken::Close(Delim::Paren)) ?;
1328 }
1329 Ok(arg_exprs)
1330 }
1331
1332
1333}
1334
1335#[derive(Debug)]
1336enum Expr {
1337 Bin {
1338 token_id: LiveTokenId,
1339 op: LiveBinOp,
1340 left_expr: Box<Expr>,
1341 right_expr: Box<Expr>,
1342 },
1343 Un {
1344 token_id: LiveTokenId,
1345 op: LiveUnOp,
1346 expr: Box<Expr>,
1347 },
1348 Call {
1349 token_id: LiveTokenId,
1350 ident: LiveId,
1351 arg_exprs: Vec<Expr>,
1352 },
1353 Member {
1354 token_id: LiveTokenId,
1355 ident: LiveId,
1356 expr: Box<Expr>
1357 },
1358 Var {
1359 token_id: LiveTokenId,
1360 ident: LiveId,
1361 },
1362 Bool {
1363 token_id: LiveTokenId,
1364 v: bool
1365 },
1366 Int {
1367 token_id: LiveTokenId,
1368 v: i64
1369 },
1370 Float {
1371 token_id: LiveTokenId,
1372 v: f64
1373 },
1374 Color {
1375 token_id: LiveTokenId,
1376 v: u32
1377 }
1378}
1379
1380
1381impl LiveBinOp {
1382 fn from_or_op(token: LiveToken) -> Option<Self> {
1383 match token {
1384 LiveToken::Punct(live_id!( ||)) => Some(Self::Or),
1385 _ => None,
1386 }
1387 }
1388
1389 fn from_and_op(token: LiveToken) -> Option<Self> {
1390 match token {
1391 LiveToken::Punct(live_id!( &&)) => Some(Self::And),
1392 _ => None,
1393 }
1394 }
1395
1396 fn from_eq_op(token: LiveToken) -> Option<Self> {
1397 match token {
1398 LiveToken::Punct(live_id!( ==)) => Some(Self::Eq),
1399 LiveToken::Punct(live_id!( !=)) => Some(Self::Ne),
1400 _ => None,
1401 }
1402 }
1403
1404 fn from_rel_op(token: LiveToken) -> Option<Self> {
1405 match token {
1406 LiveToken::Punct(live_id!(<)) => Some(Self::Lt),
1407 LiveToken::Punct(live_id!( <=)) => Some(Self::Le),
1408 LiveToken::Punct(live_id!(>)) => Some(Self::Gt),
1409 LiveToken::Punct(live_id!( >=)) => Some(Self::Ge),
1410 _ => None,
1411 }
1412 }
1413
1414 fn from_add_op(token: LiveToken) -> Option<Self> {
1415 match token {
1416 LiveToken::Punct(live_id!( +)) => Some(Self::Add),
1417 LiveToken::Punct(live_id!(-)) => Some(Self::Sub),
1418 _ => None,
1419 }
1420 }
1421
1422 fn from_mul_op(token: LiveToken) -> Option<Self> {
1423 match token {
1424 LiveToken::Punct(live_id!(*)) => Some(Self::Mul),
1425 LiveToken::Punct(live_id!( /)) => Some(Self::Div),
1426 _ => None,
1427 }
1428 }
1429}
1430
1431impl LiveUnOp {
1432 pub fn from_un_op(token: LiveToken) -> Option<Self> {
1433 match token {
1434 LiveToken::Punct(live_id!(!)) => Some(Self::Not),
1435 LiveToken::Punct(live_id!(-)) => Some(Self::Neg),
1436 _ => None,
1437 }
1438 }
1439}
1440