create new function to contain the final case, add a new type of structure comments

This commit is contained in:
primardj 2024-01-16 14:11:40 +00:00
parent 02278d482c
commit 40cf5ee7ad
2 changed files with 43 additions and 23 deletions

View file

@ -12,6 +12,7 @@ pub struct Structure {
* - def variable
* - def function
* - pipes
* - commentaires
*/
name: String,
@ -26,7 +27,7 @@ impl Structure {
* parametter define if the command is in background or not. For now, parametter can only be
* either "" or "&"
*/
fn new_commande(cont: Vec<String>) -> Self {
pub fn new_commande(cont: Vec<String>) -> Self {
Self {
name: String::from("commande"),
contents: cont,
@ -77,6 +78,14 @@ impl Structure {
contents: cont,
}
}
fn new_commentaire(cont: Vec<String>) -> Self {
Self {
name: String::from("commentaire"),
contents: cont,
}
}
pub fn empty() -> Self {
Self {
name: String::new(),

View file

@ -68,10 +68,36 @@ fn parser_word(word: String) -> Structure {
// TODO
Structure::empty()
// Dans le cas ou aucunes autres structures ne peuvent être crées,
Structure::new_commande(vec![word])
//Structure::empty()
}
fn parse_initial(mut entree: Entree, mut restant_a_parser: Vec<char>, stack_last_quote: Vec<char>, last_word: String, mut struct_in_construct: Structure) -> Entree {
fn parse_cas_fin_de_chaine(mut entree: Entree, stack_last_quote: Vec<char>, last_word: String, mut struct_in_construct: Structure) -> Entree {
// Cas final, dans le cas ou le parser recoit un caractère vide.
// Si il reste des quotes dans la stack, ajoutter des structures commentaires à la fin TODO
if struct_in_construct.is_empty() {
struct_in_construct = parser_word(last_word);
if struct_in_construct.is_empty() {
// Renvoyer entree sans rien ajoutter dedans car la structure est vide & c
// la fin de l'entree.
entree
}
else {
// Sinon, il y as une structure donc il faut l'ajoutter à entree avant de le
// renvoyer.
entree.add_structure(struct_in_construct);
entree
}
} else {
entree.add_structure(struct_in_construct.add_to_contents(last_word));
entree
}
}
fn parse_recursif(mut entree: Entree, mut restant_a_parser: Vec<char>, stack_last_quote: Vec<char>, last_word: String, mut struct_in_construct: Structure) -> Entree {
// Est le parseur initial, comence ca recursivitée avec une entree vide, une ligne de command
// en char, une pile vide, un mot vide, et une structure vide.
//
@ -94,30 +120,15 @@ fn parse_initial(mut entree: Entree, mut restant_a_parser: Vec<char>, stack_last
// Etape 0; Lis le premier caractère de ce qu'il reste à parser.
let current_char = restant_a_parser.pop();
// Etape 1, test cas ou la chaine est vide
// Etape 1, test cas ou la chaine est vide 'Cas final'
if current_char.is_none() {
// Mettre tout ce cas la dans une fonction et renvoyer le résultat de la fonction.
if struct_in_construct.is_empty() {
struct_in_construct = parser_word(last_word);
if struct_in_construct.is_empty() {
entree // Renvoyer entree sans rien ajoutter dedans car la structure est vide & c
// la fin de l'entree.
}
else {
entree.add_structure(struct_in_construct); // Sinon, il y as une structure donc il
// faut l'ajoutter à entree avant de le
// renvoyer.
entree
}
} else {
entree.add_structure(struct_in_construct.add_to_contents(last_word));
entree
}
parse_cas_fin_de_chaine(entree, stack_last_quote, last_word, struct_in_construct)
} else {
// Etape 2, regarde si la stack des quotes est vide
// TODO
entree
entree // Ici faudra rappeler le parse_recursif pour passer au caractère suivant.
}
}
@ -134,6 +145,6 @@ pub fn parse2(command_line: &String) -> Entree {
let mut empty_word: String = String::new();
// Etape 2, lancer le parser recursif. Il retournera une Entree bien parsé comme il faut.
parse_initial( entree, char_splitted_command_line, stack_char_empty, empty_word, Structure::empty())
parse_recursif( entree, char_splitted_command_line, stack_char_empty, empty_word, Structure::empty())
}