From 256fb1cd93ccfcfdb183ceecfab743b7ca6409eb Mon Sep 17 00:00:00 2001 From: xqtc Date: Thu, 8 Aug 2024 14:24:11 +0200 Subject: [PATCH] ich weiss doch auch nicht --- .config/nextest.toml | 2 + flake.nix | 6 +- src/lex/handlers.rs | 136 ++++++++++++++++++++++++++++++++++++------- src/lex/lexer.rs | 4 +- src/lib.rs | 23 +++++++- 5 files changed, 141 insertions(+), 30 deletions(-) create mode 100644 .config/nextest.toml diff --git a/.config/nextest.toml b/.config/nextest.toml new file mode 100644 index 0000000..8bccd51 --- /dev/null +++ b/.config/nextest.toml @@ -0,0 +1,2 @@ +[profile.default] +slow-timeout = "1m" diff --git a/flake.nix b/flake.nix index 49a0a5a..0a13bf1 100644 --- a/flake.nix +++ b/flake.nix @@ -52,11 +52,7 @@ shellHook = '' # For rust-analyzer 'hover' tooltips to work. export RUST_SRC_PATH=${pkgs.rustPlatform.rustLibSrc} - echo $RUST_SRC_PATH - - echo - echo "Run 'just ' to get started" - just + nu ''; buildInputs = nonRustDeps; nativeBuildInputs = with pkgs; [ diff --git a/src/lex/handlers.rs b/src/lex/handlers.rs index 83b3dc3..4f24111 100644 --- a/src/lex/handlers.rs +++ b/src/lex/handlers.rs @@ -1,3 +1,4 @@ +use core::panic; use crate::lex::lexer::Lexer; impl Lexer { @@ -35,17 +36,12 @@ impl Lexer { let mut number = String::new(); let mut is_float = false; let mut is_swizzle = false; + while let Some(c) = self.current_char { - if c.is_ascii_digit() - /* && self.peek().unwrap_or_else(|| ' ') == 'f' */ - { + if c.is_ascii_digit() { number.push(c); self.advance(); } else if c == '.' || c == 'e' || c == 'f' { - //////////////////////////////////// - //ALLES HIER DRIN IST NICHT SCHÖN// - //////////////////////////////////// - match self.peek().unwrap_or(' ') { 'x' | 'y' | 'z' | 'w' | 'r' | 'g' | 'b' | 'a' => { is_swizzle = true; @@ -57,27 +53,40 @@ impl Lexer { number.push(c); self.advance(); } + 'f' => { + is_float = true; + number.push(c); + self.advance(); + break; + } + '.' => { + if self.peek().unwrap_or(' ') == 'f' { + is_float = true; + number.push(c); + self.advance(); + number.push(self.current_char.unwrap()); // Push the 'f' + self.advance(); + break; + } + } _ => {} } - if c == 'f' { - is_float = true; - number.push(c); - self.advance(); - dbg!(&number); - break; - } - } else if c.is_alphabetic() { - if c == '.' && self.peek().unwrap_or(' ') == 'e' { - is_float = true; - self.advance(); - dbg!("break in alphabetic"); - break; + } else { + match self.peek().unwrap_or(' ') { + 'f' => { + is_float = true; + number.push(c); + self.advance(); + break; + } + _ => {} } is_swizzle = true; number.push(c); self.advance(); - } + } } + if is_float { return crate::tokens::Token::FLOATCONSTANT(number); } @@ -90,6 +99,91 @@ impl Lexer { crate::tokens::Token::INTCONSTANT(number) } + // ORIGINAL + // pub fn consume_number(&mut self) -> crate::tokens::Token { + // let mut number = String::new(); + // let mut is_float = false; + // let mut is_swizzle = false; + // while let Some(c) = self.current_char { + // if c.is_ascii_digit() + // /* && self.peek().unwrap_or_else(|| ' ') == 'f' */ + // { + // number.push(c); + // self.advance(); + // } else if c == '.' || c == 'e' || c == 'f' { + // //////////////////////////////////// + // //ALLES HIER DRIN IST NICHT SCHÖN// + // //////////////////////////////////// + // + // match self.peek().unwrap_or(' ') { + // 'x' | 'y' | 'z' | 'w' | 'r' | 'g' | 'b' | 'a' => { + // is_swizzle = true; + // number.push(c); + // self.advance(); + // } + // '0'..='9' | 'e' => { + // is_float = true; + // number.push(c); + // self.advance(); + // } + // '.' => { + // if self.peek().unwrap_or(' ') == 'f' { + // is_float = true; + // number.push(c); + // self.advance(); + // break; + // } + // // is_float = true; + // // number.push(c); + // // self.advance(); + // } + // _ => {} + // } + // // if c == 'f' { + // // is_float = true; + // // number.push(c); + // // self.advance(); + // // dbg!(&number); + // // break; + // // } + // match c { + // 'f' => { + // is_float = true; + // number.push(c); + // self.advance(); + // break; + // } + // _ => {} + // } + // } else + // /* if c.is_alphabetic() */ + // { + // match self.peek().unwrap_or(' ') { + // 'f' => { + // is_float = true; + // number.push(c); + // self.advance(); + // break; + // } + // _ => {} + // } + // is_swizzle = true; + // number.push(c); + // self.advance(); + // } + // } + // if is_float { + // return crate::tokens::Token::FLOATCONSTANT(number); + // } + // if is_swizzle { + // let split: Vec<&str> = number.split('.').collect(); + // let ident2 = split[1]; + // return crate::tokens::Token::Swizzle(vec![".".to_string(), ident2.to_string()]); + // } + // + // crate::tokens::Token::INTCONSTANT(number) + // } + pub fn consume_comment(&mut self) -> crate::tokens::Token { let mut comment = String::new(); while let Some(c) = self.current_char { diff --git a/src/lex/lexer.rs b/src/lex/lexer.rs index cf42ec5..fe21f4e 100644 --- a/src/lex/lexer.rs +++ b/src/lex/lexer.rs @@ -2,7 +2,6 @@ use crate::tokens::Token; use std::collections::HashMap; use std::sync::Arc; - pub struct Lexer { /// GLSL source pub input: Vec, @@ -17,9 +16,10 @@ pub struct Lexer { #[macro_export] macro_rules! lex { ($source:expr) => {{ - $crate::lexer::Lexer::get_tokens(&mut $crate::Lexer::new($source)) + $crate::lex::lexer::Lexer::get_tokens(&mut $crate::lex::lexer::Lexer::new($source)) }}; } +pub use lex; impl Lexer { pub fn new(input: &str) -> Self { diff --git a/src/lib.rs b/src/lib.rs index 8e9dba4..b472b24 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,20 +25,39 @@ mod tokens; #[cfg(test)] mod tests { + use super::lex; use super::lex::lexer::Lexer; use super::tokens::{Image, Token}; #[test] fn float_with_f_aggot() { let source = "5f"; - let mut lexer = Lexer::new(source); - let tokens = Lexer::get_tokens(&mut lexer); + let tokens = lex!(source); assert_eq!( tokens, vec![Token::FLOATCONSTANT("5f".to_string()), Token::EOF].into() ) } + #[test] + fn float_with_f_aggot_two() { + let source = "5.1f"; + let tokens = lex!(source); + assert_eq!( + tokens, + vec![Token::FLOATCONSTANT("5.1f".to_string()), Token::EOF].into() + ) + } + #[test] + fn float_with_f_aggot_three() { + let source = "5.f"; + let tokens = lex!(source); + assert_eq!( + tokens, + vec![Token::FLOATCONSTANT("5.f".to_string()), Token::EOF].into() + ) + } + #[test] fn keyword() { let source = "image1D";