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 ******************************************************************************/
18 %option never-interactive
21 %option noyy_top_state
22 %option prefix="coding_attrib_"
26 /* Lexer for "extension" attributes of functions, external functions and
27 * port types related to message encoding. */
31 #include "../string.hh"
32 #include "../Identifier.hh"
33 #include "Attributes.hh"
34 #include "Ttcnstuff.hh"
37 using namespace Common;
39 #include "coding_attrib_p.hh"
41 #define yylval coding_attrib_lval
42 #define yylloc coding_attrib_lloc
44 /** global variable indicating the location of the returned token to bison */
45 extern YYLTYPE yylloc;
47 /** the name of the current input file */
48 const char *coding_attrib_infile;
50 /** always points to the first character of the regexp to be recognized */
51 static int current_line, current_column;
53 /** the actual size of state condition stack */
54 static size_t stack_size = 0;
56 static void fill_location()
58 yylloc.first_line = current_line;
59 yylloc.first_column = current_column;
60 current_column += yyleng;
61 yylloc.last_line = current_line;
62 yylloc.last_column = current_column;
65 #define PUSH_STATE(new_state) yy_push_state(new_state); stack_size++
67 #define RETURN(ret_val) fill_location(); last_token = ret_val; return ret_val
71 LINECOMMENT "//"[^\r\n]*
74 IDENTIFIER [A-Za-z][A-Za-z0-9_]*
78 %s SC_prototype SC_errorbehavior
79 %s SC_encodingtype SC_encodingoptions
80 %s SC_user SC_typemappinglist SC_mappingtype SC_functionmapping
86 /* local variables of yylex() */
87 static int last_token = EOF;
88 int start_line = 0, start_column = 0; /**< used by block comments */
90 /* eat up whitespaces and line comments in all states */
93 {WHITESPACE}+|{LINECOMMENT} current_column += yyleng;
94 {WHITESPACE}*{NEWLINE} {
101 start_line = current_line;
102 start_column = current_column;
103 current_column += yyleng;
104 PUSH_STATE(SC_blockcomment);
109 current_column += yyleng;
113 [^\r\n] current_column++;
116 <SC_errorbehavior>{IDENTIFIER} {
117 yylval.str = new string(yyleng, yytext);
118 RETURN(ErrorBehaviorString);
121 <SC_encodingoptions>[^ \t\r\n\(\)]+ {
122 yylval.str = new string(yyleng, yytext);
123 RETURN(EncodingOption);
127 compact RETURN(CompactKeyword);
128 pretty RETURN(PrettyKeyword);
131 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
132 * Initial start condition. Will switch to another start condition later,
133 * when the opening paren '(' is encountered.
136 prototype RETURN(PrototypeKeyword);
138 address RETURN(AddressKeyword);
140 internal RETURN(InternalKeyword);
141 provider RETURN(ProviderKeyword);
146 anytype RETURN(AnyTypeKeyword);
148 version RETURN(VersionKeyword);
149 requires RETURN(RequiresKeyword);
150 requiresTITAN RETURN(ReqTitanKeyword);
152 transparent RETURN(TransparentKeyword);
155 /* TTCN-3 keywords are valid in all (non-exclusive) states */
157 bitstring RETURN(BitStringKeyword);
158 boolean RETURN(BooleanKeyword);
159 charstring RETURN(CharStringKeyword);
160 default RETURN(DefaultKeyword);
161 float RETURN(FloatKeyword);
162 function RETURN(FunctionKeyword);
163 hexstring RETURN(HexStringKeyword);
164 in RETURN(InKeyword);
165 integer RETURN(IntegerKeyword);
166 objid RETURN(ObjectIdentifierKeyword);
167 octetstring RETURN(OctetStringKeyword);
168 out RETURN(OutKeyword);
169 universal RETURN(UniversalKeyword);
170 verdicttype RETURN(VerdictTypeKeyword);
172 /* the rest of the keywords are context sensitive */
174 <SC_start,SC_mappingtype>{
175 decode RETURN(DecodeKeyword);
176 encode RETURN(EncodeKeyword);
177 errorbehavior RETURN(ErrorBehaviorKeyword);
178 printing RETURN(PrintingKeyword);
182 discard RETURN(DiscardKeyword);
183 simple RETURN(SimpleKeyword);
188 yylval.prototype = Def_Function_Base::PROTOTYPE_BACKTRACK;
189 RETURN(PrototypeSetting);
192 yylval.prototype = Def_Function_Base::PROTOTYPE_CONVERT;
193 RETURN(PrototypeSetting);
196 yylval.prototype = Def_Function_Base::PROTOTYPE_FAST;
197 RETURN(PrototypeSetting);
200 yylval.prototype = Def_Function_Base::PROTOTYPE_SLIDING;
201 RETURN(PrototypeSetting);
207 yylval.encoding_type = Type::CT_BER;
208 RETURN(EncodingType);
211 yylval.encoding_type = Type::CT_PER;
212 RETURN(EncodingType);
215 yylval.encoding_type = Type::CT_XER;
216 RETURN(EncodingType);
219 yylval.encoding_type = Type::CT_RAW;
220 RETURN(EncodingType);
223 yylval.encoding_type = Type::CT_TEXT;
224 RETURN(EncodingType);
227 yylval.encoding_type = Type::CT_JSON;
228 RETURN(EncodingType);
231 yylval.str = new string(yyleng, yytext);
232 RETURN(CustomEncoding);
237 yylval.id = new Identifier(Identifier::ID_TTCN, string(yyleng, yytext));
242 yylval.number = atoi(yytext);
247 "->" RETURN(RedirectSymbol);
250 switch (last_token) {
251 case PrototypeKeyword:
252 PUSH_STATE(SC_prototype);
256 PUSH_STATE(SC_encodingtype);
258 case ErrorBehaviorKeyword:
259 PUSH_STATE(SC_errorbehavior);
263 PUSH_STATE(SC_typemappinglist);
265 case FunctionKeyword:
266 PUSH_STATE(SC_functionmapping);
268 case PrintingKeyword:
269 PUSH_STATE(SC_printing);
272 PUSH_STATE(YY_START); /* Push the current state (duplicate) */
275 if (YY_START == SC_encodingoptions) {
276 yylval.str = new string(yyleng, yytext);
277 RETURN(EncodingOption);
284 if (stack_size > 0) {
288 if (YY_START == SC_encodingoptions) {
289 yylval.str = new string(yyleng, yytext);
290 RETURN(EncodingOption);
292 if (YY_START == SC_user) BEGIN(SC_start);
298 switch (YY_START) /* current start condition is... */ {
299 case SC_encodingtype: /* BER | RAW | ... */
300 BEGIN(SC_encodingoptions);
302 case SC_typemappinglist:
303 BEGIN(SC_mappingtype);
310 if (YY_START == SC_mappingtype) BEGIN(SC_typemappinglist);
314 [-.<>/] RETURN(*yytext);
319 Location loc(coding_attrib_infile, yylloc);
320 int c = (unsigned char)yytext[0];
321 loc.error("Character `%c' (0x%02X) cannot be used in extension attribute",
322 isprint(c) ? c : '?', c);
325 if (YY_START == SC_blockcomment) {
326 Location loc(coding_attrib_infile, start_line, start_column,
327 current_line, current_column);
328 loc.error("Unterminated block comment");
330 /* we must set the fake length 1 in order to report the single column number
331 * of the unexpected EOF in error messages */
339 void init_coding_attrib_lex(/*char definition_type,*/ const AttributeSpec& attrib)
342 FATAL_ERROR("init_coding_attrib_lex(): stack is not empty");
343 coding_attrib_infile = attrib.get_filename();
344 current_line = attrib.get_first_line();
345 /* skip the leading " of the attribute value */
346 current_column = attrib.get_first_column() + 1;
347 const string& s = attrib.get_spec();
348 yy_scan_bytes(s.c_str(), s.size());
352 void cleanup_coding_attrib_lex()
354 while (stack_size > 0) {
358 coding_attrib_lex_destroy();