diff options
| author | Mike Buland <mike@xagasoft.com> | 2026-08-20 08:16:58 -0700 |
|---|---|---|
| committer | Mike Buland <mike@xagasoft.com> | 2026-08-20 08:16:58 -0700 |
| commit | 12c7b851d866401ac4a32c746ed237b051aa3dd3 (patch) | |
| tree | d8c13033a295f9caa7d9ee388ca64272d5dcfa15 /deepcrow | |
| parent | d1e78b4a3454984db74ebf2a302a64b985ce6de4 (diff) | |
| download | deepcrow-12c7b851d866401ac4a32c746ed237b051aa3dd3.tar.gz deepcrow-12c7b851d866401ac4a32c746ed237b051aa3dd3.tar.bz2 deepcrow-12c7b851d866401ac4a32c746ed237b051aa3dd3.tar.xz deepcrow-12c7b851d866401ac4a32c746ed237b051aa3dd3.zip | |
Diffstat (limited to '')
| -rw-r--r-- | deepcrow/src/lexer.rs | 105 | ||||
| -rw-r--r-- | deepcrow/src/lib.rs | 20 | ||||
| -rw-r--r-- | deepcrow/src/parser.rs | 217 |
3 files changed, 155 insertions, 187 deletions
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; | |||
| 3 | use std::str::CharIndices; | 3 | use std::str::CharIndices; |
| 4 | use std::fmt; | 4 | use std::fmt; |
| 5 | 5 | ||
| 6 | use crate::Position; | 6 | use crate::{Position,Range}; |
| 7 | 7 | ||
| 8 | #[derive(Debug,Copy,Clone,PartialEq,Eq)] | 8 | #[derive(Debug,Copy,Clone,PartialEq,Eq)] |
| 9 | pub enum ErrorType { | 9 | pub enum ErrorType { |
| @@ -17,60 +17,45 @@ pub enum TokenType<'a> { | |||
| 17 | EndFlat, | 17 | EndFlat, |
| 18 | EndPoint, | 18 | EndPoint, |
| 19 | 19 | ||
| 20 | View, | ||
| 21 | Output, | ||
| 22 | Show, | ||
| 23 | Loop, | ||
| 24 | If, | ||
| 25 | ElIf, | ||
| 26 | Else, | ||
| 27 | |||
| 28 | Sharp, | 20 | Sharp, |
| 29 | Equals, | 21 | Equals, |
| 30 | Period, | 22 | Period, |
| 31 | Identifier(&'a str), | 23 | Identifier(&'a str), |
| 32 | Literal(&'a str), | 24 | LiteralStr(&'a str), |
| 33 | Text(&'a str), | 25 | Text(&'a str), |
| 34 | Error{ what: ErrorType }, | 26 | Error{ what: ErrorType }, |
| 35 | } | 27 | } |
| 36 | 28 | ||
| 37 | #[derive(Copy,Clone)] | 29 | #[derive(Copy,Clone)] |
| 38 | pub struct Symbol<'a> { | 30 | pub struct Token<'a> { |
| 39 | symbol: TokenType<'a>, | 31 | token: TokenType<'a>, |
| 40 | start: Position, | 32 | pos: Range, |
| 41 | end: Position, | ||
| 42 | } | 33 | } |
| 43 | 34 | ||
| 44 | impl<'a> fmt::Debug for Symbol<'a> { | 35 | impl<'a> fmt::Debug for Token<'a> { |
| 45 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 36 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 46 | write!(f, "{:?} @ {:?}-{:?}", self.symbol, self.start, self.end ) | 37 | write!(f, "{:?} @ {:?}", self.token, self.pos ) |
| 47 | } | 38 | } |
| 48 | } | 39 | } |
| 49 | 40 | ||
| 50 | impl<'a> Symbol<'a> { | 41 | impl<'a> Token<'a> { |
| 51 | pub fn new( symbol: TokenType<'a>, start: Position, end: Position ) -> Self { | 42 | pub fn new( token: TokenType<'a>, start: Position, end: Position ) -> Self { |
| 52 | Self { | 43 | Self { |
| 53 | symbol, start, end, | 44 | token, Range::new( start, end ), |
| 54 | } | 45 | } |
| 55 | } | 46 | } |
| 56 | 47 | ||
| 57 | pub fn check_type<T: Fn( &TokenType ) -> bool>(&self, f: T ) -> bool { | 48 | pub fn check_type<T: Fn( &TokenType ) -> bool>(&self, f: T ) -> bool { |
| 58 | f( &self.symbol ) | 49 | f( &self.token ) |
| 59 | } | ||
| 60 | |||
| 61 | pub fn symbol(&self) -> &TokenType<'a> { | ||
| 62 | &self.symbol | ||
| 63 | } | 50 | } |
| 64 | 51 | ||
| 65 | pub fn start(&self) -> &Position { | 52 | pub fn token(&self) -> &TokenType<'a> { |
| 66 | &self.start | 53 | &self.token |
| 67 | } | 54 | } |
| 68 | 55 | ||
| 69 | /* | 56 | pub fn pos(&self) -> &Range { |
| 70 | pub fn end(&self) -> &Position { | 57 | &self.pos |
| 71 | &self.end | ||
| 72 | } | 58 | } |
| 73 | */ | ||
| 74 | } | 59 | } |
| 75 | 60 | ||
| 76 | #[derive(Debug)] | 61 | #[derive(Debug)] |
| @@ -81,9 +66,7 @@ enum Mode { | |||
| 81 | 66 | ||
| 82 | pub struct Lexer<'a> { | 67 | pub struct Lexer<'a> { |
| 83 | data: &'a str, | 68 | data: &'a str, |
| 84 | chars: CharIndices<'a>, | 69 | src: LookAhead<CharIndices<'a>>, |
| 85 | cur: [Option<(usize, char)>;2], | ||
| 86 | icur: usize, | ||
| 87 | mode: Mode, | 70 | mode: Mode, |
| 88 | pos: Position, | 71 | pos: Position, |
| 89 | } | 72 | } |
| @@ -107,15 +90,10 @@ fn is_valid_token_char( c: char, first: bool ) -> bool { | |||
| 107 | 90 | ||
| 108 | impl<'a> Lexer<'a> { | 91 | impl<'a> Lexer<'a> { |
| 109 | pub fn new( data: &'a str ) -> Lexer<'a> { | 92 | pub fn new( data: &'a str ) -> Lexer<'a> { |
| 110 | let mut chars = data.char_indices(); | 93 | let mut chars = ; |
| 111 | let cur = chars.next(); | ||
| 112 | let cur2 = chars.next(); | ||
| 113 | //println!(" - cur: {:?}, peek: {:?}", cur, cur2 ); | ||
| 114 | Lexer { | 94 | Lexer { |
| 115 | data, | 95 | data, |
| 116 | chars, | 96 | src: LookAhead::new(data.char_indices()), |
| 117 | cur: [cur, cur2], | ||
| 118 | icur: 0, | ||
| 119 | mode: Mode::Text, | 97 | mode: Mode::Text, |
| 120 | pos: Position::new(1,1), | 98 | pos: Position::new(1,1), |
| 121 | } | 99 | } |
| @@ -170,8 +148,8 @@ impl<'a> Lexer<'a> { | |||
| 170 | } | 148 | } |
| 171 | } | 149 | } |
| 172 | 150 | ||
| 173 | fn error( &self, what: ErrorType ) -> Option<Symbol<'a>> { | 151 | fn error( &self, what: ErrorType ) -> Option<Token<'a>> { |
| 174 | Some(Symbol::new( TokenType::Error { | 152 | Some(Token::new( TokenType::Error { |
| 175 | what: what | 153 | what: what |
| 176 | }, self.pos, self.pos )) | 154 | }, self.pos, self.pos )) |
| 177 | } | 155 | } |
| @@ -182,7 +160,7 @@ impl<'a> Lexer<'a> { | |||
| 182 | } | 160 | } |
| 183 | } | 161 | } |
| 184 | 162 | ||
| 185 | fn next_symbol(&mut self) -> Option<Symbol<'a>> { | 163 | fn next_symbol(&mut self) -> Option<Token<'a>> { |
| 186 | // If we hit the end then we're already done. | 164 | // If we hit the end then we're already done. |
| 187 | if self.cur().is_none() { | 165 | if self.cur().is_none() { |
| 188 | return None; | 166 | return None; |
| @@ -206,7 +184,7 @@ impl<'a> Lexer<'a> { | |||
| 206 | } | 184 | } |
| 207 | } | 185 | } |
| 208 | 186 | ||
| 209 | fn parse_text(&mut self) -> Option<Symbol<'a>> { | 187 | fn parse_text(&mut self) -> Option<Token<'a>> { |
| 210 | let start = self.cur_index(); | 188 | let start = self.cur_index(); |
| 211 | let start_pos = self.pos.clone(); | 189 | let start_pos = self.pos.clone(); |
| 212 | while self.next().is_some() && !self.is_start_tag() { } | 190 | while self.next().is_some() && !self.is_start_tag() { } |
| @@ -217,11 +195,11 @@ impl<'a> Lexer<'a> { | |||
| 217 | if start == end { | 195 | if start == end { |
| 218 | None | 196 | None |
| 219 | } else { | 197 | } else { |
| 220 | Some(Symbol::new( TokenType::Text(s), start_pos, end_pos )) | 198 | Some(Token::new( TokenType::Text(s), start_pos, end_pos )) |
| 221 | } | 199 | } |
| 222 | } | 200 | } |
| 223 | 201 | ||
| 224 | fn parse_token(&mut self) -> Option<Symbol<'a>> { | 202 | fn parse_token(&mut self) -> Option<Token<'a>> { |
| 225 | self.skip_ws(); | 203 | self.skip_ws(); |
| 226 | match self.cur() { | 204 | match self.cur() { |
| 227 | Some('"') => { | 205 | Some('"') => { |
| @@ -229,15 +207,15 @@ impl<'a> Lexer<'a> { | |||
| 229 | } | 207 | } |
| 230 | Some('#') => { | 208 | Some('#') => { |
| 231 | self.next(); | 209 | self.next(); |
| 232 | return Some(Symbol::new(TokenType::Sharp, self.pos, self.pos)); | 210 | return Some(Token::new(TokenType::Sharp, self.pos, self.pos)); |
| 233 | } | 211 | } |
| 234 | Some('=') => { | 212 | Some('=') => { |
| 235 | self.next(); | 213 | self.next(); |
| 236 | return Some(Symbol::new(TokenType::Equals, self.pos, self.pos)); | 214 | return Some(Token::new(TokenType::Equals, self.pos, self.pos)); |
| 237 | } | 215 | } |
| 238 | Some('.') => { | 216 | Some('.') => { |
| 239 | self.next(); | 217 | self.next(); |
| 240 | return Some(Symbol::new(TokenType::Period, self.pos, self.pos)); | 218 | return Some(Token::new(TokenType::Period, self.pos, self.pos)); |
| 241 | } | 219 | } |
| 242 | _ => {} | 220 | _ => {} |
| 243 | } | 221 | } |
| @@ -253,20 +231,11 @@ impl<'a> Lexer<'a> { | |||
| 253 | if start == end { | 231 | if start == end { |
| 254 | None | 232 | None |
| 255 | } else { | 233 | } else { |
| 256 | Some(Symbol::new( match s { | 234 | Some(Token::new( TokenType::Identifier(s), start_pos, end_pos )) |
| 257 | "view" => TokenType::View, | ||
| 258 | "show" => TokenType::Show, | ||
| 259 | "output" => TokenType::Output, | ||
| 260 | "loop" => TokenType::Loop, | ||
| 261 | "if" => TokenType::If, | ||
| 262 | "elif" => TokenType::ElIf, | ||
| 263 | "else" => TokenType::Else, | ||
| 264 | _ => TokenType::Identifier(s) | ||
| 265 | }, start_pos, end_pos )) | ||
| 266 | } | 235 | } |
| 267 | } | 236 | } |
| 268 | 237 | ||
| 269 | fn parse_literal_str(&mut self) -> Option<Symbol<'a>> { | 238 | fn parse_literal_str(&mut self) -> Option<Token<'a>> { |
| 270 | if let Some(chr) = self.cur() && chr != '"' { | 239 | if let Some(chr) = self.cur() && chr != '"' { |
| 271 | return self.error( ErrorType::UnexpectedChar(chr) ); | 240 | return self.error( ErrorType::UnexpectedChar(chr) ); |
| 272 | } | 241 | } |
| @@ -277,7 +246,7 @@ impl<'a> Lexer<'a> { | |||
| 277 | let end_pos = self.pos.clone(); | 246 | let end_pos = self.pos.clone(); |
| 278 | self.next(); | 247 | self.next(); |
| 279 | let s = &self.data[start..end]; | 248 | let s = &self.data[start..end]; |
| 280 | Some(Symbol::new(TokenType::Literal(s), start_pos, end_pos )) | 249 | Some(Token::new(TokenType::LiteralStr(s), start_pos, end_pos )) |
| 281 | } | 250 | } |
| 282 | 251 | ||
| 283 | fn is_start_tag(&mut self) -> bool { | 252 | fn is_start_tag(&mut self) -> bool { |
| @@ -289,7 +258,7 @@ impl<'a> Lexer<'a> { | |||
| 289 | } | 258 | } |
| 290 | } | 259 | } |
| 291 | 260 | ||
| 292 | fn parse_start_tag(&mut self) -> Option<Symbol<'a>> { | 261 | fn parse_start_tag(&mut self) -> Option<Token<'a>> { |
| 293 | let start_pos = self.pos.clone(); | 262 | let start_pos = self.pos.clone(); |
| 294 | match self.cur() { | 263 | match self.cur() { |
| 295 | Some('[') => { | 264 | Some('[') => { |
| @@ -298,7 +267,7 @@ impl<'a> Lexer<'a> { | |||
| 298 | let end_pos = self.pos.clone(); | 267 | let end_pos = self.pos.clone(); |
| 299 | self.next(); | 268 | self.next(); |
| 300 | self.mode = Mode::InTag; | 269 | self.mode = Mode::InTag; |
| 301 | Some(Symbol::new( TokenType::StartFlat, start_pos, end_pos ) ) | 270 | Some(Token::new( TokenType::StartFlat, start_pos, end_pos ) ) |
| 302 | } else { | 271 | } else { |
| 303 | None | 272 | None |
| 304 | } | 273 | } |
| @@ -309,7 +278,7 @@ impl<'a> Lexer<'a> { | |||
| 309 | let end_pos = self.pos.clone(); | 278 | let end_pos = self.pos.clone(); |
| 310 | self.next(); | 279 | self.next(); |
| 311 | self.mode = Mode::InTag; | 280 | self.mode = Mode::InTag; |
| 312 | Some(Symbol::new(TokenType::StartPoint, start_pos, end_pos ) ) | 281 | Some(Token::new(TokenType::StartPoint, start_pos, end_pos ) ) |
| 313 | } else { | 282 | } else { |
| 314 | None | 283 | None |
| 315 | } | 284 | } |
| @@ -329,7 +298,7 @@ impl<'a> Lexer<'a> { | |||
| 329 | } | 298 | } |
| 330 | } | 299 | } |
| 331 | 300 | ||
| 332 | fn parse_end_tag(&mut self) -> Option<Symbol<'a>> { | 301 | fn parse_end_tag(&mut self) -> Option<Token<'a>> { |
| 333 | self.skip_ws(); | 302 | self.skip_ws(); |
| 334 | let start_pos = self.pos.clone(); | 303 | let start_pos = self.pos.clone(); |
| 335 | if let Some(chr) = self.cur() && chr == '|' { | 304 | if let Some(chr) = self.cur() && chr == '|' { |
| @@ -339,14 +308,14 @@ impl<'a> Lexer<'a> { | |||
| 339 | let end_pos = self.pos.clone(); | 308 | let end_pos = self.pos.clone(); |
| 340 | self.next(); | 309 | self.next(); |
| 341 | self.mode = Mode::Text; | 310 | self.mode = Mode::Text; |
| 342 | Some(Symbol::new( TokenType::EndFlat, start_pos, end_pos)) | 311 | Some(Token::new( TokenType::EndFlat, start_pos, end_pos)) |
| 343 | } | 312 | } |
| 344 | Some('>') => { | 313 | Some('>') => { |
| 345 | self.next(); | 314 | self.next(); |
| 346 | let end_pos = self.pos.clone(); | 315 | let end_pos = self.pos.clone(); |
| 347 | self.next(); | 316 | self.next(); |
| 348 | self.mode = Mode::Text; | 317 | self.mode = Mode::Text; |
| 349 | Some(Symbol::new( TokenType::EndPoint, start_pos, end_pos)) | 318 | Some(Token::new( TokenType::EndPoint, start_pos, end_pos)) |
| 350 | } | 319 | } |
| 351 | _ => { | 320 | _ => { |
| 352 | None | 321 | None |
| @@ -359,7 +328,7 @@ impl<'a> Lexer<'a> { | |||
| 359 | } | 328 | } |
| 360 | 329 | ||
| 361 | impl<'a> Iterator for Lexer<'a> { | 330 | impl<'a> Iterator for Lexer<'a> { |
| 362 | type Item = Symbol<'a>; | 331 | type Item = Token<'a>; |
| 363 | 332 | ||
| 364 | fn next(&mut self) -> Option<Self::Item> { | 333 | fn next(&mut self) -> Option<Self::Item> { |
| 365 | self.next_symbol() | 334 | 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 { | |||
| 58 | #[derive(Debug)] | 58 | #[derive(Debug)] |
| 59 | struct Output { | 59 | struct Output { |
| 60 | name: String, | 60 | name: String, |
| 61 | code: Vec<Token>, | 61 | code: Vec<Symbol>, |
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | type Properties = HashMap<String, String>; | 64 | type Properties = HashMap<String, String>; |
| 65 | 65 | ||
| 66 | #[derive(Debug)] | 66 | #[derive(Debug)] |
| 67 | enum Token { | 67 | enum Symbol { |
| 68 | Loop(Identifier, Vec<Token>), | 68 | Loop(Identifier, Vec<Symbol>), |
| 69 | Show(Identifier,Properties), | 69 | Show(Identifier,Properties), |
| 70 | If(Identifier, Vec<Token>, Vec<Token>), | 70 | If(Identifier, Vec<Symbol>, Vec<Symbol>), |
| 71 | Text(String), | 71 | Text(String), |
| 72 | } | 72 | } |
| 73 | 73 | ||
| @@ -175,7 +175,7 @@ impl DeepCrow { | |||
| 175 | 175 | ||
| 176 | pub fn render(&self, view: &str, input: &dyn for<'a> MappedStructure<'a> ) -> Result<String> { | 176 | pub fn render(&self, view: &str, input: &dyn for<'a> MappedStructure<'a> ) -> Result<String> { |
| 177 | if let Some(view) = self.views.get( view ) { | 177 | if let Some(view) = self.views.get( view ) { |
| 178 | //println!("::Token tree::\n{:?}", view ); | 178 | //println!("::Symbol tree::\n{:?}", view ); |
| 179 | let mut output = view.process( input )?; | 179 | let mut output = view.process( input )?; |
| 180 | if let Some(l) = &view.layout { | 180 | if let Some(l) = &view.layout { |
| 181 | self.render( l, &output ) | 181 | self.render( l, &output ) |
| @@ -195,10 +195,10 @@ impl DeepCrow { | |||
| 195 | } | 195 | } |
| 196 | } | 196 | } |
| 197 | 197 | ||
| 198 | fn exec<'a>(input: &StatefulContext<'a>, tokens: &Vec<Token>, buf: &mut String) -> Result<()> { | 198 | fn exec<'a>(input: &StatefulContext<'a>, tokens: &Vec<Symbol>, buf: &mut String) -> Result<()> { |
| 199 | for token in tokens { | 199 | for token in tokens { |
| 200 | match token { | 200 | match token { |
| 201 | Token::Loop(ident,code) => { | 201 | Symbol::Loop(ident,code) => { |
| 202 | //println!("!!! Loop over: {:?} {:?}", ident, input.get_value( &ident )); | 202 | //println!("!!! Loop over: {:?} {:?}", ident, input.get_value( &ident )); |
| 203 | if let Some(value) = input.get_value( &ident ) && | 203 | if let Some(value) = input.get_value( &ident ) && |
| 204 | let MappedValue::List(list) = value { | 204 | let MappedValue::List(list) = value { |
| @@ -213,7 +213,7 @@ fn exec<'a>(input: &StatefulContext<'a>, tokens: &Vec<Token>, buf: &mut String) | |||
| 213 | buf.push_str( &output ); | 213 | buf.push_str( &output ); |
| 214 | } | 214 | } |
| 215 | } | 215 | } |
| 216 | Token::If(ident,code,other) => { | 216 | Symbol::If(ident,code,other) => { |
| 217 | if let Some(value) = input.get_value( &ident ) && | 217 | if let Some(value) = input.get_value( &ident ) && |
| 218 | let MappedValue::Bool(b) = value && b { | 218 | let MappedValue::Bool(b) = value && b { |
| 219 | exec(input, code, buf)? | 219 | exec(input, code, buf)? |
| @@ -221,7 +221,7 @@ fn exec<'a>(input: &StatefulContext<'a>, tokens: &Vec<Token>, buf: &mut String) | |||
| 221 | exec(input, other, buf)? | 221 | exec(input, other, buf)? |
| 222 | } | 222 | } |
| 223 | } | 223 | } |
| 224 | Token::Show(ident,p) => { | 224 | Symbol::Show(ident,p) => { |
| 225 | let format = if let Some(s) = p.get("format") { | 225 | let format = if let Some(s) = p.get("format") { |
| 226 | s | 226 | s |
| 227 | } else { | 227 | } else { |
| @@ -289,7 +289,7 @@ fn exec<'a>(input: &StatefulContext<'a>, tokens: &Vec<Token>, buf: &mut String) | |||
| 289 | } | 289 | } |
| 290 | } | 290 | } |
| 291 | } | 291 | } |
| 292 | Token::Text(s) => { | 292 | Symbol::Text(s) => { |
| 293 | buf.push_str( s ); | 293 | buf.push_str( s ); |
| 294 | } | 294 | } |
| 295 | //_ => {} | 295 | //_ => {} |
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> { | |||
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | /* | 38 | /* |
| 39 | pub fn check_unwrapped<T: Fn( &TokenType, &TokenType ) -> bool>(&self, context: &str, f: T) -> Result<bool> { | 39 | pub fn check_unwrapped<T: Fn( &SymbolType, &SymbolType ) -> bool>(&self, context: &str, f: T) -> CrimResult<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(CrimError::eos( context )) |
| 42 | } else { | 42 | } else { |
| 43 | Ok(f( self.cur().unwrap().symbol(), self.peek().unwrap().symbol())) | 43 | Ok(f( self.cur().unwrap().symbol(), self.peek().unwrap().symbol())) |
| 44 | } | 44 | } |
| @@ -51,35 +51,34 @@ impl<'a> Context<'a> { | |||
| 51 | } | 51 | } |
| 52 | 52 | ||
| 53 | trait SymbolHelper { | 53 | trait SymbolHelper { |
| 54 | fn is<T: Fn( &TokenType ) -> bool>(&self, context: &str, f: T ) -> Result<bool>; | 54 | fn is<T: Fn( &SymbolType ) -> bool>(&self, context: &str, f: T ) -> CrimResult<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) -> CrimResult<bool>; |
| 57 | } | 57 | } |
| 58 | 58 | ||
| 59 | impl<'a> SymbolHelper for Option<Symbol<'a>> { | 59 | impl<'a> SymbolHelper for Option<Symbol<'a>> { |
| 60 | fn is<T: Fn( &TokenType ) -> bool>(&self, context: &str, f: T ) -> Result<bool> { | 60 | fn is<T: Fn( &SymbolType ) -> bool>(&self, context: &str, f: T ) -> CrimResult<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 { |
| 64 | Err(DeepCrowError::eos( context )) | 64 | Err(CrimError::eos( context )) |
| 65 | } | 65 | } |
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | fn is_valid_tag_name(&self) -> Result<bool> { | 68 | fn is_valid_tag_name(&self) -> CrimResult<bool> { |
| 69 | if let Some(st) = self { | 69 | if let Some(st) = self { |
| 70 | Ok(match st.symbol() { | 70 | Ok(match st.symbol() { |
| 71 | TokenType::Identifier(_) | | 71 | TokenType::Identifier("view") | |
| 72 | TokenType::View | | 72 | TokenType::Identifier("output") | |
| 73 | TokenType::Output | | 73 | TokenType::Identifier("loop") | |
| 74 | TokenType::Loop | | 74 | TokenType::Identifier("if") | |
| 75 | TokenType::If | | 75 | TokenType::Identifier("elif") | |
| 76 | TokenType::ElIf | | 76 | TokenType::Identifier("else") | |
| 77 | TokenType::Else | | 77 | TokenType::Identifier("show") => true, |
| 78 | TokenType::Show => true, | ||
| 79 | _ => false | 78 | _ => false |
| 80 | }) | 79 | }) |
| 81 | } else { | 80 | } else { |
| 82 | Err(DeepCrowError::parse( | 81 | Err(CrimError::parse( |
| 83 | Position::none(), | 82 | Position::none(), |
| 84 | "Unexpeceted end of stream.".to_string() | 83 | "Unexpeceted end of stream.".to_string() |
| 85 | )) | 84 | )) |
| @@ -183,17 +182,17 @@ impl Parser { | |||
| 183 | } | 182 | } |
| 184 | } | 183 | } |
| 185 | 184 | ||
| 186 | fn lex_error<T>(&self, error: &Symbol) -> Result<T> { | 185 | fn lex_error<T>(&self, error: &Symbol) -> CrimResult<T> { |
| 187 | if let TokenType::Error{what} = error.symbol() { | 186 | if let SymbolType::Error{what} = error.symbol() { |
| 188 | Err(DeepCrowError::parse( *error.start(), | 187 | Err(CrimError::parse( *error.start(), |
| 189 | format!("What: {:?}", *what) | 188 | format!("What: {:?}", *what) |
| 190 | )) | 189 | )) |
| 191 | } else { | 190 | } else { |
| 192 | Err(DeepCrowError::parse( Position::none(), "Not an error?".into())) | 191 | Err(CrimError::parse( Position::none(), "Not an error?".into())) |
| 193 | } | 192 | } |
| 194 | } | 193 | } |
| 195 | 194 | ||
| 196 | pub fn parse(&self, src: &str, source: usize ) -> Result<Vec<View>> { | 195 | pub fn parse(&self, src: &str, source: usize ) -> CrimResult<Vec<View>> { |
| 197 | let ll = Lexer::new( src ); | 196 | let ll = Lexer::new( src ); |
| 198 | let mut ctx = Context::new( ll, source ); | 197 | let mut ctx = Context::new( ll, source ); |
| 199 | 198 | ||
| @@ -201,24 +200,24 @@ impl Parser { | |||
| 201 | self.p_input( &mut ctx ) | 200 | self.p_input( &mut ctx ) |
| 202 | } | 201 | } |
| 203 | 202 | ||
| 204 | fn p_input(&self, ctx: &mut Context ) -> Result<Vec<View>> { | 203 | fn p_input(&self, ctx: &mut Context ) -> CrimResult<Vec<View>> { |
| 205 | let mut tags = Vec::new(); | 204 | let mut tags = Vec::new(); |
| 206 | loop { | 205 | loop { |
| 207 | if ctx.cur().is_none() { | 206 | if ctx.cur().is_none() { |
| 208 | break; | 207 | break; |
| 209 | } | 208 | } |
| 210 | match ctx.cur().unwrap().symbol() { | 209 | match ctx.cur().unwrap().symbol() { |
| 211 | TokenType::Text(_) => { /* Skip top level text */ } | 210 | SymbolType::Text(_) => { /* Skip top level text */ } |
| 212 | TokenType::StartFlat => { | 211 | SymbolType::StartFlat => { |
| 213 | let tb = self.parse_tag( ctx )?; | 212 | let tb = self.parse_tag( ctx )?; |
| 214 | let tb = self.parse_tag_set( ctx, tb )?; | 213 | let tb = self.parse_tag_set( ctx, tb )?; |
| 215 | tags.push( tb ); | 214 | tags.push( tb ); |
| 216 | } | 215 | } |
| 217 | TokenType::Error{..} => { | 216 | SymbolType::Error{..} => { |
| 218 | self.lex_error( &ctx.cur().unwrap() )?; | 217 | self.lex_error( &ctx.cur().unwrap() )?; |
| 219 | } | 218 | } |
| 220 | _ => { | 219 | _ => { |
| 221 | return Err(DeepCrowError::parse( | 220 | return Err(CrimError::parse( |
| 222 | *ctx.cur().unwrap().start(), | 221 | *ctx.cur().unwrap().start(), |
| 223 | format!("Unexpected symbol {:?} found looking for tags.", ctx.cur().unwrap().symbol() ) | 222 | format!("Unexpected symbol {:?} found looking for tags.", ctx.cur().unwrap().symbol() ) |
| 224 | )); | 223 | )); |
| @@ -231,11 +230,11 @@ impl Parser { | |||
| 231 | for tag in tags { | 230 | for tag in tags { |
| 232 | let start_pos = tag.start_pos(); | 231 | let start_pos = tag.start_pos(); |
| 233 | let name = tag.name().clone(); | 232 | let name = tag.name().clone(); |
| 234 | if tag.is_name(&TokenType::View) && | 233 | if tag.is_name(&SymbolType::View) && |
| 235 | let BuildResult::View(view) = tag.build( ctx )? { | 234 | let BuildResult::View(view) = tag.build( ctx )? { |
| 236 | views.push( view ); | 235 | views.push( view ); |
| 237 | } else { | 236 | } else { |
| 238 | return Err(DeepCrowError::parse( | 237 | return Err(CrimError::parse( |
| 239 | start_pos, | 238 | start_pos, |
| 240 | format!("Expected view at root, found {:?}", name ) | 239 | format!("Expected view at root, found {:?}", name ) |
| 241 | )); | 240 | )); |
| @@ -244,28 +243,28 @@ impl Parser { | |||
| 244 | Ok(views) | 243 | Ok(views) |
| 245 | } | 244 | } |
| 246 | 245 | ||
| 247 | fn parse_tag<'a>(&self, ctx: &mut Context<'a>) -> Result<TagBuilder<'a>> { | 246 | fn parse_tag<'a>(&self, ctx: &mut Context<'a>) -> CrimResult<TagBuilder<'a>> { |
| 248 | let start_sym = ctx.cur().unwrap(); | 247 | let start_sym = ctx.cur().unwrap(); |
| 249 | let mut tb = TagBuilder::new(&start_sym); | 248 | let mut tb = TagBuilder::new(&start_sym); |
| 250 | 249 | ||
| 251 | if ctx.next().is_some() { | 250 | if ctx.next().is_some() { |
| 252 | match ctx.cur().unwrap().symbol() { | 251 | match ctx.cur().unwrap().symbol() { |
| 253 | TokenType::View | TokenType::Show | TokenType::Loop | | 252 | SymbolType::View | SymbolType::Show | SymbolType::Loop | |
| 254 | TokenType::If | TokenType::ElIf | TokenType::Else | | 253 | SymbolType::If | SymbolType::ElIf | SymbolType::Else | |
| 255 | TokenType::Output => { | 254 | SymbolType::Output => { |
| 256 | let name_sym = ctx.cur().unwrap(); | 255 | let name_sym = ctx.cur().unwrap(); |
| 257 | ctx.next(); | 256 | ctx.next(); |
| 258 | tb.set_name( name_sym ); | 257 | tb.set_name( name_sym ); |
| 259 | } | 258 | } |
| 260 | _ => { | 259 | _ => { |
| 261 | return Err(DeepCrowError::parse( | 260 | return Err(CrimError::parse( |
| 262 | *ctx.cur().unwrap().start(), | 261 | *ctx.cur().unwrap().start(), |
| 263 | "Unexpected symbol".to_string() | 262 | "Unexpected symbol".to_string() |
| 264 | )); | 263 | )); |
| 265 | } | 264 | } |
| 266 | } | 265 | } |
| 267 | } else { | 266 | } else { |
| 268 | return Err(DeepCrowError::eos("tag type")); | 267 | return Err(CrimError::eos("tag type")); |
| 269 | } | 268 | } |
| 270 | 269 | ||
| 271 | if tb.can_have_params() || tb.can_have_expr() { | 270 | if tb.can_have_params() || tb.can_have_expr() { |
| @@ -277,11 +276,11 @@ impl Parser { | |||
| 277 | 276 | ||
| 278 | if let Some(end_sym) = ctx.cur() { | 277 | if let Some(end_sym) = ctx.cur() { |
| 279 | match end_sym.symbol() { | 278 | match end_sym.symbol() { |
| 280 | TokenType::EndPoint | TokenType::EndFlat => { | 279 | SymbolType::EndPoint | SymbolType::EndFlat => { |
| 281 | tb.set_end( &end_sym )?; | 280 | tb.set_end( &end_sym )?; |
| 282 | } | 281 | } |
| 283 | _ => { | 282 | _ => { |
| 284 | return Err(DeepCrowError::parse( | 283 | return Err(CrimError::parse( |
| 285 | *end_sym.start(), | 284 | *end_sym.start(), |
| 286 | format!("Unexpected symbol {:?} looking for end of tag.", end_sym.symbol()) | 285 | format!("Unexpected symbol {:?} looking for end of tag.", end_sym.symbol()) |
| 287 | )); | 286 | )); |
| @@ -294,21 +293,21 @@ impl Parser { | |||
| 294 | Ok(tb) | 293 | Ok(tb) |
| 295 | } | 294 | } |
| 296 | 295 | ||
| 297 | fn parse_tag_set<'a>(&self, ctx: &mut Context<'a>, mut base: TagBuilder<'a>) -> Result<TagBuilder<'a>> { | 296 | fn parse_tag_set<'a>(&self, ctx: &mut Context<'a>, mut base: TagBuilder<'a>) -> CrimResult<TagBuilder<'a>> { |
| 298 | if base.is_unary() { | 297 | if base.is_unary() { |
| 299 | return Ok(base); | 298 | return Ok(base); |
| 300 | } | 299 | } |
| 301 | 300 | ||
| 302 | loop { | 301 | loop { |
| 303 | if ctx.cur().is_none() { | 302 | if ctx.cur().is_none() { |
| 304 | return Err(DeepCrowError::eos(format!("close tag for {:?}", base.name()).as_str())); | 303 | return Err(CrimError::eos(format!("close tag for {:?}", base.name()).as_str())); |
| 305 | } | 304 | } |
| 306 | match ctx.cur().unwrap().symbol() { | 305 | match ctx.cur().unwrap().symbol() { |
| 307 | TokenType::Text(s) => { | 306 | SymbolType::Text(s) => { |
| 308 | base.add_child(Entry::Token(Token::Text(s.to_string()))); | 307 | base.add_child(Entry::Token(Token::Text(s.to_string()))); |
| 309 | ctx.next(); | 308 | ctx.next(); |
| 310 | } | 309 | } |
| 311 | TokenType::StartFlat | TokenType::StartPoint => { | 310 | SymbolType::StartFlat | SymbolType::StartPoint => { |
| 312 | let tb = self.parse_tag( ctx )?; | 311 | let tb = self.parse_tag( ctx )?; |
| 313 | 312 | ||
| 314 | match tb.tag_type() { | 313 | match tb.tag_type() { |
| @@ -320,13 +319,13 @@ impl Parser { | |||
| 320 | return Ok(base); | 319 | return Ok(base); |
| 321 | } | 320 | } |
| 322 | TagType::MultinaryClose => { | 321 | TagType::MultinaryClose => { |
| 323 | if (tb.is_name(&TokenType::If) && | 322 | if (tb.is_name(&SymbolType::If) && |
| 324 | (base.is_name(&TokenType::ElIf) || | 323 | (base.is_name(&SymbolType::ElIf) || |
| 325 | base.is_name(&TokenType::Else))) || | 324 | base.is_name(&SymbolType::Else))) || |
| 326 | tb.is_name(base.name().unwrap().symbol()) { | 325 | tb.is_name(base.name().unwrap().symbol()) { |
| 327 | return Ok(base); | 326 | return Ok(base); |
| 328 | } else { | 327 | } else { |
| 329 | return Err(DeepCrowError::parse( | 328 | return Err(CrimError::parse( |
| 330 | *tb.name().unwrap().start(), | 329 | *tb.name().unwrap().start(), |
| 331 | format!("Found {:?} looking for close of {:?}", tb.name().unwrap().symbol(), base.name().unwrap().symbol()) | 330 | format!("Found {:?} looking for close of {:?}", tb.name().unwrap().symbol(), base.name().unwrap().symbol()) |
| 332 | )); | 331 | )); |
| @@ -336,15 +335,15 @@ impl Parser { | |||
| 336 | base.add_child(Entry::TagBuilder(tb)); | 335 | base.add_child(Entry::TagBuilder(tb)); |
| 337 | } | 336 | } |
| 338 | TagType::Unknown => { | 337 | TagType::Unknown => { |
| 339 | return Err(DeepCrowError::broken(base.start_pos(), "Found unknown tag when looking for something else.")); | 338 | return Err(CrimError::broken(base.start_pos(), "Found unknown tag when looking for something else.")); |
| 340 | } | 339 | } |
| 341 | } | 340 | } |
| 342 | } | 341 | } |
| 343 | TokenType::Error{..} => { | 342 | SymbolType::Error{..} => { |
| 344 | return self.lex_error( &ctx.cur().unwrap() ); | 343 | return self.lex_error( &ctx.cur().unwrap() ); |
| 345 | } | 344 | } |
| 346 | _ => { | 345 | _ => { |
| 347 | return Err(DeepCrowError::parse( | 346 | return Err(CrimError::parse( |
| 348 | *ctx.cur().unwrap().start(), | 347 | *ctx.cur().unwrap().start(), |
| 349 | format!("Unexpected token: {:?}", ctx.cur().unwrap().symbol()), | 348 | format!("Unexpected token: {:?}", ctx.cur().unwrap().symbol()), |
| 350 | )); | 349 | )); |
| @@ -353,18 +352,18 @@ impl Parser { | |||
| 353 | } | 352 | } |
| 354 | } | 353 | } |
| 355 | 354 | ||
| 356 | fn parse_tag_params(&self, ctx: &mut Context, tb: &mut TagBuilder ) -> Result<()> { | 355 | fn parse_tag_params(&self, ctx: &mut Context, tb: &mut TagBuilder ) -> CrimResult<()> { |
| 357 | loop { | 356 | loop { |
| 358 | if ctx.cur().is_none() { | 357 | if ctx.cur().is_none() { |
| 359 | return Err(DeepCrowError::eos("tag parameters")); | 358 | return Err(CrimError::eos("tag parameters")); |
| 360 | } | 359 | } |
| 361 | match ctx.cur().unwrap().symbol() { | 360 | match ctx.cur().unwrap().symbol() { |
| 362 | TokenType::Literal(s) => { | 361 | SymbolType::LiteralStr(s) => { |
| 363 | tb.add_param( ParamValue::Literal(s.to_string()) ); | 362 | tb.add_param( ParamValue::LiteralStr(s.to_string()) ); |
| 364 | ctx.next(); | 363 | ctx.next(); |
| 365 | } | 364 | } |
| 366 | TokenType::Identifier(_) | TokenType::Sharp => { | 365 | SymbolType::Token(_) | SymbolType::Sharp => { |
| 367 | if ctx.peek().is("tag data", |s| *s == TokenType::Equals)? { | 366 | if ctx.peek().is("tag data", |s| *s == SymbolType::Equals)? { |
| 368 | break; | 367 | break; |
| 369 | } | 368 | } |
| 370 | tb.add_param( self.parse_identifier( ctx )? ); | 369 | tb.add_param( self.parse_identifier( ctx )? ); |
| @@ -377,23 +376,23 @@ impl Parser { | |||
| 377 | Ok(()) | 376 | Ok(()) |
| 378 | } | 377 | } |
| 379 | 378 | ||
| 380 | fn parse_identifier(&self, ctx: &mut Context ) -> Result<ParamValue> { | 379 | fn parse_identifier(&self, ctx: &mut Context ) -> CrimResult<ParamValue> { |
| 381 | let mut id = Identifier::new(); | 380 | let mut id = Identifier::new(); |
| 382 | 381 | ||
| 383 | if ctx.cur().is("identifier",|s| matches!( s, TokenType::Sharp))? { | 382 | if ctx.cur().is("identifier",|s| matches!( s, SymbolType::Sharp))? { |
| 384 | id.push( IdentifierValue::Root ); | 383 | id.push( IdentifierValue::Root ); |
| 385 | ctx.next(); | 384 | ctx.next(); |
| 386 | } | 385 | } |
| 387 | loop { | 386 | loop { |
| 388 | if ctx.cur().is("identifier",|s| matches!( s, TokenType::Identifier(_) ))? { | 387 | if ctx.cur().is("identifier",|s| matches!( s, SymbolType::Token(_) ))? { |
| 389 | if let TokenType::Identifier(s) = ctx.cur().unwrap().symbol() { | 388 | if let SymbolType::Token(s) = ctx.cur().unwrap().symbol() { |
| 390 | id.push( IdentifierValue::Name(s.to_string()) ); | 389 | id.push( IdentifierValue::Name(s.to_string()) ); |
| 391 | } | 390 | } |
| 392 | } else { | 391 | } else { |
| 393 | return Err(DeepCrowError::parse( ctx.cur().unwrap().start().clone(), format!("Expeceted identifier, found {:?}", ctx.cur().unwrap().symbol() ) ) ); | 392 | return Err(CrimError::parse( ctx.cur().unwrap().start().clone(), format!("Expeceted identifier, found {:?}", ctx.cur().unwrap().symbol() ) ) ); |
| 394 | } | 393 | } |
| 395 | 394 | ||
| 396 | if !ctx.next().is("identifier seperator",|s| *s == TokenType::Period )? { | 395 | if !ctx.next().is("identifier seperator",|s| *s == SymbolType::Period )? { |
| 397 | break; | 396 | break; |
| 398 | } | 397 | } |
| 399 | ctx.next(); | 398 | ctx.next(); |
| @@ -402,19 +401,19 @@ impl Parser { | |||
| 402 | Ok(ParamValue::Identifier(id)) | 401 | Ok(ParamValue::Identifier(id)) |
| 403 | } | 402 | } |
| 404 | 403 | ||
| 405 | fn parse_tag_props(&self, ctx: &mut Context, tb: &mut TagBuilder ) -> Result<()> { | 404 | fn parse_tag_props(&self, ctx: &mut Context, tb: &mut TagBuilder ) -> CrimResult<()> { |
| 406 | loop { | 405 | loop { |
| 407 | if ctx.cur().is_none() { | 406 | if ctx.cur().is_none() { |
| 408 | return Err(DeepCrowError::eos("tag properties")); | 407 | return Err(CrimError::eos("tag properties")); |
| 409 | } | 408 | } |
| 410 | if let TokenType::Identifier(s) = ctx.cur().unwrap().symbol() { | 409 | if let SymbolType::Token(s) = ctx.cur().unwrap().symbol() { |
| 411 | if ctx.peek().is_some_and(|s|s.check_type(|t| matches!(t,TokenType::Equals))) { | 410 | if ctx.peek().is_some_and(|s|s.check_type(|t| matches!(t,SymbolType::Equals))) { |
| 412 | ctx.next(); | 411 | ctx.next(); |
| 413 | if let Some(sym) = ctx.next() && | 412 | if let Some(sym) = ctx.next() && |
| 414 | let TokenType::Literal(lv) = sym.symbol() { | 413 | let SymbolType::LiteralStr(lv) = sym.symbol() { |
| 415 | tb.add_prop( s.to_string(), lv.to_string() ); | 414 | tb.add_prop( s.to_string(), lv.to_string() ); |
| 416 | } else { | 415 | } else { |
| 417 | return Err(DeepCrowError::parse(Position::none(),"Expected quoted literal string".to_string())); | 416 | return Err(CrimError::parse(Position::none(),"Expected quoted literal string".to_string())); |
| 418 | } | 417 | } |
| 419 | } else { | 418 | } else { |
| 420 | break; | 419 | break; |
| @@ -447,15 +446,15 @@ type EntryList<'a> = Vec<Entry<'a>>; | |||
| 447 | 446 | ||
| 448 | trait EntryListConverter { | 447 | trait EntryListConverter { |
| 449 | fn has_outputs(&self) -> bool; | 448 | fn has_outputs(&self) -> bool; |
| 450 | fn to_outputs(&mut self, ctx: &mut Context ) -> Result<Vec<Output>>; | 449 | fn to_outputs(&mut self, ctx: &mut Context ) -> CrimResult<Vec<Output>>; |
| 451 | fn to_tokens(&mut self, ctx: &mut Context) -> Result<Vec<Token>>; | 450 | fn to_tokens(&mut self, ctx: &mut Context) -> CrimResult<Vec<Token>>; |
| 452 | } | 451 | } |
| 453 | 452 | ||
| 454 | impl<'a> EntryListConverter for EntryList<'a> { | 453 | impl<'a> EntryListConverter for EntryList<'a> { |
| 455 | fn has_outputs(&self) -> bool { | 454 | fn has_outputs(&self) -> bool { |
| 456 | self.iter().any(|e| | 455 | self.iter().any(|e| |
| 457 | if let Entry::TagBuilder(tb) = e | 456 | if let Entry::TagBuilder(tb) = e |
| 458 | && tb.is_name(&TokenType::Output) { | 457 | && tb.is_name(&SymbolType::Output) { |
| 459 | true | 458 | true |
| 460 | } else { | 459 | } else { |
| 461 | false | 460 | false |
| @@ -463,7 +462,7 @@ impl<'a> EntryListConverter for EntryList<'a> { | |||
| 463 | ) | 462 | ) |
| 464 | } | 463 | } |
| 465 | 464 | ||
| 466 | fn to_outputs(&mut self, ctx: &mut Context) -> Result<Vec<Output>> { | 465 | fn to_outputs(&mut self, ctx: &mut Context) -> CrimResult<Vec<Output>> { |
| 467 | let mut outputs = Vec::new(); | 466 | let mut outputs = Vec::new(); |
| 468 | 467 | ||
| 469 | if self.has_outputs() { | 468 | if self.has_outputs() { |
| @@ -489,7 +488,7 @@ impl<'a> EntryListConverter for EntryList<'a> { | |||
| 489 | Ok(outputs) | 488 | Ok(outputs) |
| 490 | } | 489 | } |
| 491 | 490 | ||
| 492 | fn to_tokens(&mut self, ctx: &mut Context) -> Result<Vec<Token>> { | 491 | fn to_tokens(&mut self, ctx: &mut Context) -> CrimResult<Vec<Token>> { |
| 493 | let mut tokens : Vec<Token> = Vec::new(); | 492 | let mut tokens : Vec<Token> = Vec::new(); |
| 494 | for e in self.drain(..) { | 493 | for e in self.drain(..) { |
| 495 | match e { | 494 | match e { |
| @@ -500,7 +499,7 @@ impl<'a> EntryListConverter for EntryList<'a> { | |||
| 500 | if let BuildResult::Token(token) = tb.build(ctx)? { | 499 | if let BuildResult::Token(token) = tb.build(ctx)? { |
| 501 | tokens.push( token ); | 500 | tokens.push( token ); |
| 502 | } else { | 501 | } else { |
| 503 | return Err(DeepCrowError::broken( Position::none(), "Non-token result built.")); | 502 | return Err(CrimError::broken( Position::none(), "Non-token result built.")); |
| 504 | } | 503 | } |
| 505 | } | 504 | } |
| 506 | } | 505 | } |
| @@ -522,7 +521,7 @@ struct TagBuilder<'a> { | |||
| 522 | 521 | ||
| 523 | #[derive(Debug)] | 522 | #[derive(Debug)] |
| 524 | enum ParamValue { | 523 | enum ParamValue { |
| 525 | Literal(String), | 524 | LiteralStr(String), |
| 526 | Identifier(Identifier), | 525 | Identifier(Identifier), |
| 527 | } | 526 | } |
| 528 | 527 | ||
| @@ -545,25 +544,25 @@ impl<'a> TagBuilder<'a> { | |||
| 545 | } | 544 | } |
| 546 | } | 545 | } |
| 547 | 546 | ||
| 548 | pub fn set_end(&mut self, end: &Symbol) -> Result<()> { | 547 | pub fn set_end(&mut self, end: &Symbol) -> CrimResult<()> { |
| 549 | if *self.start.symbol() == TokenType::StartFlat { | 548 | if *self.start.symbol() == SymbolType::StartFlat { |
| 550 | if *end.symbol() == TokenType::EndFlat { | 549 | if *end.symbol() == SymbolType::EndFlat { |
| 551 | self.tag_type = TagType::Unary; | 550 | self.tag_type = TagType::Unary; |
| 552 | } else if *end.symbol() == TokenType::EndPoint { | 551 | } else if *end.symbol() == SymbolType::EndPoint { |
| 553 | self.tag_type = TagType::MultinaryOpen; | 552 | self.tag_type = TagType::MultinaryOpen; |
| 554 | } else { | 553 | } else { |
| 555 | return Err(DeepCrowError::broken( *end.start(), "Invalid bracket token type.")); | 554 | return Err(CrimError::broken( *end.start(), "Invalid bracket token type.")); |
| 556 | } | 555 | } |
| 557 | } else if *self.start.symbol() == TokenType::StartPoint { | 556 | } else if *self.start.symbol() == SymbolType::StartPoint { |
| 558 | if *end.symbol() == TokenType::EndFlat { | 557 | if *end.symbol() == SymbolType::EndFlat { |
| 559 | self.tag_type = TagType::MultinaryClose; | 558 | self.tag_type = TagType::MultinaryClose; |
| 560 | } else if *end.symbol() == TokenType::EndPoint { | 559 | } else if *end.symbol() == SymbolType::EndPoint { |
| 561 | self.tag_type = TagType::MultinaryMid; | 560 | self.tag_type = TagType::MultinaryMid; |
| 562 | } else { | 561 | } else { |
| 563 | return Err(DeepCrowError::broken( *end.start(), "Invalid bracket token type.")); | 562 | return Err(CrimError::broken( *end.start(), "Invalid bracket token type.")); |
| 564 | } | 563 | } |
| 565 | } else { | 564 | } else { |
| 566 | return Err(DeepCrowError::broken( *self.start.start(), "Invalid bracket token type.")); | 565 | return Err(CrimError::broken( *self.start.start(), "Invalid bracket token type.")); |
| 567 | } | 566 | } |
| 568 | Ok(()) | 567 | Ok(()) |
| 569 | } | 568 | } |
| @@ -582,18 +581,18 @@ impl<'a> TagBuilder<'a> { | |||
| 582 | */ | 581 | */ |
| 583 | pub fn can_have_params(&self) -> bool { | 582 | pub fn can_have_params(&self) -> bool { |
| 584 | match self.name.unwrap().symbol() { | 583 | match self.name.unwrap().symbol() { |
| 585 | TokenType::View | TokenType::Output | TokenType::Loop => true, | 584 | SymbolType::View | SymbolType::Output | SymbolType::Loop => true, |
| 586 | TokenType::Show | TokenType::If | TokenType::ElIf | | 585 | SymbolType::Show | SymbolType::If | SymbolType::ElIf | |
| 587 | TokenType::Else => false, | 586 | SymbolType::Else => false, |
| 588 | _ => false, | 587 | _ => false, |
| 589 | } | 588 | } |
| 590 | } | 589 | } |
| 591 | 590 | ||
| 592 | pub fn can_have_expr(&self) -> bool { | 591 | pub fn can_have_expr(&self) -> bool { |
| 593 | match self.name.unwrap().symbol() { | 592 | match self.name.unwrap().symbol() { |
| 594 | TokenType::View | TokenType::Output | TokenType::Loop => false, | 593 | SymbolType::View | SymbolType::Output | SymbolType::Loop => false, |
| 595 | TokenType::Show | TokenType::If | TokenType::ElIf | | 594 | SymbolType::Show | SymbolType::If | SymbolType::ElIf | |
| 596 | TokenType::Else => true, | 595 | SymbolType::Else => true, |
| 597 | _ => false, | 596 | _ => false, |
| 598 | } | 597 | } |
| 599 | } | 598 | } |
| @@ -611,7 +610,7 @@ impl<'a> TagBuilder<'a> { | |||
| 611 | } | 610 | } |
| 612 | } | 611 | } |
| 613 | */ | 612 | */ |
| 614 | pub fn is_name(&self, name: &TokenType<'a>) -> bool { | 613 | pub fn is_name(&self, name: &SymbolType<'a>) -> bool { |
| 615 | if let Some(a) = self.name { | 614 | if let Some(a) = self.name { |
| 616 | a.symbol() == name | 615 | a.symbol() == name |
| 617 | } else { | 616 | } else { |
| @@ -659,14 +658,14 @@ impl<'a> TagBuilder<'a> { | |||
| 659 | self.tag_type | 658 | self.tag_type |
| 660 | } | 659 | } |
| 661 | 660 | ||
| 662 | pub fn build(mut self, ctx: &mut Context ) -> Result<BuildResult> { | 661 | pub fn build(mut self, ctx: &mut Context ) -> CrimResult<BuildResult> { |
| 663 | if let Some(sym) = self.name { | 662 | if let Some(sym) = self.name { |
| 664 | match sym.symbol() { | 663 | match sym.symbol() { |
| 665 | TokenType::View => { | 664 | SymbolType::View => { |
| 666 | let name = if let ParamValue::Literal(s) = self.params.swap_remove(0) { | 665 | let name = if let ParamValue::LiteralStr(s) = self.params.swap_remove(0) { |
| 667 | s.to_string() | 666 | s.to_string() |
| 668 | } else { | 667 | } else { |
| 669 | return Err(DeepCrowError::parse(Position::none(), "Expected string literal for view name.".to_string())); | 668 | return Err(CrimError::parse(Position::none(), "Expected string literal for view name.".to_string())); |
| 670 | }; | 669 | }; |
| 671 | Ok(BuildResult::View(View { | 670 | Ok(BuildResult::View(View { |
| 672 | name: name, | 671 | name: name, |
| @@ -676,23 +675,23 @@ impl<'a> TagBuilder<'a> { | |||
| 676 | layout: self.props.get("layout").cloned(), | 675 | layout: self.props.get("layout").cloned(), |
| 677 | })) | 676 | })) |
| 678 | } | 677 | } |
| 679 | TokenType::Output => { | 678 | SymbolType::Output => { |
| 680 | let name = if let ParamValue::Literal(s) = self.params.swap_remove(0) { | 679 | let name = if let ParamValue::LiteralStr(s) = self.params.swap_remove(0) { |
| 681 | s.to_string() | 680 | s.to_string() |
| 682 | } else { | 681 | } else { |
| 683 | return Err(DeepCrowError::parse(Position::none(), "Expected string literal for output name.".into())); | 682 | return Err(CrimError::parse(Position::none(), "Expected string literal for output name.".into())); |
| 684 | }; | 683 | }; |
| 685 | Ok(BuildResult::Output(Output { | 684 | Ok(BuildResult::Output(Output { |
| 686 | name: name, | 685 | name: name, |
| 687 | code: self.children.to_tokens(ctx)?, | 686 | code: self.children.to_tokens(ctx)?, |
| 688 | })) | 687 | })) |
| 689 | } | 688 | } |
| 690 | TokenType::Loop => { | 689 | SymbolType::Loop => { |
| 691 | let id = if let ParamValue::Identifier(id) | 690 | let id = if let ParamValue::Identifier(id) |
| 692 | = self.params.swap_remove(0) { | 691 | = self.params.swap_remove(0) { |
| 693 | id | 692 | id |
| 694 | } else { | 693 | } else { |
| 695 | return Err(DeepCrowError::parse( | 694 | return Err(CrimError::parse( |
| 696 | Position::none(), | 695 | Position::none(), |
| 697 | "Identifier for loop variable name.".into()) | 696 | "Identifier for loop variable name.".into()) |
| 698 | ); | 697 | ); |
| @@ -701,24 +700,24 @@ impl<'a> TagBuilder<'a> { | |||
| 701 | Token::Loop(id, self.children.to_tokens(ctx)?) | 700 | Token::Loop(id, self.children.to_tokens(ctx)?) |
| 702 | )) | 701 | )) |
| 703 | } | 702 | } |
| 704 | TokenType::Show => { | 703 | SymbolType::Show => { |
| 705 | let id = if let ParamValue::Identifier(id) | 704 | let id = if let ParamValue::Identifier(id) |
| 706 | = self.params.swap_remove(0) { | 705 | = self.params.swap_remove(0) { |
| 707 | id | 706 | id |
| 708 | } else { | 707 | } else { |
| 709 | return Err(DeepCrowError::parse( | 708 | return Err(CrimError::parse( |
| 710 | Position::none(), | 709 | Position::none(), |
| 711 | "Identifier for show variable name.".into()) | 710 | "Identifier for show variable name.".into()) |
| 712 | ); | 711 | ); |
| 713 | }; | 712 | }; |
| 714 | Ok(BuildResult::Token(Token::Show(id, self.props))) | 713 | Ok(BuildResult::Token(Token::Show(id, self.props))) |
| 715 | } | 714 | } |
| 716 | TokenType::If => { | 715 | SymbolType::If => { |
| 717 | let id = if let ParamValue::Identifier(id) | 716 | let id = if let ParamValue::Identifier(id) |
| 718 | = self.params.swap_remove(0) { | 717 | = self.params.swap_remove(0) { |
| 719 | id | 718 | id |
| 720 | } else { | 719 | } else { |
| 721 | return Err(DeepCrowError::parse( | 720 | return Err(CrimError::parse( |
| 722 | Position::none(), | 721 | Position::none(), |
| 723 | "Identifier for if variable name.".into()) | 722 | "Identifier for if variable name.".into()) |
| 724 | ); | 723 | ); |
| @@ -726,7 +725,7 @@ impl<'a> TagBuilder<'a> { | |||
| 726 | 725 | ||
| 727 | let is_else = if self.chain.len() == 1 && | 726 | let is_else = if self.chain.len() == 1 && |
| 728 | let Entry::TagBuilder(tb) = &self.chain[0] && | 727 | let Entry::TagBuilder(tb) = &self.chain[0] && |
| 729 | tb.is_name(&TokenType::Else) && | 728 | tb.is_name(&SymbolType::Else) && |
| 730 | tb.params.len() == 0 { | 729 | tb.params.len() == 0 { |
| 731 | true | 730 | true |
| 732 | } else { | 731 | } else { |
| @@ -740,11 +739,11 @@ impl<'a> TagBuilder<'a> { | |||
| 740 | } | 739 | } |
| 741 | Ok(BuildResult::Token(Token::If(id, self.children.to_tokens(ctx)?, self.chain.to_tokens(ctx)?))) | 740 | Ok(BuildResult::Token(Token::If(id, self.children.to_tokens(ctx)?, self.chain.to_tokens(ctx)?))) |
| 742 | } | 741 | } |
| 743 | TokenType::ElIf => { | 742 | SymbolType::ElIf => { |
| 744 | let id = if let ParamValue::Identifier(id) = self.params.swap_remove(0) { | 743 | let id = if let ParamValue::Identifier(id) = self.params.swap_remove(0) { |
| 745 | id | 744 | id |
| 746 | } else { | 745 | } else { |
| 747 | return Err(DeepCrowError::parse( | 746 | return Err(CrimError::parse( |
| 748 | Position::none(), | 747 | Position::none(), |
| 749 | "Identifier for elif variable name.".into()) | 748 | "Identifier for elif variable name.".into()) |
| 750 | ); | 749 | ); |
| @@ -755,7 +754,7 @@ impl<'a> TagBuilder<'a> { | |||
| 755 | // ...but I can't do that right now. | 754 | // ...but I can't do that right now. |
| 756 | let is_else = if self.chain.len() == 1 && | 755 | let is_else = if self.chain.len() == 1 && |
| 757 | let Entry::TagBuilder(tb) = &self.chain[0] && | 756 | let Entry::TagBuilder(tb) = &self.chain[0] && |
| 758 | tb.is_name(&TokenType::Else) && | 757 | tb.is_name(&SymbolType::Else) && |
| 759 | tb.params.len() == 0 { | 758 | tb.params.len() == 0 { |
| 760 | true | 759 | true |
| 761 | } else { | 760 | } else { |
| @@ -771,15 +770,15 @@ impl<'a> TagBuilder<'a> { | |||
| 771 | 770 | ||
| 772 | Ok(BuildResult::Token(Token::If(id, self.children.to_tokens(ctx)?, self.chain.to_tokens(ctx)?))) | 771 | Ok(BuildResult::Token(Token::If(id, self.children.to_tokens(ctx)?, self.chain.to_tokens(ctx)?))) |
| 773 | } | 772 | } |
| 774 | TokenType::Else => { | 773 | SymbolType::Else => { |
| 775 | Ok(BuildResult::Token(Token::If(Identifier::new(), self.children.to_tokens(ctx)?, self.chain.to_tokens(ctx)?))) | 774 | Ok(BuildResult::Token(Token::If(Identifier::new(), self.children.to_tokens(ctx)?, self.chain.to_tokens(ctx)?))) |
| 776 | } | 775 | } |
| 777 | _ => { | 776 | _ => { |
| 778 | Err(DeepCrowError::parse( self.start_pos(), "Bad tag type".into())) | 777 | Err(CrimError::parse( self.start_pos(), "Bad tag type".into())) |
| 779 | } | 778 | } |
| 780 | } | 779 | } |
| 781 | } else { | 780 | } else { |
| 782 | Err(DeepCrowError::parse(self.start_pos(), "Bad tag type".into())) | 781 | Err(CrimError::parse(self.start_pos(), "Bad tag type".into())) |
| 783 | } | 782 | } |
| 784 | } | 783 | } |
| 785 | } | 784 | } |
