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
14 * Szabo, Janos Zoltan – initial implementation
16 ******************************************************************************/
19 * Lexical analyzer for TTCN-3 character patterns.
21 * \author Matyas Forstner (Matyas.Forstner@eth.ericsson.se)
30 %option never-interactive
31 %option prefix="pattern_yy"
33 %{ /* ****************** C declarations ***************** */
38 #include "pattern_p.hh"
40 /* Access the semantic value of the bison parser. Usually this is done by
41 * #defining yylval to the appropriate symbol whose name depends on the
42 * %name-prefix of the parser, e.g. pattern_yylval or pattern_unilval.
44 * Because we need to be able to access either one or the other,
45 * we keep a pointer which is set by the parser when it calls
46 * init_pattern_yylex() */
47 static YYSTYPE *yylval_ptr;
48 #define yylval (*yylval_ptr)
50 static unsigned int nof_parentheses = 0;
51 static bool meta = false;
53 %} /* ***************** definitions ***************** */
55 NUMBER 0|([1-9][0-9]*)
57 /* start conditions */
58 %x SC_Set SC_Hash SC_HashParen SC_Quadruple SC_Quadruple_Set
60 %% /* ***************** rules ************************* */
62 /* drop whitespaces */
63 <SC_Hash,SC_HashParen,SC_Quadruple,SC_Quadruple_Set>[ \t\r\n\v\f]+
75 return KW_Set_Dash_End;
87 yylval.u = yytext[0] - '0';
98 <SC_HashParen,SC_Quadruple,SC_Quadruple_Set>
103 yylval.u = strtoul(yytext, NULL, 10);
104 if (errno != 0) TTCN_pattern_error("Number `%s' is too large to be "
105 "represented in memory. (%s)", yytext, strerror(errno));
111 } /* SC_HashParen,SC_Quadruple,SC_Quadruple_Set */
118 <SC_Quadruple,SC_Quadruple_Set>
124 if (YY_START == SC_Quadruple) BEGIN(INITIAL);
129 } /* SC_Quadruple,SC_Quadruple_Set */
131 "*" { meta = true; return '*'; }
132 "+" { meta = true; return '+'; }
133 "?" { meta = true; return '?'; }
134 "|" { meta = true; return '|'; }
139 return KW_Group_Begin;
143 if (nof_parentheses > 0) {
147 TTCN_pattern_error("Unmatched `)'.");
162 return KW_Set_Begin_Neg;
168 return KW_Set_Begin_Rsbrkt;
174 return KW_Set_Begin_Neg_Rsbrkt;
178 TTCN_pattern_error("Unmatched `]'.");
192 /* \metacharacters */
193 "\\d" { meta = true; return KW_BS_d; }
194 "\\w" { meta = true; return KW_BS_w; }
195 "\\t" { meta = true; return KW_BS_t; }
196 "\\n" { meta = true; return KW_BS_n; }
197 "\\r" { meta = true; return KW_BS_r; }
198 "\\s" { meta = true; return KW_BS_s; }
199 "\\b" { meta = true; return KW_BS_b; }
203 if (YY_START == INITIAL) BEGIN(SC_Quadruple);
204 else BEGIN(SC_Quadruple_Set);
208 /* escaped special characters: ? * \ [ ] - ^ | ( ) # + { } */
209 \\[][?*\\^|()#+{}-] {
210 yylval.c = yytext[1];
211 return TOK_Char; /* not meta */
214 /* invalid escape sequences */
216 if (isprint((unsigned char)yytext[1]))
217 TTCN_pattern_warning("Use of unrecognized escape sequence `\\%c' is "
218 "deprecated.", yytext[1]);
219 else TTCN_pattern_warning("Use of unrecognized escape sequence `\\' + "
220 "character code %u (0x%02X) is deprecated.", (unsigned char)yytext[1],
221 (unsigned char)yytext[1]);
222 yylval.c = yytext[1];
226 /* single backslash (at the end) */
228 TTCN_pattern_error("Invalid single backslash (`\\') character at the end "
233 yylval.c = yytext[0];
237 } /* INITIAL, SC_Set */
239 /* erroneous characters */
242 if (isprint((unsigned char)yytext[0]))
243 TTCN_pattern_error("A digit or `(' was expected after `#' instead of "
244 "character `%c'.", yytext[0]);
245 else TTCN_pattern_error("A digit or `(' was expected after `#' instead of "
246 "character with code %u (0x%02X).", (unsigned char)yytext[0],
247 (unsigned char)yytext[0]);
251 if (isprint((unsigned char)yytext[0]))
252 TTCN_pattern_error("A number, `,' or `)' was expected after `#(' instead "
253 "of character `%c'.", yytext[0]);
254 else TTCN_pattern_error("A number, `,' or `)' was expected after `#(' "
255 "instead of character with code %u (0x%02X).", (unsigned char)yytext[0],
256 (unsigned char)yytext[0]);
259 <SC_Quadruple,SC_Quadruple_Set>. {
260 if (isprint((unsigned char)yytext[0]))
261 TTCN_pattern_error("A number, `,' or `}' was expected after `\\q{' "
262 "instead of character `%c'.", yytext[0]);
263 else TTCN_pattern_error("A number, `,' or `}' was expected after `\\q{' "
264 "instead of character with code %u (0x%02X).", (unsigned char)yytext[0],
265 (unsigned char)yytext[0]);
270 unsigned int get_nof_parentheses()
272 return nof_parentheses;
280 void init_pattern_yylex(YYSTYPE *sema_val)
283 yylval_ptr = sema_val;