parser : un backslash suivi d'un newline demande à l'utilisateur d'entrer à nouveau

This commit is contained in:
Ahurac 2024-04-19 12:10:38 +02:00
parent 1e72ff63f8
commit fb09cfc8c6
3 changed files with 32 additions and 23 deletions

View file

@ -10,7 +10,7 @@ pub fn run() {
let mut variables = Variables::new();
loop {
let user_input = get_user_input();
let user_input = get_user_input("$ ");
if user_input.is_some() {
let mut command_sequence = parse(user_input.unwrap(), &variables);

View file

@ -1,13 +1,9 @@
use std::io::Write;
use std::io::{stdin, stdout};
fn print_prompt() {
print!("$ ");
pub fn get_user_input(prompt: &str) -> Option<String> {
print!("{}", prompt);
stdout().flush().unwrap();
}
pub fn get_user_input() -> Option<String> {
print_prompt();
let mut user_input = String::new();

View file

@ -1,53 +1,66 @@
use crate::command::command_builder::CommandBuilder;
use crate::command::command_sequence::CommandSequence;
use crate::interface::get_user_input;
use crate::variables::Variables;
use std::collections::VecDeque;
fn parse_variable(characters: &mut Vec<char>, variables: &Variables) -> String {
fn parse_variable(characters: &mut VecDeque<char>, variables: &Variables) -> String {
if characters.is_empty() {
String::new()
} else {
let mut current_char = characters.pop().unwrap();
let mut current_char = characters.pop_front().unwrap();
let mut var_name = String::new();
while !characters.is_empty() && !current_char.is_whitespace() {
var_name.push(current_char);
current_char = characters.pop().unwrap();
current_char = characters.pop_front().unwrap();
}
if current_char.is_whitespace() {
characters.push(current_char);
characters.push_front(current_char);
}
variables.get(var_name.as_str())
}
}
fn parse_quote(characters: &mut Vec<char>) -> String {
fn parse_quote(characters: &mut VecDeque<char>) -> String {
let mut quoted = String::new();
if !characters.is_empty() {
let mut current_char = characters.pop().unwrap();
let mut current_char = characters.pop_front().unwrap();
while !characters.is_empty() && current_char != '\'' {
quoted.push(current_char);
current_char = characters.pop().unwrap();
current_char = characters.pop_front().unwrap();
}
}
quoted
}
fn parse_backslash(characters: &mut Vec<char>) -> String {
let escaped_char = match characters.is_empty() {
false => String::from(characters.pop().unwrap()),
true => String::new(),
};
fn parse_backslash(characters: &mut VecDeque<char>) -> String {
let next_character: char;
escaped_char
if !characters.is_empty() {
next_character = characters.pop_front().unwrap();
if next_character == '\n' {
let mut new_characters: VecDeque<char> =
get_user_input("> ").unwrap().chars().collect();
characters.append(&mut new_characters);
String::new()
} else {
String::from(next_character)
}
} else {
String::new()
}
}
fn parse_main(
characters: &mut Vec<char>,
characters: &mut VecDeque<char>,
current_arg: &mut String,
variables: &Variables,
) -> Vec<String> {
@ -60,7 +73,7 @@ fn parse_main(
last_arg
} else {
let current_char = characters.pop().unwrap();
let current_char = characters.pop_front().unwrap();
if current_char.is_whitespace() {
if !current_arg.is_empty() {
@ -96,7 +109,7 @@ fn parse_main(
}
pub fn parse(line: String, variables: &Variables) -> CommandSequence {
let mut characters: Vec<char> = line.chars().rev().collect();
let mut characters: VecDeque<char> = line.chars().collect();
let mut command_sequence = CommandSequence::new();
while !characters.is_empty() {