Première version du parser

Capable de découper des mots en fonction des whitespaces
This commit is contained in:
Hippolyte Chauvin 2023-10-28 14:35:43 +02:00
parent 0ec9935938
commit de900a725a
2 changed files with 53 additions and 3 deletions

View file

@ -1,3 +1,9 @@
mod parser;
fn main() {
println!("Hello, world!");
let command_line = String::from("ls -l");
let command_line_as_array: Vec<String> = parser::parse(&command_line);
println!("{:?}", command_line_as_array);
}

View file

@ -1,3 +1,47 @@
pub fn parse(command_line: String) -> Vec<String> {
todo!();
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
}