diff options
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 30 |
1 files changed, 20 insertions, 10 deletions
| @@ -2,10 +2,13 @@ use std::path::{PathBuf,Path}; | |||
| 2 | use std::collections::HashMap; | 2 | use std::collections::HashMap; |
| 3 | use core::error::Error; | 3 | use core::error::Error; |
| 4 | use std::time::SystemTime; | 4 | use std::time::SystemTime; |
| 5 | use std::fs::File; | ||
| 6 | use std::io::Read; | ||
| 5 | 7 | ||
| 6 | mod lexer; | 8 | mod lexer; |
| 9 | mod parser; | ||
| 7 | 10 | ||
| 8 | pub struct Tx { | 11 | pub struct Crimtag { |
| 9 | fragments: HashMap<String, Fragment>, | 12 | fragments: HashMap<String, Fragment>, |
| 10 | sources: Vec<FragmentSource>, | 13 | sources: Vec<FragmentSource>, |
| 11 | } | 14 | } |
| @@ -40,23 +43,22 @@ enum Token { | |||
| 40 | Fragment(String,Properties,Vec<Token>), | 43 | Fragment(String,Properties,Vec<Token>), |
| 41 | Section(String,Properties,Vec<Token>), | 44 | Section(String,Properties,Vec<Token>), |
| 42 | Text(String), | 45 | Text(String), |
| 43 | |||
| 44 | } | 46 | } |
| 45 | 47 | ||
| 46 | struct State { | 48 | struct State { |
| 47 | } | 49 | } |
| 48 | 50 | ||
| 49 | impl Tx { | 51 | impl Crimtag { |
| 50 | pub fn new() -> Self { | 52 | pub fn new() -> Self { |
| 51 | Self { | 53 | Self { |
| 52 | fragments: HashMap::new(), | 54 | fragments: HashMap::new(), |
| 53 | sources: vec![Frament::Static], | 55 | sources: vec![FragmentSource::Static], |
| 54 | } | 56 | } |
| 55 | } | 57 | } |
| 56 | 58 | ||
| 57 | fn id_source(&mut self, src: &FragmentSource ) -> usize { | 59 | fn id_source(&mut self, src: &FragmentSource ) -> usize { |
| 58 | for idx in 0..self.sources.len() { | 60 | for idx in 0..self.sources.len() { |
| 59 | if self.sources[idx] == src { | 61 | if self.sources[idx] == *src { |
| 60 | return idx; | 62 | return idx; |
| 61 | } | 63 | } |
| 62 | } | 64 | } |
| @@ -77,16 +79,18 @@ impl Tx { | |||
| 77 | SystemTime::now() | 79 | SystemTime::now() |
| 78 | } | 80 | } |
| 79 | } else { | 81 | } else { |
| 80 | SystemTime.now() | 82 | SystemTime::now() |
| 81 | }; | 83 | }; |
| 82 | 84 | ||
| 83 | let mut file = File::open( path )?; | 85 | let mut p = parser::Parser::new(); |
| 86 | p.parse( self, &String::from_utf8( std::fs::read( path )? )? ); | ||
| 84 | 87 | ||
| 85 | let mut ll = lexer::Lexer( &String::from_utf8( file.read()? )? ); | ||
| 86 | Ok(()) | 88 | Ok(()) |
| 87 | } | 89 | } |
| 88 | 90 | ||
| 89 | pub fn load_static(&mut self, data: &str) -> Result<(),Box::<dyn Error>> { | 91 | pub fn load_static(&mut self, data: &str) -> Result<(),Box::<dyn Error>> { |
| 92 | let mut p = parser::Parser::new(); | ||
| 93 | p.parse( self, &data ); | ||
| 90 | 94 | ||
| 91 | Ok(()) | 95 | Ok(()) |
| 92 | } | 96 | } |
| @@ -102,15 +106,21 @@ mod tests { | |||
| 102 | 106 | ||
| 103 | #[test] | 107 | #[test] |
| 104 | fn lexing() { | 108 | fn lexing() { |
| 105 | let data = r#"Leading comment: [|fragment "basic"|>Hello there <|fragment|] Trailing text"#; | 109 | let data = r#"Leading comment: [|fragment "basic" something="yeup"|>Hello there <|fragment|] Trailing text"#; |
| 106 | let ll = lexer::Lexer::new( &data ); | 110 | let ll = lexer::Lexer::new( &data ); |
| 107 | for sym in ll { | 111 | for sym in ll { |
| 108 | if let lexer::Symbol::Error{line,row,what} = sym { | 112 | if let lexer::Symbol::Error{line,row,what} = sym { |
| 109 | println!("Error {}:{}: {}", line, row, what ); | 113 | println!("Error {}:{}: {:?}", line, row, what ); |
| 110 | break; | 114 | break; |
| 111 | } else { | 115 | } else { |
| 112 | println!("Symbol: {:?}", sym ); | 116 | println!("Symbol: {:?}", sym ); |
| 113 | } | 117 | } |
| 114 | } | 118 | } |
| 115 | } | 119 | } |
| 120 | |||
| 121 | #[test] | ||
| 122 | fn parsing() { | ||
| 123 | let mut ct = Crimtag::new(); | ||
| 124 | ct.load_static(r#"Here is a sample [|frament "index" theme="standard"|><html><body>Hi</body></html><|fragment|] That was fun!"#); | ||
| 125 | } | ||
| 116 | } | 126 | } |
