From 74053a26ae3d9aa75619c0ec36f849471a18ed99 Mon Sep 17 00:00:00 2001 From: mStar <12024604-mstarongitlab@users.noreply.gitlab.com> Date: Mon, 5 Feb 2024 15:52:55 +0100 Subject: [PATCH] no idea anymore --- src/parser/preparser.rs | 87 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/src/parser/preparser.rs b/src/parser/preparser.rs index 64dd87b..298808a 100644 --- a/src/parser/preparser.rs +++ b/src/parser/preparser.rs @@ -1,5 +1,90 @@ +use std::fmt::Write; + // Prepares the given raw code to be consumed by the tokeniser // It achieves this by adding missing semicolons and adjusting whitespace (normalising to one space for everything outside of strings) pub fn preparse(raw: &str) -> String { - String::from("value") + let result = clear_spaces(raw); + + result +} + +fn clear_spaces(raw: &str) -> String { + let mut cleared = String::new(); + + let mut currently_quoted = false; + let mut quote_is_single = false; + let mut inside_quoted_codeblock = false; + let mut previous_char = 'a'; + + for c in raw.chars() { + if currently_quoted { + cleared.write_char(c.clone()).expect("Failed to write character to out string during whitespace normalisation"); + previous_char = c; + } else { + match c { + '"' => { + if currently_quoted && !quote_is_single && !inside_quoted_codeblock { + currently_quoted = false; + } else if !currently_quoted { + currently_quoted = true; + quote_is_single = false; + // Just overwrite it here already in case of some weird stuff + // Assume that only ever one codeblock will be nested in a string + // If there's more, this code will explode + // Not really, but spaces might get messed up + inside_quoted_codeblock = false; + } + cleared.write_char(c.clone()).expect("Failed to write character to out string during whitespace normalisation"); + } + '\'' => { + if currently_quoted && quote_is_single { + currently_quoted = false; + } else { + currently_quoted = true; + quote_is_single = true; + } + cleared.write_char(c.clone()).expect("Failed to write character to out string during whitespace normalisation"); + } + '{' => { + if currently_quoted && !inside_quoted_codeblock && !quote_is_single { + inside_quoted_codeblock = true; + } else { + cleared.write_char(c.clone()).expect("Failed to write character to out string during whitespace normalisation"); + } + } + '}' => { + if currently_quoted && !inside_quoted_codeblock && !quote_is_single { + inside_quoted_codeblock = false; + } else { + cleared.write_char(c.clone()).expect("Failed to write character to out string during whitespace normalisation"); + } + } + x => { + if !previous_char.is_whitespace() && !x.is_whitespace() { + cleared.write_char(x.clone()).expect("Failed to write character to out string during whitespace normalisation"); + } + previous_char = x; + + } + } + } + } + + cleared +} + +#[test] +fn test_clear_spaces1() { + assert_eq!(clear_spaces(" "), " ".to_owned()) +} + +#[test] +fn test_clear_spaces2() { + let res = clear_spaces("foo bar"); + assert_eq!(res, "foo bar".to_owned()) +} + +#[test] +fn test_clear_spaces3() { + assert_eq!(clear_spaces("\"foo bar\""), "\"foo bar\"".to_owned()) } \ No newline at end of file