frostwalker/src/formatter.rs

43 lines
1.3 KiB
Rust
Raw Normal View History

use super::{Token, Class};
use std::collections::HashMap;
pub fn format(tree: Vec<Token>) -> HashMap<String, String> {
let mut output = HashMap::new();
let mut current_key = "".to_string();
let mut current_index;
let mut i = 0;
while i < tree.len() {
if tree[i].class == Class::IDENTIFIER {
current_key = tree[i].value.clone().unwrap_or("".to_string());
i = i + 1;
continue;
}
if (tree[i].class == Class::LITERAL || tree[i].class == Class::BOOLEAN) && current_key != "".to_string() {
output.insert(current_key, tree[i].value.clone().unwrap_or("".to_string()));
current_key = "".to_string();
i = i + 1;
continue;
}
if tree[i].class == Class::SEPARATOR && current_key != "".to_string() && tree[i].value.clone().unwrap_or("".to_string()) == "[".to_string() {
current_index = 0;
loop {
i = i + 1;
output.insert(format!("{}[{}]", current_key, current_index), tree[i].value.clone().unwrap_or("".to_string()));
current_index = current_index + 1;
i = i + 1;
if tree[i].class == Class::SEPARATOR && tree[i].value.clone().unwrap_or("".to_string()) == ",".to_string() {
continue;
} else {
break;
}
}
output.insert(current_key.clone(), (current_index).to_string());
}
i = i + 1;
}
return output;
}