From d1e78b4a3454984db74ebf2a302a64b985ce6de4 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 19 Aug 2026 13:05:01 -0700 Subject: Rearranged and renamed some bits. --- deepcrow/src/parser.rs | 118 ++++++++++++++++++++++++------------------------- 1 file changed, 59 insertions(+), 59 deletions(-) (limited to 'deepcrow/src/parser.rs') 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> { } /* - pub fn check_unwrapped bool>(&self, context: &str, f: T) -> Result { + pub fn check_unwrapped bool>(&self, context: &str, f: T) -> Result { if self.cur().is_none() || self.peek().is_none() { Err(DeepCrowError::eos( context )) } else { @@ -51,13 +51,13 @@ impl<'a> Context<'a> { } trait SymbolHelper { - fn is bool>(&self, context: &str, f: T ) -> Result; + fn is bool>(&self, context: &str, f: T ) -> Result; #[allow(dead_code)] fn is_valid_tag_name(&self) -> Result; } impl<'a> SymbolHelper for Option> { - fn is bool>(&self, context: &str, f: T ) -> Result { + fn is bool>(&self, context: &str, f: T ) -> Result { if let Some(st) = self { Ok(f( &st.symbol() )) } else { @@ -68,14 +68,14 @@ impl<'a> SymbolHelper for Option> { fn is_valid_tag_name(&self) -> Result { if let Some(st) = self { Ok(match st.symbol() { - SymbolType::Token(_) | - SymbolType::View | - SymbolType::Output | - SymbolType::Loop | - SymbolType::If | - SymbolType::ElIf | - SymbolType::Else | - SymbolType::Show => true, + TokenType::Identifier(_) | + TokenType::View | + TokenType::Output | + TokenType::Loop | + TokenType::If | + TokenType::ElIf | + TokenType::Else | + TokenType::Show => true, _ => false }) } else { @@ -184,7 +184,7 @@ impl Parser { } fn lex_error(&self, error: &Symbol) -> Result { - if let SymbolType::Error{what} = error.symbol() { + if let TokenType::Error{what} = error.symbol() { Err(DeepCrowError::parse( *error.start(), format!("What: {:?}", *what) )) @@ -208,13 +208,13 @@ impl Parser { break; } match ctx.cur().unwrap().symbol() { - SymbolType::Text(_) => { /* Skip top level text */ } - SymbolType::StartFlat => { + TokenType::Text(_) => { /* Skip top level text */ } + TokenType::StartFlat => { let tb = self.parse_tag( ctx )?; let tb = self.parse_tag_set( ctx, tb )?; tags.push( tb ); } - SymbolType::Error{..} => { + TokenType::Error{..} => { self.lex_error( &ctx.cur().unwrap() )?; } _ => { @@ -231,7 +231,7 @@ impl Parser { for tag in tags { let start_pos = tag.start_pos(); let name = tag.name().clone(); - if tag.is_name(&SymbolType::View) && + if tag.is_name(&TokenType::View) && let BuildResult::View(view) = tag.build( ctx )? { views.push( view ); } else { @@ -250,9 +250,9 @@ impl Parser { if ctx.next().is_some() { match ctx.cur().unwrap().symbol() { - SymbolType::View | SymbolType::Show | SymbolType::Loop | - SymbolType::If | SymbolType::ElIf | SymbolType::Else | - SymbolType::Output => { + TokenType::View | TokenType::Show | TokenType::Loop | + TokenType::If | TokenType::ElIf | TokenType::Else | + TokenType::Output => { let name_sym = ctx.cur().unwrap(); ctx.next(); tb.set_name( name_sym ); @@ -277,7 +277,7 @@ impl Parser { if let Some(end_sym) = ctx.cur() { match end_sym.symbol() { - SymbolType::EndPoint | SymbolType::EndFlat => { + TokenType::EndPoint | TokenType::EndFlat => { tb.set_end( &end_sym )?; } _ => { @@ -304,11 +304,11 @@ impl Parser { return Err(DeepCrowError::eos(format!("close tag for {:?}", base.name()).as_str())); } match ctx.cur().unwrap().symbol() { - SymbolType::Text(s) => { + TokenType::Text(s) => { base.add_child(Entry::Token(Token::Text(s.to_string()))); ctx.next(); } - SymbolType::StartFlat | SymbolType::StartPoint => { + TokenType::StartFlat | TokenType::StartPoint => { let tb = self.parse_tag( ctx )?; match tb.tag_type() { @@ -320,9 +320,9 @@ impl Parser { return Ok(base); } TagType::MultinaryClose => { - if (tb.is_name(&SymbolType::If) && - (base.is_name(&SymbolType::ElIf) || - base.is_name(&SymbolType::Else))) || + if (tb.is_name(&TokenType::If) && + (base.is_name(&TokenType::ElIf) || + base.is_name(&TokenType::Else))) || tb.is_name(base.name().unwrap().symbol()) { return Ok(base); } else { @@ -340,7 +340,7 @@ impl Parser { } } } - SymbolType::Error{..} => { + TokenType::Error{..} => { return self.lex_error( &ctx.cur().unwrap() ); } _ => { @@ -359,12 +359,12 @@ impl Parser { return Err(DeepCrowError::eos("tag parameters")); } match ctx.cur().unwrap().symbol() { - SymbolType::Literal(s) => { + TokenType::Literal(s) => { tb.add_param( ParamValue::Literal(s.to_string()) ); ctx.next(); } - SymbolType::Token(_) | SymbolType::Sharp => { - if ctx.peek().is("tag data", |s| *s == SymbolType::Equals)? { + TokenType::Identifier(_) | TokenType::Sharp => { + if ctx.peek().is("tag data", |s| *s == TokenType::Equals)? { break; } tb.add_param( self.parse_identifier( ctx )? ); @@ -380,20 +380,20 @@ impl Parser { fn parse_identifier(&self, ctx: &mut Context ) -> Result { let mut id = Identifier::new(); - if ctx.cur().is("identifier",|s| matches!( s, SymbolType::Sharp))? { + if ctx.cur().is("identifier",|s| matches!( s, TokenType::Sharp))? { id.push( IdentifierValue::Root ); ctx.next(); } loop { - if ctx.cur().is("identifier",|s| matches!( s, SymbolType::Token(_) ))? { - if let SymbolType::Token(s) = ctx.cur().unwrap().symbol() { + if ctx.cur().is("identifier",|s| matches!( s, TokenType::Identifier(_) ))? { + if let TokenType::Identifier(s) = ctx.cur().unwrap().symbol() { id.push( IdentifierValue::Name(s.to_string()) ); } } else { return Err(DeepCrowError::parse( ctx.cur().unwrap().start().clone(), format!("Expeceted identifier, found {:?}", ctx.cur().unwrap().symbol() ) ) ); } - if !ctx.next().is("identifier seperator",|s| *s == SymbolType::Period )? { + if !ctx.next().is("identifier seperator",|s| *s == TokenType::Period )? { break; } ctx.next(); @@ -407,11 +407,11 @@ impl Parser { if ctx.cur().is_none() { return Err(DeepCrowError::eos("tag properties")); } - if let SymbolType::Token(s) = ctx.cur().unwrap().symbol() { - if ctx.peek().is_some_and(|s|s.check_type(|t| matches!(t,SymbolType::Equals))) { + if let TokenType::Identifier(s) = ctx.cur().unwrap().symbol() { + if ctx.peek().is_some_and(|s|s.check_type(|t| matches!(t,TokenType::Equals))) { ctx.next(); if let Some(sym) = ctx.next() && - let SymbolType::Literal(lv) = sym.symbol() { + let TokenType::Literal(lv) = sym.symbol() { tb.add_prop( s.to_string(), lv.to_string() ); } else { return Err(DeepCrowError::parse(Position::none(),"Expected quoted literal string".to_string())); @@ -455,7 +455,7 @@ impl<'a> EntryListConverter for EntryList<'a> { fn has_outputs(&self) -> bool { self.iter().any(|e| if let Entry::TagBuilder(tb) = e - && tb.is_name(&SymbolType::Output) { + && tb.is_name(&TokenType::Output) { true } else { false @@ -546,18 +546,18 @@ impl<'a> TagBuilder<'a> { } pub fn set_end(&mut self, end: &Symbol) -> Result<()> { - if *self.start.symbol() == SymbolType::StartFlat { - if *end.symbol() == SymbolType::EndFlat { + if *self.start.symbol() == TokenType::StartFlat { + if *end.symbol() == TokenType::EndFlat { self.tag_type = TagType::Unary; - } else if *end.symbol() == SymbolType::EndPoint { + } else if *end.symbol() == TokenType::EndPoint { self.tag_type = TagType::MultinaryOpen; } else { return Err(DeepCrowError::broken( *end.start(), "Invalid bracket token type.")); } - } else if *self.start.symbol() == SymbolType::StartPoint { - if *end.symbol() == SymbolType::EndFlat { + } else if *self.start.symbol() == TokenType::StartPoint { + if *end.symbol() == TokenType::EndFlat { self.tag_type = TagType::MultinaryClose; - } else if *end.symbol() == SymbolType::EndPoint { + } else if *end.symbol() == TokenType::EndPoint { self.tag_type = TagType::MultinaryMid; } else { return Err(DeepCrowError::broken( *end.start(), "Invalid bracket token type.")); @@ -582,18 +582,18 @@ impl<'a> TagBuilder<'a> { */ pub fn can_have_params(&self) -> bool { match self.name.unwrap().symbol() { - SymbolType::View | SymbolType::Output | SymbolType::Loop => true, - SymbolType::Show | SymbolType::If | SymbolType::ElIf | - SymbolType::Else => false, + TokenType::View | TokenType::Output | TokenType::Loop => true, + TokenType::Show | TokenType::If | TokenType::ElIf | + TokenType::Else => false, _ => false, } } pub fn can_have_expr(&self) -> bool { match self.name.unwrap().symbol() { - SymbolType::View | SymbolType::Output | SymbolType::Loop => false, - SymbolType::Show | SymbolType::If | SymbolType::ElIf | - SymbolType::Else => true, + TokenType::View | TokenType::Output | TokenType::Loop => false, + TokenType::Show | TokenType::If | TokenType::ElIf | + TokenType::Else => true, _ => false, } } @@ -611,7 +611,7 @@ impl<'a> TagBuilder<'a> { } } */ - pub fn is_name(&self, name: &SymbolType<'a>) -> bool { + pub fn is_name(&self, name: &TokenType<'a>) -> bool { if let Some(a) = self.name { a.symbol() == name } else { @@ -662,7 +662,7 @@ impl<'a> TagBuilder<'a> { pub fn build(mut self, ctx: &mut Context ) -> Result { if let Some(sym) = self.name { match sym.symbol() { - SymbolType::View => { + TokenType::View => { let name = if let ParamValue::Literal(s) = self.params.swap_remove(0) { s.to_string() } else { @@ -676,7 +676,7 @@ impl<'a> TagBuilder<'a> { layout: self.props.get("layout").cloned(), })) } - SymbolType::Output => { + TokenType::Output => { let name = if let ParamValue::Literal(s) = self.params.swap_remove(0) { s.to_string() } else { @@ -687,7 +687,7 @@ impl<'a> TagBuilder<'a> { code: self.children.to_tokens(ctx)?, })) } - SymbolType::Loop => { + TokenType::Loop => { let id = if let ParamValue::Identifier(id) = self.params.swap_remove(0) { id @@ -701,7 +701,7 @@ impl<'a> TagBuilder<'a> { Token::Loop(id, self.children.to_tokens(ctx)?) )) } - SymbolType::Show => { + TokenType::Show => { let id = if let ParamValue::Identifier(id) = self.params.swap_remove(0) { id @@ -713,7 +713,7 @@ impl<'a> TagBuilder<'a> { }; Ok(BuildResult::Token(Token::Show(id, self.props))) } - SymbolType::If => { + TokenType::If => { let id = if let ParamValue::Identifier(id) = self.params.swap_remove(0) { id @@ -726,7 +726,7 @@ impl<'a> TagBuilder<'a> { let is_else = if self.chain.len() == 1 && let Entry::TagBuilder(tb) = &self.chain[0] && - tb.is_name(&SymbolType::Else) && + tb.is_name(&TokenType::Else) && tb.params.len() == 0 { true } else { @@ -740,7 +740,7 @@ impl<'a> TagBuilder<'a> { } Ok(BuildResult::Token(Token::If(id, self.children.to_tokens(ctx)?, self.chain.to_tokens(ctx)?))) } - SymbolType::ElIf => { + TokenType::ElIf => { let id = if let ParamValue::Identifier(id) = self.params.swap_remove(0) { id } else { @@ -755,7 +755,7 @@ impl<'a> TagBuilder<'a> { // ...but I can't do that right now. let is_else = if self.chain.len() == 1 && let Entry::TagBuilder(tb) = &self.chain[0] && - tb.is_name(&SymbolType::Else) && + tb.is_name(&TokenType::Else) && tb.params.len() == 0 { true } else { @@ -771,7 +771,7 @@ impl<'a> TagBuilder<'a> { Ok(BuildResult::Token(Token::If(id, self.children.to_tokens(ctx)?, self.chain.to_tokens(ctx)?))) } - SymbolType::Else => { + TokenType::Else => { Ok(BuildResult::Token(Token::If(Identifier::new(), self.children.to_tokens(ctx)?, self.chain.to_tokens(ctx)?))) } _ => { -- cgit v1.2.3