File: pattern_uni.y

package info (click to toggle)
eclipse-titan 6.5.0-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 101,128 kB
  • sloc: cpp: 259,139; ansic: 47,560; yacc: 22,554; makefile: 14,074; sh: 12,630; lex: 9,101; xml: 5,362; java: 4,849; perl: 3,784; awk: 48; php: 32; python: 13
file content (668 lines) | stat: -rw-r--r-- 17,379 bytes parent folder | download
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
/******************************************************************************
 * Copyright (c) 2000-2018 Ericsson Telecom AB
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
 *
 * Contributors:
 *   Balasko, Jeno
 *   Raduly, Csaba
 *   Szalai, Gabor
 *   Zalanyi, Balazs Andor
 *
 ******************************************************************************/
 
/**
 *  Based on pattern_p.y
 */

%{

/*********************************************************************
 * C(++) declarations
 *********************************************************************/

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#if defined(__CYGWIN__) && defined(__clang__)
/* Cygwin's clang 3.0 has its own limits.h, which does not bring in
   the system's limits.h unless we define this macro: */
#define __STDC_HOSTED__ 1
#define _GCC_NEXT_LIMITS_H
#endif
#include <limits.h>

#include <regex.h>
#if !defined(RE_DUP_MAX)
/* RE_DUP_MAX is defined in limits.h or regex.h, except on Cygwin 1.5 */
# include <sys/syslimits.h>
#endif

#include "memory.h"
#include "pattern.hh"

#include "Quadruple.hh"
#include "UnicharPattern.hh"

  union YYSTYPE;
/* defined in lexer c-file: */

  extern int pattern_yylex();
  inline int pattern_unilex() { return pattern_yylex(); }
  extern void init_pattern_yylex(YYSTYPE*);
  struct yy_buffer_state;
  extern yy_buffer_state* pattern_yy_scan_string(const char*);
  extern void pattern_yy_delete_buffer(yy_buffer_state*);
  extern unsigned int get_nof_parentheses();

/* defined in this file: */

  /** The converted regexp. */
  static char *ret_val;
  /** The parser error reporting function. */
  static void pattern_unierror(const char *error_str);

  static int user_groups;

  static bool nocase;

#define YYERROR_VERBOSE

static void yyprint(FILE *file, int type, const YYSTYPE& value);
#define YYPRINT(f,t,v) yyprint(f,t,v)

%}

/*********************************************************************
 * Bison declarations
 *********************************************************************/

%name-prefix="pattern_uni"
%output="pattern_uni.cc"
%defines
%verbose
%expect 0
%start Pattern
%debug

/*********************************************************************
 * The union-type
 * Must be kept in sync with the one in pattern_p.y !
 *********************************************************************/

%union {
  int b; /* boolean */
  char c; /* single character */
  char *s; /* character string */
  unsigned long int u; /* unsigned integer */
  struct character_set *set; // used by nonterminals in pattern_p.y

  union {
    unsigned int value;
#if defined(__sparc__) || defined(__sparc)
    struct {
      unsigned char group;
      unsigned char plane;
      unsigned char row;
      unsigned char cell;
    } comp;
#else
    struct {
      unsigned char cell;
      unsigned char row;
      unsigned char plane;
      unsigned char group;
    } comp;
#endif
  } q;  // single universal char, used by nonterminals in pattern_uni.y
  class QuadSet* qset;  // used by nonterminals in pattern_uni.y
}

/*********************************************************************
 * Tokens
 *********************************************************************/

%token <c> TOK_Char "<ordinary character>"
%token <u> TOK_Number "<number>"
%token <u> TOK_Digit "<digit>"

/*********************************************************************
 * Keywords
 *********************************************************************/

%token KW_BS_q "\\q"
%token KW_BS_d "\\d"
%token KW_BS_w "\\w"
%token KW_BS_t "\\t"
%token KW_BS_n "\\n"
%token KW_BS_r "\\r"
%token KW_BS_s "\\s"
%token KW_BS_b "\\b"

%token KW_Group_Begin "("
%token KW_Group_End ")"
%token KW_Set_Begin "["
%token KW_Set_Begin_Neg "[^"
%token KW_Set_Begin_Rsbrkt "[]"
%token KW_Set_Begin_Neg_Rsbrkt "[^]"
%token KW_Set_End "]"
%token KW_Set_Dash_End "-]"

/*********************************************************************
 * semantic types of nonterminals
 *********************************************************************/

%type <b> RE_Set_Begin RE_Set_Begin_Rsbrkt RE_Set_End
%type <q> RE_Set_Range_Char RE_Quadruple
%type <s> RE_Body RE_Elems RE_Alter_Elem RE_Concat_Elem
          RE_Multiply_Elem RE_Multiply_Statement RE_Group
          RE_OneCharPos
%type <qset> RE_Set RE_Set_Body RE_Set_Elem RE_Set_NoRange_Char

/*********************************************************************
 * Destructors
 *********************************************************************/

%destructor { Free($$); }
RE_Alter_Elem
RE_Body
RE_Concat_Elem
RE_Elems
RE_Group
RE_Multiply_Elem
RE_Multiply_Statement
RE_OneCharPos

%destructor { delete $$; }
RE_Set
RE_Set_Body
RE_Set_Elem
RE_Set_NoRange_Char

%%

/*********************************************************************
 * Grammar
 *********************************************************************/

Pattern:
  RE_Body {ret_val=$1;}
;

RE_Body:
  /* empty */
  {
    $$ = mcopystr("^$");
  }
| RE_Elems
  {
    if ($1 != NULL) {
      $$ = mprintf("^%s$", $1);
      Free($1);
    } else $$ = mcopystr("^$");
  }
;

RE_Elems:
  RE_Alter_Elem { $$ = $1; }
| RE_Elems '|' RE_Alter_Elem
  {
    unsigned int nof_pars = get_nof_parentheses() + (yychar==KW_Group_End ? 1 : 0);
    if ($3 != NULL) {
      if ($1 != NULL) $$ = mputprintf($1, nof_pars ? "|%s" : "$|^%s", $3);
      else $$ = mprintf( nof_pars ? "()|%s" : "()$|^%s" , $3);
      Free($3);
    } else {
      if ($1 != NULL) $$ = mputstr($1, nof_pars ? "|()" : "$|^()");
      else $$ = NULL;
    }
  }
;

RE_Alter_Elem:
  RE_Concat_Elem { $$ = $1; }
| RE_Alter_Elem RE_Concat_Elem
  {
    $$ = mputstr($1, $2);
    Free($2);
  }
;

RE_Concat_Elem:
  RE_Multiply_Elem {$$=$1;}
| RE_Multiply_Elem RE_Multiply_Statement
  {
    if ($1 != NULL && $2 != NULL) {
      $$ = mputstr($1, $2);
      Free($2);
    } else {
      Free($1);
      Free($2);
      $$ = NULL;
    }
  }
| '*' {$$=mcopystr("(........)*");}
;

RE_Multiply_Elem:
  RE_Group {$$=$1;}
| RE_OneCharPos {$$=$1;}
;

RE_Group:
  KW_Group_Begin KW_Group_End
  {
    user_groups++;
    $$ = mcopystr("<)");
  }
| KW_Group_Begin RE_Elems KW_Group_End
  {
    user_groups++;
    if ($2 != NULL) {
      $$ = mprintf("<%s)", $2);
      Free($2);
    } else {
      $$ = mcopystr("<)");
    }
  }
;

RE_Multiply_Statement:
  '+'
  {
    $$ = mcopystr("+");
  }
| '#' '(' ',' ')'
  {
    $$ = mcopystr("*");
  }
| '#' TOK_Digit
  {
    if ($2 == 0) {
      TTCN_pattern_warning("The number of repetitions is zero: `#0'.");
      $$ = NULL;
    } else if ($2 == 1) $$ = memptystr();
    else {
      if ($2 > 9) TTCN_pattern_warning("Internal error: Invalid number of "
  "repetitions: `#%lu'.", $2);
      $$ = mprintf("{%lu}", $2);
    }
  }
| '#' '(' TOK_Number ')'
  {
    if ($3 == 0) {
      TTCN_pattern_warning("The number of repetitions is zero: `#(0)'.");
      $$ = NULL;
    } else if ($3 == 1) $$ = memptystr();
    else {
#ifdef RE_DUP_MAX
      if ($3 > RE_DUP_MAX) TTCN_pattern_warning("The number of repetitions in "
  "`#(%lu)' exceeds the limit allowed by this system (%d).", $3,
  RE_DUP_MAX);
#endif
      $$ = mprintf("{%lu}", $3);
    }
  }
| '#' '(' TOK_Number ',' TOK_Number ')'
  {
#ifdef RE_DUP_MAX
    if ($3 > RE_DUP_MAX) TTCN_pattern_warning("The minimum number of "
      "repetitions in `#(%lu,%lu)' exceeds the limit allowed by this system "
      "(%d).", $3, $5, RE_DUP_MAX);
    if ($5 > RE_DUP_MAX) TTCN_pattern_warning("The maximum number of "
      "repetitions in `#(%lu,%lu)' exceeds the limit allowed by this system "
      "(%d).", $3, $5, RE_DUP_MAX);
#endif
    if ($3 > $5) TTCN_pattern_error("The lower bound is higher than the upper "
      "bound in the number of repetitions: `#(%lu,%lu)'.", $3, $5);
    if ($3 == $5) {
      if ($3 == 0) {
  TTCN_pattern_warning("The number of repetitions is zero: `#(0,0)'.");
  $$ = NULL;
      } else if ($3 == 1) $$ = memptystr();
      else {
  $$ = mprintf("{%lu}", $3);
      }
    } else {

  if ($3 == 0 && $5 == 1) $$ = mcopystr("?");
  else $$ = mprintf("{%lu,%lu}", $3, $5);

    }
  }
| '#' '(' ',' TOK_Number ')'
  {
    if ($4 == 0) {
      TTCN_pattern_warning("The number of repetitions is zero: `#(,0)'.");
      $$ = NULL;
    } else {
#ifdef RE_DUP_MAX
      if ($4 > RE_DUP_MAX) TTCN_pattern_warning("The maximum number of "
  "repetitions in `#(,%lu)' exceeds the limit allowed by this system "
  "(%d).", $4, RE_DUP_MAX);
#endif

  if ($4 == 1) $$ = mcopystr("?");
  else $$ = mprintf("{0,%lu}", $4);

    }
  }
| '#' '(' TOK_Number ',' ')'
  {
    if ($3 == 0) $$ = mcopystr("*");
    else {
#ifdef RE_DUP_MAX
      if ($3 > RE_DUP_MAX) TTCN_pattern_warning("The minimum number of "
  "repetitions in `#(%lu,)' exceeds the limit allowed by this system "
  "(%d).", $3, RE_DUP_MAX);
#endif

  if ($3 == 1) $$ = mcopystr("+");
  else $$ = mprintf("{%lu,}", $3);

    }
  }
;

RE_OneCharPos:
  '?' {$$=mcopystr("(........)");}
| KW_BS_d {$$=mcopystr("(AAAAAAD[A-J])");}
| KW_BS_w {$$=mcopystr("(AAAAAAD[A-J]|AAAAAA(E[B-P]|F[A-K])|AAAAAA(G[B-P]|H[A-K]))");}
| KW_BS_t {$$=mcopystr("AAAAAAAJ");}
| KW_BS_n {$$=mcopystr("(AAAAAAA[K-N])");}
| KW_BS_r {$$=mcopystr("AAAAAAAN");}
| KW_BS_s {$$=mcopystr("(AAAAAAA[J-N]|AAAAAACA)");}
| KW_BS_b
  {
    TTCN_pattern_warning("Metacharacter `\\b' is not supported yet.");
    $$ = NULL;
  }
| TOK_Char
  {
    unsigned char c = $1;
    if ($1 <= 0) TTCN_pattern_error("Character with code %u "
      "(0x%02x) cannot be used in a pattern for type universal charstring.", $1, $1);
    $$ = Quad::get_hexrepr(nocase ? tolower(c) : c);
  }
| RE_Quadruple
  {
    $$ = Quad::get_hexrepr(nocase ?
      unichar_pattern.convert_quad_to_lowercase($1.value).get_value() : $1.value);
  }
| RE_Set
  {
    if ($1->is_empty()) {
      TTCN_pattern_error("Empty character set.");
      $$ = NULL;
    } else
      $$ = $1->generate_posix();
    delete $1;
  }
;

RE_Set:
  /* RE_Set_Begin        is 1 for "[^",  0 for "["
   * RE_Set_Begin_Rsbrkt is 1 for "[^]", 0 for "[]"
   * RE_Set_End          is 1 for "-]",  0 for "]"
   */
  RE_Set_Begin RE_Set_Body RE_Set_End
  {
    if ($2 != NULL)
      $$ = $2;
    else
      $$ = new QuadSet();
    if ($3 && !$$->add(new Quad('-')))
        TTCN_pattern_warning("Duplicate character `-' in the character set.");
    if ($1)
      $$->set_negate(true);
  }
| RE_Set_Begin '-' RE_Set_Body RE_Set_End
  {
    if ($3 != NULL)
      $$ = $3;
    else
      $$ = new QuadSet();
    if (!$$->add(new Quad('-')))
      TTCN_pattern_warning("Duplicate character `-' in the character set.");
    if ($1)
      $$->set_negate(true);
  }
| RE_Set_Begin_Rsbrkt RE_Set_Body RE_Set_End
  {
    if ($2 != NULL)
      $$ = $2;
    else
      $$ = new QuadSet();
    if (!$$->add(new Quad(']')))
      TTCN_pattern_warning("Duplicate character `]' in the character set.");
    if ($3 && !$$->add(new Quad('-')))
      TTCN_pattern_warning("Duplicate character `-' in the character set.");
    if ($1)
      $$->set_negate(true);
  }
| RE_Set_Begin_Rsbrkt '-' RE_Set_Range_Char RE_Set_Body RE_Set_End
  {
    if ($4 != NULL)
      $$ = $4;
    else
      $$ = new QuadSet();
    if (static_cast<unsigned int>(']') > $3.value) {
      TTCN_pattern_error("Invalid range in the character set: the "
        "character code of the lower bound (%u) is higher than that of the "
        "upper bound (%u).", ']', static_cast<unsigned int>($3.value));
    }
    $$->add(new QuadInterval(Quad(']'), Quad($3.value)));
    if ($5) {
      if (!$$->add(new Quad('-')))
        TTCN_pattern_warning("Duplicate character `-' in the character set.");
    }
    if ($1)
      $$->set_negate(true);
  }
;

RE_Set_Begin:
  KW_Set_Begin { $$ = 0; }
| KW_Set_Begin_Neg { $$ = 1; }
;

RE_Set_Begin_Rsbrkt:
  KW_Set_Begin_Rsbrkt { $$ = 0; }
| KW_Set_Begin_Neg_Rsbrkt { $$ = 1; }
;

RE_Set_End:
  KW_Set_End { $$ = 0; }
| KW_Set_Dash_End { $$ = 1; }
;

RE_Set_Body:
  /* empty */ { $$ = NULL; }
| RE_Set_Body RE_Set_Elem
  {
    if ($1 != NULL) {
      $$ = $1;
      $$->join($2);
      delete($2);
    } else
      $$ = $2;
  }
;

RE_Set_Elem:
  RE_Set_Range_Char
  {
    $$ = new QuadSet();
    $$->add(new Quad($1.value));
  }
| RE_Set_NoRange_Char { $$ = $1; }
| RE_Set_Range_Char '-' RE_Set_Range_Char
  {
    if ($1.value > $3.value) {
      TTCN_pattern_error("Invalid range in the character set: the "
        "character code of the lower bound (%u) is higher than that of the "
        "upper bound (%u).", static_cast<unsigned int>($1.value), static_cast<unsigned int>($3.value));
    }
    $$ = new QuadSet();
    $$->add(new QuadInterval(Quad($1.value), Quad($3.value)));
  }
;

RE_Set_Range_Char:
  KW_BS_t { $$.value = '\t'; }
| KW_BS_r { $$.value = '\r'; }
| TOK_Char
  {
    if ($1 <= 0) TTCN_pattern_error("Character with code %u "
      "(0x%02x) cannot be used in a pattern for type universal charstring.", $1, $1);
    $$.value = nocase ? tolower($1) : $1;
  }
| RE_Quadruple { $$.value = nocase ?
    unichar_pattern.convert_quad_to_lowercase($1.value).get_value() : $1.value; }
;

RE_Set_NoRange_Char:
  KW_BS_d
  {
    $$ = new QuadSet();
    $$->add(new QuadInterval(Quad('0'), Quad('9')));
  }
| KW_BS_w
  {
    $$ = new QuadSet();
    $$->add(new QuadInterval(Quad('0'), Quad('9')));
    $$->add(new QuadInterval(Quad('A'), Quad('Z')));
    $$->add(new QuadInterval(Quad('a'), Quad('z')));
  }
| KW_BS_n
  {
    $$ = new QuadSet();
    $$->add(new QuadInterval(Quad('\n'), Quad('\r')));
  }
| KW_BS_s
  {
    $$ = new QuadSet();
    $$->add(new QuadInterval(Quad('\t'), Quad('\r')));
    $$->add(new Quad(' '));
  }
| KW_BS_b
  {
    $$ = new QuadSet();
    TTCN_pattern_error("Metacharacter `\\b' does not make any sense in a "
      "character set.");
  }
;

RE_Quadruple:
  KW_BS_q '{' TOK_Number ',' TOK_Number ',' TOK_Number ',' TOK_Number '}'
  {
    if ($3 > 127) TTCN_pattern_error("The first number (group) of quadruple "
      "`\\q{%lu,%lu,%lu,%lu}' is too large. It should be in the range 0..127 "
      "instead of %lu.", $3, $5, $7, $9, $3);
    if ($5 > 255) TTCN_pattern_error("The second number (plane) of quadruple "
      "`\\q{%lu,%lu,%lu,%lu}' is too large. It should be in the range 0..255 "
      "instead of %lu.", $3, $5, $7, $9, $5);
    if ($7 > 255) TTCN_pattern_error("The third number (row) of quadruple "
      "`\\q{%lu,%lu,%lu,%lu}' is too large. It should be in the range 0..255 "
      "instead of %lu.", $3, $5, $7, $9, $7);
    if ($9 > 255) TTCN_pattern_error("The fourth number (cell) of quadruple "
      "`\\q{%lu,%lu,%lu,%lu}' is too large. It should be in the range 0..255 "
      "instead of %lu.", $3, $5, $7, $9, $9);
    if ($3 == 0 && $5 == 0 && $7 == 0 && $9 == 0) TTCN_pattern_error("Zero "
      "character (i.e. quadruple `\\q{0,0,0,0}') is not supported in a "
      "pattern for type universal charstring.");
    $$.comp.group = static_cast<unsigned char>($3);
    $$.comp.plane = static_cast<unsigned char>($5);
    $$.comp.row = static_cast<unsigned char>($7);
    $$.comp.cell = static_cast<unsigned char>($9);
  }
;

%%

/*********************************************************************
 * Interface
 *********************************************************************/

char* TTCN_pattern_to_regexp_uni(const char* p_pattern, bool p_nocase, int** groups)
{
  /* if you want to debug */
  //pattern_unidebug=1;

  ret_val=NULL;
  user_groups = 0;
  nocase = p_nocase;

  yy_buffer_state *flex_buffer = pattern_yy_scan_string(p_pattern);
  if(flex_buffer == NULL) {
    TTCN_pattern_error("Flex buffer creation failed.");
    return NULL;
  }
  init_pattern_yylex(&yylval);
  if(pattern_uniparse()) {
    Free(ret_val);
    ret_val=NULL;
  }
  pattern_yy_delete_buffer(flex_buffer);

  // needed by regexp to find user specified groups
  if (user_groups /*&& groups*/) {
    if (groups) {
      *groups = static_cast<int*>(Malloc(sizeof(int) * (user_groups + 1)));
      (*groups)[0] = user_groups;
    }

    int par = -1, index = 1;
    for (size_t i = 0; i < strlen(ret_val); i++) {
      if (ret_val[i] == '(') {
        par++;
      }
      if (ret_val[i] == '<') {
        ret_val[i] = '(';
        par++;
        if (groups) (*groups)[index++] = par;
      }
    }
  } else if (groups)
    *groups = static_cast<int*>(0);

  return ret_val;
}

// Backwards compatibility shim
char* TTCN_pattern_to_regexp_uni(const char* p_pattern, int ere, int** /*groups*/)
{
  TTCN_pattern_warning("TTCN_pattern_to_regexp_uni"
    "(const char* p_pattern, int ere, int** groups) is deprecated");
  if (ere != 1) TTCN_pattern_error(
    "BRE is not supported for TTCN_pattern_to_regexp_uni");
  return TTCN_pattern_to_regexp(p_pattern);
}


/*********************************************************************
 * Static functions
 *********************************************************************/

void pattern_unierror(const char *error_str)
{
  TTCN_pattern_error("%s", error_str);
}

void yyprint(FILE *file, int type, const YYSTYPE& value)
{
  switch (type) {
  case TOK_Char:
    fprintf(file, "'%c'", value.c);
    break;
  case TOK_Digit: case TOK_Number:
    fprintf(file, "'%lu'", value.u);
    break;
  default:
    break;
  }
}