summaryrefslogtreecommitdiff
path: root/src/analysis/scan/grammar.y
blob: 10e1d4246719a4cd7fb67774867cccc6e64062c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631

%{

#include "decl.h"
#include "tokens.h"


/* Affiche un message d'erreur suite à l'analyse en échec. */
static int yyerror(GContentScanner *, yyscan_t, GScanRule **, void/*GBytesPattern*/ **, char **, size_t *, size_t *, char *);

%}


%code requires {

#define YY_TYPEDEF_YY_SCANNER_T
typedef void *yyscan_t;

#include "scanner.h"
#include "exprs/access.h"
#include "exprs/arithmetic.h"
#include "exprs/call.h"
#include "exprs/counter.h"
#include "exprs/intersect.h"
#include "exprs/literal.h"
#include "exprs/logical.h"
#include "exprs/set.h"
#include "exprs/relational.h"
#include "exprs/strop.h"
#include "patterns/tokens/plain.h"


#if 0 /////////////////////////////////////////////////////////////////////////::
#define handle_coder_conversions(c, r)                              \
    ({                                                              \
        encoding_spec *__spec;                                      \
        encoding_syntax *__syntax;                                  \
        conv_list *__list;                                          \
        bool __status;                                              \
        __spec = get_current_encoding_spec(c);                      \
        __syntax = get_current_encoding_syntax(__spec);             \
        __list = get_conversions_in_encoding_syntax(__syntax);      \
        __status = load_convs_from_raw_block(__list, r);            \
        if (!__status) YYABORT;                                     \
    })
#endif ///////////////////////////////////////////////////////////////////////////

}

%union {

    unsigned long long unsigned_integer;    /* Valeur entière #1           */
    signed long long signed_integer;        /* Valeur entière #2           */
    //double floating_number;                 /* Valeur à virgule flottante  */
    sized_string_t sized_cstring;           /* Chaîne de caractères        */
    //char byte;                              /* Octet unique                */





    GScanRule *rule;                        /* Nouvelle règle à intégrer   */
    void/*GBytesPattern*/ *pattern;                 /* Nouveau motif à considérer  */
    GScanExpression *expr;                  /* Expression de condition     */

    struct {
        GScanExpression **args;             /* Liste d'arguments à fournir */
        size_t count;                       /* Quantité de ces arguments   */
    } args_list;

}


/**
 * Cf.
 * http://stackoverflow.com/questions/34418381/how-to-reference-lex-or-parse-parameters-in-flex-rules/34420950
 */

%define api.pure full

%parse-param { GContentScanner *scanner } { yyscan_t yyscanner } { GScanRule **built_rule } { void /*GBytesPattern*/ **built_pattern } { char **buf } { size_t *allocated } { size_t *used }
%lex-param { yyscan_t yyscanner } { void/*GBytesPattern*/ **built_pattern } { char **buf } { size_t *allocated } { size_t *used }

%code provides {

#define YY_DECL \
    int rost_lex(YYSTYPE *yylval_param, yyscan_t yyscanner, void/*GBytesPattern*/ **built_pattern, char **buf, size_t *allocated, size_t *used)

YY_DECL;

}


%token INCLUDE          "include"

%token RAW_RULE
%token RULE_NAME

%token STRINGS CONDITION
%token IDENTIFIER
%token BYTES_ID
%token BYTES_ID_COUNTER
%token BYTES_ID_LOCATION
%token BYTES_ID_LENGTH
%token NAME

%token BRACE_IN BRACE_OUT ASSIGN COLON


%token PLAIN_STRING
%token MASKED_STRING

%token TRUE_            "true"
%token FALSE_           "false"
%token SIGNED_INTEGER
%token UNSIGNED_INTEGER
%token STRING

%token KB MB GB

%token AND              "and"
%token OR               "or"
%token NOT              "not"

%token LT               "<"
%token LE               "<="
%token EQ               "=="
%token NE               "!="
%token GT               ">"
%token GE               ">="

%token CONTAINS     "contains"
%token STARTSWITH   "startswith"
%token ENDSWITH     "endswith"
%token MATCHES      "matches"
%token ICONTAINS    "icontains"
%token ISTARTSWITH  "istartswith"
%token IENDSWITH    "iendswith"
%token IEQUALS      "iequals"

%token PLUS             "+"
%token MINUS            "-"
%token MUL              "*"
%token DIV              "/"
%token MOD              "%"

%token PAREN_O          "("
%token PAREN_C          ")"
%token COMMA            ","
%token DOT              "."

%token NONE             "none"
%token ANY              "any"
%token ALL              "all"
%token OF               "of"
%token THEM             "them"
%token IN               "in"


%type <sized_cstring> RULE_NAME


%type <sized_cstring> IDENTIFIER BYTES_ID_COUNTER
%type <sized_cstring> NAME


%type <signed_integer> SIGNED_INTEGER
%type <unsigned_integer> UNSIGNED_INTEGER
%type <sized_cstring> STRING

%type <rule> rule

%type <sized_cstring> PLAIN_STRING
%type <pattern> MASKED_STRING

%type <expr> cexpression _cexpression
%type <expr> pattern_match
%type <expr> literal
%type <expr> item_chain
%type <args_list> call_args
%type <expr> logical_expr
%type <expr> relational_expr
%type <expr> string_op
%type <expr> arithm_expr
%type <expr> set_counter
%type <expr> set
%type <expr> set_items
%type <expr> intersection


%left OR
%left AND
%left EQ NE
%left CONTAINS STARTSWITH ENDSWITH MATCHES ICONTAINS ISTARTSWITH IENDSWITH IEQUALS
%left LT LE GT GE
%left PLUS MINUS
%left MUL DIV MOD
%left IN
%right NOT



%destructor { printf("-------- Discarding symbol %p.\n", $$); } <rule>


%%



 /*


<raw_block>[ \t\n]+             { }
<raw_block>"{"                  {
                                    read_block(temp);
                                    yylvalp->cstring = temp; return RAW_BLOCK;
                                }
<raw_block>"}"                  { yy_pop_state(); }

  */


rules : /* empty */
      | external rules
      | rule rules { g_content_scanner_add_rule(scanner, $1); }

        //rule : RAW_RULE RULE_NAME { printf("RULE %s\n", $2); } RAW_BLOCK { printf("BLOCK: %s\n", $4); }


external : "include" STRING
         {
             bool __status;
             __status = g_content_scanner_include_resource(scanner, $2.data);
             if (!__status)
                 YYERROR;
         }


rule : RAW_RULE RULE_NAME
     {
         //printf("--------built rule '%s'\n", $2.data);
         *built_rule = g_scan_rule_new($2.data);
         $<rule>$ = *built_rule;
     }
     BRACE_IN strings condition BRACE_OUT
     {
         $$ = $<rule>3;
         //printf("RULE %s -> %p\n", $2, $$);
         //printf("end of rule\n");
     } 




strings : /* empty */
        | STRINGS COLON string_decls
        ;


string_decls : string_decl
             | string_decls string_decl
             ;

string_decl : IDENTIFIER ASSIGN PLAIN_STRING
            {
                GSearchPattern *__pat;
                __pat = g_plain_bytes_new((uint8_t *)$3.data, $3.len);
                g_search_pattern_set_name(__pat, $1.data, $1.len);
                g_scan_rule_add_local_variable(*built_rule, __pat);
                g_object_unref(G_OBJECT(__pat));

                /*
                string_token_t *__token;
                //printf("built plain %s\n", $3.cstring);
                GBytesPattern *__pat;
                __token = create_plain_string_token($3.cstring, $3.len);
                printf("token: %p\n", __token);
                __pat = g_bytes_pattern_new();
                g_bytes_pattern_append_string(__pat, $3.cstring, $3.len);
                g_scan_rule_add_local_variable(*built_rule, $1, G_SEARCH_PATTERN(__pat));
                g_object_unref(G_OBJECT(__pat));
                */
            }
            | IDENTIFIER ASSIGN MASKED_STRING
            {
                printf("built %p\n", $3);
                /*
                GBytesPattern *__pat;
                __pat = g_bytes_pattern_new();
                g_search_pattern_set_name(__pat, $1.cstring, $1.len);
                g_bytes_pattern_append_string(__pat, "\xd9\x74\x24\xf4", 4);
                g_scan_rule_add_local_variable(*built_rule, G_SEARCH_PATTERN(__pat));
                */
                /*
                GSearchPattern *__pat;
                __pat = G_SEARCH_PATTERN($3);
                if (g_search_pattern_prepare(__pat))
                    g_scan_rule_add_local_variable(*built_rule, $1, __pat);
                g_clear_object(built_pattern);
                */
            }
            ;

      condition : CONDITION COLON cexpression
                {
                    g_scan_rule_set_match_condition(*built_rule, $3);
                    g_object_unref(G_OBJECT($3));
                }
                ;

    cexpression : _cexpression { $$ = $1; if ($$ == NULL) { printf("ERROR !!!\n"); YYERROR; } }

   _cexpression : IDENTIFIER
                {
                printf("named var: %s\n", "$1");
                $$ = NULL;
                /*
                   GSearchPattern *__pat;
                   GMatchCounter *__counter;
                   __pat = g_scan_rule_get_local_variable(*built_rule, $1);
                   if (__pat != NULL)
                   {
                       __counter = g_match_counter_new(__pat);
                       g_scan_rule_add_condition(*built_rule, G_MATCH_CONDITION(__counter));
                       g_object_unref(G_OBJECT(__counter));
                       g_object_unref(G_OBJECT(__pat));
                   }
                */
                }
                | literal { $$ = $1; }
                | pattern_match { $$ = $1; }
                | item_chain { $$ = $1; }
                | logical_expr { $$ = $1; }
                | relational_expr { $$ = $1; }
                | string_op { $$ = $1; }
                | arithm_expr { $$ = $1; }
                | set_counter { $$ = $1; }
                | set { $$ = $1; }
                | intersection { $$ = $1; }
                | "(" cexpression ")" { $$ = $2; }
                ;

  pattern_match : BYTES_ID_COUNTER
                {
                    GSearchPattern *__pat;
                    __pat = g_scan_rule_get_local_variable(*built_rule, $1.data);
                    if (__pat == NULL)
                        $$ = NULL;
                    else
                    {
                        $$ = g_scan_match_counter_new(__pat);
                        g_object_unref(G_OBJECT(__pat));
                    }
                }
                ;

        literal : "true"
                {
                    $$ = g_scan_literal_expression_new(LVT_BOOLEAN, (bool []){ true });
                }
                | "false"
                {
                    $$ = g_scan_literal_expression_new(LVT_BOOLEAN, (bool []){ false });
                }
                | SIGNED_INTEGER
                {
                    $$ = g_scan_literal_expression_new(LVT_SIGNED_INTEGER, &$1);
                }
                | UNSIGNED_INTEGER
                {
                    $$ = g_scan_literal_expression_new(LVT_UNSIGNED_INTEGER, &$1);
                }
                | UNSIGNED_INTEGER KB
                {
                    unsigned long long __converted;
                    __converted = $1 * 1024;
                    $$ = g_scan_literal_expression_new(LVT_UNSIGNED_INTEGER, &__converted);
                }
                | UNSIGNED_INTEGER MB
                {
                    unsigned long long __converted;
                    __converted = $1 * 1048576;
                    $$ = g_scan_literal_expression_new(LVT_UNSIGNED_INTEGER, &__converted);
                }
                | UNSIGNED_INTEGER GB
                {
                    unsigned long long __converted;
                    __converted = $1 * 1073741824;
                    $$ = g_scan_literal_expression_new(LVT_UNSIGNED_INTEGER, &__converted);
                }
                | STRING
                {
                    $$ = g_scan_literal_expression_new(LVT_STRING, &$1);
                }
                ;

     item_chain : NAME { $$ = g_scan_named_access_new(&$1); }
                | NAME "(" ")" { $$ = g_scan_pending_call_new(&$1, NULL, 0); }
                | NAME "(" call_args ")"
                {
                    size_t __i;
                    $$ = g_scan_pending_call_new(&$1, $3.args, $3.count);
                    for (__i = 0; __i < $3.count; __i++)
                        g_object_unref(G_OBJECT($3.args[__i]));
                    free($3.args);
                }
                | item_chain "." NAME
                {
                    GScanExpression *__next;
                    __next = g_scan_named_access_new(&$3);
                    g_scan_named_access_attach_next(G_SCAN_NAMED_ACCESS($1), G_SCAN_NAMED_ACCESS(__next));
                    $$ = $1;
                }
                | item_chain "." NAME "(" ")"
                {
                    GScanExpression *__next;
                    __next = g_scan_pending_call_new(&$3, NULL, 0);
                    g_scan_named_access_attach_next(G_SCAN_NAMED_ACCESS($1), G_SCAN_NAMED_ACCESS(__next));
                    $$ = $1;
                }
                | item_chain "." NAME "(" call_args ")"
                {
                    GScanExpression *__next;
                    size_t __i;
                    __next = g_scan_pending_call_new(&$3, $5.args, $5.count);
                    for (__i = 0; __i < $5.count; __i++)
                        g_object_unref(G_OBJECT($5.args[__i]));
                    free($5.args);
                    g_scan_named_access_attach_next(G_SCAN_NAMED_ACCESS($1), G_SCAN_NAMED_ACCESS(__next));
                    $$ = $1;
                }
                ;

      call_args : cexpression
                {
                    $$.count = 1;
                    $$.args = malloc(sizeof(GScanExpression *));
                    $$.args[0] = $1;
                }
                | call_args "," cexpression
                {
                    $1.count++;
                    $1.args = realloc($1.args, $1.count * sizeof(GScanExpression *));
                    $1.args[$1.count - 1] = $3;
                    $$ = $1;
                }
                ;

   logical_expr : cexpression "and" cexpression { $$ = g_scan_logical_operation_new(BOT_AND, $1, $3); }
                | cexpression "or" cexpression  { $$ = g_scan_logical_operation_new(BOT_OR, $1, $3); }
                | "not" "(" cexpression ")"     { $$ = g_scan_logical_operation_new(BOT_NOT, $3, NULL); }
                ;

relational_expr : cexpression "<" cexpression  { $$ = g_scan_relational_operation_new(RCO_LT, $1, $3); }
                | cexpression "<=" cexpression { $$ = g_scan_relational_operation_new(RCO_LE, $1, $3); }
                | cexpression "==" cexpression { $$ = g_scan_relational_operation_new(RCO_EQ, $1, $3); }
                | cexpression "!=" cexpression { $$ = g_scan_relational_operation_new(RCO_NE, $1, $3); }
                | cexpression ">" cexpression  { $$ = g_scan_relational_operation_new(RCO_GT, $1, $3); }
                | cexpression ">=" cexpression { $$ = g_scan_relational_operation_new(RCO_GE, $1, $3); }
                ;

      string_op : cexpression "contains" cexpression
                {
                    $$ = g_scan_string_operation_new(SOT_CONTAINS, $1, $3, true);
                }
                | cexpression "startswith" cexpression
                {
                    $$ = g_scan_string_operation_new(SOT_STARTSWITH, $1, $3, true);
                }
                | cexpression "endswith" cexpression
                {
                    $$ = g_scan_string_operation_new(SOT_ENDSWITH, $1, $3, true);
                }
                | cexpression "matches" cexpression
                {
                    $$ = g_scan_string_operation_new(SOT_MATCHES, $1, $3, true);
                }
                | cexpression "icontains" cexpression
                {
                    $$ = g_scan_string_operation_new(SOT_CONTAINS, $1, $3, false);
                }
                | cexpression "istartswith" cexpression
                {
                    $$ = g_scan_string_operation_new(SOT_STARTSWITH, $1, $3, false);
                }
                | cexpression "iendswith" cexpression
                {
                    $$ = g_scan_string_operation_new(SOT_ENDSWITH, $1, $3, false);
                }
                | cexpression "iequals" cexpression
                {
                    $$ = g_scan_string_operation_new(SOT_IEQUALS, $1, $3, false);
                }
                ;

    arithm_expr : cexpression "+" cexpression { $$ = g_scan_arithmetic_operation_new(AEO_PLUS, $1, $3); }
                | cexpression "-" cexpression { $$ = g_scan_arithmetic_operation_new(AEO_MINUS, $1, $3); }
                | cexpression "*" cexpression { $$ = g_scan_arithmetic_operation_new(AEO_MUL, $1, $3); }
                | cexpression "/" cexpression { $$ = g_scan_arithmetic_operation_new(AEO_DIV, $1, $3); }
                | cexpression "%" cexpression { $$ = g_scan_arithmetic_operation_new(AEO_MOD, $1, $3); }
                ;

set_counter : "none" "of" "them" { $$ = g_scan_literal_expression_new(LVT_BOOLEAN, (bool []){ true }); }
            | "any" "of" "them"  { $$ = g_scan_literal_expression_new(LVT_BOOLEAN, (bool []){ true }); }
            | "all" "of" "them"  { $$ = g_scan_literal_expression_new(LVT_BOOLEAN, (bool []){ true }); }
            ;


            set : "(" ")"
                {
                    $$ = g_scan_generic_set_new();
                }
                | "(" cexpression "," ")"
                {
                    $$ = g_scan_generic_set_new();
                    g_scan_generic_set_add_item(G_SCAN_GENERIC_SET($$), $2);
                    g_object_unref(G_OBJECT($2));
                }
                | "(" set_items ")"
                {
                    $$ = $2;
                }
                ;

      set_items : cexpression "," cexpression
                {
                    $$ = g_scan_generic_set_new();
                    g_scan_generic_set_add_item(G_SCAN_GENERIC_SET($$), $1);
                    g_object_unref(G_OBJECT($1));
                    g_scan_generic_set_add_item(G_SCAN_GENERIC_SET($$), $3);
                    g_object_unref(G_OBJECT($3));
                }
                | set_items "," cexpression
                {
                    $$ = $1;
                    g_scan_generic_set_add_item(G_SCAN_GENERIC_SET($$), $3);
                    g_object_unref(G_OBJECT($3));
                }
                ;

   intersection : cexpression "in" cexpression
                {
                    $$ = g_scan_sets_intersection_new($1, $3);
                    g_object_unref(G_OBJECT($1));
                    g_object_unref(G_OBJECT($3));
                }
                ;




%%


/******************************************************************************
*                                                                             *
*  Paramètres  : scanner = décodeur impliqué dans le processus.               *
*                temp    = zone de travail à destination des lectures.        *
*                msg     = message d'erreur.                                  *
*                                                                             *
*  Description : Affiche un message d'erreur suite à l'analyse en échec.      *
*                                                                             *
*  Retour      : 0                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static int yyerror(GContentScanner *scanner, yyscan_t yyscanner, GScanRule **built_rule, void/*GBytesPattern*/ **built_pattern, char **buf, size_t *allocated, size_t *used, char *msg)
{
	printf("YYERROR line %d: %s\n", yyget_lineno(yyscanner), msg);

	return 0;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : scanner = chercheur de motifs à préparer.                    *
*                text    = définitions des règles à charger.                  *
*                length  = longueur de ces définitions.                       *
*                                                                             *
*  Description : Complète une recherche de motifs avec des règles.            *
*                                                                             *
*  Retour      : Bilan à retourner.                                           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool process_rules_definitions(GContentScanner *scanner, const char *text, size_t length)
{
    bool result;                            /* Bilan à renvoyer            */
    GScanRule *built_rule;                  /* Règle en construction       */
    void /*GBytesPattern*/ *built_pattern;           /* Motif en construction       */
    char *buf;                              /* Zone de travail temporaire  */
    size_t allocated;                       /* Taille de mémoire allouée   */
    size_t used;                            /* Quantité utilisée           */
    yyscan_t lexstate;                      /* Gestion d'analyse lexicale  */
    YY_BUFFER_STATE state;                  /* Contexte d'analyse          */
    int status;                             /* Bilan d'une analyse         */

    result = false;

    built_rule = NULL;
    built_pattern = NULL;

    allocated = 256;
    used = 0;

    buf = malloc(allocated * sizeof(char));
    buf[0] = '\0';

    rost_lex_init(&lexstate);

    state = rost__scan_bytes(text, length, lexstate);

    status = yyparse(scanner, lexstate, &built_rule, &built_pattern, &buf, &allocated, &used);

    result = (status == EXIT_SUCCESS);

    yy_delete_buffer(state, lexstate);

    rost_lex_destroy(lexstate);

    free(buf);

    return result;

}