parser : prise en compte des simple quotes

This commit is contained in:
Ahurac 2024-04-14 12:40:09 +02:00
parent 04fd13b801
commit bb5598aee7

View file

@ -1,6 +1,22 @@
use crate::command::command_builder::CommandBuilder;
use crate::command::command_sequence::CommandSequence;
fn parse_quote(characters: &mut Vec<char>) -> Result<String, UnmatchedQuoteError> {
if characters.is_empty() {
Err(UnmatchedQuoteError)
} else {
let mut quoted_arg = String::default();
let mut current_char = characters.pop().unwrap();
while !characters.is_empty() && current_char != '\'' {
quoted_arg.push(current_char);
current_char = characters.pop().unwrap();
}
Ok(quoted_arg)
}
}
fn build_argv(characters: &mut Vec<char>, current_arg: &mut String) -> Vec<String> {
if characters.is_empty() {
vec![]
@ -16,6 +32,11 @@ fn build_argv(characters: &mut Vec<char>, current_arg: &mut String) -> Vec<Strin
} else {
build_argv(characters, current_arg)
}
} else if current_char == '\'' {
let mut argv = vec![parse_quote(characters).unwrap()];
argv.append(&mut build_argv(characters, &mut String::default()));
argv
} else {
current_arg.push(current_char);
build_argv(characters, current_arg)
@ -25,7 +46,7 @@ fn build_argv(characters: &mut Vec<char>, current_arg: &mut String) -> Vec<Strin
pub fn parse_command_line(line: String) -> Option<CommandSequence> {
let mut characters: Vec<char> = line.chars().rev().collect();
let argv = build_argv(&mut characters, &mut String::from(""));
let argv = build_argv(&mut characters, &mut String::default());
if !argv.is_empty() {
let command = CommandBuilder::new(argv).build();
@ -34,3 +55,6 @@ pub fn parse_command_line(line: String) -> Option<CommandSequence> {
None
}
}
#[derive(Debug)]
struct UnmatchedQuoteError;