parser : un backslash suivi d'un newline demande à l'utilisateur d'entrer à nouveau
This commit is contained in:
parent
1e72ff63f8
commit
fb09cfc8c6
3 changed files with 32 additions and 23 deletions
|
@ -10,7 +10,7 @@ pub fn run() {
|
||||||
let mut variables = Variables::new();
|
let mut variables = Variables::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let user_input = get_user_input();
|
let user_input = get_user_input("$ ");
|
||||||
|
|
||||||
if user_input.is_some() {
|
if user_input.is_some() {
|
||||||
let mut command_sequence = parse(user_input.unwrap(), &variables);
|
let mut command_sequence = parse(user_input.unwrap(), &variables);
|
||||||
|
|
|
@ -1,13 +1,9 @@
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::io::{stdin, stdout};
|
use std::io::{stdin, stdout};
|
||||||
|
|
||||||
fn print_prompt() {
|
pub fn get_user_input(prompt: &str) -> Option<String> {
|
||||||
print!("$ ");
|
print!("{}", prompt);
|
||||||
stdout().flush().unwrap();
|
stdout().flush().unwrap();
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_user_input() -> Option<String> {
|
|
||||||
print_prompt();
|
|
||||||
|
|
||||||
let mut user_input = String::new();
|
let mut user_input = String::new();
|
||||||
|
|
||||||
|
|
|
@ -1,53 +1,66 @@
|
||||||
use crate::command::command_builder::CommandBuilder;
|
use crate::command::command_builder::CommandBuilder;
|
||||||
use crate::command::command_sequence::CommandSequence;
|
use crate::command::command_sequence::CommandSequence;
|
||||||
|
use crate::interface::get_user_input;
|
||||||
use crate::variables::Variables;
|
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() {
|
if characters.is_empty() {
|
||||||
String::new()
|
String::new()
|
||||||
} else {
|
} else {
|
||||||
let mut current_char = characters.pop().unwrap();
|
let mut current_char = characters.pop_front().unwrap();
|
||||||
let mut var_name = String::new();
|
let mut var_name = String::new();
|
||||||
|
|
||||||
while !characters.is_empty() && !current_char.is_whitespace() {
|
while !characters.is_empty() && !current_char.is_whitespace() {
|
||||||
var_name.push(current_char);
|
var_name.push(current_char);
|
||||||
current_char = characters.pop().unwrap();
|
current_char = characters.pop_front().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
if current_char.is_whitespace() {
|
if current_char.is_whitespace() {
|
||||||
characters.push(current_char);
|
characters.push_front(current_char);
|
||||||
}
|
}
|
||||||
|
|
||||||
variables.get(var_name.as_str())
|
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();
|
let mut quoted = String::new();
|
||||||
|
|
||||||
if !characters.is_empty() {
|
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 != '\'' {
|
while !characters.is_empty() && current_char != '\'' {
|
||||||
quoted.push(current_char);
|
quoted.push(current_char);
|
||||||
current_char = characters.pop().unwrap();
|
current_char = characters.pop_front().unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
quoted
|
quoted
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_backslash(characters: &mut Vec<char>) -> String {
|
fn parse_backslash(characters: &mut VecDeque<char>) -> String {
|
||||||
let escaped_char = match characters.is_empty() {
|
let next_character: char;
|
||||||
false => String::from(characters.pop().unwrap()),
|
|
||||||
true => String::new(),
|
|
||||||
};
|
|
||||||
|
|
||||||
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(
|
fn parse_main(
|
||||||
characters: &mut Vec<char>,
|
characters: &mut VecDeque<char>,
|
||||||
current_arg: &mut String,
|
current_arg: &mut String,
|
||||||
variables: &Variables,
|
variables: &Variables,
|
||||||
) -> Vec<String> {
|
) -> Vec<String> {
|
||||||
|
@ -60,7 +73,7 @@ fn parse_main(
|
||||||
|
|
||||||
last_arg
|
last_arg
|
||||||
} else {
|
} else {
|
||||||
let current_char = characters.pop().unwrap();
|
let current_char = characters.pop_front().unwrap();
|
||||||
|
|
||||||
if current_char.is_whitespace() {
|
if current_char.is_whitespace() {
|
||||||
if !current_arg.is_empty() {
|
if !current_arg.is_empty() {
|
||||||
|
@ -96,7 +109,7 @@ fn parse_main(
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(line: String, variables: &Variables) -> CommandSequence {
|
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();
|
let mut command_sequence = CommandSequence::new();
|
||||||
|
|
||||||
while !characters.is_empty() {
|
while !characters.is_empty() {
|
||||||
|
|
Loading…
Reference in a new issue