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