From 0f2bd09d269862105e603a9865201f521f49490b Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 8 Jun 2026 21:45:30 -0700 Subject: It works again! and more! About to change a few things: - symbols and errors will have a range not a position - remove view/output token type, those should just be View and Output structs - Figure out some formatting stuff? (add a dep? I dunno) - Move to non-quoted dot-delimeted variable references. - Add loops. - Add conditionals? - Add more! --- src/lib.rs | 80 ++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 46 insertions(+), 34 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 5dbb379..208cea1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,22 +5,10 @@ use std::time::SystemTime; mod lexer; mod parser; +mod position; pub use parser::ParseError; - -#[derive(Debug,Copy,Clone)] -pub struct Position { - pub line: u32, - pub column: u32, -} - -impl Position { - pub fn new( line: u32, column: u32 ) -> Self { - Self { - line, column, - } - } -} +pub use position::*; #[derive(Debug)] pub struct Crimtag { @@ -29,7 +17,7 @@ pub struct Crimtag { } #[derive(PartialEq,Eq,Debug,Clone)] -enum ViewSource { +pub enum ViewSource { File { path: PathBuf, loaded: SystemTime, @@ -52,7 +40,7 @@ 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 { + 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) ); @@ -74,17 +62,9 @@ type Properties = HashMap; enum Token { Root(Vec), View(String,Properties,Vec), - Output(String,Properties,Vec), + Output(String,/*Properties,*/ Vec), Show(String,Properties), Text(String), - Literal(String), - /* - Tag{ - name: String, - params: Vec, - props: Properties, - children: Vec - },*/ } #[derive(Debug)] @@ -203,6 +183,18 @@ impl Crimtag { } } + pub fn get_view_source(&self, view: &str) -> Option { + if let Some(view) = self.views.get(view) { + if view.source < self.sources.len() { + Some(self.sources[view.source].clone()) + } else { + None + } + } else { + None + } + } + pub fn view_partial(&self, view: &str, input: &Value ) -> Result { if let Some(view) = self.views.get(view) { return view.process( input ) @@ -213,12 +205,10 @@ impl Crimtag { pub fn view(&self, view: &str, input: &Value ) -> Result { if let Some(view) = self.views.get( view ) { - let mut output = view.process( input )?; + let output = view.process( input )?; if let Some(l) = &view.layout { - println!("Layout: {}", l ); self.view( l, &output ) } else { - println!("No layout, at the top"); if let Value::Dictionary(mut d) = output && let Some(content) = d.remove("content") && let Value::String(s) = content { @@ -242,18 +232,40 @@ fn exec(input: &Value, tokens: &Vec, buf: &mut String) -> Result<(),Parse println!("Too high!?"); // Error? } - Token::Show(s,_) => { + Token::Show(s,p) => { + let format = if let Some(s) = p.get("format") { + s + } else { + &"".to_string() + }; if let Value::Dictionary(d) = input && - let Some(Value::String(s)) = d.get(s) { - buf.push_str(s); + let Some(value) = d.get(s) { + match value { + Value::Dictionary(_) => { + buf.push_str(""); + } + Value::List(_) => { + buf.push_str(""); + } + Value::String(s) => { + buf.push_str(s); + } + Value::Int(i) => { + buf.push_str(&i.to_string()); + } + Value::Float(f) => { + if format == "" { + buf.push_str(&f.to_string()); + } else { + buf.push_str(&f.to_string()); + } + } + } } } Token::Text(s) => { buf.push_str( s ); } - Token::Literal(..) => { - println!("Literal!?"); - } } } Ok(()) -- cgit v1.2.3