From 40cf5ee7ad094e8b9247b7c82ab3f772267099a7 Mon Sep 17 00:00:00 2001 From: primardj Date: Tue, 16 Jan 2024 14:11:40 +0000 Subject: [PATCH] create new function to contain the final case, add a new type of structure comments --- src/objects/structure.rs | 11 +++++++- src/parser.rs | 55 ++++++++++++++++++++++++---------------- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/objects/structure.rs b/src/objects/structure.rs index 94714e2..387eff5 100644 --- a/src/objects/structure.rs +++ b/src/objects/structure.rs @@ -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) -> Self { + pub fn new_commande(cont: Vec) -> Self { Self { name: String::from("commande"), contents: cont, @@ -77,6 +78,14 @@ impl Structure { contents: cont, } } + + fn new_commentaire(cont: Vec) -> Self { + Self { + name: String::from("commentaire"), + contents: cont, + } + } + pub fn empty() -> Self { Self { name: String::new(), diff --git a/src/parser.rs b/src/parser.rs index d1b568a..7b3128e 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -67,11 +67,37 @@ fn parser_word(word: String) -> Structure { // une structure 'commande' // 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, stack_last_quote: Vec, last_word: String, mut struct_in_construct: Structure) -> Entree { +fn parse_cas_fin_de_chaine(mut entree: Entree, stack_last_quote: Vec, 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, stack_last_quote: Vec, 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, 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()) }