1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
12 * Zalanyi, Balazs Andor
14 ******************************************************************************/
17 * Based on pattern_p.y
22 /*********************************************************************
24 *********************************************************************/
30 #if defined(__CYGWIN__) && defined(__clang__)
31 /* Cygwin's clang 3.0 has its own limits.h, which does not bring in
32 the system's limits.h unless we define this macro: */
33 #define __STDC_HOSTED__ 1
34 #define _GCC_NEXT_LIMITS_H
39 #if !defined(RE_DUP_MAX)
40 /* RE_DUP_MAX is defined in limits.h or regex.h, except on Cygwin 1.5 */
41 # include <sys/syslimits.h>
47 #include "Quadruple.hh"
50 /* defined in lexer c-file: */
52 extern int pattern_yylex();
53 inline int pattern_unilex() { return pattern_yylex(); }
54 extern void init_pattern_yylex(YYSTYPE*);
55 struct yy_buffer_state;
56 extern yy_buffer_state* pattern_yy_scan_string(const char*);
57 extern void pattern_yy_delete_buffer(yy_buffer_state*);
58 extern unsigned int get_nof_parentheses();
60 /* defined in this file: */
62 /** The converted regexp. */
64 /** The parser error reporting function. */
65 static void pattern_unierror(const char *error_str);
67 static int user_groups;
69 #define YYERROR_VERBOSE
71 static void yyprint(FILE *file, int type, const YYSTYPE& value);
72 #define YYPRINT(f,t,v) yyprint(f,t,v)
76 /*********************************************************************
78 *********************************************************************/
80 %name-prefix="pattern_uni"
81 %output="pattern_uni.cc"
88 /*********************************************************************
90 * Must be kept in sync with the one in pattern_p.y !
91 *********************************************************************/
95 char c; /* single character */
96 char *s; /* character string */
97 unsigned long int u; /* unsigned integer */
98 struct character_set *set; // used by nonterminals in pattern_p.y
102 #if defined(__sparc__) || defined(__sparc)
117 } q; // single universal char, used by nonterminals in pattern_uni.y
118 class QuadSet* qset; // used by nonterminals in pattern_uni.y
121 /*********************************************************************
123 *********************************************************************/
125 %token <c> TOK_Char "<ordinary character>"
126 %token <u> TOK_Number "<number>"
127 %token <u> TOK_Digit "<digit>"
129 /*********************************************************************
131 *********************************************************************/
142 %token KW_Group_Begin "("
143 %token KW_Group_End ")"
144 %token KW_Set_Begin "["
145 %token KW_Set_Begin_Neg "[^"
146 %token KW_Set_Begin_Rsbrkt "[]"
147 %token KW_Set_Begin_Neg_Rsbrkt "[^]"
148 %token KW_Set_End "]"
149 %token KW_Set_Dash_End "-]"
151 /*********************************************************************
152 * semantic types of nonterminals
153 *********************************************************************/
155 %type <b> RE_Set_Begin RE_Set_Begin_Rsbrkt RE_Set_End
156 %type <q> RE_Set_Range_Char RE_Quadruple
157 %type <s> RE_Body RE_Elems RE_Alter_Elem RE_Concat_Elem
158 RE_Multiply_Elem RE_Multiply_Statement RE_Group
160 %type <qset> RE_Set RE_Set_Body RE_Set_Elem RE_Set_NoRange_Char
162 /*********************************************************************
164 *********************************************************************/
166 %destructor { Free($$); }
173 RE_Multiply_Statement
176 %destructor { delete $$; }
184 /*********************************************************************
186 *********************************************************************/
189 RE_Body {ret_val=$1;}
200 $$ = mprintf("^%s$", $1);
202 } else $$ = mcopystr("^$");
207 RE_Alter_Elem { $$ = $1; }
208 | RE_Elems '|' RE_Alter_Elem
210 unsigned int nof_pars = get_nof_parentheses() + (yychar==KW_Group_End ? 1 : 0);
212 if ($1 != NULL) $$ = mputprintf($1, nof_pars ? "|%s" : "$|^%s", $3);
213 else $$ = mprintf( nof_pars ? "()|%s" : "()$|^%s" , $3);
216 if ($1 != NULL) $$ = mputstr($1, nof_pars ? "|()" : "$|^()");
223 RE_Concat_Elem { $$ = $1; }
224 | RE_Alter_Elem RE_Concat_Elem
226 $$ = mputstr($1, $2);
232 RE_Multiply_Elem {$$=$1;}
233 | RE_Multiply_Elem RE_Multiply_Statement
235 if ($1 != NULL && $2 != NULL) {
236 $$ = mputstr($1, $2);
244 | '*' {$$=mcopystr("(........)*");}
249 | RE_OneCharPos {$$=$1;}
253 KW_Group_Begin KW_Group_End
258 | KW_Group_Begin RE_Elems KW_Group_End
262 $$ = mprintf("<%s)", $2);
270 RE_Multiply_Statement:
282 TTCN_pattern_warning("The number of repetitions is zero: `#0'.");
284 } else if ($2 == 1) $$ = memptystr();
286 if ($2 > 9) TTCN_pattern_warning("Internal error: Invalid number of "
287 "repetitions: `#%lu'.", $2);
288 $$ = mprintf("{%lu}", $2);
291 | '#' '(' TOK_Number ')'
294 TTCN_pattern_warning("The number of repetitions is zero: `#(0)'.");
296 } else if ($3 == 1) $$ = memptystr();
299 if ($3 > RE_DUP_MAX) TTCN_pattern_warning("The number of repetitions in "
300 "`#(%lu)' exceeds the limit allowed by this system (%d).", $3,
303 $$ = mprintf("{%lu}", $3);
306 | '#' '(' TOK_Number ',' TOK_Number ')'
309 if ($3 > RE_DUP_MAX) TTCN_pattern_warning("The minimum number of "
310 "repetitions in `#(%lu,%lu)' exceeds the limit allowed by this system "
311 "(%d).", $3, $5, RE_DUP_MAX);
312 if ($5 > RE_DUP_MAX) TTCN_pattern_warning("The maximum number of "
313 "repetitions in `#(%lu,%lu)' exceeds the limit allowed by this system "
314 "(%d).", $3, $5, RE_DUP_MAX);
316 if ($3 > $5) TTCN_pattern_error("The lower bound is higher than the upper "
317 "bound in the number of repetitions: `#(%lu,%lu)'.", $3, $5);
320 TTCN_pattern_warning("The number of repetitions is zero: `#(0,0)'.");
322 } else if ($3 == 1) $$ = memptystr();
324 $$ = mprintf("{%lu}", $3);
328 if ($3 == 0 && $5 == 1) $$ = mcopystr("?");
329 else $$ = mprintf("{%lu,%lu}", $3, $5);
333 | '#' '(' ',' TOK_Number ')'
336 TTCN_pattern_warning("The number of repetitions is zero: `#(,0)'.");
340 if ($4 > RE_DUP_MAX) TTCN_pattern_warning("The maximum number of "
341 "repetitions in `#(,%lu)' exceeds the limit allowed by this system "
342 "(%d).", $4, RE_DUP_MAX);
345 if ($4 == 1) $$ = mcopystr("?");
346 else $$ = mprintf("{0,%lu}", $4);
350 | '#' '(' TOK_Number ',' ')'
352 if ($3 == 0) $$ = mcopystr("*");
355 if ($3 > RE_DUP_MAX) TTCN_pattern_warning("The minimum number of "
356 "repetitions in `#(%lu,)' exceeds the limit allowed by this system "
357 "(%d).", $3, RE_DUP_MAX);
360 if ($3 == 1) $$ = mcopystr("+");
361 else $$ = mprintf("{%lu,}", $3);
368 '?' {$$=mcopystr("(........)");}
369 | KW_BS_d {$$=mcopystr("(AAAAAAD[A-J])");}
370 | KW_BS_w {$$=mcopystr("(AAAAAAD[A-J]|AAAAAA(E[B-P]|F[A-K])|AAAAAA(G[B-P]|H[A-K]))");}
371 | KW_BS_t {$$=mcopystr("AAAAAAAJ");}
372 | KW_BS_n {$$=mcopystr("(AAAAAAA[K-N])");}
373 | KW_BS_r {$$=mcopystr("AAAAAAAN");}
374 | KW_BS_s {$$=mcopystr("(AAAAAAA[J-N]|AAAAAACA)");}
377 TTCN_pattern_warning("Metacharacter `\\b' is not supported yet.");
382 unsigned char c = $1;
383 if ($1 <= 0) TTCN_pattern_error("Character with code %u "
384 "(0x%02x) cannot be used in a pattern for type charstring.", $1, $1);
385 $$ = Quad::get_hexrepr(c);
389 $$ = Quad::get_hexrepr($1.value);
393 if ($1->is_empty()) {
394 TTCN_pattern_error("Empty character set.");
397 $$ = $1->generate_posix();
403 /* RE_Set_Begin is 1 for "[^", 0 for "["
404 * RE_Set_Begin_Rsbrkt is 1 for "[^]", 0 for "[]"
405 * RE_Set_End is 1 for "-]", 0 for "]"
407 RE_Set_Begin RE_Set_Body RE_Set_End
413 if ($3 && !$$->add(new Quad('-')))
414 TTCN_pattern_warning("Duplicate character `-' in the character set.");
416 $$->set_negate(true);
418 | RE_Set_Begin '-' RE_Set_Body RE_Set_End
424 if (!$$->add(new Quad('-')))
425 TTCN_pattern_warning("Duplicate character `-' in the character set.");
427 $$->set_negate(true);
429 | RE_Set_Begin_Rsbrkt RE_Set_Body RE_Set_End
435 if (!$$->add(new Quad(']')))
436 TTCN_pattern_warning("Duplicate character `]' in the character set.");
437 if ($3 && !$$->add(new Quad('-')))
438 TTCN_pattern_warning("Duplicate character `-' in the character set.");
440 $$->set_negate(true);
442 | RE_Set_Begin_Rsbrkt '-' RE_Set_Range_Char RE_Set_Body RE_Set_End
448 if ((unsigned int)']' > $3.value) {
449 TTCN_pattern_error("Invalid range in the character set: the "
450 "character code of the lower bound (%u) is higher than that of the "
451 "upper bound (%u).", ']', (unsigned int)$3.value);
453 $$->add(new QuadInterval(Quad(']'), Quad($3.value)));
455 if (!$$->add(new Quad('-')))
456 TTCN_pattern_warning("Duplicate character `-' in the character set.");
459 $$->set_negate(true);
464 KW_Set_Begin { $$ = 0; }
465 | KW_Set_Begin_Neg { $$ = 1; }
469 KW_Set_Begin_Rsbrkt { $$ = 0; }
470 | KW_Set_Begin_Neg_Rsbrkt { $$ = 1; }
474 KW_Set_End { $$ = 0; }
475 | KW_Set_Dash_End { $$ = 1; }
479 /* empty */ { $$ = NULL; }
480 | RE_Set_Body RE_Set_Elem
495 $$->add(new Quad($1.value));
497 | RE_Set_NoRange_Char { $$ = $1; }
498 | RE_Set_Range_Char '-' RE_Set_Range_Char
500 if ($1.value > $3.value) {
501 TTCN_pattern_error("Invalid range in the character set: the "
502 "character code of the lower bound (%u) is higher than that of the "
503 "upper bound (%u).", (unsigned int)$1.value, (unsigned int)$3.value);
506 $$->add(new QuadInterval(Quad($1.value), Quad($3.value)));
511 KW_BS_t { $$.value = '\t'; }
512 | KW_BS_r { $$.value = '\r'; }
515 if ($1 <= 0) TTCN_pattern_error("Character with code %u "
516 "(0x%02x) cannot be used in a pattern for type charstring.", $1, $1);
519 | RE_Quadruple { $$.value = $1.value; }
526 $$->add(new QuadInterval(Quad('0'), Quad('9')));
531 $$->add(new QuadInterval(Quad('0'), Quad('9')));
532 $$->add(new QuadInterval(Quad('A'), Quad('Z')));
533 $$->add(new QuadInterval(Quad('a'), Quad('z')));
538 $$->add(new QuadInterval(Quad('\n'), Quad('\r')));
543 $$->add(new QuadInterval(Quad('\t'), Quad('\r')));
544 $$->add(new Quad(' '));
549 TTCN_pattern_error("Metacharacter `\\b' does not make any sense in a "
555 KW_BS_q '{' TOK_Number ',' TOK_Number ',' TOK_Number ',' TOK_Number '}'
557 if ($3 > 127) TTCN_pattern_error("The first number (group) of quadruple "
558 "`\\q{%lu,%lu,%lu,%lu}' is too large. It should be in the range 0..127 "
559 "instead of %lu.", $3, $5, $7, $9, $3);
560 if ($5 > 255) TTCN_pattern_error("The second number (plane) of quadruple "
561 "`\\q{%lu,%lu,%lu,%lu}' is too large. It should be in the range 0..255 "
562 "instead of %lu.", $3, $5, $7, $9, $5);
563 if ($7 > 255) TTCN_pattern_error("The third number (row) of quadruple "
564 "`\\q{%lu,%lu,%lu,%lu}' is too large. It should be in the range 0..255 "
565 "instead of %lu.", $3, $5, $7, $9, $7);
566 if ($9 > 255) TTCN_pattern_error("The fourth number (cell) of quadruple "
567 "`\\q{%lu,%lu,%lu,%lu}' is too large. It should be in the range 0..255 "
568 "instead of %lu.", $3, $5, $7, $9, $9);
569 if ($3 == 0 && $5 == 0 && $7 == 0 && $9 == 0) TTCN_pattern_error("Zero "
570 "character (i.e. quadruple `\\q{0,0,0,0}') is not supported in a "
571 "pattern for type universal charstring.");
581 /*********************************************************************
583 *********************************************************************/
585 char* TTCN_pattern_to_regexp_uni(const char* p_pattern, int** groups)
587 /* if you want to debug */
588 //pattern_unidebug=1;
593 yy_buffer_state *flex_buffer = pattern_yy_scan_string(p_pattern);
594 if(flex_buffer == NULL) {
595 TTCN_pattern_error("Flex buffer creation failed.");
598 init_pattern_yylex(&yylval);
599 if(pattern_uniparse()) {
603 pattern_yy_delete_buffer(flex_buffer);
605 // needed by regexp to find user specified groups
606 if (user_groups && groups) {
607 *groups = (int*)Malloc(sizeof(int) * (user_groups + 1));
608 (*groups)[0] = user_groups;
610 int par = -1, index = 1;
611 for (size_t i = 0; i < strlen(ret_val); i++) {
612 if (ret_val[i] == '(') {
615 if (ret_val[i] == '<') {
618 (*groups)[index++] = par;
627 // Backwards compatibility shim
628 char* TTCN_pattern_to_regexp_uni(const char* p_pattern, int ere, int** /*groups*/)
630 TTCN_pattern_warning("TTCN_pattern_to_regexp_uni"
631 "(const char* p_pattern, int ere, int** groups) is deprecated");
632 if (ere != 1) TTCN_pattern_error(
633 "BRE is not supported for TTCN_pattern_to_regexp_uni");
634 return TTCN_pattern_to_regexp(p_pattern);
638 /*********************************************************************
640 *********************************************************************/
642 void pattern_unierror(const char *error_str)
644 TTCN_pattern_error("%s", error_str);
647 void yyprint(FILE *file, int type, const YYSTYPE& value)
651 fprintf(file, "'%c'", value.c);
653 case TOK_Digit: case TOK_Number:
654 fprintf(file, "'%lu'", value.u);