File: pattern_la.l

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 (286 lines) | stat: -rw-r--r-- 6,327 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
/******************************************************************************
 * 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:
 *   Baji, Laszlo
 *   Balasko, Jeno
 *   Delic, Adam
 *   Forstner, Matyas
 *   Raduly, Csaba
 *   Szabo, Janos Zoltan – initial implementation
 *
 ******************************************************************************/
 
/**
 * Lexical analyzer for TTCN-3 character patterns.
 *
 * \author Matyas Forstner (Matyas.Forstner@eth.ericsson.se)
 *
 * 20031121
 */

%option nostack
%option noyylineno
%option noyywrap
%option nounput
%option never-interactive
%option prefix="pattern_yy"

%{ /* ****************** C declarations ***************** */

#include <ctype.h>
#include <stddef.h>
#include "pattern.hh"
#include "pattern_p.hh"

  /* Access the semantic value of the bison parser. Usually this is done by
   * #defining yylval to the appropriate symbol whose name depends on the
   * %name-prefix of the parser, e.g. pattern_yylval or pattern_unilval.
   *
   * Because we need to be able to access either one or the other,
   * we keep a pointer which is set by the parser when it calls
   * init_pattern_yylex() */
static YYSTYPE  *yylval_ptr;
#define yylval (*yylval_ptr)

static unsigned int nof_parentheses = 0;
static bool meta = false;

%} /* ***************** definitions ***************** */

NUMBER 0|([1-9][0-9]*)

/* start conditions */
%x SC_Set SC_Hash SC_HashParen SC_Quadruple SC_Quadruple_Set

%% /* ***************** rules ************************* */

  /* drop whitespaces */
<SC_Hash,SC_HashParen,SC_Quadruple,SC_Quadruple_Set>[ \t\r\n\v\f]+

<SC_Set>
{

"]" {
  BEGIN(INITIAL);
  return KW_Set_End;
}

"-]" {
  BEGIN(INITIAL);
  return KW_Set_Dash_End;
}

"-" return '-';

} /* SC_Set */

<SC_Hash>
{

[0-9] {
  BEGIN(INITIAL);
  yylval.u = yytext[0] - '0';
  return TOK_Digit;
}

"(" {
  BEGIN(SC_HashParen);
  return '(';
}

} /* SC_Hash */

<SC_HashParen,SC_Quadruple,SC_Quadruple_Set>
{

{NUMBER} {
  errno = 0;
  yylval.u = strtoul(yytext, NULL, 10);
  if (errno != 0) TTCN_pattern_error("Number `%s' is too large to be "
    "represented in memory. (%s)", yytext, strerror(errno));
  return TOK_Number;
}

"," return ',';

} /* SC_HashParen,SC_Quadruple,SC_Quadruple_Set */

<SC_HashParen>")" {
  BEGIN(INITIAL);
  return ')';
}

<SC_Quadruple,SC_Quadruple_Set>
{

"{" return '{';

"}" {
  if (YY_START == SC_Quadruple) BEGIN(INITIAL);
  else BEGIN(SC_Set);
  return '}';
}

} /* SC_Quadruple,SC_Quadruple_Set */

"*" { meta = true; return '*'; }
"+" { meta = true; return '+'; }
"?" { meta = true; return '?'; }
"|" { meta = true; return '|'; }

"(" {
  nof_parentheses++;
  meta = true;
  return KW_Group_Begin;
}

")" {
  if (nof_parentheses > 0) {
    nof_parentheses--;
    return KW_Group_End;
  } else {
    TTCN_pattern_error("Unmatched `)'.");
    yylval.c = ')';
    return TOK_Char;
  }
}

"[" {
  BEGIN(SC_Set);
  meta = true;
  return KW_Set_Begin;
}

"[^" {
  BEGIN(SC_Set);
  meta = true;
  return KW_Set_Begin_Neg;
}

"[]" {
  BEGIN(SC_Set);
  meta = true;
  return KW_Set_Begin_Rsbrkt;
}

"[^]" {
  BEGIN(SC_Set);
  meta = true;
  return KW_Set_Begin_Neg_Rsbrkt;
}

"]" {
  TTCN_pattern_error("Unmatched `]'.");
  yylval.c = ']';
  return TOK_Char;
}

"#" {
  BEGIN(SC_Hash);
  meta = true;
  return '#';
}

<INITIAL,SC_Set>
{

  /* \metacharacters */
"\\d" { meta = true; return KW_BS_d; }
"\\w" { meta = true; return KW_BS_w; }
"\\t" { meta = true; return KW_BS_t; }
"\\n" { meta = true; return KW_BS_n; }
"\\r" { meta = true; return KW_BS_r; }
"\\s" { meta = true; return KW_BS_s; }
"\\b" { meta = true; return KW_BS_b; }

"\\q" {
  meta = true;
  if (YY_START == INITIAL) BEGIN(SC_Quadruple);
  else BEGIN(SC_Quadruple_Set);
  return KW_BS_q;
}

  /* escaped special characters: ? * \ [ ] - ^ | ( ) # + { } */
\\[][?*\\^|()#+{}-] {
  yylval.c = yytext[1];
  return TOK_Char; /* not meta */
}

 /* invalid escape sequences */
"\\"(.|"\n") {
  if (isprint(static_cast<unsigned char>(yytext[1])))
    TTCN_pattern_warning("Use of unrecognized escape sequence `\\%c' is "
      "deprecated.", yytext[1]);
  else TTCN_pattern_warning("Use of unrecognized escape sequence `\\' + "
    "character code %u (0x%02X) is deprecated.", static_cast<unsigned char>(yytext[1]),
    static_cast<unsigned char>(yytext[1]));
  yylval.c = yytext[1];
  return TOK_Char;
}

 /* single backslash (at the end) */
\\ {
  TTCN_pattern_error("Invalid single backslash (`\\') character at the end "
    "of the pattern.");
}

.|"\n" {
  yylval.c = yytext[0];
  return TOK_Char;
}

} /* INITIAL, SC_Set */

 /* erroneous characters */

<SC_Hash>.|\n {
  if (isprint(static_cast<unsigned char>(yytext[0])))
    TTCN_pattern_error("A digit or `(' was expected after `#' instead of "
      "character `%c'.", yytext[0]);
  else TTCN_pattern_error("A digit or `(' was expected after `#' instead of "
    "character with code %u (0x%02X).", static_cast<unsigned char>(yytext[0]),
    static_cast<unsigned char>(yytext[0]));
}

<SC_HashParen>. {
  if (isprint(static_cast<unsigned char>(yytext[0])))
    TTCN_pattern_error("A number, `,' or `)' was expected after `#(' instead "
      "of character `%c'.", yytext[0]);
  else TTCN_pattern_error("A number, `,' or `)' was expected after `#(' "
    "instead of character with code %u (0x%02X).", static_cast<unsigned char>(yytext[0]),
    static_cast<unsigned char>(yytext[0]));
}

<SC_Quadruple,SC_Quadruple_Set>. {
  if (isprint(static_cast<unsigned char>(yytext[0])))
    TTCN_pattern_error("A number, `,' or `}' was expected after `\\q{' "
      "instead of character `%c'.", yytext[0]);
  else TTCN_pattern_error("A number, `,' or `}' was expected after `\\q{' "
    "instead of character with code %u (0x%02X).", static_cast<unsigned char>(yytext[0]),
    static_cast<unsigned char>(yytext[0]));
}

%%

unsigned int get_nof_parentheses()
{
  return nof_parentheses;
}

bool has_meta()
{
  return meta;
}

void init_pattern_yylex(YYSTYPE *sema_val)
{
  BEGIN(INITIAL);
  yylval_ptr = sema_val;
  nof_parentheses = 0;
  meta = false;
}