config_preproc_la.l 8.67 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
2
 * Copyright (c) 2000-2019 Ericsson Telecom AB
Elemer Lelik's avatar
Elemer Lelik committed
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v2.0
Elemer Lelik's avatar
Elemer Lelik committed
5
 * which accompanies this distribution, and is available at
6
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
Elemer Lelik's avatar
Elemer Lelik committed
7
8
9
10
11
12
13
14
15
16
17
 *
 * Contributors:
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Beres, Szabolcs
 *   Delic, Adam
 *   Forstner, Matyas
 *   Kovacs, Ferenc
 *   Raduly, Csaba
 *   Szabo, Janos Zoltan – initial implementation
 *
Elemer Lelik's avatar
Elemer Lelik committed
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
 ******************************************************************************/
 
%option noyywrap
%option never-interactive
%option nounput
%option prefix="config_preproc_yy"

%{

#include <string.h>
#include <unistd.h>
#include <errno.h>

#include <deque>
#include <string>

#include "Path2.hh"
#include "cfg_process_utils.hh"

#include "config_preproc.h"
#include "path.h"
#include "config_preproc_p.tab.hh"

//#include "dbgnew.hh"

#define yylineno config_preproc_yylineno

extern int add_include_file(const std::string&);

/* If there are errors in the parsed define sections this value changes to 1.
   flex and bison for preprocessing can set it to 1, sometimes without printing
   the error message because strings and some other stuff is also parsed by the
   actual configuration parser which prints errors */
int preproc_error_flag;

/* used to track the curly brackets in the define section */
static int paren_stack = 0;

static std::deque<IncludeElem<YY_BUFFER_STATE> >* include_chain = NULL;

std::string get_cfg_preproc_current_file() {
  if (include_chain && !include_chain->empty()) {
    return include_chain->back().get_full_path();
  }
  return std::string();
}

%}

WHITESPACE [ \t]
WS {WHITESPACE}*
NEWLINE \r|\n|\r\n
LINECOMMENT ("//"|"#")[^\r\n]*{NEWLINE}
NUMBER 0|([1-9][0-9]*)

TTCN3IDENTIFIER [A-Za-z][A-Za-z0-9_]*

HEX [0-9A-Fa-f]

MACRORVALUE ([0-9A-Za-z._-]+)|{IPV6}
IPV6 [0-9A-Fa-f:.]+(%[0-9A-Za-z]+)?

MACRO_REFERENCE \${TTCN3IDENTIFIER}|\$"{"{WS}{TTCN3IDENTIFIER}{WS}(","{WS}charstring{WS})?"}"
MACRO_REFERENCE_INT \$"{"{WS}{TTCN3IDENTIFIER}{WS}(","{WS}integer{WS})?"}"


%x SC_blockcomment SC_cstring
%s SC_include SC_define SC_define_structured SC_ordered_include
%s SC_module_parameters SC_testport_parameters

%%
  /* valid in states SC_blockcomment, SC_cstring */
  int caller_state = INITIAL;
  /* valid in state SC_cstring */
  std::string cstring;

  preproc_error_flag = 0;

"/*" {
  caller_state = YY_START;
  BEGIN(SC_blockcomment);
}

<SC_blockcomment>
{
"*/" {
  BEGIN(caller_state);
  if (YY_START==SC_define || YY_START==SC_define_structured) {
    config_preproc_yylval.str_val = NULL;
    return FillerStuff;
  }
}

{NEWLINE} yylineno++;

. /* do nothing */

<<EOF>> {
  preproc_error_flag = 1; /* unterminated block comment, error msg. in parser */
  BEGIN(caller_state);
}
} /* SC_blockcomment */

{WHITESPACE} {
  if (YY_START==SC_define || YY_START==SC_define_structured) {
    config_preproc_yylval.str_val = mcopystrn(yytext, yyleng); return FillerStuff;
  }
}

{NEWLINE}|{LINECOMMENT} {
  yylineno++;
  if (YY_START==SC_define || YY_START==SC_define_structured) {
    config_preproc_yylval.str_val = NULL;
    return FillerStuff;
  }
}

  /* Section delimiters */

"["{WS}INCLUDE{WS}"]" BEGIN(SC_include);

"["{WS}ORDERED_INCLUDE{WS}"]" {
  caller_state = YY_START;
  BEGIN(SC_ordered_include);
}

"["{WS}DEFINE{WS}"]" BEGIN(SC_define);

"["{WS}MODULE_PARAMETERS{WS}"]"   BEGIN(SC_module_parameters);
"["{WS}TESTPORT_PARAMETERS{WS}"]" BEGIN(SC_testport_parameters);

Elemer Lelik's avatar
Elemer Lelik committed
149
<SC_testport_parameters>
Elemer Lelik's avatar
Elemer Lelik committed
150
151
{
  "["{NUMBER}"]" ;
Elemer Lelik's avatar
Elemer Lelik committed
152
153
154
155
156
157
158
159
160
}

<SC_module_parameters>
{
  "["[ \t0-9a-zA-Z+*/&-]*"]" ;
}

<SC_module_parameters,SC_testport_parameters>
{
Elemer Lelik's avatar
Elemer Lelik committed
161
162
163
164
  "["[^\r\n\[\]]*{MACRO_REFERENCE_INT}{WS}[^\r\n\[\]]*"]" ;
}

"["{WS}LOGGING{WS}"]"             BEGIN(INITIAL);
Elemer Lelik's avatar
Elemer Lelik committed
165
"["{WS}PROFILER{WS}"]"            BEGIN(INITIAL);
Elemer Lelik's avatar
Elemer Lelik committed
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
"["{WS}EXECUTE{WS}"]"             BEGIN(INITIAL);
"["{WS}EXTERNAL_COMMANDS{WS}"]"   BEGIN(INITIAL);
"["{WS}GROUPS{WS}"]"              BEGIN(INITIAL);
"["{WS}COMPONENTS{WS}"]"          BEGIN(INITIAL);
"["{WS}MAIN_CONTROLLER{WS}"]"     BEGIN(INITIAL);

"["[^\r\n\[\]]*"]" {
  preproc_error_flag = 1; 
  config_preproc_error("Invalid section name `%s'", yytext);
}

\" {
  caller_state = YY_START;
  BEGIN(SC_cstring);
  cstring.clear();

  if (caller_state == SC_define_structured) {
    cstring += '"';
  }
}

<SC_cstring>
{
\"\" cstring += '"';

\" {
  /* end of the string */
  BEGIN(caller_state);
  switch (YY_START) {
  case SC_define_structured:
    cstring += '"';
      /* No break */
  case SC_define:
    config_preproc_yylval.str_val = mcopystrn(cstring.c_str(), cstring.size());
    cstring.clear();
    return Cstring;
  case SC_include:
    if (add_include_file(cstring)) preproc_error_flag = 1;
    cstring.clear();
    break;
  case SC_ordered_include: 
    {
      std::string error_msg = switch_lexer(include_chain, cstring,
          YY_CURRENT_BUFFER, yy_create_buffer, yy_switch_to_buffer, yylineno,
          YY_BUF_SIZE);
      if (error_msg.empty()) {
        BEGIN(INITIAL);
      } else {
        preproc_error_flag = 1; 
Elemer Lelik's avatar
Elemer Lelik committed
215
        config_preproc_error("%s", error_msg.c_str());
Elemer Lelik's avatar
Elemer Lelik committed
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
      }
    }
    /* no break */
  default:
    cstring.clear();
  } /* switch */
} /* end of string */

\\[\\'"?] cstring += yytext[1]; /* backslash-quoted \ or " or ' or ? */
\\{NEWLINE} yylineno++;
\\a cstring += '\a';
\\b cstring += '\b';
\\f cstring += '\f';
\\n cstring += '\n';
\\r cstring += '\r';
\\t cstring += '\t';
\\v cstring += '\v';

\\[0-7]{1,3} {
  unsigned int c;
  sscanf(yytext + 1, "%o", &c);
  /* do not report error in case of invalid octal escape sequences */
  if (c <= 255) cstring += c;
  else preproc_error_flag = 1;
}

\\x{HEX}{1,2} {
  unsigned int c;
  sscanf(yytext + 2, "%x", &c);
  cstring += c;
}

\\(x[^\\\"]|.) preproc_error_flag = 1;

{NEWLINE} {
  cstring.append(yytext, yyleng);
  yylineno++;
}

. { cstring += yytext[0]; }

<<EOF>> {
  preproc_error_flag = 1; /* unterminated string literal, error msg. in parser */
  cstring.clear();
  BEGIN(caller_state);
  return EOF;
}
} /* SC_cstring */


<SC_define>
{

{TTCN3IDENTIFIER} {
  config_preproc_yylval.str_val = mcopystrn(yytext, yyleng);
  return Identifier;
}

":="|"=" { return AssignmentChar; }

{MACRORVALUE} {
  config_preproc_yylval.str_val = mcopystrn(yytext, yyleng);
  return MacroRValue;
}

{MACRO_REFERENCE} {
  config_preproc_yylval.str_val = mcopystrn(yytext, yyleng);
  return MacroReference;
}

"{" {
  ++paren_stack;
  BEGIN(SC_define_structured);
  return LCurly;
}
} /* SC_define */

<SC_define_structured>
{

{MACRO_REFERENCE} {
  config_preproc_yylval.str_val = mcopystrn(yytext, yyleng);
  return MacroReference;
}

"{" {
  ++paren_stack;
  return LCurly;
}

"}" {
  if (paren_stack == 0) { /* We don't have any opened curly brackets. */
    preproc_error_flag = 1;
    config_preproc_error("Invalid structured definition.");
    BEGIN(SC_define);
    return RCurly;
  }

  --paren_stack;
  if (paren_stack == 0) { /* The end of a structured definition. */
    BEGIN(SC_define);
  }
  return RCurly;
}

\\\" { /* \" is handled separately in the structured definitions */
  config_preproc_yylval.str_val = mcopystr("\""); 
  return FString;
}

\\. { /* Handle escaped characters */
  config_preproc_yylval.str_val = mcopystrn(yytext, yyleng);
  return FString;
}

[^{}"\\$\n\r\t #/]+  { /* Anything except {,},\,$,#,/ and whitespace */
  config_preproc_yylval.str_val = mcopystrn(yytext, yyleng);
  return FString;
}

"/" {
  config_preproc_yylval.str_val = mcopystrn(yytext, yyleng);
  return FString;
}

} /* SC_define_structured */

<<EOF>> { 
  if (include_chain->size() > 1) { // The last fp is owned by the parser
    yy_delete_buffer(YY_CURRENT_BUFFER);
    fclose(include_chain->back().fp);
    include_chain->pop_back();
    yy_switch_to_buffer(include_chain->back().buffer_state);
    BEGIN(SC_ordered_include);
  } else {
    return EOF; 
  }
}

. {
  switch (YY_START) {
  case SC_define:
  case SC_define_structured:
    preproc_error_flag = 1;
    config_preproc_error("Invalid character in [DEFINE] section: '%c'.", yytext[0]);
    break;
  case SC_include:
  case SC_ordered_include:
    preproc_error_flag = 1;
    config_preproc_error("Invalid character in [%s] section: '%c'.",
     (YY_START==SC_include || YY_START==SC_ordered_include) ? "INCLUDE" : "ORDERED_INCLUDE",
     yytext[0]);
  default:
    break;
  }
}


%%

void config_preproc_reset(const std::string& filename) {
  if (!include_chain) {
    include_chain = new std::deque<IncludeElem<YY_BUFFER_STATE> >();
  } else {
    include_chain->clear();
  }

  include_chain->push_back(IncludeElem<YY_BUFFER_STATE>(
        filename, config_preproc_yyin));
}

void config_preproc_close() {
  delete include_chain;
  include_chain = NULL;
}