This repository has been archived on 2024-04-09. You can view files and clone it, but cannot push or open issues or pull requests.
rash.old/src/parser.rs

48 lines
1.7 KiB
Rust
Raw Normal View History

fn parse_after_whitespace(char_splitted_command_line: &mut Vec<char>, argv: &mut Vec<String>) -> Vec<String> {
let current_char_option = char_splitted_command_line.pop();
if current_char_option.is_none() {
argv.to_vec()
} else {
let current_char = current_char_option.unwrap();
if current_char.is_whitespace() {
parse_after_whitespace(char_splitted_command_line, argv)
} else {
parse_initial_state(char_splitted_command_line, &mut String::from(current_char.to_string()), argv)
}
}
}
fn parse_initial_state(char_splitted_command_line: &mut Vec<char>, current_arg: &mut String, argv: &mut Vec<String>) -> Vec<String> {
let current_char_option = char_splitted_command_line.pop();
if current_char_option.is_none() {
argv.push(current_arg.to_string());
argv.to_vec()
} else {
let current_char = current_char_option.unwrap();
if current_char.is_whitespace() {
argv.push(current_arg.to_string());
parse_after_whitespace(char_splitted_command_line, argv)
} else {
current_arg.push(current_char);
parse_initial_state(char_splitted_command_line, current_arg, argv)
}
}
}
fn split_string_in_chars(string_to_split: &String) -> Vec<char> {
string_to_split.chars().rev().collect()
}
pub fn parse(command_line: &String) -> Vec<String> {
let mut argv: Vec<String> = Vec::new();
let mut char_splitted_command_line: Vec<char> = split_string_in_chars(&command_line);
let mut new_arg = String::new();
argv = parse_initial_state(&mut char_splitted_command_line, &mut new_arg, &mut argv);
argv
}