From 194ebd377f27499a3010b2bbd7697cb91da510a8 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Thu, 16 Nov 2023 01:30:49 +0100
Subject: Clean the Python script converting the ROST grammar.

---
 src/analysis/scan/grammar.y | 52 ++++++++++++++++++++++++++-------------------
 src/analysis/scan/tokens.l  | 25 ----------------------
 2 files changed, 30 insertions(+), 47 deletions(-)

diff --git a/src/analysis/scan/grammar.y b/src/analysis/scan/grammar.y
index 8380a81..8298855 100644
--- a/src/analysis/scan/grammar.y
+++ b/src/analysis/scan/grammar.y
@@ -6,10 +6,10 @@
 
 
 /* Affiche un message d'erreur suite à l'analyse en échec. */
-static int yyerror(GContentScanner *, yyscan_t, GScanRule **, sized_string_t *, sized_string_t *, void/*GBytesPattern*/ **, char *);
+static int yyerror(GContentScanner *, yyscan_t, GScanRule **, sized_string_t *, sized_string_t *, char *);
 
 #define raise_error(msg) \
-    yyerror(scanner, yyscanner, built_rule, tmp_0, tmp_1, NULL, msg)
+    yyerror(scanner, yyscanner, built_rule, tmp_0, tmp_1, msg)
 
 %}
 
@@ -76,7 +76,6 @@ typedef void *yyscan_t;
     } masked;
 
     ScanRuleFlags rule_flags;               /* Fanions pour règle          */
-    GScanRule *rule;                        /* Nouvelle règle à intégrer   */
 
     GScanTokenNode *node;                   /* Bribe de motif à intégrer   */
     GSearchPattern *pattern;                /* Nouveau motif à considérer  */
@@ -85,7 +84,6 @@ typedef void *yyscan_t;
     modifier_arg_t mod_arg;                 /* Argument pour modificateur  */
     ScanPlainNodeFlags str_flags;           /* Fanions pour texte          */
 
-
     GScanExpression *expr;                  /* Expression de condition     */
 
     struct {
@@ -103,13 +101,13 @@ typedef void *yyscan_t;
 
 %define api.pure full
 
-%parse-param { GContentScanner *scanner } { yyscan_t yyscanner } { GScanRule **built_rule } { sized_string_t *tmp_0} { sized_string_t *tmp_1} { void /*GBytesPattern*/ **built_pattern }
-%lex-param { yyscan_t yyscanner } { sized_string_t *tmp_0} { sized_string_t *tmp_1} { void/*GBytesPattern*/ **built_pattern }
+%parse-param { GContentScanner *scanner } { yyscan_t yyscanner } { GScanRule **built_rule } { sized_string_t *tmp_0} { sized_string_t *tmp_1}
+%lex-param { yyscan_t yyscanner } { sized_string_t *tmp_0} { sized_string_t *tmp_1}
 
 %code provides {
 
 #define YY_DECL \
-    int rost_lex(YYSTYPE *yylval_param, yyscan_t yyscanner, sized_string_t *tmp_0, sized_string_t *tmp_1, void/*GBytesPattern*/ **built_pattern)
+    int rost_lex(YYSTYPE *yylval_param, yyscan_t yyscanner, sized_string_t *tmp_0, sized_string_t *tmp_1)
 
 YY_DECL;
 
@@ -246,7 +244,6 @@ YY_DECL;
 
 %type <rule_flags> rule_flags
 %type <rule_flags> rule_flag
-%type <rule> rule
 
 %type <sized_cstring> PLAIN_TEXT
 %type <tmp_cstring> ESCAPED_TEXT
@@ -323,20 +320,36 @@ YY_DECL;
 %left HOOK_O HOOK_C
 
 
+       %destructor { g_object_unref(G_OBJECT($$)); } <node>
+
+       %destructor { g_object_unref(G_OBJECT($$)); } <pattern>
+
+       %destructor { g_object_unref(G_OBJECT($$)); } <modifier>
+
+       %destructor { g_object_unref(G_OBJECT($$)); } <expr>
+
+       %destructor {
+                       size_t __i;
+
+                       for (__i = 0; __i < $$.count; __i++)
+                           g_object_unref(G_OBJECT($$.args[__i]));
 
+                       if ($$.args != NULL)
+                           free($$.args);
 
-%destructor { printf("-------- Discarding symbol %p.\n", $$); } <rule>
+                   } <args_list>
 
 
 %%
 
              rules : /* empty */
                    | external rules
-                   | rule rules
+                   | rule
                    {
-                       g_content_scanner_add_rule(scanner, $1);
-                       g_object_unref(G_OBJECT($1));
+                       g_content_scanner_add_rule(scanner, *built_rule);
+                       g_clear_object(built_rule);
                    }
+                   rules
                    ;
 
 
@@ -368,12 +381,8 @@ YY_DECL;
               rule : rule_flags "rule" RULE_IDENTIFIER
                    {
                        *built_rule = g_scan_rule_new($1, $3.data);
-                       $<rule>$ = *built_rule;
                    }
                    tags "{" meta bytes condition "}"
-                   {
-                       $$ = $<rule>4;
-                   }
                    ;
 
 
@@ -1880,9 +1889,9 @@ relational_expr : cexpression "<" cexpression
 *                                                                             *
 ******************************************************************************/
 
-static int yyerror(GContentScanner *scanner, yyscan_t yyscanner, GScanRule **built_rule, sized_string_t *tmp_0, sized_string_t *tmp_1, void/*GBytesPattern*/ **built_pattern, char *msg)
+static int yyerror(GContentScanner *scanner, yyscan_t yyscanner, GScanRule **built_rule, sized_string_t *tmp_0, sized_string_t *tmp_1, char *msg)
 {
-	printf("YYERROR line %d: %s\n", yyget_lineno(yyscanner), msg);
+	//printf("YYERROR line %d: %s\n", yyget_lineno(yyscanner), msg);
 
 	return 0;
 
@@ -1909,7 +1918,6 @@ bool process_rules_definitions(GContentScanner *scanner, const char *text, size_
     GScanRule *built_rule;                  /* Règle en construction       */
     sized_string_t tmp_0;                   /* Zone tampon #1              */
     sized_string_t tmp_1;                   /* Zone tampon #2              */
-    void /*GBytesPattern*/ *built_pattern;           /* Motif en construction       */
     yyscan_t lexstate;                      /* Gestion d'analyse lexicale  */
     YY_BUFFER_STATE state;                  /* Contexte d'analyse          */
     int status;                             /* Bilan d'une analyse         */
@@ -1924,13 +1932,11 @@ bool process_rules_definitions(GContentScanner *scanner, const char *text, size_
     tmp_1.data = malloc((length + 1) * sizeof(bin_t));
     tmp_1.len = 0;
 
-    built_pattern = NULL;
-
     rost_lex_init(&lexstate);
 
     state = rost__scan_bytes(text, length, lexstate);
 
-    status = yyparse(scanner, lexstate, &built_rule, &tmp_0, &tmp_1, &built_pattern);
+    status = yyparse(scanner, lexstate, &built_rule, &tmp_0, &tmp_1);
 
     result = (status == EXIT_SUCCESS);
 
@@ -1941,6 +1947,8 @@ bool process_rules_definitions(GContentScanner *scanner, const char *text, size_
     exit_szstr(&tmp_0);
     exit_szstr(&tmp_1);
 
+    g_clear_object(&built_rule);
+
     return result;
 
 }
diff --git a/src/analysis/scan/tokens.l b/src/analysis/scan/tokens.l
index b1543c9..0dff9f4 100644
--- a/src/analysis/scan/tokens.l
+++ b/src/analysis/scan/tokens.l
@@ -982,31 +982,6 @@ bytes_fuzzy_id [\*A-Za-z_][\*A-Za-z0-9_]*
 
 
 
-<bytes_value>"\""                    {
-                                    POP_STATE;
-                                    // *built_pattern = g_bytes_pattern_new();
-                                    PUSH_STATE(bytes_value_raw);
-                                }
-
-<bytes_value_raw>"\""                { POP_STATE; /*yylval->pattern = *built_pattern*/; return 11111/*MASKED_STRING*/; }
-
-<bytes_value_raw>"\\\""              { }//g_bytes_pattern_append_data(*built_pattern, '"', 0xff); }
-<bytes_value_raw>"\\t"               { }//g_bytes_pattern_append_data(*built_pattern, '\t', 0xff); }
-<bytes_value_raw>"\\r"               { }//g_bytes_pattern_append_data(*built_pattern, '\r', 0xff); }
-<bytes_value_raw>"\\n"               { }//g_bytes_pattern_append_data(*built_pattern, '\n', 0xff); }
-<bytes_value_raw>"\\\\"              { }//g_bytes_pattern_append_data(*built_pattern, '\\', 0xff); }
-
-<bytes_value_raw>\\x[0-9a-fA-F]{2} {
-                                    uint8_t __ch;
-                                    __ch = strtol(yytext + 2, NULL, 16);
-                                    printf("__ch: %hhx\n", __ch);
-                                    //g_bytes_pattern_append_data(*built_pattern, __ch, 0xff);
-                                }
-
-<bytes_value_raw>.                   { }//g_bytes_pattern_append_data(*built_pattern, *yytext, 0xff); }
-
-
-
 
 
 
-- 
cgit v0.11.2-87-g4458