From 11f5bc0c5a8bcfb6ac8fef8abc33297c4addc81b Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 5 Jun 2026 11:28:07 -0700 Subject: Started on the parser. Thinking about some tweaks. - Creating a LookaheadIterator as a general class that could be used. - Wrap symbols in a symbol struct that tracks symbol start and end position for error reporting. - Make a struct for position in a file as well. --- src/lib.rs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 16bef07..0f6d01d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,10 +2,13 @@ use std::path::{PathBuf,Path}; use std::collections::HashMap; use core::error::Error; use std::time::SystemTime; +use std::fs::File; +use std::io::Read; mod lexer; +mod parser; -pub struct Tx { +pub struct Crimtag { fragments: HashMap, sources: Vec, } @@ -40,23 +43,22 @@ enum Token { Fragment(String,Properties,Vec), Section(String,Properties,Vec), Text(String), - } struct State { } -impl Tx { +impl Crimtag { pub fn new() -> Self { Self { fragments: HashMap::new(), - sources: vec![Frament::Static], + sources: vec![FragmentSource::Static], } } fn id_source(&mut self, src: &FragmentSource ) -> usize { for idx in 0..self.sources.len() { - if self.sources[idx] == src { + if self.sources[idx] == *src { return idx; } } @@ -77,16 +79,18 @@ impl Tx { SystemTime::now() } } else { - SystemTime.now() + SystemTime::now() }; - let mut file = File::open( path )?; + let mut p = parser::Parser::new(); + p.parse( self, &String::from_utf8( std::fs::read( path )? )? ); - let mut ll = lexer::Lexer( &String::from_utf8( file.read()? )? ); Ok(()) } pub fn load_static(&mut self, data: &str) -> Result<(),Box::> { + let mut p = parser::Parser::new(); + p.parse( self, &data ); Ok(()) } @@ -102,15 +106,21 @@ mod tests { #[test] fn lexing() { - let data = r#"Leading comment: [|fragment "basic"|>Hello there <|fragment|] Trailing text"#; + let data = r#"Leading comment: [|fragment "basic" something="yeup"|>Hello there <|fragment|] Trailing text"#; let ll = lexer::Lexer::new( &data ); for sym in ll { if let lexer::Symbol::Error{line,row,what} = sym { - println!("Error {}:{}: {}", line, row, what ); + println!("Error {}:{}: {:?}", line, row, what ); break; } else { println!("Symbol: {:?}", sym ); } } } + + #[test] + fn parsing() { + let mut ct = Crimtag::new(); + ct.load_static(r#"Here is a sample [|frament "index" theme="standard"|>Hi<|fragment|] That was fun!"#); + } } -- cgit v1.2.3