1 /******************************************************************************
2 * Copyright (c) 2000-2014 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
7 ******************************************************************************/
10 * Based on pattern_p.y
15 /*********************************************************************
17 *********************************************************************/
23 #if defined(__CYGWIN__) && defined(__clang__)
24 /* Cygwin's clang 3.0 has its own limits.h, which does not bring in
25 the system's limits.h unless we define this macro: */
26 #define __STDC_HOSTED__ 1
27 #define _GCC_NEXT_LIMITS_H
32 #if !defined(RE_DUP_MAX)
33 /* RE_DUP_MAX is defined in limits.h or regex.h, except on Cygwin 1.5 */
34 # include <sys/syslimits.h>
40 #include "Quadruple.hh"
43 /* defined in lexer c-file: */
45 extern int pattern_yylex();
46 inline int pattern_unilex() { return pattern_yylex(); }
47 extern void init_pattern_yylex(YYSTYPE*);
48 struct yy_buffer_state;
49 extern yy_buffer_state* pattern_yy_scan_string(const char*);
50 extern void pattern_yy_delete_buffer(yy_buffer_state*);
51 extern unsigned int get_nof_parentheses();
53 /* defined in this file: */
55 /** The converted regexp. */
57 /** The parser error reporting function. */
58 static void pattern_unierror(const char *error_str);
60 static int user_groups;
62 #define YYERROR_VERBOSE
64 static void yyprint(FILE *file, int type, const YYSTYPE& value);
65 #define YYPRINT(f,t,v) yyprint(f,t,v)
69 /*********************************************************************
71 *********************************************************************/
73 %name-prefix="pattern_uni"
74 %output="pattern_uni.cc"
81 /*********************************************************************
83 * Must be kept in sync with the one in pattern_p.y !
84 *********************************************************************/
88 char c; /* single character */
89 char *s; /* character string */
90 unsigned long int u; /* unsigned integer */
91 struct character_set *set; // used by nonterminals in pattern_p.y
95 #if defined(__sparc__) || defined(__sparc)
110 } q; // single universal char, used by nonterminals in pattern_uni.y
111 class QuadSet* qset; // used by nonterminals in pattern_uni.y
114 /*********************************************************************
116 *********************************************************************/
118 %token <c> TOK_Char "<ordinary character>"
119 %token <u> TOK_Number "<number>"
120 %token <u> TOK_Digit "<digit>"
122 /*********************************************************************
124 *********************************************************************/
135 %token KW_Group_Begin "("
136 %token KW_Group_End ")"
137 %token KW_Set_Begin "["
138 %token KW_Set_Begin_Neg "[^"
139 %token KW_Set_Begin_Rsbrkt "[]"
140 %token KW_Set_Begin_Neg_Rsbrkt "[^]"
141 %token KW_Set_End "]"
142 %token KW_Set_Dash_End "-]"
144 /*********************************************************************
145 * semantic types of nonterminals
146 *********************************************************************/
148 %type <b> RE_Set_Begin RE_Set_Begin_Rsbrkt RE_Set_End
149 %type <q> RE_Set_Range_Char RE_Quadruple
150 %type <s> RE_Body RE_Elems RE_Alter_Elem RE_Concat_Elem
151 RE_Multiply_Elem RE_Multiply_Statement RE_Group
153 %type <qset> RE_Set RE_Set_Body RE_Set_Elem RE_Set_NoRange_Char
155 /*********************************************************************
157 *********************************************************************/
159 %destructor { Free($$); }
166 RE_Multiply_Statement
169 %destructor { delete $$; }
177 /*********************************************************************
179 *********************************************************************/
182 RE_Body {ret_val=$1;}
193 $$ = mprintf("^%s$", $1);
195 } else $$ = mcopystr("^$");
200 RE_Alter_Elem { $$ = $1; }
201 | RE_Elems '|' RE_Alter_Elem
203 unsigned int nof_pars = get_nof_parentheses() + (yychar==KW_Group_End ? 1 : 0);
205 if ($1 != NULL) $$ = mputprintf($1, nof_pars ? "|%s" : "$|^%s", $3);
206 else $$ = mprintf( nof_pars ? "()|%s" : "()$|^%s" , $3);
209 if ($1 != NULL) $$ = mputstr($1, nof_pars ? "|()" : "$|^()");
216 RE_Concat_Elem { $$ = $1; }
217 | RE_Alter_Elem RE_Concat_Elem
219 $$ = mputstr($1, $2);
225 RE_Multiply_Elem {$$=$1;}
226 | RE_Multiply_Elem RE_Multiply_Statement
228 if ($1 != NULL && $2 != NULL) {
229 $$ = mputstr($1, $2);
237 | '*' {$$=mcopystr("(........)*");}
242 | RE_OneCharPos {$$=$1;}
246 KW_Group_Begin KW_Group_End
251 | KW_Group_Begin RE_Elems KW_Group_End
255 $$ = mprintf("<%s)", $2);
263 RE_Multiply_Statement:
275 TTCN_pattern_warning("The number of repetitions is zero: `#0'.");
277 } else if ($2 == 1) $$ = memptystr();
279 if ($2 > 9) TTCN_pattern_warning("Internal error: Invalid number of "
280 "repetitions: `#%lu'.", $2);
281 $$ = mprintf("{%lu}", $2);
284 | '#' '(' TOK_Number ')'
287 TTCN_pattern_warning("The number of repetitions is zero: `#(0)'.");
289 } else if ($3 == 1) $$ = memptystr();
292 if ($3 > RE_DUP_MAX) TTCN_pattern_warning("The number of repetitions in "
293 "`#(%lu)' exceeds the limit allowed by this system (%d).", $3,
296 $$ = mprintf("{%lu}", $3);
299 | '#' '(' TOK_Number ',' TOK_Number ')'
302 if ($3 > RE_DUP_MAX) TTCN_pattern_warning("The minimum number of "
303 "repetitions in `#(%lu,%lu)' exceeds the limit allowed by this system "
304 "(%d).", $3, $5, RE_DUP_MAX);
305 if ($5 > RE_DUP_MAX) TTCN_pattern_warning("The maximum number of "
306 "repetitions in `#(%lu,%lu)' exceeds the limit allowed by this system "
307 "(%d).", $3, $5, RE_DUP_MAX);
309 if ($3 > $5) TTCN_pattern_error("The lower bound is higher than the upper "
310 "bound in the number of repetitions: `#(%lu,%lu)'.", $3, $5);
313 TTCN_pattern_warning("The number of repetitions is zero: `#(0,0)'.");
315 } else if ($3 == 1) $$ = memptystr();
317 $$ = mprintf("{%lu}", $3);
321 if ($3 == 0 && $5 == 1) $$ = mcopystr("?");
322 else $$ = mprintf("{%lu,%lu}", $3, $5);
326 | '#' '(' ',' TOK_Number ')'
329 TTCN_pattern_warning("The number of repetitions is zero: `#(,0)'.");
333 if ($4 > RE_DUP_MAX) TTCN_pattern_warning("The maximum number of "
334 "repetitions in `#(,%lu)' exceeds the limit allowed by this system "
335 "(%d).", $4, RE_DUP_MAX);
338 if ($4 == 1) $$ = mcopystr("?");
339 else $$ = mprintf("{0,%lu}", $4);
343 | '#' '(' TOK_Number ',' ')'
345 if ($3 == 0) $$ = mcopystr("*");
348 if ($3 > RE_DUP_MAX) TTCN_pattern_warning("The minimum number of "
349 "repetitions in `#(%lu,)' exceeds the limit allowed by this system "
350 "(%d).", $3, RE_DUP_MAX);
353 if ($3 == 1) $$ = mcopystr("+");
354 else $$ = mprintf("{%lu,}", $3);
361 '?' {$$=mcopystr("(........)");}
362 | KW_BS_d {$$=mcopystr("(AAAAAAD[A-J])");}
363 | KW_BS_w {$$=mcopystr("(AAAAAAD[A-J]|AAAAAA(E[B-P]|F[A-K])|AAAAAA(G[B-P]|H[A-K]))");}
364 | KW_BS_t {$$=mcopystr("AAAAAAAJ");}
365 | KW_BS_n {$$=mcopystr("(AAAAAAA[K-N])");}
366 | KW_BS_r {$$=mcopystr("AAAAAAAN");}
367 | KW_BS_s {$$=mcopystr("(AAAAAAA[J-N]|AAAAAACA)");}
370 TTCN_pattern_warning("Metacharacter `\\b' is not supported yet.");
375 unsigned char c = $1;
376 if ($1 <= 0) TTCN_pattern_error("Character with code %u "
377 "(0x%02x) cannot be used in a pattern for type charstring.", $1, $1);
378 $$ = Quad::get_hexrepr(c);
382 $$ = Quad::get_hexrepr($1.value);
386 if ($1->is_empty()) {
387 TTCN_pattern_error("Empty character set.");
390 $$ = $1->generate_posix();
396 /* RE_Set_Begin is 1 for "[^", 0 for "["
397 * RE_Set_Begin_Rsbrkt is 1 for "[^]", 0 for "[]"
398 * RE_Set_End is 1 for "-]", 0 for "]"
400 RE_Set_Begin RE_Set_Body RE_Set_End
406 if ($3 && !$$->add(new Quad('-')))
407 TTCN_pattern_warning("Duplicate character `-' in the character set.");
409 $$->set_negate(true);
411 | RE_Set_Begin '-' RE_Set_Body RE_Set_End
417 if (!$$->add(new Quad('-')))
418 TTCN_pattern_warning("Duplicate character `-' in the character set.");
420 $$->set_negate(true);
422 | RE_Set_Begin_Rsbrkt RE_Set_Body RE_Set_End
428 if (!$$->add(new Quad(']')))
429 TTCN_pattern_warning("Duplicate character `]' in the character set.");
430 if ($3 && !$$->add(new Quad('-')))
431 TTCN_pattern_warning("Duplicate character `-' in the character set.");
433 $$->set_negate(true);
435 | RE_Set_Begin_Rsbrkt '-' RE_Set_Range_Char RE_Set_Body RE_Set_End
441 if ((unsigned int)']' > $3.value) {
442 TTCN_pattern_error("Invalid range in the character set: the "
443 "character code of the lower bound (%u) is higher than that of the "
444 "upper bound (%u).", ']', (unsigned int)$3.value);
446 $$->add(new QuadInterval(Quad(']'), Quad($3.value)));
448 if (!$$->add(new Quad('-')))
449 TTCN_pattern_warning("Duplicate character `-' in the character set.");
452 $$->set_negate(true);
457 KW_Set_Begin { $$ = 0; }
458 | KW_Set_Begin_Neg { $$ = 1; }
462 KW_Set_Begin_Rsbrkt { $$ = 0; }
463 | KW_Set_Begin_Neg_Rsbrkt { $$ = 1; }
467 KW_Set_End { $$ = 0; }
468 | KW_Set_Dash_End { $$ = 1; }
472 /* empty */ { $$ = NULL; }
473 | RE_Set_Body RE_Set_Elem
488 $$->add(new Quad($1.value));
490 | RE_Set_NoRange_Char { $$ = $1; }
491 | RE_Set_Range_Char '-' RE_Set_Range_Char
493 if ($1.value > $3.value) {
494 TTCN_pattern_error("Invalid range in the character set: the "
495 "character code of the lower bound (%u) is higher than that of the "
496 "upper bound (%u).", (unsigned int)$1.value, (unsigned int)$3.value);
499 $$->add(new QuadInterval(Quad($1.value), Quad($3.value)));
504 KW_BS_t { $$.value = '\t'; }
505 | KW_BS_r { $$.value = '\r'; }
508 if ($1 <= 0) TTCN_pattern_error("Character with code %u "
509 "(0x%02x) cannot be used in a pattern for type charstring.", $1, $1);
512 | RE_Quadruple { $$.value = $1.value; }
519 $$->add(new QuadInterval(Quad('0'), Quad('9')));
524 $$->add(new QuadInterval(Quad('0'), Quad('9')));
525 $$->add(new QuadInterval(Quad('A'), Quad('Z')));
526 $$->add(new QuadInterval(Quad('a'), Quad('z')));
531 $$->add(new QuadInterval(Quad('\n'), Quad('\r')));
536 $$->add(new QuadInterval(Quad('\t'), Quad('\r')));
537 $$->add(new Quad(' '));
542 TTCN_pattern_error("Metacharacter `\\b' does not make any sense in a "
548 KW_BS_q '{' TOK_Number ',' TOK_Number ',' TOK_Number ',' TOK_Number '}'
550 if ($3 > 127) TTCN_pattern_error("The first number (group) of quadruple "
551 "`\\q{%lu,%lu,%lu,%lu}' is too large. It should be in the range 0..127 "
552 "instead of %lu.", $3, $5, $7, $9, $3);
553 if ($5 > 255) TTCN_pattern_error("The second number (plane) of quadruple "
554 "`\\q{%lu,%lu,%lu,%lu}' is too large. It should be in the range 0..255 "
555 "instead of %lu.", $3, $5, $7, $9, $5);
556 if ($7 > 255) TTCN_pattern_error("The third number (row) of quadruple "
557 "`\\q{%lu,%lu,%lu,%lu}' is too large. It should be in the range 0..255 "
558 "instead of %lu.", $3, $5, $7, $9, $7);
559 if ($9 > 255) TTCN_pattern_error("The fourth number (cell) of quadruple "
560 "`\\q{%lu,%lu,%lu,%lu}' is too large. It should be in the range 0..255 "
561 "instead of %lu.", $3, $5, $7, $9, $9);
562 if ($3 == 0 && $5 == 0 && $7 == 0 && $9 == 0) TTCN_pattern_error("Zero "
563 "character (i.e. quadruple `\\q{0,0,0,0}') is not supported in a "
564 "pattern for type universal charstring.");
574 /*********************************************************************
576 *********************************************************************/
578 char* TTCN_pattern_to_regexp_uni(const char* p_pattern, int** groups)
580 /* if you want to debug */
581 //pattern_unidebug=1;
586 yy_buffer_state *flex_buffer = pattern_yy_scan_string(p_pattern);
587 if(flex_buffer == NULL) {
588 TTCN_pattern_error("Flex buffer creation failed.");
591 init_pattern_yylex(&yylval);
592 if(pattern_uniparse()) {
596 pattern_yy_delete_buffer(flex_buffer);
598 // needed by regexp to find user specified groups
599 if (user_groups && groups) {
600 *groups = (int*)Malloc(sizeof(int) * (user_groups + 1));
601 (*groups)[0] = user_groups;
603 int par = -1, index = 1;
604 for (size_t i = 0; i < strlen(ret_val); i++) {
605 if (ret_val[i] == '(') {
608 if (ret_val[i] == '<') {
611 (*groups)[index++] = par;
620 // Backwards compatibility shim
621 char* TTCN_pattern_to_regexp_uni(const char* p_pattern, int ere, int** /*groups*/)
623 TTCN_pattern_warning("TTCN_pattern_to_regexp_uni"
624 "(const char* p_pattern, int ere, int** groups) is deprecated");
625 if (ere != 1) TTCN_pattern_error(
626 "BRE is not supported for TTCN_pattern_to_regexp_uni");
627 return TTCN_pattern_to_regexp(p_pattern);
631 /*********************************************************************
633 *********************************************************************/
635 void pattern_unierror(const char *error_str)
637 TTCN_pattern_error("%s", error_str);
640 void yyprint(FILE *file, int type, const YYSTYPE& value)
644 fprintf(file, "'%c'", value.c);
646 case TOK_Digit: case TOK_Number:
647 fprintf(file, "'%lu'", value.u);