diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/context.rs | 7 | ||||
| -rw-r--r-- | src/lexer.rs | 4 | ||||
| -rw-r--r-- | src/lib.rs | 23 | ||||
| -rw-r--r-- | src/parser.rs | 28 |
4 files changed, 53 insertions, 9 deletions
diff --git a/src/context.rs b/src/context.rs index 78a7940..e4cd5c2 100644 --- a/src/context.rs +++ b/src/context.rs | |||
| @@ -44,6 +44,7 @@ pub enum Value { | |||
| 44 | String(String), | 44 | String(String), |
| 45 | Int(i64), | 45 | Int(i64), |
| 46 | Float(f64), | 46 | Float(f64), |
| 47 | Bool(bool), | ||
| 47 | Identifier(Identifier), | 48 | Identifier(Identifier), |
| 48 | } | 49 | } |
| 49 | 50 | ||
| @@ -71,6 +72,12 @@ impl From<f64> for Value { | |||
| 71 | } | 72 | } |
| 72 | } | 73 | } |
| 73 | 74 | ||
| 75 | impl From<bool> for Value { | ||
| 76 | fn from(val: bool) -> Value { | ||
| 77 | Value::Bool(val) | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 74 | impl From<Vec<Value>> for Value { | 81 | impl From<Vec<Value>> for Value { |
| 75 | fn from(val: Vec<Value>) -> Value { | 82 | fn from(val: Vec<Value>) -> Value { |
| 76 | Value::List(val) | 83 | Value::List(val) |
diff --git a/src/lexer.rs b/src/lexer.rs index 2fa29ce..2bf7e9e 100644 --- a/src/lexer.rs +++ b/src/lexer.rs | |||
| @@ -23,7 +23,7 @@ pub enum SymbolType<'a> { | |||
| 23 | Equals, | 23 | Equals, |
| 24 | Period, | 24 | Period, |
| 25 | If, | 25 | If, |
| 26 | ElseIf, | 26 | ElIf, |
| 27 | Else, | 27 | Else, |
| 28 | Token(&'a str), | 28 | Token(&'a str), |
| 29 | Literal(&'a str), | 29 | Literal(&'a str), |
| @@ -250,7 +250,7 @@ impl<'a> Lexer<'a> { | |||
| 250 | "output" => SymbolType::Output, | 250 | "output" => SymbolType::Output, |
| 251 | "loop" => SymbolType::Loop, | 251 | "loop" => SymbolType::Loop, |
| 252 | "if" => SymbolType::If, | 252 | "if" => SymbolType::If, |
| 253 | "elseif" => SymbolType::ElseIf, | 253 | "elif" => SymbolType::ElIf, |
| 254 | "else" => SymbolType::Else, | 254 | "else" => SymbolType::Else, |
| 255 | _ => SymbolType::Token(s) | 255 | _ => SymbolType::Token(s) |
| 256 | }, start_pos, end_pos )) | 256 | }, start_pos, end_pos )) |
| @@ -65,7 +65,7 @@ enum Token { | |||
| 65 | Loop(Identifier, Vec<Token>), | 65 | Loop(Identifier, Vec<Token>), |
| 66 | Show(Identifier,Properties), | 66 | Show(Identifier,Properties), |
| 67 | If(Identifier, Vec<Token>), | 67 | If(Identifier, Vec<Token>), |
| 68 | ElseIf(Identifier, Vec<Token>), | 68 | ElIf(Identifier, Vec<Token>), |
| 69 | Else(Vec<Token>), | 69 | Else(Vec<Token>), |
| 70 | Text(String), | 70 | Text(String), |
| 71 | } | 71 | } |
| @@ -205,6 +205,14 @@ fn exec(input: &impl MappedStructure, tokens: &Vec<Token>, buf: &mut String) -> | |||
| 205 | } | 205 | } |
| 206 | } | 206 | } |
| 207 | } | 207 | } |
| 208 | Token::If(ident,code) => { | ||
| 209 | if let Some(value) = input.get_value( &ident ) && | ||
| 210 | let Value::Bool(b) = value && | ||
| 211 | b { | ||
| 212 | |||
| 213 | exec(input, code, buf)? | ||
| 214 | } | ||
| 215 | } | ||
| 208 | Token::Show(ident,p) => { | 216 | Token::Show(ident,p) => { |
| 209 | let format = if let Some(s) = p.get("format") { | 217 | let format = if let Some(s) = p.get("format") { |
| 210 | s | 218 | s |
| @@ -219,6 +227,13 @@ fn exec(input: &impl MappedStructure, tokens: &Vec<Token>, buf: &mut String) -> | |||
| 219 | Value::List(_) => { | 227 | Value::List(_) => { |
| 220 | buf.push_str("<list>"); | 228 | buf.push_str("<list>"); |
| 221 | } | 229 | } |
| 230 | Value::Bool(b) => { | ||
| 231 | if b { | ||
| 232 | buf.push_str("true"); | ||
| 233 | } else { | ||
| 234 | buf.push_str("false"); | ||
| 235 | } | ||
| 236 | } | ||
| 222 | Value::String(s) => { | 237 | Value::String(s) => { |
| 223 | buf.push_str(s.as_str()); | 238 | buf.push_str(s.as_str()); |
| 224 | } | 239 | } |
| @@ -313,7 +328,7 @@ Now with explicit outputs: | |||
| 313 | let mut ct = Crimtag::new(); | 328 | let mut ct = Crimtag::new(); |
| 314 | if let Err(e) = ct.load_static(r#"Looping code: | 329 | if let Err(e) = ct.load_static(r#"Looping code: |
| 315 | [|view "index"|>We will enumerate people here:[|loop people|> | 330 | [|view "index"|>We will enumerate people here:[|loop people|> |
| 316 | - [|show last_name|], [|show first_name|]: [|show title|] [|loop tags|> [|show tag|]<|loop|] <|loop|] | 331 | - [|show last_name|], [|show first_name|][|if show_title |>: [|show title|] <|if|] [|loop tags|> [|show tag|]<|loop|] <|loop|] |
| 317 | <|view|] | 332 | <|view|] |
| 318 | "#) { | 333 | "#) { |
| 319 | println!("Error: {:?}", e ); | 334 | println!("Error: {:?}", e ); |
| @@ -325,21 +340,25 @@ Now with explicit outputs: | |||
| 325 | [ | 340 | [ |
| 326 | ("first_name".into(), "Joe".into()), | 341 | ("first_name".into(), "Joe".into()), |
| 327 | ("last_name".into(), "Smith".into()), | 342 | ("last_name".into(), "Smith".into()), |
| 343 | ("show_title".into(), true.into()), | ||
| 328 | ("title".into(), "CEO".into()), | 344 | ("title".into(), "CEO".into()), |
| 329 | ].into(), | 345 | ].into(), |
| 330 | [ | 346 | [ |
| 331 | ("first_name".into(), "Chris".into()), | 347 | ("first_name".into(), "Chris".into()), |
| 332 | ("last_name".into(), "Perkens".into()), | 348 | ("last_name".into(), "Perkens".into()), |
| 349 | ("show_title".into(), false.into()), | ||
| 333 | ("title".into(), "Baconeer".into()), | 350 | ("title".into(), "Baconeer".into()), |
| 334 | ].into(), | 351 | ].into(), |
| 335 | [ | 352 | [ |
| 336 | ("first_name".into(), "Will".into()), | 353 | ("first_name".into(), "Will".into()), |
| 337 | ("last_name".into(), "Power".into()), | 354 | ("last_name".into(), "Power".into()), |
| 355 | ("show_title".into(), false.into()), | ||
| 338 | ("title".into(), "CFO".into()), | 356 | ("title".into(), "CFO".into()), |
| 339 | ].into(), | 357 | ].into(), |
| 340 | [ | 358 | [ |
| 341 | ("first_name".into(), "Justin".into()), | 359 | ("first_name".into(), "Justin".into()), |
| 342 | ("last_name".into(), "Time".into()), | 360 | ("last_name".into(), "Time".into()), |
| 361 | ("show_title".into(), true.into()), | ||
| 343 | ("title".into(), "CIO".into()), | 362 | ("title".into(), "CIO".into()), |
| 344 | ].into(), | 363 | ].into(), |
| 345 | ].into()), | 364 | ].into()), |
diff --git a/src/parser.rs b/src/parser.rs index be5492c..6ba6cfe 100644 --- a/src/parser.rs +++ b/src/parser.rs | |||
| @@ -70,7 +70,7 @@ impl<'a> SymbolHelper for Option<Symbol<'a>> { | |||
| 70 | SymbolType::Output | | 70 | SymbolType::Output | |
| 71 | SymbolType::Loop | | 71 | SymbolType::Loop | |
| 72 | SymbolType::If | | 72 | SymbolType::If | |
| 73 | SymbolType::ElseIf | | 73 | SymbolType::ElIf | |
| 74 | SymbolType::Else | | 74 | SymbolType::Else | |
| 75 | SymbolType::Show => true, | 75 | SymbolType::Show => true, |
| 76 | _ => false | 76 | _ => false |
| @@ -88,13 +88,23 @@ pub struct Parser { | |||
| 88 | } | 88 | } |
| 89 | 89 | ||
| 90 | /** | 90 | /** |
| 91 | * input: input tag | 91 | * input: input complete_tag |
| 92 | * | input tag_pair | ||
| 93 | * | input text | 92 | * | input text |
| 94 | * | | 93 | * | |
| 95 | * ; | 94 | * ; |
| 96 | * | 95 | * |
| 97 | * tag: '[|' tag_guts '|]' | 96 | * tag: unary_tag |
| 97 | * | multinary_open_tag | ||
| 98 | * | multinary_mid_tag | ||
| 99 | * | multinary_close_tag | ||
| 100 | * ; | ||
| 101 | * | ||
| 102 | * unary_tag: '[|' tag_guts '|]' | ||
| 103 | * ; | ||
| 104 | * | ||
| 105 | * | '[|' tag_guts '|>' | ||
| 106 | * | '<|' tag_guts '|>' | ||
| 107 | * | '<|' tag_guts '|]' | ||
| 98 | * ; | 108 | * ; |
| 99 | * | 109 | * |
| 100 | * tag_pair: open_tag tag_body close_tag | 110 | * tag_pair: open_tag tag_body close_tag |
| @@ -295,7 +305,7 @@ impl Parser { | |||
| 295 | if ctx.next().is_some() { | 305 | if ctx.next().is_some() { |
| 296 | match ctx.cur().unwrap().symbol() { | 306 | match ctx.cur().unwrap().symbol() { |
| 297 | SymbolType::View | SymbolType::Show | SymbolType::Loop | | 307 | SymbolType::View | SymbolType::Show | SymbolType::Loop | |
| 298 | SymbolType::If | SymbolType::ElseIf | SymbolType::Else | | 308 | SymbolType::If | SymbolType::ElIf | SymbolType::Else | |
| 299 | SymbolType::Output => { | 309 | SymbolType::Output => { |
| 300 | let name_sym = ctx.cur().unwrap(); | 310 | let name_sym = ctx.cur().unwrap(); |
| 301 | ctx.next(); | 311 | ctx.next(); |
| @@ -638,6 +648,14 @@ impl<'a> TagBuilder<'a> { | |||
| 638 | }; | 648 | }; |
| 639 | Ok(Token::Show(id, self.props)) | 649 | Ok(Token::Show(id, self.props)) |
| 640 | } | 650 | } |
| 651 | SymbolType::If => { | ||
| 652 | let id = if let ParamValue::Identifier(id) = self.params.swap_remove(0) { | ||
| 653 | id | ||
| 654 | } else { | ||
| 655 | return Err(CrimError::parse(Position::none(), "Identifier for show variable name.".into())); | ||
| 656 | }; | ||
| 657 | Ok(Token::If(id, self.children)) | ||
| 658 | } | ||
| 641 | _ => { | 659 | _ => { |
| 642 | Err(CrimError::parse( self.start_pos, "Bad tag type".into())) | 660 | Err(CrimError::parse( self.start_pos, "Bad tag type".into())) |
| 643 | } | 661 | } |
