began writing parser first pass
This commit is contained in:
parent
952c01abfc
commit
315d97b358
1 changed files with 54 additions and 4 deletions
58
src/lib.rs
58
src/lib.rs
|
@ -6,22 +6,72 @@ mod tests {
|
|||
use crate::*;
|
||||
|
||||
#[test]
|
||||
fn tests() {
|
||||
fn test_00() {
|
||||
dbg!(parse_args("Hello world", 0x00));
|
||||
}
|
||||
#[test]
|
||||
fn test_01() {
|
||||
dbg!(parse_args("\"Hello world\'\"", 0x07));
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses the command line arguments into a list of parameters.
|
||||
pub fn parse_args(input: &str, mode: u8) -> Vec<String> {
|
||||
let input: Vec<_> = input.chars().map(|arg| arg.clone()).collect();
|
||||
let input: Vec<_> = input.chars().map(|arg| arg.clone()).collect(); // Converts from the input string to a vector of chars.
|
||||
dbg!(&input);
|
||||
|
||||
match mode {
|
||||
/// Returns exact input string.
|
||||
0x00 => {vec![String::from_iter(input.iter())]},
|
||||
0x00 => {vec![String::from_iter(input.iter())]}, // Simply converts the vector of chars into a string, and puts it as the first element.
|
||||
/// Seperates all strings
|
||||
0x07 => {
|
||||
// First pass: Quoting
|
||||
let mut single_quote_begin: Vec<usize> = vec![]; // List of beginning single quotation marks.
|
||||
let mut single_quote_end: Vec<usize> = vec![]; // List of ending single quotation marks.
|
||||
let mut is_single_quoted: bool = false; // Boolean showing if the current contents are quoted.
|
||||
|
||||
let mut double_quote_begin: Vec<usize> = vec![]; // List of beginning double quotation marks.
|
||||
let mut double_quote_end: Vec<usize> = vec![]; // List of ending double quotation marks.
|
||||
let mut is_double_quoted: bool = false; // boolean showing if the current contents are quoted.
|
||||
|
||||
for (i, c) in input.iter().enumerate() {
|
||||
let c = *c;
|
||||
|
||||
|
||||
let escaped = if i != 0 {*input.get(i - 1).unwrap() == '\\'} else {false} // Checks if previous character is backslash (and index is not zero
|
||||
&& if i > 1 {*input.get(i - 2).unwrap() == '\\'} else {false}; // Checks if 2 characters ago is backslash (and index is greater than 1)
|
||||
match c {
|
||||
|
||||
'\'' => { // Single quoted stuff will always be taken literally, no matter what they contain. See the bash manual§3.1.2.2.
|
||||
if !escaped && !is_double_quoted {
|
||||
if is_single_quoted {
|
||||
single_quote_end.push(i);
|
||||
is_single_quoted = false;
|
||||
} else {
|
||||
single_quote_begin.push(i);
|
||||
is_single_quoted = true;
|
||||
}
|
||||
}
|
||||
},
|
||||
'\"' => { // Double quoted stuff works kind of like graves do in JavaScript. See the bash manual§3.1.2.3.
|
||||
if !escaped && !is_single_quoted {
|
||||
if is_double_quoted {
|
||||
double_quote_end.push(i);
|
||||
is_double_quoted = false;
|
||||
} else {
|
||||
double_quote_begin.push(i);
|
||||
is_double_quoted = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if single_quote_begin.len() != single_quote_end.len() {
|
||||
panic!("Hanging single quotation mark: {}", single_quote_begin.get(single_quote_begin.len()).unwrap_or(&0));
|
||||
}
|
||||
if double_quote_begin.len() != double_quote_end.len() {
|
||||
panic!("Hanging double quotation mark: {}", double_quote_begin.get(double_quote_begin.len()).unwrap_or(&0));
|
||||
}
|
||||
vec![]
|
||||
},
|
||||
///
|
||||
|
|
Loading…
Reference in a new issue