no idea anymore
This commit is contained in:
parent
ce402276bc
commit
74053a26ae
1 changed files with 86 additions and 1 deletions
|
@ -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())
|
||||
}
|
Loading…
Reference in a new issue