From f724cea337965419618d7d587015c9593a67834d Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 9 Jun 2026 10:16:19 -0700 Subject: Removed root, view, and output AST tokens. They are built as their final objects during parsing now. It's no more complex and we have no useless tokens now. --- src/lib.rs | 98 ++++++++++++++++++-------------------------------------------- 1 file changed, 28 insertions(+), 70 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 4ebb643..7790b9a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,6 +30,7 @@ pub enum ViewSource { #[derive(Debug)] struct View { + name: String, source: usize, outputs: Vec, // theme: Option @@ -40,11 +41,9 @@ impl View { fn process(&self, input: &Value) -> Result { let mut out_vars = HashMap::new(); for output in &self.outputs { - if let Token::Output(_,/*_,*/tokens) = &output.ast_root { - let mut buf = String::new(); - exec( input, tokens, &mut buf )?; - out_vars.insert( output.name.clone(), Value::String(buf) ); - } + let mut buf = String::new(); + exec( input, &output.code, &mut buf )?; + out_vars.insert( output.name.clone(), Value::String(buf) ); } Ok(Value::Dictionary(out_vars)) } @@ -53,16 +52,13 @@ impl View { #[derive(Debug)] struct Output { name: String, - ast_root: Token, + code: Vec, } type Properties = HashMap; #[derive(Debug)] enum Token { - Root(Vec), - View(String,Properties,Vec), - Output(String,/*Properties,*/ Vec), Show(String,Properties), Text(String), } @@ -109,78 +105,44 @@ impl Crimtag { } else { SystemTime::now() }; - + let source_id = self.id_source( + &ViewSource::File { + path: path.to_path_buf(), + loaded: time, + } + ); let p = parser::Parser::new(); self.register_views( - p.parse( &String::from_utf8( std::fs::read( path )? )? )?, - ViewSource::File { - path: path.to_path_buf(), - loaded: time, - })?; + p.parse( + &String::from_utf8( std::fs::read( path )? )?, + source_id + )?, + )?; Ok(()) } pub fn load_static(&mut self, data: &str) -> Result<(),ParseError> { + let source_id = self.id_source( &ViewSource::Static ); let p = parser::Parser::new(); - self.register_views( p.parse( &data )?, ViewSource::Static ) + self.register_views( p.parse( &data, source_id )? ) } pub fn load_external(&mut self, key: &str, data: &str) -> Result<(),ParseError> { - let p = parser::Parser::new(); + let source_id = self.id_source( + &ViewSource::External{ key: key.to_string()} + ); + + let p = parser::Parser::new(); self.register_views( - p.parse( &data )?, - ViewSource::External{ key: key.to_string()} + p.parse( &data, source_id )?, ) } - fn register_views(&mut self, token: Token, source: ViewSource) -> Result<(),ParseError> { - let source_id = self.id_source( &source ); - if let Token::Root(views) = token { - for token in views { - if let Token::View(name,props,output_tokens) = token { - let mut outputs = Vec::new(); - for ot in output_tokens { - let output_name = if let Token::Output(on,..) = &ot { - on.clone() - } else { - return Err(ParseError::new( - Position::new(0,0), - "Broken parser? Item in view is not an output." - )); - }; - outputs.push( - Output { - name: output_name, - ast_root: ot, - } - ); - } - - let layout = if let Some(l) = props.get("layout") { - Some(l.clone()) - } else { - None - }; - - self.views.insert( name, View { - source: source_id, - outputs: outputs, - layout: layout, - }); - } else { - return Err(ParseError::new( - Position::new(0,0), - "Broken parser? Item in root is not a view." - )); - } - } - Ok(()) - } else { - Err(ParseError::new( - Position::new(0,0), - "Broken parser? Root is not a root token." - )) + fn register_views(&mut self, views: Vec) -> Result<(),ParseError> { + for view in views { + self.views.insert( view.name.clone(), view ); } + Ok(()) } pub fn get_view_source(&self, view: &str) -> Option { @@ -228,10 +190,6 @@ impl Crimtag { fn exec(input: &Value, tokens: &Vec, buf: &mut String) -> Result<(),ParseError> { for token in tokens { match token { - Token::Root(..) | Token::View(..) | Token::Output(..) => { - println!("Too high!?"); - // Error? - } Token::Show(s,p) => { let format = if let Some(s) = p.get("format") { s -- cgit v1.2.3