rash/src/parser.rs

37 lines
1.1 KiB
Rust
Raw Normal View History

2024-04-11 19:09:19 +02:00
use crate::command::command_builder::CommandBuilder;
2024-04-11 10:16:09 +02:00
use crate::command::command_sequence::CommandSequence;
2024-04-10 12:31:29 +02:00
2024-04-14 12:07:42 +02:00
fn build_argv(characters: &mut Vec<char>, current_arg: &mut String) -> Vec<String> {
if characters.is_empty() {
vec![]
} else {
let current_char = characters.pop().unwrap();
if current_char.is_whitespace() {
if !current_arg.is_empty() {
let mut argv: Vec<String> = vec![current_arg.clone()];
argv.append(&mut build_argv(characters, &mut String::from("")));
argv
} else {
build_argv(characters, current_arg)
}
} else {
current_arg.push(current_char);
build_argv(characters, current_arg)
}
}
}
pub fn parse_command_line(line: String) -> Option<CommandSequence> {
2024-04-14 12:07:42 +02:00
let mut characters: Vec<char> = line.chars().rev().collect();
let argv = build_argv(&mut characters, &mut String::from(""));
2024-04-10 12:31:29 +02:00
if !argv.is_empty() {
let command = CommandBuilder::new(argv).build();
2024-04-11 19:09:19 +02:00
Some(CommandSequence::new(command))
} else {
None
}
2024-04-10 12:31:29 +02:00
}