From 12c7b851d866401ac4a32c746ed237b051aa3dd3 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 20 Aug 2026 08:16:58 -0700 Subject: Making progress cleaning up. --- deepcrow/src/lexer.rs | 105 +++++++++--------------- deepcrow/src/lib.rs | 20 ++--- deepcrow/src/parser.rs | 217 ++++++++++++++++++++++++------------------------- 3 files changed, 155 insertions(+), 187 deletions(-) (limited to 'deepcrow/src') diff --git a/deepcrow/src/lexer.rs b/deepcrow/src/lexer.rs index 2528cb6..98fc1c7 100644 --- a/deepcrow/src/lexer.rs +++ b/deepcrow/src/lexer.rs @@ -3,7 +3,7 @@ use std::iter::Iterator; use std::str::CharIndices; use std::fmt; -use crate::Position; +use crate::{Position,Range}; #[derive(Debug,Copy,Clone,PartialEq,Eq)] pub enum ErrorType { @@ -17,60 +17,45 @@ pub enum TokenType<'a> { EndFlat, EndPoint, - View, - Output, - Show, - Loop, - If, - ElIf, - Else, - Sharp, Equals, Period, Identifier(&'a str), - Literal(&'a str), + LiteralStr(&'a str), Text(&'a str), Error{ what: ErrorType }, } #[derive(Copy,Clone)] -pub struct Symbol<'a> { - symbol: TokenType<'a>, - start: Position, - end: Position, +pub struct Token<'a> { + token: TokenType<'a>, + pos: Range, } -impl<'a> fmt::Debug for Symbol<'a> { +impl<'a> fmt::Debug for Token<'a> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{:?} @ {:?}-{:?}", self.symbol, self.start, self.end ) + write!(f, "{:?} @ {:?}", self.token, self.pos ) } } -impl<'a> Symbol<'a> { - pub fn new( symbol: TokenType<'a>, start: Position, end: Position ) -> Self { +impl<'a> Token<'a> { + pub fn new( token: TokenType<'a>, start: Position, end: Position ) -> Self { Self { - symbol, start, end, + token, Range::new( start, end ), } } pub fn check_type bool>(&self, f: T ) -> bool { - f( &self.symbol ) - } - - pub fn symbol(&self) -> &TokenType<'a> { - &self.symbol + f( &self.token ) } - pub fn start(&self) -> &Position { - &self.start + pub fn token(&self) -> &TokenType<'a> { + &self.token } - /* - pub fn end(&self) -> &Position { - &self.end + pub fn pos(&self) -> &Range { + &self.pos } - */ } #[derive(Debug)] @@ -81,9 +66,7 @@ enum Mode { pub struct Lexer<'a> { data: &'a str, - chars: CharIndices<'a>, - cur: [Option<(usize, char)>;2], - icur: usize, + src: LookAhead>, mode: Mode, pos: Position, } @@ -107,15 +90,10 @@ fn is_valid_token_char( c: char, first: bool ) -> bool { impl<'a> Lexer<'a> { pub fn new( data: &'a str ) -> Lexer<'a> { - let mut chars = data.char_indices(); - let cur = chars.next(); - let cur2 = chars.next(); - //println!(" - cur: {:?}, peek: {:?}", cur, cur2 ); + let mut chars = ; Lexer { data, - chars, - cur: [cur, cur2], - icur: 0, + src: LookAhead::new(data.char_indices()), mode: Mode::Text, pos: Position::new(1,1), } @@ -170,8 +148,8 @@ impl<'a> Lexer<'a> { } } - fn error( &self, what: ErrorType ) -> Option> { - Some(Symbol::new( TokenType::Error { + fn error( &self, what: ErrorType ) -> Option> { + Some(Token::new( TokenType::Error { what: what }, self.pos, self.pos )) } @@ -182,7 +160,7 @@ impl<'a> Lexer<'a> { } } - fn next_symbol(&mut self) -> Option> { + fn next_symbol(&mut self) -> Option> { // If we hit the end then we're already done. if self.cur().is_none() { return None; @@ -206,7 +184,7 @@ impl<'a> Lexer<'a> { } } - fn parse_text(&mut self) -> Option> { + fn parse_text(&mut self) -> Option> { let start = self.cur_index(); let start_pos = self.pos.clone(); while self.next().is_some() && !self.is_start_tag() { } @@ -217,11 +195,11 @@ impl<'a> Lexer<'a> { if start == end { None } else { - Some(Symbol::new( TokenType::Text(s), start_pos, end_pos )) + Some(Token::new( TokenType::Text(s), start_pos, end_pos )) } } - fn parse_token(&mut self) -> Option> { + fn parse_token(&mut self) -> Option> { self.skip_ws(); match self.cur() { Some('"') => { @@ -229,15 +207,15 @@ impl<'a> Lexer<'a> { } Some('#') => { self.next(); - return Some(Symbol::new(TokenType::Sharp, self.pos, self.pos)); + return Some(Token::new(TokenType::Sharp, self.pos, self.pos)); } Some('=') => { self.next(); - return Some(Symbol::new(TokenType::Equals, self.pos, self.pos)); + return Some(Token::new(TokenType::Equals, self.pos, self.pos)); } Some('.') => { self.next(); - return Some(Symbol::new(TokenType::Period, self.pos, self.pos)); + return Some(Token::new(TokenType::Period, self.pos, self.pos)); } _ => {} } @@ -253,20 +231,11 @@ impl<'a> Lexer<'a> { if start == end { None } else { - Some(Symbol::new( match s { - "view" => TokenType::View, - "show" => TokenType::Show, - "output" => TokenType::Output, - "loop" => TokenType::Loop, - "if" => TokenType::If, - "elif" => TokenType::ElIf, - "else" => TokenType::Else, - _ => TokenType::Identifier(s) - }, start_pos, end_pos )) + Some(Token::new( TokenType::Identifier(s), start_pos, end_pos )) } } - fn parse_literal_str(&mut self) -> Option> { + fn parse_literal_str(&mut self) -> Option> { if let Some(chr) = self.cur() && chr != '"' { return self.error( ErrorType::UnexpectedChar(chr) ); } @@ -277,7 +246,7 @@ impl<'a> Lexer<'a> { let end_pos = self.pos.clone(); self.next(); let s = &self.data[start..end]; - Some(Symbol::new(TokenType::Literal(s), start_pos, end_pos )) + Some(Token::new(TokenType::LiteralStr(s), start_pos, end_pos )) } fn is_start_tag(&mut self) -> bool { @@ -289,7 +258,7 @@ impl<'a> Lexer<'a> { } } - fn parse_start_tag(&mut self) -> Option> { + fn parse_start_tag(&mut self) -> Option> { let start_pos = self.pos.clone(); match self.cur() { Some('[') => { @@ -298,7 +267,7 @@ impl<'a> Lexer<'a> { let end_pos = self.pos.clone(); self.next(); self.mode = Mode::InTag; - Some(Symbol::new( TokenType::StartFlat, start_pos, end_pos ) ) + Some(Token::new( TokenType::StartFlat, start_pos, end_pos ) ) } else { None } @@ -309,7 +278,7 @@ impl<'a> Lexer<'a> { let end_pos = self.pos.clone(); self.next(); self.mode = Mode::InTag; - Some(Symbol::new(TokenType::StartPoint, start_pos, end_pos ) ) + Some(Token::new(TokenType::StartPoint, start_pos, end_pos ) ) } else { None } @@ -329,7 +298,7 @@ impl<'a> Lexer<'a> { } } - fn parse_end_tag(&mut self) -> Option> { + fn parse_end_tag(&mut self) -> Option> { self.skip_ws(); let start_pos = self.pos.clone(); if let Some(chr) = self.cur() && chr == '|' { @@ -339,14 +308,14 @@ impl<'a> Lexer<'a> { let end_pos = self.pos.clone(); self.next(); self.mode = Mode::Text; - Some(Symbol::new( TokenType::EndFlat, start_pos, end_pos)) + Some(Token::new( TokenType::EndFlat, start_pos, end_pos)) } Some('>') => { self.next(); let end_pos = self.pos.clone(); self.next(); self.mode = Mode::Text; - Some(Symbol::new( TokenType::EndPoint, start_pos, end_pos)) + Some(Token::new( TokenType::EndPoint, start_pos, end_pos)) } _ => { None @@ -359,7 +328,7 @@ impl<'a> Lexer<'a> { } impl<'a> Iterator for Lexer<'a> { - type Item = Symbol<'a>; + type Item = Token<'a>; fn next(&mut self) -> Option { self.next_symbol() diff --git a/deepcrow/src/lib.rs b/deepcrow/src/lib.rs index b9a4ffe..969adf0 100644 --- a/deepcrow/src/lib.rs +++ b/deepcrow/src/lib.rs @@ -58,16 +58,16 @@ impl View { #[derive(Debug)] struct Output { name: String, - code: Vec, + code: Vec, } type Properties = HashMap; #[derive(Debug)] -enum Token { - Loop(Identifier, Vec), +enum Symbol { + Loop(Identifier, Vec), Show(Identifier,Properties), - If(Identifier, Vec, Vec), + If(Identifier, Vec, Vec), Text(String), } @@ -175,7 +175,7 @@ impl DeepCrow { pub fn render(&self, view: &str, input: &dyn for<'a> MappedStructure<'a> ) -> Result { if let Some(view) = self.views.get( view ) { - //println!("::Token tree::\n{:?}", view ); + //println!("::Symbol tree::\n{:?}", view ); let mut output = view.process( input )?; if let Some(l) = &view.layout { self.render( l, &output ) @@ -195,10 +195,10 @@ impl DeepCrow { } } -fn exec<'a>(input: &StatefulContext<'a>, tokens: &Vec, buf: &mut String) -> Result<()> { +fn exec<'a>(input: &StatefulContext<'a>, tokens: &Vec, buf: &mut String) -> Result<()> { for token in tokens { match token { - Token::Loop(ident,code) => { + Symbol::Loop(ident,code) => { //println!("!!! Loop over: {:?} {:?}", ident, input.get_value( &ident )); if let Some(value) = input.get_value( &ident ) && let MappedValue::List(list) = value { @@ -213,7 +213,7 @@ fn exec<'a>(input: &StatefulContext<'a>, tokens: &Vec, buf: &mut String) buf.push_str( &output ); } } - Token::If(ident,code,other) => { + Symbol::If(ident,code,other) => { if let Some(value) = input.get_value( &ident ) && let MappedValue::Bool(b) = value && b { exec(input, code, buf)? @@ -221,7 +221,7 @@ fn exec<'a>(input: &StatefulContext<'a>, tokens: &Vec, buf: &mut String) exec(input, other, buf)? } } - Token::Show(ident,p) => { + Symbol::Show(ident,p) => { let format = if let Some(s) = p.get("format") { s } else { @@ -289,7 +289,7 @@ fn exec<'a>(input: &StatefulContext<'a>, tokens: &Vec, buf: &mut String) } } } - Token::Text(s) => { + Symbol::Text(s) => { buf.push_str( s ); } //_ => {} diff --git a/deepcrow/src/parser.rs b/deepcrow/src/parser.rs index 529c58e..5c6680e 100644 --- a/deepcrow/src/parser.rs +++ b/deepcrow/src/parser.rs @@ -36,9 +36,9 @@ 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) -> CrimResult { if self.cur().is_none() || self.peek().is_none() { - Err(DeepCrowError::eos( context )) + Err(CrimError::eos( context )) } else { Ok(f( self.cur().unwrap().symbol(), self.peek().unwrap().symbol())) } @@ -51,35 +51,34 @@ impl<'a> Context<'a> { } trait SymbolHelper { - fn is bool>(&self, context: &str, f: T ) -> Result; + fn is bool>(&self, context: &str, f: T ) -> CrimResult; #[allow(dead_code)] - fn is_valid_tag_name(&self) -> Result; + fn is_valid_tag_name(&self) -> CrimResult; } impl<'a> SymbolHelper for Option> { - fn is bool>(&self, context: &str, f: T ) -> Result { + fn is bool>(&self, context: &str, f: T ) -> CrimResult { if let Some(st) = self { Ok(f( &st.symbol() )) } else { - Err(DeepCrowError::eos( context )) + Err(CrimError::eos( context )) } } - fn is_valid_tag_name(&self) -> Result { + fn is_valid_tag_name(&self) -> CrimResult { if let Some(st) = self { Ok(match st.symbol() { - TokenType::Identifier(_) | - TokenType::View | - TokenType::Output | - TokenType::Loop | - TokenType::If | - TokenType::ElIf | - TokenType::Else | - TokenType::Show => true, + TokenType::Identifier("view") | + TokenType::Identifier("output") | + TokenType::Identifier("loop") | + TokenType::Identifier("if") | + TokenType::Identifier("elif") | + TokenType::Identifier("else") | + TokenType::Identifier("show") => true, _ => false }) } else { - Err(DeepCrowError::parse( + Err(CrimError::parse( Position::none(), "Unexpeceted end of stream.".to_string() )) @@ -183,17 +182,17 @@ impl Parser { } } - fn lex_error(&self, error: &Symbol) -> Result { - if let TokenType::Error{what} = error.symbol() { - Err(DeepCrowError::parse( *error.start(), + fn lex_error(&self, error: &Symbol) -> CrimResult { + if let SymbolType::Error{what} = error.symbol() { + Err(CrimError::parse( *error.start(), format!("What: {:?}", *what) )) } else { - Err(DeepCrowError::parse( Position::none(), "Not an error?".into())) + Err(CrimError::parse( Position::none(), "Not an error?".into())) } } - pub fn parse(&self, src: &str, source: usize ) -> Result> { + pub fn parse(&self, src: &str, source: usize ) -> CrimResult> { let ll = Lexer::new( src ); let mut ctx = Context::new( ll, source ); @@ -201,24 +200,24 @@ impl Parser { self.p_input( &mut ctx ) } - fn p_input(&self, ctx: &mut Context ) -> Result> { + fn p_input(&self, ctx: &mut Context ) -> CrimResult> { let mut tags = Vec::new(); loop { if ctx.cur().is_none() { break; } match ctx.cur().unwrap().symbol() { - TokenType::Text(_) => { /* Skip top level text */ } - TokenType::StartFlat => { + SymbolType::Text(_) => { /* Skip top level text */ } + SymbolType::StartFlat => { let tb = self.parse_tag( ctx )?; let tb = self.parse_tag_set( ctx, tb )?; tags.push( tb ); } - TokenType::Error{..} => { + SymbolType::Error{..} => { self.lex_error( &ctx.cur().unwrap() )?; } _ => { - return Err(DeepCrowError::parse( + return Err(CrimError::parse( *ctx.cur().unwrap().start(), format!("Unexpected symbol {:?} found looking for tags.", ctx.cur().unwrap().symbol() ) )); @@ -231,11 +230,11 @@ impl Parser { for tag in tags { let start_pos = tag.start_pos(); let name = tag.name().clone(); - if tag.is_name(&TokenType::View) && + if tag.is_name(&SymbolType::View) && let BuildResult::View(view) = tag.build( ctx )? { views.push( view ); } else { - return Err(DeepCrowError::parse( + return Err(CrimError::parse( start_pos, format!("Expected view at root, found {:?}", name ) )); @@ -244,28 +243,28 @@ impl Parser { Ok(views) } - fn parse_tag<'a>(&self, ctx: &mut Context<'a>) -> Result> { + fn parse_tag<'a>(&self, ctx: &mut Context<'a>) -> CrimResult> { let start_sym = ctx.cur().unwrap(); let mut tb = TagBuilder::new(&start_sym); if ctx.next().is_some() { match ctx.cur().unwrap().symbol() { - TokenType::View | TokenType::Show | TokenType::Loop | - TokenType::If | TokenType::ElIf | TokenType::Else | - TokenType::Output => { + SymbolType::View | SymbolType::Show | SymbolType::Loop | + SymbolType::If | SymbolType::ElIf | SymbolType::Else | + SymbolType::Output => { let name_sym = ctx.cur().unwrap(); ctx.next(); tb.set_name( name_sym ); } _ => { - return Err(DeepCrowError::parse( + return Err(CrimError::parse( *ctx.cur().unwrap().start(), "Unexpected symbol".to_string() )); } } } else { - return Err(DeepCrowError::eos("tag type")); + return Err(CrimError::eos("tag type")); } if tb.can_have_params() || tb.can_have_expr() { @@ -277,11 +276,11 @@ impl Parser { if let Some(end_sym) = ctx.cur() { match end_sym.symbol() { - TokenType::EndPoint | TokenType::EndFlat => { + SymbolType::EndPoint | SymbolType::EndFlat => { tb.set_end( &end_sym )?; } _ => { - return Err(DeepCrowError::parse( + return Err(CrimError::parse( *end_sym.start(), format!("Unexpected symbol {:?} looking for end of tag.", end_sym.symbol()) )); @@ -294,21 +293,21 @@ impl Parser { Ok(tb) } - fn parse_tag_set<'a>(&self, ctx: &mut Context<'a>, mut base: TagBuilder<'a>) -> Result> { + fn parse_tag_set<'a>(&self, ctx: &mut Context<'a>, mut base: TagBuilder<'a>) -> CrimResult> { if base.is_unary() { return Ok(base); } loop { if ctx.cur().is_none() { - return Err(DeepCrowError::eos(format!("close tag for {:?}", base.name()).as_str())); + return Err(CrimError::eos(format!("close tag for {:?}", base.name()).as_str())); } match ctx.cur().unwrap().symbol() { - TokenType::Text(s) => { + SymbolType::Text(s) => { base.add_child(Entry::Token(Token::Text(s.to_string()))); ctx.next(); } - TokenType::StartFlat | TokenType::StartPoint => { + SymbolType::StartFlat | SymbolType::StartPoint => { let tb = self.parse_tag( ctx )?; match tb.tag_type() { @@ -320,13 +319,13 @@ impl Parser { return Ok(base); } TagType::MultinaryClose => { - if (tb.is_name(&TokenType::If) && - (base.is_name(&TokenType::ElIf) || - base.is_name(&TokenType::Else))) || + if (tb.is_name(&SymbolType::If) && + (base.is_name(&SymbolType::ElIf) || + base.is_name(&SymbolType::Else))) || tb.is_name(base.name().unwrap().symbol()) { return Ok(base); } else { - return Err(DeepCrowError::parse( + return Err(CrimError::parse( *tb.name().unwrap().start(), format!("Found {:?} looking for close of {:?}", tb.name().unwrap().symbol(), base.name().unwrap().symbol()) )); @@ -336,15 +335,15 @@ impl Parser { base.add_child(Entry::TagBuilder(tb)); } TagType::Unknown => { - return Err(DeepCrowError::broken(base.start_pos(), "Found unknown tag when looking for something else.")); + return Err(CrimError::broken(base.start_pos(), "Found unknown tag when looking for something else.")); } } } - TokenType::Error{..} => { + SymbolType::Error{..} => { return self.lex_error( &ctx.cur().unwrap() ); } _ => { - return Err(DeepCrowError::parse( + return Err(CrimError::parse( *ctx.cur().unwrap().start(), format!("Unexpected token: {:?}", ctx.cur().unwrap().symbol()), )); @@ -353,18 +352,18 @@ impl Parser { } } - fn parse_tag_params(&self, ctx: &mut Context, tb: &mut TagBuilder ) -> Result<()> { + fn parse_tag_params(&self, ctx: &mut Context, tb: &mut TagBuilder ) -> CrimResult<()> { loop { if ctx.cur().is_none() { - return Err(DeepCrowError::eos("tag parameters")); + return Err(CrimError::eos("tag parameters")); } match ctx.cur().unwrap().symbol() { - TokenType::Literal(s) => { - tb.add_param( ParamValue::Literal(s.to_string()) ); + SymbolType::LiteralStr(s) => { + tb.add_param( ParamValue::LiteralStr(s.to_string()) ); ctx.next(); } - TokenType::Identifier(_) | TokenType::Sharp => { - if ctx.peek().is("tag data", |s| *s == TokenType::Equals)? { + SymbolType::Token(_) | SymbolType::Sharp => { + if ctx.peek().is("tag data", |s| *s == SymbolType::Equals)? { break; } tb.add_param( self.parse_identifier( ctx )? ); @@ -377,23 +376,23 @@ impl Parser { Ok(()) } - fn parse_identifier(&self, ctx: &mut Context ) -> Result { + fn parse_identifier(&self, ctx: &mut Context ) -> CrimResult { let mut id = Identifier::new(); - if ctx.cur().is("identifier",|s| matches!( s, TokenType::Sharp))? { + if ctx.cur().is("identifier",|s| matches!( s, SymbolType::Sharp))? { id.push( IdentifierValue::Root ); ctx.next(); } loop { - if ctx.cur().is("identifier",|s| matches!( s, TokenType::Identifier(_) ))? { - if let TokenType::Identifier(s) = ctx.cur().unwrap().symbol() { + if ctx.cur().is("identifier",|s| matches!( s, SymbolType::Token(_) ))? { + if let SymbolType::Token(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() ) ) ); + return Err(CrimError::parse( ctx.cur().unwrap().start().clone(), format!("Expeceted identifier, found {:?}", ctx.cur().unwrap().symbol() ) ) ); } - if !ctx.next().is("identifier seperator",|s| *s == TokenType::Period )? { + if !ctx.next().is("identifier seperator",|s| *s == SymbolType::Period )? { break; } ctx.next(); @@ -402,19 +401,19 @@ impl Parser { Ok(ParamValue::Identifier(id)) } - fn parse_tag_props(&self, ctx: &mut Context, tb: &mut TagBuilder ) -> Result<()> { + fn parse_tag_props(&self, ctx: &mut Context, tb: &mut TagBuilder ) -> CrimResult<()> { loop { if ctx.cur().is_none() { - return Err(DeepCrowError::eos("tag properties")); + return Err(CrimError::eos("tag properties")); } - if let TokenType::Identifier(s) = ctx.cur().unwrap().symbol() { - if ctx.peek().is_some_and(|s|s.check_type(|t| matches!(t,TokenType::Equals))) { + if let SymbolType::Token(s) = ctx.cur().unwrap().symbol() { + if ctx.peek().is_some_and(|s|s.check_type(|t| matches!(t,SymbolType::Equals))) { ctx.next(); if let Some(sym) = ctx.next() && - let TokenType::Literal(lv) = sym.symbol() { + let SymbolType::LiteralStr(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())); + return Err(CrimError::parse(Position::none(),"Expected quoted literal string".to_string())); } } else { break; @@ -447,15 +446,15 @@ type EntryList<'a> = Vec>; trait EntryListConverter { fn has_outputs(&self) -> bool; - fn to_outputs(&mut self, ctx: &mut Context ) -> Result>; - fn to_tokens(&mut self, ctx: &mut Context) -> Result>; + fn to_outputs(&mut self, ctx: &mut Context ) -> CrimResult>; + fn to_tokens(&mut self, ctx: &mut Context) -> CrimResult>; } impl<'a> EntryListConverter for EntryList<'a> { fn has_outputs(&self) -> bool { self.iter().any(|e| if let Entry::TagBuilder(tb) = e - && tb.is_name(&TokenType::Output) { + && tb.is_name(&SymbolType::Output) { true } else { false @@ -463,7 +462,7 @@ impl<'a> EntryListConverter for EntryList<'a> { ) } - fn to_outputs(&mut self, ctx: &mut Context) -> Result> { + fn to_outputs(&mut self, ctx: &mut Context) -> CrimResult> { let mut outputs = Vec::new(); if self.has_outputs() { @@ -489,7 +488,7 @@ impl<'a> EntryListConverter for EntryList<'a> { Ok(outputs) } - fn to_tokens(&mut self, ctx: &mut Context) -> Result> { + fn to_tokens(&mut self, ctx: &mut Context) -> CrimResult> { let mut tokens : Vec = Vec::new(); for e in self.drain(..) { match e { @@ -500,7 +499,7 @@ impl<'a> EntryListConverter for EntryList<'a> { if let BuildResult::Token(token) = tb.build(ctx)? { tokens.push( token ); } else { - return Err(DeepCrowError::broken( Position::none(), "Non-token result built.")); + return Err(CrimError::broken( Position::none(), "Non-token result built.")); } } } @@ -522,7 +521,7 @@ struct TagBuilder<'a> { #[derive(Debug)] enum ParamValue { - Literal(String), + LiteralStr(String), Identifier(Identifier), } @@ -545,25 +544,25 @@ impl<'a> TagBuilder<'a> { } } - pub fn set_end(&mut self, end: &Symbol) -> Result<()> { - if *self.start.symbol() == TokenType::StartFlat { - if *end.symbol() == TokenType::EndFlat { + pub fn set_end(&mut self, end: &Symbol) -> CrimResult<()> { + if *self.start.symbol() == SymbolType::StartFlat { + if *end.symbol() == SymbolType::EndFlat { self.tag_type = TagType::Unary; - } else if *end.symbol() == TokenType::EndPoint { + } else if *end.symbol() == SymbolType::EndPoint { self.tag_type = TagType::MultinaryOpen; } else { - return Err(DeepCrowError::broken( *end.start(), "Invalid bracket token type.")); + return Err(CrimError::broken( *end.start(), "Invalid bracket token type.")); } - } else if *self.start.symbol() == TokenType::StartPoint { - if *end.symbol() == TokenType::EndFlat { + } else if *self.start.symbol() == SymbolType::StartPoint { + if *end.symbol() == SymbolType::EndFlat { self.tag_type = TagType::MultinaryClose; - } else if *end.symbol() == TokenType::EndPoint { + } else if *end.symbol() == SymbolType::EndPoint { self.tag_type = TagType::MultinaryMid; } else { - return Err(DeepCrowError::broken( *end.start(), "Invalid bracket token type.")); + return Err(CrimError::broken( *end.start(), "Invalid bracket token type.")); } } else { - return Err(DeepCrowError::broken( *self.start.start(), "Invalid bracket token type.")); + return Err(CrimError::broken( *self.start.start(), "Invalid bracket token type.")); } Ok(()) } @@ -582,18 +581,18 @@ impl<'a> TagBuilder<'a> { */ pub fn can_have_params(&self) -> bool { match self.name.unwrap().symbol() { - TokenType::View | TokenType::Output | TokenType::Loop => true, - TokenType::Show | TokenType::If | TokenType::ElIf | - TokenType::Else => false, + SymbolType::View | SymbolType::Output | SymbolType::Loop => true, + SymbolType::Show | SymbolType::If | SymbolType::ElIf | + SymbolType::Else => false, _ => false, } } pub fn can_have_expr(&self) -> bool { match self.name.unwrap().symbol() { - TokenType::View | TokenType::Output | TokenType::Loop => false, - TokenType::Show | TokenType::If | TokenType::ElIf | - TokenType::Else => true, + SymbolType::View | SymbolType::Output | SymbolType::Loop => false, + SymbolType::Show | SymbolType::If | SymbolType::ElIf | + SymbolType::Else => true, _ => false, } } @@ -611,7 +610,7 @@ impl<'a> TagBuilder<'a> { } } */ - pub fn is_name(&self, name: &TokenType<'a>) -> bool { + pub fn is_name(&self, name: &SymbolType<'a>) -> bool { if let Some(a) = self.name { a.symbol() == name } else { @@ -659,14 +658,14 @@ impl<'a> TagBuilder<'a> { self.tag_type } - pub fn build(mut self, ctx: &mut Context ) -> Result { + pub fn build(mut self, ctx: &mut Context ) -> CrimResult { if let Some(sym) = self.name { match sym.symbol() { - TokenType::View => { - let name = if let ParamValue::Literal(s) = self.params.swap_remove(0) { + SymbolType::View => { + let name = if let ParamValue::LiteralStr(s) = self.params.swap_remove(0) { s.to_string() } else { - return Err(DeepCrowError::parse(Position::none(), "Expected string literal for view name.".to_string())); + return Err(CrimError::parse(Position::none(), "Expected string literal for view name.".to_string())); }; Ok(BuildResult::View(View { name: name, @@ -676,23 +675,23 @@ impl<'a> TagBuilder<'a> { layout: self.props.get("layout").cloned(), })) } - TokenType::Output => { - let name = if let ParamValue::Literal(s) = self.params.swap_remove(0) { + SymbolType::Output => { + let name = if let ParamValue::LiteralStr(s) = self.params.swap_remove(0) { s.to_string() } else { - return Err(DeepCrowError::parse(Position::none(), "Expected string literal for output name.".into())); + return Err(CrimError::parse(Position::none(), "Expected string literal for output name.".into())); }; Ok(BuildResult::Output(Output { name: name, code: self.children.to_tokens(ctx)?, })) } - TokenType::Loop => { + SymbolType::Loop => { let id = if let ParamValue::Identifier(id) = self.params.swap_remove(0) { id } else { - return Err(DeepCrowError::parse( + return Err(CrimError::parse( Position::none(), "Identifier for loop variable name.".into()) ); @@ -701,24 +700,24 @@ impl<'a> TagBuilder<'a> { Token::Loop(id, self.children.to_tokens(ctx)?) )) } - TokenType::Show => { + SymbolType::Show => { let id = if let ParamValue::Identifier(id) = self.params.swap_remove(0) { id } else { - return Err(DeepCrowError::parse( + return Err(CrimError::parse( Position::none(), "Identifier for show variable name.".into()) ); }; Ok(BuildResult::Token(Token::Show(id, self.props))) } - TokenType::If => { + SymbolType::If => { let id = if let ParamValue::Identifier(id) = self.params.swap_remove(0) { id } else { - return Err(DeepCrowError::parse( + return Err(CrimError::parse( Position::none(), "Identifier for if variable name.".into()) ); @@ -726,7 +725,7 @@ impl<'a> TagBuilder<'a> { let is_else = if self.chain.len() == 1 && let Entry::TagBuilder(tb) = &self.chain[0] && - tb.is_name(&TokenType::Else) && + tb.is_name(&SymbolType::Else) && tb.params.len() == 0 { true } else { @@ -740,11 +739,11 @@ impl<'a> TagBuilder<'a> { } Ok(BuildResult::Token(Token::If(id, self.children.to_tokens(ctx)?, self.chain.to_tokens(ctx)?))) } - TokenType::ElIf => { + SymbolType::ElIf => { let id = if let ParamValue::Identifier(id) = self.params.swap_remove(0) { id } else { - return Err(DeepCrowError::parse( + return Err(CrimError::parse( Position::none(), "Identifier for elif variable name.".into()) ); @@ -755,7 +754,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(&TokenType::Else) && + tb.is_name(&SymbolType::Else) && tb.params.len() == 0 { true } else { @@ -771,15 +770,15 @@ impl<'a> TagBuilder<'a> { Ok(BuildResult::Token(Token::If(id, self.children.to_tokens(ctx)?, self.chain.to_tokens(ctx)?))) } - TokenType::Else => { + SymbolType::Else => { Ok(BuildResult::Token(Token::If(Identifier::new(), self.children.to_tokens(ctx)?, self.chain.to_tokens(ctx)?))) } _ => { - Err(DeepCrowError::parse( self.start_pos(), "Bad tag type".into())) + Err(CrimError::parse( self.start_pos(), "Bad tag type".into())) } } } else { - Err(DeepCrowError::parse(self.start_pos(), "Bad tag type".into())) + Err(CrimError::parse(self.start_pos(), "Bad tag type".into())) } } } -- cgit v1.2.3