summaryrefslogtreecommitdiff
path: root/deepcrow/src/parser.rs
diff options
context:
space:
mode:
authorMike Buland <mike@xagasoft.com>2026-08-19 13:05:01 -0700
committerMike Buland <mike@xagasoft.com>2026-08-19 13:05:01 -0700
commitd1e78b4a3454984db74ebf2a302a64b985ce6de4 (patch)
treed3953cd8aad144a1be131ff3c38688f93cbccbb5 /deepcrow/src/parser.rs
parent5602585e7afe431f25e68f30ac54761db2a710e5 (diff)
downloaddeepcrow-d1e78b4a3454984db74ebf2a302a64b985ce6de4.tar.gz
deepcrow-d1e78b4a3454984db74ebf2a302a64b985ce6de4.tar.bz2
deepcrow-d1e78b4a3454984db74ebf2a302a64b985ce6de4.tar.xz
deepcrow-d1e78b4a3454984db74ebf2a302a64b985ce6de4.zip
Rearranged and renamed some bits.
Diffstat (limited to 'deepcrow/src/parser.rs')
-rw-r--r--deepcrow/src/parser.rs118
1 files changed, 59 insertions, 59 deletions
diff --git a/deepcrow/src/parser.rs b/deepcrow/src/parser.rs
index fbfc44e..529c58e 100644
--- a/deepcrow/src/parser.rs
+++ b/deepcrow/src/parser.rs
@@ -36,7 +36,7 @@ impl<'a> Context<'a> {
36 } 36 }
37 37
38 /* 38 /*
39 pub fn check_unwrapped<T: Fn( &SymbolType, &SymbolType ) -> bool>(&self, context: &str, f: T) -> Result<bool> { 39 pub fn check_unwrapped<T: Fn( &TokenType, &TokenType ) -> bool>(&self, context: &str, f: T) -> Result<bool> {
40 if self.cur().is_none() || self.peek().is_none() { 40 if self.cur().is_none() || self.peek().is_none() {
41 Err(DeepCrowError::eos( context )) 41 Err(DeepCrowError::eos( context ))
42 } else { 42 } else {
@@ -51,13 +51,13 @@ impl<'a> Context<'a> {
51} 51}
52 52
53trait SymbolHelper { 53trait SymbolHelper {
54 fn is<T: Fn( &SymbolType ) -> bool>(&self, context: &str, f: T ) -> Result<bool>; 54 fn is<T: Fn( &TokenType ) -> bool>(&self, context: &str, f: T ) -> Result<bool>;
55 #[allow(dead_code)] 55 #[allow(dead_code)]
56 fn is_valid_tag_name(&self) -> Result<bool>; 56 fn is_valid_tag_name(&self) -> Result<bool>;
57} 57}
58 58
59impl<'a> SymbolHelper for Option<Symbol<'a>> { 59impl<'a> SymbolHelper for Option<Symbol<'a>> {
60 fn is<T: Fn( &SymbolType ) -> bool>(&self, context: &str, f: T ) -> Result<bool> { 60 fn is<T: Fn( &TokenType ) -> bool>(&self, context: &str, f: T ) -> Result<bool> {
61 if let Some(st) = self { 61 if let Some(st) = self {
62 Ok(f( &st.symbol() )) 62 Ok(f( &st.symbol() ))
63 } else { 63 } else {
@@ -68,14 +68,14 @@ impl<'a> SymbolHelper for Option<Symbol<'a>> {
68 fn is_valid_tag_name(&self) -> Result<bool> { 68 fn is_valid_tag_name(&self) -> Result<bool> {
69 if let Some(st) = self { 69 if let Some(st) = self {
70 Ok(match st.symbol() { 70 Ok(match st.symbol() {
71 SymbolType::Token(_) | 71 TokenType::Identifier(_) |
72 SymbolType::View | 72 TokenType::View |
73 SymbolType::Output | 73 TokenType::Output |
74 SymbolType::Loop | 74 TokenType::Loop |
75 SymbolType::If | 75 TokenType::If |
76 SymbolType::ElIf | 76 TokenType::ElIf |
77 SymbolType::Else | 77 TokenType::Else |
78 SymbolType::Show => true, 78 TokenType::Show => true,
79 _ => false 79 _ => false
80 }) 80 })
81 } else { 81 } else {
@@ -184,7 +184,7 @@ impl Parser {
184 } 184 }
185 185
186 fn lex_error<T>(&self, error: &Symbol) -> Result<T> { 186 fn lex_error<T>(&self, error: &Symbol) -> Result<T> {
187 if let SymbolType::Error{what} = error.symbol() { 187 if let TokenType::Error{what} = error.symbol() {
188 Err(DeepCrowError::parse( *error.start(), 188 Err(DeepCrowError::parse( *error.start(),
189 format!("What: {:?}", *what) 189 format!("What: {:?}", *what)
190 )) 190 ))
@@ -208,13 +208,13 @@ impl Parser {
208 break; 208 break;
209 } 209 }
210 match ctx.cur().unwrap().symbol() { 210 match ctx.cur().unwrap().symbol() {
211 SymbolType::Text(_) => { /* Skip top level text */ } 211 TokenType::Text(_) => { /* Skip top level text */ }
212 SymbolType::StartFlat => { 212 TokenType::StartFlat => {
213 let tb = self.parse_tag( ctx )?; 213 let tb = self.parse_tag( ctx )?;
214 let tb = self.parse_tag_set( ctx, tb )?; 214 let tb = self.parse_tag_set( ctx, tb )?;
215 tags.push( tb ); 215 tags.push( tb );
216 } 216 }
217 SymbolType::Error{..} => { 217 TokenType::Error{..} => {
218 self.lex_error( &ctx.cur().unwrap() )?; 218 self.lex_error( &ctx.cur().unwrap() )?;
219 } 219 }
220 _ => { 220 _ => {
@@ -231,7 +231,7 @@ impl Parser {
231 for tag in tags { 231 for tag in tags {
232 let start_pos = tag.start_pos(); 232 let start_pos = tag.start_pos();
233 let name = tag.name().clone(); 233 let name = tag.name().clone();
234 if tag.is_name(&SymbolType::View) && 234 if tag.is_name(&TokenType::View) &&
235 let BuildResult::View(view) = tag.build( ctx )? { 235 let BuildResult::View(view) = tag.build( ctx )? {
236 views.push( view ); 236 views.push( view );
237 } else { 237 } else {
@@ -250,9 +250,9 @@ impl Parser {
250 250
251 if ctx.next().is_some() { 251 if ctx.next().is_some() {
252 match ctx.cur().unwrap().symbol() { 252 match ctx.cur().unwrap().symbol() {
253 SymbolType::View | SymbolType::Show | SymbolType::Loop | 253 TokenType::View | TokenType::Show | TokenType::Loop |
254 SymbolType::If | SymbolType::ElIf | SymbolType::Else | 254 TokenType::If | TokenType::ElIf | TokenType::Else |
255 SymbolType::Output => { 255 TokenType::Output => {
256 let name_sym = ctx.cur().unwrap(); 256 let name_sym = ctx.cur().unwrap();
257 ctx.next(); 257 ctx.next();
258 tb.set_name( name_sym ); 258 tb.set_name( name_sym );
@@ -277,7 +277,7 @@ impl Parser {
277 277
278 if let Some(end_sym) = ctx.cur() { 278 if let Some(end_sym) = ctx.cur() {
279 match end_sym.symbol() { 279 match end_sym.symbol() {
280 SymbolType::EndPoint | SymbolType::EndFlat => { 280 TokenType::EndPoint | TokenType::EndFlat => {
281 tb.set_end( &end_sym )?; 281 tb.set_end( &end_sym )?;
282 } 282 }
283 _ => { 283 _ => {
@@ -304,11 +304,11 @@ impl Parser {
304 return Err(DeepCrowError::eos(format!("close tag for {:?}", base.name()).as_str())); 304 return Err(DeepCrowError::eos(format!("close tag for {:?}", base.name()).as_str()));
305 } 305 }
306 match ctx.cur().unwrap().symbol() { 306 match ctx.cur().unwrap().symbol() {
307 SymbolType::Text(s) => { 307 TokenType::Text(s) => {
308 base.add_child(Entry::Token(Token::Text(s.to_string()))); 308 base.add_child(Entry::Token(Token::Text(s.to_string())));
309 ctx.next(); 309 ctx.next();
310 } 310 }
311 SymbolType::StartFlat | SymbolType::StartPoint => { 311 TokenType::StartFlat | TokenType::StartPoint => {
312 let tb = self.parse_tag( ctx )?; 312 let tb = self.parse_tag( ctx )?;
313 313
314 match tb.tag_type() { 314 match tb.tag_type() {
@@ -320,9 +320,9 @@ impl Parser {
320 return Ok(base); 320 return Ok(base);
321 } 321 }
322 TagType::MultinaryClose => { 322 TagType::MultinaryClose => {
323 if (tb.is_name(&SymbolType::If) && 323 if (tb.is_name(&TokenType::If) &&
324 (base.is_name(&SymbolType::ElIf) || 324 (base.is_name(&TokenType::ElIf) ||
325 base.is_name(&SymbolType::Else))) || 325 base.is_name(&TokenType::Else))) ||
326 tb.is_name(base.name().unwrap().symbol()) { 326 tb.is_name(base.name().unwrap().symbol()) {
327 return Ok(base); 327 return Ok(base);
328 } else { 328 } else {
@@ -340,7 +340,7 @@ impl Parser {
340 } 340 }
341 } 341 }
342 } 342 }
343 SymbolType::Error{..} => { 343 TokenType::Error{..} => {
344 return self.lex_error( &ctx.cur().unwrap() ); 344 return self.lex_error( &ctx.cur().unwrap() );
345 } 345 }
346 _ => { 346 _ => {
@@ -359,12 +359,12 @@ impl Parser {
359 return Err(DeepCrowError::eos("tag parameters")); 359 return Err(DeepCrowError::eos("tag parameters"));
360 } 360 }
361 match ctx.cur().unwrap().symbol() { 361 match ctx.cur().unwrap().symbol() {
362 SymbolType::Literal(s) => { 362 TokenType::Literal(s) => {
363 tb.add_param( ParamValue::Literal(s.to_string()) ); 363 tb.add_param( ParamValue::Literal(s.to_string()) );
364 ctx.next(); 364 ctx.next();
365 } 365 }
366 SymbolType::Token(_) | SymbolType::Sharp => { 366 TokenType::Identifier(_) | TokenType::Sharp => {
367 if ctx.peek().is("tag data", |s| *s == SymbolType::Equals)? { 367 if ctx.peek().is("tag data", |s| *s == TokenType::Equals)? {
368 break; 368 break;
369 } 369 }
370 tb.add_param( self.parse_identifier( ctx )? ); 370 tb.add_param( self.parse_identifier( ctx )? );
@@ -380,20 +380,20 @@ impl Parser {
380 fn parse_identifier(&self, ctx: &mut Context ) -> Result<ParamValue> { 380 fn parse_identifier(&self, ctx: &mut Context ) -> Result<ParamValue> {
381 let mut id = Identifier::new(); 381 let mut id = Identifier::new();
382 382
383 if ctx.cur().is("identifier",|s| matches!( s, SymbolType::Sharp))? { 383 if ctx.cur().is("identifier",|s| matches!( s, TokenType::Sharp))? {
384 id.push( IdentifierValue::Root ); 384 id.push( IdentifierValue::Root );
385 ctx.next(); 385 ctx.next();
386 } 386 }
387 loop { 387 loop {
388 if ctx.cur().is("identifier",|s| matches!( s, SymbolType::Token(_) ))? { 388 if ctx.cur().is("identifier",|s| matches!( s, TokenType::Identifier(_) ))? {
389 if let SymbolType::Token(s) = ctx.cur().unwrap().symbol() { 389 if let TokenType::Identifier(s) = ctx.cur().unwrap().symbol() {
390 id.push( IdentifierValue::Name(s.to_string()) ); 390 id.push( IdentifierValue::Name(s.to_string()) );
391 } 391 }
392 } else { 392 } else {
393 return Err(DeepCrowError::parse( ctx.cur().unwrap().start().clone(), format!("Expeceted identifier, found {:?}", ctx.cur().unwrap().symbol() ) ) ); 393 return Err(DeepCrowError::parse( ctx.cur().unwrap().start().clone(), format!("Expeceted identifier, found {:?}", ctx.cur().unwrap().symbol() ) ) );
394 } 394 }
395 395
396 if !ctx.next().is("identifier seperator",|s| *s == SymbolType::Period )? { 396 if !ctx.next().is("identifier seperator",|s| *s == TokenType::Period )? {
397 break; 397 break;
398 } 398 }
399 ctx.next(); 399 ctx.next();
@@ -407,11 +407,11 @@ impl Parser {
407 if ctx.cur().is_none() { 407 if ctx.cur().is_none() {
408 return Err(DeepCrowError::eos("tag properties")); 408 return Err(DeepCrowError::eos("tag properties"));
409 } 409 }
410 if let SymbolType::Token(s) = ctx.cur().unwrap().symbol() { 410 if let TokenType::Identifier(s) = ctx.cur().unwrap().symbol() {
411 if ctx.peek().is_some_and(|s|s.check_type(|t| matches!(t,SymbolType::Equals))) { 411 if ctx.peek().is_some_and(|s|s.check_type(|t| matches!(t,TokenType::Equals))) {
412 ctx.next(); 412 ctx.next();
413 if let Some(sym) = ctx.next() && 413 if let Some(sym) = ctx.next() &&
414 let SymbolType::Literal(lv) = sym.symbol() { 414 let TokenType::Literal(lv) = sym.symbol() {
415 tb.add_prop( s.to_string(), lv.to_string() ); 415 tb.add_prop( s.to_string(), lv.to_string() );
416 } else { 416 } else {
417 return Err(DeepCrowError::parse(Position::none(),"Expected quoted literal string".to_string())); 417 return Err(DeepCrowError::parse(Position::none(),"Expected quoted literal string".to_string()));
@@ -455,7 +455,7 @@ impl<'a> EntryListConverter for EntryList<'a> {
455 fn has_outputs(&self) -> bool { 455 fn has_outputs(&self) -> bool {
456 self.iter().any(|e| 456 self.iter().any(|e|
457 if let Entry::TagBuilder(tb) = e 457 if let Entry::TagBuilder(tb) = e
458 && tb.is_name(&SymbolType::Output) { 458 && tb.is_name(&TokenType::Output) {
459 true 459 true
460 } else { 460 } else {
461 false 461 false
@@ -546,18 +546,18 @@ impl<'a> TagBuilder<'a> {
546 } 546 }
547 547
548 pub fn set_end(&mut self, end: &Symbol) -> Result<()> { 548 pub fn set_end(&mut self, end: &Symbol) -> Result<()> {
549 if *self.start.symbol() == SymbolType::StartFlat { 549 if *self.start.symbol() == TokenType::StartFlat {
550 if *end.symbol() == SymbolType::EndFlat { 550 if *end.symbol() == TokenType::EndFlat {
551 self.tag_type = TagType::Unary; 551 self.tag_type = TagType::Unary;
552 } else if *end.symbol() == SymbolType::EndPoint { 552 } else if *end.symbol() == TokenType::EndPoint {
553 self.tag_type = TagType::MultinaryOpen; 553 self.tag_type = TagType::MultinaryOpen;
554 } else { 554 } else {
555 return Err(DeepCrowError::broken( *end.start(), "Invalid bracket token type.")); 555 return Err(DeepCrowError::broken( *end.start(), "Invalid bracket token type."));
556 } 556 }
557 } else if *self.start.symbol() == SymbolType::StartPoint { 557 } else if *self.start.symbol() == TokenType::StartPoint {
558 if *end.symbol() == SymbolType::EndFlat { 558 if *end.symbol() == TokenType::EndFlat {
559 self.tag_type = TagType::MultinaryClose; 559 self.tag_type = TagType::MultinaryClose;
560 } else if *end.symbol() == SymbolType::EndPoint { 560 } else if *end.symbol() == TokenType::EndPoint {
561 self.tag_type = TagType::MultinaryMid; 561 self.tag_type = TagType::MultinaryMid;
562 } else { 562 } else {
563 return Err(DeepCrowError::broken( *end.start(), "Invalid bracket token type.")); 563 return Err(DeepCrowError::broken( *end.start(), "Invalid bracket token type."));
@@ -582,18 +582,18 @@ impl<'a> TagBuilder<'a> {
582*/ 582*/
583 pub fn can_have_params(&self) -> bool { 583 pub fn can_have_params(&self) -> bool {
584 match self.name.unwrap().symbol() { 584 match self.name.unwrap().symbol() {
585 SymbolType::View | SymbolType::Output | SymbolType::Loop => true, 585 TokenType::View | TokenType::Output | TokenType::Loop => true,
586 SymbolType::Show | SymbolType::If | SymbolType::ElIf | 586 TokenType::Show | TokenType::If | TokenType::ElIf |
587 SymbolType::Else => false, 587 TokenType::Else => false,
588 _ => false, 588 _ => false,
589 } 589 }
590 } 590 }
591 591
592 pub fn can_have_expr(&self) -> bool { 592 pub fn can_have_expr(&self) -> bool {
593 match self.name.unwrap().symbol() { 593 match self.name.unwrap().symbol() {
594 SymbolType::View | SymbolType::Output | SymbolType::Loop => false, 594 TokenType::View | TokenType::Output | TokenType::Loop => false,
595 SymbolType::Show | SymbolType::If | SymbolType::ElIf | 595 TokenType::Show | TokenType::If | TokenType::ElIf |
596 SymbolType::Else => true, 596 TokenType::Else => true,
597 _ => false, 597 _ => false,
598 } 598 }
599 } 599 }
@@ -611,7 +611,7 @@ impl<'a> TagBuilder<'a> {
611 } 611 }
612 } 612 }
613*/ 613*/
614 pub fn is_name(&self, name: &SymbolType<'a>) -> bool { 614 pub fn is_name(&self, name: &TokenType<'a>) -> bool {
615 if let Some(a) = self.name { 615 if let Some(a) = self.name {
616 a.symbol() == name 616 a.symbol() == name
617 } else { 617 } else {
@@ -662,7 +662,7 @@ impl<'a> TagBuilder<'a> {
662 pub fn build(mut self, ctx: &mut Context ) -> Result<BuildResult> { 662 pub fn build(mut self, ctx: &mut Context ) -> Result<BuildResult> {
663 if let Some(sym) = self.name { 663 if let Some(sym) = self.name {
664 match sym.symbol() { 664 match sym.symbol() {
665 SymbolType::View => { 665 TokenType::View => {
666 let name = if let ParamValue::Literal(s) = self.params.swap_remove(0) { 666 let name = if let ParamValue::Literal(s) = self.params.swap_remove(0) {
667 s.to_string() 667 s.to_string()
668 } else { 668 } else {
@@ -676,7 +676,7 @@ impl<'a> TagBuilder<'a> {
676 layout: self.props.get("layout").cloned(), 676 layout: self.props.get("layout").cloned(),
677 })) 677 }))
678 } 678 }
679 SymbolType::Output => { 679 TokenType::Output => {
680 let name = if let ParamValue::Literal(s) = self.params.swap_remove(0) { 680 let name = if let ParamValue::Literal(s) = self.params.swap_remove(0) {
681 s.to_string() 681 s.to_string()
682 } else { 682 } else {
@@ -687,7 +687,7 @@ impl<'a> TagBuilder<'a> {
687 code: self.children.to_tokens(ctx)?, 687 code: self.children.to_tokens(ctx)?,
688 })) 688 }))
689 } 689 }
690 SymbolType::Loop => { 690 TokenType::Loop => {
691 let id = if let ParamValue::Identifier(id) 691 let id = if let ParamValue::Identifier(id)
692 = self.params.swap_remove(0) { 692 = self.params.swap_remove(0) {
693 id 693 id
@@ -701,7 +701,7 @@ impl<'a> TagBuilder<'a> {
701 Token::Loop(id, self.children.to_tokens(ctx)?) 701 Token::Loop(id, self.children.to_tokens(ctx)?)
702 )) 702 ))
703 } 703 }
704 SymbolType::Show => { 704 TokenType::Show => {
705 let id = if let ParamValue::Identifier(id) 705 let id = if let ParamValue::Identifier(id)
706 = self.params.swap_remove(0) { 706 = self.params.swap_remove(0) {
707 id 707 id
@@ -713,7 +713,7 @@ impl<'a> TagBuilder<'a> {
713 }; 713 };
714 Ok(BuildResult::Token(Token::Show(id, self.props))) 714 Ok(BuildResult::Token(Token::Show(id, self.props)))
715 } 715 }
716 SymbolType::If => { 716 TokenType::If => {
717 let id = if let ParamValue::Identifier(id) 717 let id = if let ParamValue::Identifier(id)
718 = self.params.swap_remove(0) { 718 = self.params.swap_remove(0) {
719 id 719 id
@@ -726,7 +726,7 @@ impl<'a> TagBuilder<'a> {
726 726
727 let is_else = if self.chain.len() == 1 && 727 let is_else = if self.chain.len() == 1 &&
728 let Entry::TagBuilder(tb) = &self.chain[0] && 728 let Entry::TagBuilder(tb) = &self.chain[0] &&
729 tb.is_name(&SymbolType::Else) && 729 tb.is_name(&TokenType::Else) &&
730 tb.params.len() == 0 { 730 tb.params.len() == 0 {
731 true 731 true
732 } else { 732 } else {
@@ -740,7 +740,7 @@ impl<'a> TagBuilder<'a> {
740 } 740 }
741 Ok(BuildResult::Token(Token::If(id, self.children.to_tokens(ctx)?, self.chain.to_tokens(ctx)?))) 741 Ok(BuildResult::Token(Token::If(id, self.children.to_tokens(ctx)?, self.chain.to_tokens(ctx)?)))
742 } 742 }
743 SymbolType::ElIf => { 743 TokenType::ElIf => {
744 let id = if let ParamValue::Identifier(id) = self.params.swap_remove(0) { 744 let id = if let ParamValue::Identifier(id) = self.params.swap_remove(0) {
745 id 745 id
746 } else { 746 } else {
@@ -755,7 +755,7 @@ impl<'a> TagBuilder<'a> {
755 // ...but I can't do that right now. 755 // ...but I can't do that right now.
756 let is_else = if self.chain.len() == 1 && 756 let is_else = if self.chain.len() == 1 &&
757 let Entry::TagBuilder(tb) = &self.chain[0] && 757 let Entry::TagBuilder(tb) = &self.chain[0] &&
758 tb.is_name(&SymbolType::Else) && 758 tb.is_name(&TokenType::Else) &&
759 tb.params.len() == 0 { 759 tb.params.len() == 0 {
760 true 760 true
761 } else { 761 } else {
@@ -771,7 +771,7 @@ impl<'a> TagBuilder<'a> {
771 771
772 Ok(BuildResult::Token(Token::If(id, self.children.to_tokens(ctx)?, self.chain.to_tokens(ctx)?))) 772 Ok(BuildResult::Token(Token::If(id, self.children.to_tokens(ctx)?, self.chain.to_tokens(ctx)?)))
773 } 773 }
774 SymbolType::Else => { 774 TokenType::Else => {
775 Ok(BuildResult::Token(Token::If(Identifier::new(), self.children.to_tokens(ctx)?, self.chain.to_tokens(ctx)?))) 775 Ok(BuildResult::Token(Token::If(Identifier::new(), self.children.to_tokens(ctx)?, self.chain.to_tokens(ctx)?)))
776 } 776 }
777 _ => { 777 _ => {