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
15 * Szabo, Janos Zoltan – initial implementation
17 ******************************************************************************/
22 %option never-interactive
23 %option prefix="pstring_yy"
25 %{ /* ****************** C declarations ***************** */
30 #include "../Identifier.hh"
32 #include "PatternString.hh"
35 using namespace Common;
37 /** Adjust location information.
39 * \c start_index and \c end_index index into \c yytext.
40 * \c current_line and \c current_column index into the actual source line
41 * (only a part of which is in \c yytext).
43 * UPDATE_LOCATION should be called to "cover" all of yytext.
45 * @param [in] start_index index into yytext (the first character to consider)
46 * @param [in] end_index index into yytext (one-past)
47 * @param [in,out] current_line
48 * @param [in,out] current_column
50 static void update_location(size_t start_index, size_t end_index,
51 size_t& current_line, size_t& current_column)
53 for (size_t i = start_index; i < yyleng && i < end_index; i++) {
54 // count CR, count LF, but count CR + LF as one
61 if (i == 0 || yytext[i - 1] != '\r') {
72 // This must be side-effect-free !
73 #define UPDATE_LOCATION(start_index, end_index) \
74 update_location(start_index, end_index, current_line, current_column)
76 #define YY_DECL static PatternString *yylex(const char *current_file, \
77 size_t current_line, size_t current_column)
79 %} /* ***************** definitions ***************** */
81 IDENTIFIER [A-Za-z][A-Za-z0-9_]*
82 NUMBER 0|([1-9][0-9]*)
86 %% /* ***************** rules ************************* */
88 bool in_set = false; /* inside a [] */
89 PatternString* ps = new PatternString();
91 "{"{WS}{IDENTIFIER}(({WS}\.{WS}{IDENTIFIER})|({WS}\[{WS}({IDENTIFIER}|{NUMBER}){WS}\]))*{WS}"}" {
93 // in a set a reference (without the \N at start) is just simple text,
94 // the matched token does not contain characters that are special in a set
95 ps->addString(yytext);
96 UPDATE_LOCATION(0, yyleng);
98 vector<string> identifiers;
99 vector<int> beginnings;
101 const char * beg = yytext;
102 const char * end = 0;
103 int id_begin = 0; // the first alphanumeric character of an identifier
105 while (isspace(*++beg) || (*beg=='[') ) ; // skip whitespace and [
106 int *current_begin = new int(beg - yytext);
107 if (!id_begin) id_begin = *current_begin;
109 while (isalnum(*++end) || *end=='_') ; // scan for the end of identifier
111 string* identifier = new string(end-beg, beg);
112 identifiers.add(identifier);
113 beginnings .add(current_begin);
115 beg = end; // end remembers the position before the whitespace
116 while (isspace(*beg) || (*beg==']') ) ++beg; // skip whitespace and ]
117 if (*beg=='}') break;
120 size_t num_id = identifiers.size();
122 Ttcn::Reference *ref = 0;
123 size_t last = 0; // last "consumed" index into yytext
124 int old_column = 0, old_line = 0; // the beginning of the entire reference
126 for (size_t i = 0; i < num_id; ++i) {
127 const string & id_str = *identifiers[i];
128 const size_t & id_beg = *beginnings [i];
129 if (Identifier::is_reserved_word(id_str, Identifier::ID_TTCN)) {
130 UPDATE_LOCATION(last, id_beg); // consume before identifier
131 int first_line = current_line, first_column = current_column;
132 UPDATE_LOCATION(id_beg, id_beg+id_str.size()); // consume identifier
133 Location loc(current_file, first_line, first_column, current_line,
134 current_column); // location covers the identifier
135 UPDATE_LOCATION(id_beg+id_str.size(), yyleng); // consume to end
136 loc.error("Invalid reference expression: `%s' is a reserved word in TTCN-3",
141 UPDATE_LOCATION(last, id_beg); // consume before identifier
145 UPDATE_LOCATION(0, id_begin); // consume before identifier
146 old_column = current_column; old_line = current_line;
147 ref = new Ttcn::Reference(new Identifier(Identifier::ID_TTCN, id_str));
148 UPDATE_LOCATION(id_begin, last = id_begin + id_str.size());
150 UPDATE_LOCATION(last, id_beg);
151 Location loc(current_file, current_line, current_column, current_line,
152 current_column + id_str.size());
153 UPDATE_LOCATION(id_beg, last = id_beg + id_str.size());
156 FieldOrArrayRef *fieldref = 0;
158 if(1 != sscanf( (id_str.c_str()), "%d", &temp_i)){
159 fieldref = new FieldOrArrayRef(new Identifier(Identifier::ID_TTCN, id_str));
162 int_val_t* temp_intvalt = new int_val_t(temp_i);
163 Value* temp_value = new Value(Value::V_INT, temp_intvalt);
164 fieldref = new FieldOrArrayRef(temp_value);
167 ref->set_location(loc);
170 loc.error("Invalid reference expression");
177 delete ref; // safe even if NULL
179 UPDATE_LOCATION(last, yyleng); // consume to the end
180 Location loc(current_file, old_line, old_column, current_line, current_column);
182 ref->set_location(loc);
185 loc.error("Invalid reference expression");
189 // cleanup (can't be done in the second loop because it may end early)
190 for (size_t i = 0; i < num_id; ++i) {
191 delete identifiers[i];
192 delete beginnings [i];
200 int first_line = current_line, first_column = current_column;
201 UPDATE_LOCATION(0, yyleng);
202 Location loc(current_file, first_line, first_column, current_line,
204 loc.error("Invalid reference expression: `%s'", yytext);
207 "\\N"{WS}"{"{WS}{IDENTIFIER}{WS}"}" {
209 while (!isalpha(yytext[id_begin])) id_begin++;
211 while (isalnum(yytext[id_begin + id_len]) || yytext[id_begin + id_len] == '_')
213 string id_str(id_len, yytext + id_begin);
215 Ttcn::Reference *ref = new Ttcn::Reference(new Identifier(
216 Identifier::ID_TTCN, id_str));
217 ref->set_location(loc);
218 ps->addRefdCharSet(ref);
220 int first_line = current_line, first_column = current_column;
221 UPDATE_LOCATION(0, yyleng);
222 Location loc(current_file, first_line, first_column, current_line,
224 if (Identifier::is_reserved_word(id_str, Identifier::ID_TTCN)) {
225 loc.error("Invalid character set reference: `%s' is a reserved word in "
226 "TTCN-3", id_str.c_str());
228 loc.warning("Character set reference `\\N{%s}' is not supported, "
229 "dropped out from the set", id_str.c_str());
231 loc.warning("Character set reference `\\N{%s}' is not supported, "
232 "substituted with `?'", id_str.c_str());
237 "\\N"{WS}"{"[^}]*"}" {
238 int first_line = current_line, first_column = current_column;
239 UPDATE_LOCATION(0, yyleng);
240 Location loc(current_file, first_line, first_column, current_line,
242 loc.error("Invalid character set reference: `%s'", yytext);
245 "\\q"{WS}"{"{WS}{NUMBER}{WS}","{WS}{NUMBER}{WS}","{WS}{NUMBER}{WS}","{WS}{NUMBER}{WS}"}" {
246 size_t group_begin = 3;
247 while (!isdigit(yytext[group_begin])) group_begin++;
248 UPDATE_LOCATION(0, group_begin);
249 size_t group_len = 1;
250 while (isdigit(yytext[group_begin + group_len])) group_len++;
251 string group_str(group_len, yytext + group_begin);
252 Location group_loc(current_file, current_line, current_column, current_line,
253 current_column + group_len);
254 Int group = string2Int(group_str, group_loc);
255 if (group < 0 || group > 127) {
256 group_loc.error("The first number of quadruple (group) must be within "
257 "the range 0 .. 127 instead of %s", Int2string(group).c_str());
258 group = group < 0 ? 0 : 127;
260 size_t plane_begin = group_begin + group_len + 1;
261 while (!isdigit(yytext[plane_begin])) plane_begin++;
262 UPDATE_LOCATION(group_begin, plane_begin);
264 while (isdigit(yytext[plane_begin + plane_len])) plane_len++;
265 string plane_str(plane_len, yytext + plane_begin);
266 Location plane_loc(current_file, current_line, current_column, current_line,
267 current_column + plane_len);
268 Int plane = string2Int(plane_str, plane_loc);
269 if (plane < 0 || plane > 255) {
270 plane_loc.error("The second number of quadruple (plane) must be within "
271 "the range 0 .. 255 instead of %s", Int2string(plane).c_str());
272 plane = plane < 0 ? 0 : 255;
274 size_t row_begin = plane_begin + plane_len + 1;
275 while (!isdigit(yytext[row_begin])) row_begin++;
276 UPDATE_LOCATION(plane_begin, row_begin);
278 while (isdigit(yytext[row_begin + row_len])) row_len++;
279 string row_str(row_len, yytext + row_begin);
280 Location row_loc(current_file, current_line, current_column, current_line,
281 current_column + row_len);
282 Int row = string2Int(row_str, row_loc);
283 if (row < 0 || row > 255) {
284 row_loc.error("The third number of quadruple (row) must be within "
285 "the range 0 .. 255 instead of %s", Int2string(row).c_str());
286 row = row < 0 ? 0 : 255;
288 size_t cell_begin = row_begin + row_len + 1;
289 while (!isdigit(yytext[cell_begin])) cell_begin++;
290 UPDATE_LOCATION(row_begin, cell_begin);
292 while (isdigit(yytext[cell_begin + cell_len])) cell_len++;
293 string cell_str(cell_len, yytext + cell_begin);
294 Location cell_loc(current_file, current_line, current_column, current_line,
295 current_column + cell_len);
296 Int cell = string2Int(cell_str, cell_loc);
297 if (cell < 0 || cell > 255) {
298 cell_loc.error("The fourth number of quadruple (cell) must be within "
299 "the range 0 .. 255 instead of %s", Int2string(cell).c_str());
300 cell = cell < 0 ? 0 : 255;
302 bool add_quadruple = true;
303 if (group == 0 && plane == 0 && row == 0) {
327 add_quadruple = false;
331 ps->addString("\\t");
332 add_quadruple = false;
335 ps->addString("\\r");
336 add_quadruple = false;
341 ps->addString("\\q{" + Int2string(group) + "," + Int2string(plane) + "," +
342 Int2string(row) + "," + Int2string(cell) + "}");
344 UPDATE_LOCATION(cell_begin, yyleng);
347 "\\q"({WS}"{"[^}]*"}")? {
348 int first_line = current_line, first_column = current_column;
349 UPDATE_LOCATION(0, yyleng);
350 Location loc(current_file, first_line, first_column, current_line,
352 loc.error("Invalid quadruple notation: `%s'", yytext);
357 ps->addString("\\[]");
360 ps->addString("[\\]");
368 ps->addString("\\[\\^]");
371 ps->addString("[^\\]");
379 ps->addString("\\[");
392 Location loc(current_file, current_line, current_column, current_line,
394 loc.error("Unmatched `]'. Did you mean `\\]'?");
395 ps->addString("\\]");
401 Location loc(current_file, current_line, current_column, current_line,
403 loc.warning("Unmatched `%c' was treated literally", yytext[0]);
405 ps->addChar(yytext[0]);
413 /* \metachars and escaped metachars */
414 \\[dwtnrsb?*\\\[\]\-\^|()#+] {
415 ps->addString(yytext);
420 int first_line = current_line, first_column = current_column;
421 UPDATE_LOCATION(0, yyleng);
422 Location loc(current_file, first_line, first_column, current_line,
424 if (isprint((unsigned char)yytext[1]))
425 loc.warning("Use of unrecognized escape sequence `\\%c' is deprecated",
427 else loc.warning("Use of unrecognized escape sequence is deprecated");
428 ps->addString(yytext + 1);
432 int first_line = current_line, first_column = current_column;
433 UPDATE_LOCATION(0, yyleng);
435 Location loc(current_file, first_line, first_column, current_line,
437 loc.error("Number of repetitions `#n' cannot be given inside a set "
439 } else if (yytext[yyleng - 1] != '1') {
441 ps->addChar(yytext[yyleng - 1]);
445 "#"{WS}"("{WS}{NUMBER}{WS}")" {
447 int first_line = current_line, first_column = current_column;
448 UPDATE_LOCATION(0, yyleng);
449 Location loc(current_file, first_line, first_column, current_line,
451 loc.error("Number of repetitions `#(n)' cannot be given inside a set "
454 size_t number_begin = 2;
455 while (!isdigit(yytext[number_begin])) number_begin++;
456 UPDATE_LOCATION(0, number_begin);
458 while (isdigit(yytext[number_begin + number_len])) number_len++;
459 string number_str(number_len, yytext + number_begin);
460 Location number_loc(current_file, current_line, current_column,
461 current_line, current_column + number_len);
462 UPDATE_LOCATION(number_begin, yyleng);
463 Int number = string2Int(number_str, number_loc);
465 number_loc.error("A non-negative integer value was expected as the "
466 "number of repetitions instead of %s", Int2string(number).c_str());
467 } else if (number != 1) ps->addString("#(" + Int2string(number) + ")");
471 "#"{WS}"("{WS}{NUMBER}{WS}","{WS}{NUMBER}{WS}")" {
472 int first_line = current_line, first_column = current_column;
474 UPDATE_LOCATION(0, yyleng);
475 Location loc(current_file, first_line, first_column, current_line,
477 loc.error("Number of repetitions `#(n,m)' cannot be given inside a set "
480 size_t lower_begin = 2;
481 while (!isdigit(yytext[lower_begin])) lower_begin++;
482 UPDATE_LOCATION(0, lower_begin);
484 while (isdigit(yytext[lower_begin + lower_len])) lower_len++;
485 string lower_str(lower_len, yytext + lower_begin);
486 Location lower_loc(current_file, current_line, current_column,
487 current_line, current_column + lower_len);
488 Int lower = string2Int(lower_str, lower_loc);
490 lower_loc.error("A non-negative integer value was expected as the "
491 "minimum number of repetitions instead of %s",
492 Int2string(lower).c_str());
495 size_t upper_begin = lower_begin + lower_len + 1;
496 while (!isdigit(yytext[upper_begin])) upper_begin++;
497 UPDATE_LOCATION(lower_begin, upper_begin);
499 while (isdigit(yytext[upper_begin + upper_len])) upper_len++;
500 string upper_str(upper_len, yytext + upper_begin);
501 Location upper_loc(current_file, current_line, current_column,
502 current_line, current_column + upper_len);
503 UPDATE_LOCATION(upper_begin, yyleng);
504 Int upper = string2Int(upper_str, upper_loc);
506 upper_loc.error("A non-negative integer value was expected as the "
507 "maximum number of repetitions instead of %s",
508 Int2string(upper).c_str());
509 } else if (lower > upper) {
510 Location loc(current_file, first_line, first_column, current_line,
512 loc.error("The lower bound is higher than the upper bound in the number "
513 "of repetitions: `#(%s,%s)'", Int2string(lower).c_str(),
514 Int2string(upper).c_str());
515 } else if (lower == upper) {
516 if (lower != 1) ps->addString("#(" + Int2string(lower) + ")");
518 if (lower == 0) ps->addString("#(," + Int2string(upper) + ")");
519 else ps->addString("#(" + Int2string(lower) + "," + Int2string(upper) +
525 "#"{WS}"("{WS}{NUMBER}{WS}","{WS}")" {
527 int first_line = current_line, first_column = current_column;
528 UPDATE_LOCATION(0, yyleng);
529 Location loc(current_file, first_line, first_column, current_line,
531 loc.error("Number of repetitions `#(n,)' cannot be given inside a set "
534 size_t lower_begin = 2;
535 while (!isdigit(yytext[lower_begin])) lower_begin++;
536 UPDATE_LOCATION(0, lower_begin);
538 while (isdigit(yytext[lower_begin + lower_len])) lower_len++;
539 string lower_str(lower_len, yytext + lower_begin);
540 Location lower_loc(current_file, current_line, current_column,
541 current_line, current_column + lower_len);
542 UPDATE_LOCATION(lower_begin, yyleng);
543 Int lower = string2Int(lower_str, lower_loc);
545 lower_loc.error("A non-negative integer value was expected as the "
546 "minimum number of repetitions instead of %s",
547 Int2string(lower).c_str());
548 } else if (lower == 1) ps->addChar('+');
549 else ps->addString("#(" + Int2string(lower) + ",)");
553 "#"{WS}"("{WS}","{WS}{NUMBER}{WS}")" {
555 int first_line = current_line, first_column = current_column;
556 UPDATE_LOCATION(0, yyleng);
557 Location loc(current_file, first_line, first_column, current_line,
559 loc.error("Number of repetitions `#(,m)' cannot be given inside a set "
562 size_t upper_begin = 3;
563 while (!isdigit(yytext[upper_begin])) upper_begin++;
564 UPDATE_LOCATION(0, upper_begin);
566 while (isdigit(yytext[upper_begin + upper_len])) upper_len++;
567 string upper_str(upper_len, yytext + upper_begin);
568 Location upper_loc(current_file, current_line, current_column,
569 current_line, current_column + upper_len);
570 UPDATE_LOCATION(upper_begin, yyleng);
571 Int upper = string2Int(upper_str, upper_loc);
573 upper_loc.error("A non-negative integer value was expected as the "
574 "maximum number of repetitions instead of %s",
575 Int2string(upper).c_str());
576 } else ps->addString("#(," + Int2string(upper) + ")");
580 "#"{WS}"("{WS}","{WS}")" {
581 int first_line = current_line, first_column = current_column;
582 UPDATE_LOCATION(0, yyleng);
584 Location loc(current_file, first_line, first_column, current_line,
586 loc.error("Number of repetitions `#(,)' cannot be given inside a set "
588 } else ps->addString("#(,)");
592 int first_line = current_line, first_column = current_column;
593 UPDATE_LOCATION(0, yyleng);
594 Location loc(current_file, first_line, first_column, current_line,
596 loc.error("Invalid notation for the number of repetitions: `%s'", yytext);
600 Location loc(current_file, current_line, current_column, current_line,
603 loc.warning("Unescaped `#' inside character set was treated literally");
607 loc.error("Syntax error in the number of repetitions `#...'");
614 Location loc(current_file, current_line, current_column, current_line,
616 loc.warning("Unescaped `+' inside character set was treated literally");
624 ps->addString(yytext);
625 UPDATE_LOCATION(0, yyleng);
630 Location loc(current_file, current_line, current_column, current_line,
632 loc.error("Missing `]' at the end of the string");
640 PatternString* parse_pattern(const char *str, const Location& p_loc)
642 Error_Context cntxt(&p_loc, "In character string pattern");
643 struct yy_buffer_state *flex_buffer=pstring_yy_scan_string(str);
645 FATAL_ERROR("parse_pattern(): flex buffer creation failed");
649 PatternString *ps = yylex(p_loc.get_filename(), p_loc.get_first_line(),
650 p_loc.get_first_column() + 1);
651 pstring_yylex_destroy();