summaryrefslogtreecommitdiff
path: root/src/analysis/scan/grammar.y
blob: c0aa52d09aa93a59d4ecc762efcaf77e165fba4e (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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022

%{

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


/* 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 **, size_t *, size_t *, char *);

%}


%code requires {

#define YY_TYPEDEF_YY_SCANNER_T
typedef void *yyscan_t;

#include "core.h"
#include "scanner.h"
#include "exprs/access.h"
#include "exprs/arithmetic.h"
#include "exprs/call.h"
#include "exprs/counter.h"
#include "exprs/handler.h"
#include "exprs/intersect.h"
#include "exprs/item.h"
#include "exprs/literal.h"
#include "exprs/logical.h"
#include "exprs/set.h"
#include "exprs/relational.h"
#include "exprs/strop.h"
#include "patterns/modifier.h"
#include "patterns/modifiers/list.h"
#include "patterns/tokens/hex.h"
#include "patterns/tokens/plain.h"
#include "patterns/tokens/nodes/plain.h"
#include "../../core/logs.h"

}

%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                */




    sized_string_t *tmp_cstring;            /* Série d'octets reconstituée */

    struct {
        bin_t byte;                         /* Valeur partielle recherchée */
        uint8_t mask;                       /* Masque associé              */
    } semi_mask;



    GScanRule *rule;                        /* Nouvelle règle à intégrer   */



    GScanTokenNode *node;                   /* Bribe de motif à intégrer   */
    GSearchPattern *pattern;                /* Nouveau motif à considérer  */

    GScanTokenModifier *modifier;


    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 } { sized_string_t *tmp_0} { sized_string_t *tmp_1} { void /*GBytesPattern*/ **built_pattern } { char **buf } { size_t *allocated } { size_t *used }
%lex-param { yyscan_t yyscanner } { sized_string_t *tmp_0} { sized_string_t *tmp_1} { 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, sized_string_t *tmp_0, sized_string_t *tmp_1, 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 BYTES_ID
%token BYTES_ID_COUNTER
%token BYTES_ID_START
%token BYTES_ID_LENGTH
%token BYTES_ID_END
%token NAME


%token HEX_BYTES
%token FULL_MASK
%token SEMI_MASK


%token REGEX_BYTES
%token REGEX_CLASSES
%token REGEX_RANGE



%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 TILDE            "~"

%token HOOK_O           "["
%token HOOK_C           "]"

%token BRACKET_O        "{"
%token BRACKET_C        "}"
%token QUESTION         "?"

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

%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> BYTES_ID
%type <sized_cstring> BYTES_ID_COUNTER
%type <sized_cstring> BYTES_ID_START
%type <sized_cstring> BYTES_ID_LENGTH
%type <sized_cstring> BYTES_ID_END
%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 <tmp_cstring> HEX_BYTES
%type <unsigned_integer> FULL_MASK
%type <semi_mask> SEMI_MASK

%type <tmp_cstring> REGEX_BYTES


%type <modifier> modifiers
%type <modifier> _modifiers
%type <modifier> chained_modifiers
%type <modifier> mod_stage
%type <modifier> modifier

%type <pattern> hex_pattern
%type <node> hex_tokens
%type <node> hex_token



%type <expr> cexpression _cexpression

%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> set_access
%type <expr> intersection
%type <expr> pattern_handler





%left PIPE



%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



%left HOOK_O HOOK_C




%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
                   | hex_pattern
                   {
                       if ($1 == NULL) YYERROR;
                       g_scan_rule_add_local_variable(*built_rule, $1);
                       g_object_unref(G_OBJECT($1));
                   }
                   | regex_pattern
                   | string_decls string_decl
                   | string_decls hex_pattern
                   {
                       if ($2 == NULL) YYERROR;
                       g_scan_rule_add_local_variable(*built_rule, $2);
                       g_object_unref(G_OBJECT($2));
                   }
                   | string_decls regex_pattern
                   ;

string_decl : BYTES_ID ASSIGN PLAIN_STRING modifiers
            {
                GSearchPattern *__pat;
                __pat = g_scan_plain_bytes_new(&$3, NULL, SPBF_NONE);
                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));
                */
            }
            | BYTES_ID 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);
                */
            }
            ;


/**
 * Prise en charge des modificateurs.
 */

         modifiers : /* empty */
                   {
                       $$ = NULL;
                   }
                   | _modifiers
                   {

                    // if (...) useless

                   }
                   ;

        _modifiers : mod_stage
                   {
                       $$ = $1;
                   }
                   | chained_modifiers
                   {
                       $$ = $1;
                   }
                   ;

 chained_modifiers : _modifiers "|" _modifiers
                   ;

         mod_stage : modifier
                   {
                       $$ = $1;
                   }
                   | mod_stage modifier
                   {
                       bool status;

                       if (G_IS_SCAN_MODIFIER_LIST($1))
                           $$ = $1;
                       else
                       {
                           $$ = g_scan_modifier_list_new();
                           g_scan_modifier_list_add(G_SCAN_MODIFIER_LIST($$), $1);
                       }

                       status = g_scan_modifier_list_add(G_SCAN_MODIFIER_LIST($$), $2);
                       if (!status)
                       {
                           if (1)
                               log_simple_message(LMT_WARNING, "modifier already taken into account!");
                           g_object_unref(G_OBJECT($2));
                       }

                   }
                   ;

          modifier : NAME
                   {
                       $$ = find_scan_token_modifiers_for_name($1.data);
                       if ($$ == NULL) YYERROR;
                   }
                   | "(" chained_modifiers ")"
                   {
                       $$ = $2;
                   }
                   ;

/**
 * Définition de motif en hexadécimal.
 */

       hex_pattern : BYTES_ID ASSIGN hex_tokens
                   {
                       $$ = g_scan_hex_bytes_new($3);
                       g_search_pattern_set_name($$, $1.data, $1.len);
                   }
                   ;

        hex_tokens : hex_token
                   {
                       $$ = $1;
                   }
                   | hex_tokens hex_token
                   {

                   }
                   ;

         hex_token : HEX_BYTES
                   {
                       $$ = g_scan_token_node_plain_new($1, NULL, SPNF_NONE);
                   }
                   | FULL_MASK
                   {
                       printf("mask len: %llu\n", $1);
                   }
                   | SEMI_MASK
                   {
                       printf("semi mask: %hhx / %hhx \n", $1.byte, $1.mask);
                   }
                   | hex_range
                   {
                       printf("...range...\n");
                   }
                   | "~" hex_token
                   {

                       printf("hex -- NOT --\n");

                   }
                   | "(" hex_token "|" hex_token ")"
                   {

                       printf("hex -- OR --\n");

                   }
                   ;

         hex_range : "[" "-" "]"
                   {

                       printf("got inf range\n");

                   }
                   | "[" UNSIGNED_INTEGER "]"
                   {

                       printf("got range [%llu]\n", $2);

                   }
                   | "[" UNSIGNED_INTEGER "-" "]"
                   {

                       printf("got range [%llu -> ]\n", $2);

                   }
                   | "[" "-" UNSIGNED_INTEGER "]"
                   {

                       printf("got range [ -> %llu]\n", $3);

                   }
                   | "[" UNSIGNED_INTEGER "-" UNSIGNED_INTEGER "]"
                   {

                       printf("got range [%llu -> %llu]\n", $2, $4);

                   }
                   ;

/**
 * Définition de motif sous forme d'expression régulière
 */

     regex_pattern : BYTES_ID ASSIGN regex_tokens
                   {

                   }
                   ;

      regex_tokens : regex_token
                   {

                   }
                   | regex_tokens regex_token
                   {

                   }
                   | "(" regex_tokens_list ")"
                   {

                       printf("regex -- OR --\n");

                   }
                   | regex_tokens "(" regex_tokens_list ")"
                   {

                       printf("regex -- OR --\n");

                   }
                   ;


 regex_tokens_list : regex_tokens
                   | regex_tokens_list "|" regex_tokens
                   ;


       regex_token : _regex_token
                   {

                   }
                   | _regex_token regex_repeat
                   {

                   }
                   ;

      _regex_token : DOT
                   {
                       printf("reg dot!\n");
                   }
                   | REGEX_BYTES
                   {
                       printf("reg bytes: '%s' (l=%zu)\n", $1->data, $1->len);
                   }
                   | REGEX_CLASSES
                   {
                       printf("reg class!\n");
                   }
                   | "[" REGEX_RANGE "]"
                   {
                       printf("reg range!\n");
                   }
                   ;

      regex_repeat : "*"
                   {
                       printf("  .. repeat: *\n");
                   }
                   | "+"
                   {
                       printf("  .. repeat: +\n");
                   }
                   | "?"
                   {
                       printf("  .. repeat: ?\n");
                   }
                   | "{" UNSIGNED_INTEGER "}"
                   {

                       printf("  .. repeat {%llu}\n", $2);

                   }
                   | "{" UNSIGNED_INTEGER "," "}"
                   {

                       printf("  .. repeat {%llu,}\n", $2);

                   }
                   | "{" "," UNSIGNED_INTEGER "}"
                   {

                       printf("  .. repeat {,%llu}\n", $3);

                   }
                   | "{" UNSIGNED_INTEGER "," UNSIGNED_INTEGER "}"
                   {

                       printf("  .. repeat {%llu,%llu}\n", $2, $4);

                   }
                   ;



/**
 * Définition des conditions.
 */

      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 : literal { $$ = $1; }
                   | item_chain { $$ = $1; }
                   | logical_expr { $$ = $1; }
                   | relational_expr { $$ = $1; }
                   | string_op { $$ = $1; }
                   | arithm_expr { $$ = $1; }
                   | set_counter { $$ = $1; }
                   | set { $$ = $1; }
                   | set_access { $$ = $1; }
                   | intersection { $$ = $1; }
                   | pattern_handler { $$ = $1; }
                   | "(" cexpression ")" { $$ = $2; }
                   ;

        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));
                }
                ;

        set_access : cexpression "[" cexpression "]"
                   {
                       $$ = g_scan_set_item_new($1, $3);
                       g_object_unref(G_OBJECT($1));
                       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));
                   }
                   ;

   pattern_handler : BYTES_ID
                   {
                       GSearchPattern *__pat;
                       __pat = g_scan_rule_get_local_variable(*built_rule, $1.data);
                       if (__pat == NULL)
                           $$ = NULL;
                       else
                       {
                           $$ = g_scan_pattern_handler_new(__pat, SHT_RAW);
                           g_object_unref(G_OBJECT(__pat));
                       }
                   }
                   | 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));
                       }
                   }
                   | BYTES_ID_START
                   {
                       GSearchPattern *__pat;
                       __pat = g_scan_rule_get_local_variable(*built_rule, $1.data);
                       if (__pat == NULL)
                           $$ = NULL;
                       else
                       {
                           $$ = g_scan_pattern_handler_new(__pat, SHT_START);
                           g_object_unref(G_OBJECT(__pat));
                       }
                   }
                   | BYTES_ID_LENGTH
                   {
                       GSearchPattern *__pat;
                       __pat = g_scan_rule_get_local_variable(*built_rule, $1.data);
                       if (__pat == NULL)
                           $$ = NULL;
                       else
                       {
                           $$ = g_scan_pattern_handler_new(__pat, SHT_LENGTH);
                           g_object_unref(G_OBJECT(__pat));
                       }
                   }
                   | BYTES_ID_END
                   {
                       GSearchPattern *__pat;
                       __pat = g_scan_rule_get_local_variable(*built_rule, $1.data);
                       if (__pat == NULL)
                           $$ = NULL;
                       else
                       {
                           $$ = g_scan_pattern_handler_new(__pat, SHT_END);
                           g_object_unref(G_OBJECT(__pat));
                       }
                   }
                   ;

%%


/******************************************************************************
*                                                                             *
*  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, sized_string_t *tmp_0, sized_string_t *tmp_1, 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       */
    sized_string_t tmp_0;                   /* Zone tampon #1              */
    sized_string_t tmp_1;                   /* Zone tampon #2              */
    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;

    tmp_0.data = malloc((length + 1) * sizeof(bin_t));
    tmp_0.len = 0;

    tmp_1.data = malloc((length + 1) * sizeof(bin_t));
    tmp_1.len = 0;

    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, &tmp_0, &tmp_1, &built_pattern, &buf, &allocated, &used);

    result = (status == EXIT_SUCCESS);

    yy_delete_buffer(state, lexstate);

    rost_lex_destroy(lexstate);

    exit_szstr(&tmp_0);
    exit_szstr(&tmp_1);

    free(buf);

    return result;

}