rawAST.l 22.6 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
Adam Knapp's avatar
Adam Knapp committed
2
 * Copyright (c) 2000-2021 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
 *
 * Contributors:
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Delic, Adam
 *   Forstner, Matyas
 *   Raduly, Csaba
 *   Szabados, Kristof
15
 *   Szabo, Bence Janos
Elemer Lelik's avatar
Elemer Lelik committed
16
17
18
19
 *   Szabo, Janos Zoltan – initial implementation
 *   Szalai, Gabor
 *   Zalanyi, Balazs Andor
 *
Elemer Lelik's avatar
Elemer Lelik committed
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
 ******************************************************************************/
%option noyywrap
%option never-interactive
%option nounput

%{

#include "../../common/memory.h"
#include "../Int.hh"
#include "../Real.hh"
#include "../Setting.hh"
#include "../Value.hh"
#include "../main.hh"
#include "RawAST.hh"
#include "../XerAttributes.hh"
#include "rawASTspec.h"
// the next one is the header generated from the .y
#include "rawAST.tab.hh"
#include "AST_ttcn3.hh"
#include "Attributes.hh"
#include "BerAST.hh"

using namespace Common;

#define ALLOCATE_IF_NULL(retv, oldptr, ptype) \
    if((oldptr)==NULL){\
        retv = (ptype*)Malloc(sizeof(ptype));  \
    } else \
        retv = oldptr;

const Common::Module *mymod;
RawAST *rawstruct;
TextAST *textstruct;
XerAttributes *xerstruct;
BerAST *berstruct;
JsonAST *jsonstruct;
bool raw_f;
bool text_f;
bool xer_f;
bool ber_f;
bool json_f;
int length_multiplier;
62
Common::Type::MessageEncodingType_t selected_codec;
Elemer Lelik's avatar
Elemer Lelik committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

extern void rawAST_error(const char *str); /* defined in this file */
extern int rawAST_parse(); /* in rawAST.tab.cc, generated from rawAST.y */

//rawAST_full_spec rawAST_result;
//rawAST_encoding_spec rawAST_partial_result;

extern int rawAST_debug;

#define yylval rawAST_lval
#define yylloc rawAST_lloc

extern YYLTYPE yylloc;

/* always points to the first character of the regexp to be recognized */
78
static size_t current_line, current_column;
Elemer Lelik's avatar
Elemer Lelik committed
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

static void update_lloc()
{
  yylloc.first_line = current_line;
  yylloc.first_column = current_column;
  current_column += yyleng;
  yylloc.last_line = current_line;
  yylloc.last_column = current_column;
}

#define RETURN(retv) update_lloc(); return retv

%}

/* definitions */

LINECOMMENT "//"[^\r\n]*
WHITESPACE [ \t\v\f]+
NEWLINE \r|\n|\r\n

BIN 0|1
HEX [0-9A-Fa-f]
OCT {HEX}{HEX}
FLOAT ({NUMBER}\.[0-9]+)|((({NUMBER}(\.[0-9]+)?)|(\.[0-9]+))[Ee][+-]?{NUMBER})
NUMBER 0|([1-9][0-9]*)
IDENTIFIER [A-Za-z][A-Za-z0-9_]*
DQUOTE (\"|\\)\"
CHAR [^\"\\]|\\[^\"]|{DQUOTE}{DQUOTE}
MATCH_CHAR [^'\\]|"''"|\\(.|\n)
RPAREN [)]

/* start conditions (x: exclusive) */

%x ccomment
%x textcodec
%x rawcodec
%x jsoncodec
%x jsonvalue

%%
  int comment_caller = INITIAL;

<INITIAL,textcodec,rawcodec>{           /* all but ccomment */
"/*"	{
  yylloc.first_line = current_line;
  yylloc.first_column = current_column;
  current_column += yyleng;
  comment_caller = YY_START;
  BEGIN(ccomment);
}

{WHITESPACE}|{LINECOMMENT} current_column += yyleng;

}       /* end <all but ccomment> */

<*>{NEWLINE} current_line++; current_column = 0;

<ccomment>{
"*/" current_column += yyleng; BEGIN(comment_caller);
.    current_column++;
<<EOF>>	{
  yylloc.last_line = current_line;
  yylloc.last_column = current_column;
  Location loc(infile, yylloc);
  loc.error("Unterminated block comment in variant attribute");
  RETURN(EOF);
}
}       /* ccomment end */

<INITIAL,textcodec,rawcodec>{NUMBER}  {
  update_lloc();
  Location loc(infile, yylloc);
  yylval.intval = string2Int(yytext, loc);
  return XNumber;
}

<rawcodec,INITIAL>{

{FLOAT}	  {
  update_lloc();
  Location loc(infile, yylloc);
  yylval.floatval = string2Real(yytext, loc);
  return XFloatValue;
}
true	{ yylval.boolval = true;
          RETURN(XBooleanConst);}
false	{ yylval.boolval = false;
          RETURN(XBooleanConst);}
none 	{ yylval.verdictval = Value::Verdict_NONE;
	  RETURN(XVerdictConst);}
pass	{ yylval.verdictval = Value::Verdict_PASS;
	  RETURN(XVerdictConst);}
inconc	{ yylval.verdictval = Value::Verdict_INCONC;
	  RETURN(XVerdictConst);}
fail	{ yylval.verdictval = Value::Verdict_FAIL;
	  RETURN(XVerdictConst);}
error	{ yylval.verdictval = Value::Verdict_ERROR;
	  RETURN(XVerdictConst);}
null	RETURN(XNullKeyword);
NULL	RETURN(XNULLKeyword);
omit	RETURN(XOmitKeyword);

{DQUOTE}{CHAR}*{DQUOTE}	{
  yylloc.first_line = current_line;
  /* we have to cheat because parse_charstring_value() expects one quotation
   * mark character as delimiter, but {DQUOTE} matches two characters */
  yylloc.first_column = current_column + 1;
  current_column += 2;
  bool backslash_flag = false;
  yylval.str = memptystr();
189
190
  size_t len = (size_t)yyleng - 2;
  for (size_t i = 2; i < len; ) {
Elemer Lelik's avatar
Elemer Lelik committed
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
287
288
289
290
291
    if (!backslash_flag && (yytext[i] == '\\' || yytext[i] == '"') &&
	yytext[i + 1] == '"' &&
	(yytext[i + 2] == '\\' || yytext[i + 2] == '"') &&
	yytext[i + 3] == '"') {
      /* transform embedded escaped quotes: \"\" or """" -> "" */
      /* we must use the octal notation to keep the column numbers in synch */
      yylval.str = mputstr(yylval.str, "\\042");
      current_column += 4;
      i += 4;
    } else if (yytext[i] == '\r' && yytext[i + 1] == '\n') {
      /* handle the CR-LF sequence as a single newline */
      yylval.str = mputstr(yylval.str, "\r\n");
      current_line++;
      current_column = 0;
      backslash_flag = false;
      i += 2;
    } else {
      yylval.str = mputc(yylval.str, yytext[i]);
      if (yytext[i] == '\r' || yytext[i] == '\n') {
	  current_line++;
	  current_column = 0;
      } else current_column++;
      if (backslash_flag) backslash_flag = false;
      else if (yytext[i] == '\\') backslash_flag = true;
      i++;
    }
  }
  current_column += 2;
  yylloc.last_line = current_line;
  yylloc.last_column = current_column - 1;
  return XCstring;
}

'{BIN}*'B {
  yytext[yyleng - 2] = '\0';
  yylval.str = mcopystr(yytext + 1);
  RETURN(XBstring);
}

'[^\']*'B { /* a properly delimited bit string with incorrect content */
  /* the backslash in the character class is redundant, but helps editors
   * get the syntax highlighting right */
  update_lloc();
  rawAST_error("invalid bitstring value");
  yylval.str = memptystr();
  return XBstring;
}

'{HEX}*'H {
  yytext[yyleng - 2] = '\0';
  yylval.str = mcopystr(yytext + 1);
  RETURN(XHstring);
}

'[^\']*'H { /* a properly delimited hex string with incorrect content */
  update_lloc();
  rawAST_error("invalid hexstring value");
  yylval.str = memptystr();
  return XHstring;
}

'{OCT}*'O {
  yytext[yyleng - 2] = '\0';
  yylval.str = mcopystr(yytext + 1);
  RETURN(XOstring);
}

'[^\']*'O { /* a properly delimited octet string with incorrect content */
  update_lloc();
  rawAST_error("invalid octetstring value");
  yylval.str = memptystr();
  return XOstring;
}


'[^\']*' { /* A string delimited by a pair of ' */
  yytext[yyleng - 1] = '\0';
  yylval.str = mcopystr(yytext + 1);
  RETURN(Xstring);
}

}       /* end <rawcodec,INITIAL> */

    /* RAW encoder keywords */
PADDING     { BEGIN(rawcodec); RETURN(XPaddingKeyword); }
PREPADDING  { BEGIN(rawcodec); RETURN(XPrePaddingKeyword); }
PADDING_PATTERN  { BEGIN(rawcodec); RETURN(XPaddingPatternKeyword); }
PADDALL     RETURN(XPaddAllKeyword);
FIELDORDER  { BEGIN(rawcodec); RETURN(XFieldOrderKeyword); }
EXTENSION_BIT { BEGIN(rawcodec); RETURN(XExtensionBitKeyword); }
EXTENSION_BIT_GROUP { BEGIN(rawcodec); RETURN(XExtensionBitGroupKeyword); }
LENGTHTO    { BEGIN(rawcodec); RETURN(XLengthToKeyword); }
POINTERTO   { BEGIN(rawcodec); RETURN(XPointerToKeyword); }
UNIT        { BEGIN(rawcodec); RETURN(XUnitKeyword); }
PTRUNIT     { BEGIN(rawcodec); RETURN(XPtrUnitKeyword); }
REPEATABLE  { BEGIN(rawcodec); RETURN(XRepeatableKeyword); }
PTROFFSET   { BEGIN(rawcodec); RETURN(XPtrOffsetKeyword); }
LENGTHINDEX { BEGIN(rawcodec); RETURN(XLengthIndexKeyword); }
TAG         { BEGIN(rawcodec); RETURN(XTagKeyword); }
CROSSTAG    { BEGIN(rawcodec); RETURN(XCrossTagKeyword); }
PRESENCE    { BEGIN(rawcodec); RETURN(XPresenceKeyword); }
292
FORCEOMIT   { BEGIN(rawcodec); RETURN(XForceOmitKeyword); }
Elemer Lelik's avatar
Elemer Lelik committed
293
294
295
296
297
298
299
300
301
302
FIELDLENGTH { BEGIN(rawcodec); RETURN(XFieldLengthKeyword); }
FORMAT      { BEGIN(rawcodec); RETURN(XFieldLengthKeyword); }
ALIGN       { BEGIN(rawcodec); RETURN(XAlignKeyword); }
BYTEORDER   { BEGIN(rawcodec); RETURN(XByteOrderKeyword); }
COMP        { BEGIN(rawcodec); RETURN(XCompKeyword); }
BITORDER    { BEGIN(rawcodec); RETURN(XBitOrderKeyword); }
BITORDERINFIELD    { BEGIN(rawcodec); RETURN(XBitOrderInFieldKeyword); }
BITORDERINOCTET    { BEGIN(rawcodec); RETURN(XBitOrderInOctetKeyword); }
HEXORDER    { BEGIN(rawcodec); RETURN(XHexOrderKeyword); }
TOPLEVEL    { BEGIN(rawcodec); RETURN(XToplevelKeyword); }
Elemer Lelik's avatar
Elemer Lelik committed
303
IntX        { RETURN(XIntXKeyword); }
Elemer Lelik's avatar
Elemer Lelik committed
304
bit         { RETURN(XBitKeyword); }
305
bits        { RETURN(XBitKeyword); }
Elemer Lelik's avatar
Elemer Lelik committed
306
unsigned    { RETURN(XUnsignedKeyword); }
307
308
309
310
"UTF-8"     { RETURN(XUTF8Keyword); }
"UTF-16"    { RETURN(XUTF16Keyword); }
"IEEE754 float"  { RETURN(XIEEE754FloatKeyword); }
"IEEE754 double" { RETURN(XIEEE754DoubleKeyword); }
311
"CSN.1 L/H" { RETURN(XCsn1LHKeyword); }
Elemer Lelik's avatar
Elemer Lelik committed
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327

<rawcodec>{
yes         { yylval.enumval = XDEFYES; RETURN(XYes); }
no          { yylval.enumval = XDEFNO; RETURN(XNo); }
reverse     { yylval.enumval = XDEFREVERSE; RETURN (XReverse); }
msb         { yylval.enumval = XDEFMSB; RETURN(XMsb); }
lsb         { yylval.enumval = XDEFLSB; RETURN(XLsb); }
bits        { yylval.enumval = 1; RETURN(XBits); }
bit         { yylval.enumval = 1; RETURN(XBits); }
octets      { yylval.enumval = 8; RETURN(XOctets); }
octet       { yylval.enumval = 8; RETURN(XOctets); }
nibble      { yylval.enumval = 4; RETURN(XOctets); }
word16      { yylval.enumval = 16; RETURN(XOctets); }
dword32     { yylval.enumval = 32; RETURN(XOctets); }
elements    { yylval.enumval = -1; RETURN(XOctets); }
variable    { yylval.intval = 0; RETURN(XNumber);}
328
null_terminated    { yylval.intval = -1; RETURN(XNumber);}
Elemer Lelik's avatar
Elemer Lelik committed
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
IEEE754[ ]double    { yylval.intval = 64; RETURN(XNumber);}
IEEE754[ ]float    { yylval.intval = 32; RETURN(XNumber);}
left        { yylval.enumval = XDEFLEFT; RETURN(XLeft); }
right       { yylval.enumval = XDEFRIGHT; RETURN(XRight); }
nosign      { yylval.enumval = XDEFUNSIGNED; RETURN(XUnsigned); }
2scompl     { yylval.enumval = XDEFCOMPL; RETURN(XCompl); }
signbit     { yylval.enumval = XDEFSIGNBIT; RETURN(XSignbit); }
first       { yylval.enumval = XDEFFIRST; RETURN(XFirst); }
last        { yylval.enumval = XDEFLAST; RETURN(XLast); }
low         { yylval.enumval = XDEFLOW; RETURN(XLow); }
high        { yylval.enumval = XDEFHIGH; RETURN(XHigh); }
OTHERWISE   RETURN(XOtherwise);

{RPAREN} {BEGIN(INITIAL);RETURN(*yytext);}

}       /* end <rawcodec> */

        /* TEXT codec starters */
BEGIN       { BEGIN(textcodec);RETURN(XBeginKeyword);}
END         { BEGIN(textcodec);RETURN(XEndKeyword);}
SEPARATOR   { BEGIN(textcodec);RETURN(XSeparatorKeyword);}
TEXT_CODING { BEGIN(textcodec);RETURN(XCodingKeyword); }

<INITIAL>{

        /* XER attributes are not as well delimited like TEXT/RAW,
         * so no start condition for them (it would be difficult to determine
         * when to return to INITIAL) */

	/* First, the "starter" attributes */
Elemer Lelik's avatar
Elemer Lelik committed
359
abstract RETURN(XKWabstract);
Elemer Lelik's avatar
Elemer Lelik committed
360
361
362
363
anyAttributes	RETURN(XKWanyAttributes);
anyElement	RETURN(XKWanyElement);
attribute	RETURN(XKWattribute);
attributeFormQualified RETURN(XKWattributeFormQualified);
Elemer Lelik's avatar
Elemer Lelik committed
364
block RETURN(XKWblock);
Elemer Lelik's avatar
Elemer Lelik committed
365
366
367
368
369
370
controlNamespace RETURN(XKWcontrolNamespace);
defaultForEmpty	RETURN(XKWdefaultForEmpty);
element 	RETURN(XKWelement);
elementFormQualified  RETURN(XKWelementFormQualified);
embedValues     RETURN(XKWembedValues);
form            RETURN(XKWform);
371
fractionDigits RETURN(XKWfractionDigits);
Elemer Lelik's avatar
Elemer Lelik committed
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
list		RETURN(XKWlist);
name		RETURN(XKWname);
namespace	RETURN(XKWnamespace);
 /* pi-or-comment */
text		RETURN(XKWtext);
untagged	RETURN(XKWuntagged);
useNil  	RETURN(XKWuseNil);
useNumber	RETURN(XKWuseNumber);
useOrder	RETURN(XKWuseOrder);
useType         RETURN(XKWuseType);
useUnion	RETURN(XKWuseUnion);
whiteSpace	RETURN(XKWwhiteSpace);
XSD		RETURN(XSD);

as		RETURN(XKWas);
all		RETURN(XKWall);
in		RETURN(XKWin);
		/* name mangling */
capitalized	RETURN(XKWcapitalized);
uncapitalized	RETURN(XKWuncapitalized);
lowercased	RETURN(XKWlowercased);
uppercased	RETURN(XKWuppercased);

qualified	RETURN(XKWqualified);
unqualified	RETURN(XKWunqualified); /* ASN uses ABSENT */
except		RETURN(XKWexcept);
from		RETURN(XKWfrom);
prefix		RETURN(XKWprefix);
                /* whitespace actions */
preserve        RETURN(XKWpreserve);
collapse	RETURN(XKWcollapse);
replace 	RETURN(XKWreplace);

                /* XSD:something */
string			RETURN(XSDstring);
normalizedString	RETURN(XSDnormalizedString);
token			RETURN(XSDtoken);
Name			RETURN(XSDName);
NMTOKEN			RETURN(XSDNMTOKEN);
NCName			RETURN(XSDNCName);
ID			RETURN(XSDID);
IDREF			RETURN(XSDIDREF);
ENTITY			RETURN(XSDENTITY);
hexBinary		RETURN(XSDhexBinary);
base64Binary		RETURN(XSDbase64Binary);
anyURI			RETURN(XSDanyURI);
language		RETURN(XSDlanguage);
integer			RETURN(XSDinteger);
positiveInteger		RETURN(XSDpositiveInteger);
nonPositiveInteger	RETURN(XSDnonPositiveInteger);
negativeInteger		RETURN(XSDnegativeInteger);
nonNegativeInteger	RETURN(XSDnonNegativeInteger);
  /* already taken by BER: long	RETURN(XSDlong);*/
unsignedLong		RETURN(XSDunsignedLong);
int			RETURN(XSDint);
unsignedInt		RETURN(XSDunsignedInt);
  /* already taken by BER: short	RETURN(XSDshort);*/
unsignedShort		RETURN(XSDunsignedShort);
byte			RETURN(XSDbyte);
unsignedByte		RETURN(XSDunsignedByte);
decimal			RETURN(XSDdecimal);
float			RETURN(XSDfloat);
double			RETURN(XSDdouble);
duration		RETURN(XSDduration);
dateTime		RETURN(XSDdateTime);
time			RETURN(XSDtime);
date			RETURN(XSDdate);
gYearMonth		RETURN(XSDgYearMonth);
gYear			RETURN(XSDgYear);
gMonthDay		RETURN(XSDgMonthDay);
gDay			RETURN(XSDgDay);
gMonth			RETURN(XSDgMonth);
NMTOKENS		RETURN(XSDNMTOKENS);
IDREFS			RETURN(XSDIDREFS);
ENTITIES		RETURN(XSDENTITIES);
QName			RETURN(XSDQName);
boolean			RETURN(XSDboolean);

anySimpleType		RETURN(XSDanySimpleType);
anyType			RETURN(XSDanyType);

}       /* end XER keywords in INITIAL */

  
456
  /* prefix for legacy JSON attributes */
Elemer Lelik's avatar
Elemer Lelik committed
457
458
JSON	{ BEGIN(jsoncodec); RETURN(XKWjson); }

459
  /* legacy JSON attributes */
Elemer Lelik's avatar
Elemer Lelik committed
460
461
462
463
464
465
466
467
<jsoncodec>{
[: \t]        RETURN(*yytext);
omit          RETURN(XKWomit);
as            RETURN(XKWas);
null          RETURN(XKWnull);
name          RETURN(XKWname);
value         RETURN(XKWvalue);
default       RETURN(XKWdefault);
Elemer Lelik's avatar
Elemer Lelik committed
468
469
470
471
extend        RETURN(XKWextend);
metainfo      RETURN(XKWmetainfo);
for           RETURN(XKWfor);
unbound       RETURN(XKWunbound);
472
number        RETURN(XKWnumber);
473
474
475
476
477
478
integer       RETURN(XKWinteger);
string        RETURN(XKWstring);
array         RETURN(XKWarray);
object        RETURN(XKWobject);
objectMember  RETURN(XKWobjectMember);
literal       RETURN(XKWliteral);
Elemer Lelik's avatar
Elemer Lelik committed
479
480
481
482
483
484
[(]           { BEGIN(jsonvalue); RETURN(XJsonValueStart); }
{IDENTIFIER}  { yylval.str = mcopystr(yytext); RETURN(XAliasToken); }
[^: \t]       { rawAST_error("invalid JSON token"); }
}

<jsonvalue>{
Elemer Lelik's avatar
Elemer Lelik committed
485
486
487
488
489
[\\][\\][)] { /* \\) -> \) would not work with the other rules */
  yylval.str = mcopystr("\\)");
  RETURN(XJsonValueSegment);
}
[\\]. { /* \) -> ), all others are treated literally */
Elemer Lelik's avatar
Elemer Lelik committed
490
491
  if (yytext[1] == ')') yylval.str = mcopystr(")");
  else yylval.str = mcopystr(yytext);
Elemer Lelik's avatar
Elemer Lelik committed
492
  RETURN(XJsonValueSegment);
Elemer Lelik's avatar
Elemer Lelik committed
493
}
494
[)]           { BEGIN(INITIAL); RETURN(XJsonValueEnd); }
Elemer Lelik's avatar
Elemer Lelik committed
495
496
[\"][\"]      { yylval.str = mcopystr("\\\""); RETURN(XJsonValueSegment); }
[^\"\\)]+     { yylval.str = mcopystr(yytext); RETURN(XJsonValueSegment); }
497
498
499
500
501
502
503
504
505
506
507
}

  /* tokens for new JSON attributes (standard-compliant) */
<INITIAL>{
asValue         RETURN(XAsValueKeyword);
default       { BEGIN(jsoncodec); RETURN(XKWdefault); }
number        RETURN(XKWnumber);
extend        { BEGIN(jsoncodec); RETURN(XKWextend); }
metainfo      RETURN(XKWmetainfo);
for           RETURN(XKWfor);
unbound       RETURN(XKWunbound);
508
509
chosen        RETURN(XChosenKeyword);
otherwise     RETURN(XJsonOtherwise);
510
map           RETURN(XJsonMap);
511
512
513
escape        RETURN(XKWescape);
usi           RETURN(XKWusi);
transparent   RETURN(XKWtransparent);
Elemer Lelik's avatar
Elemer Lelik committed
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
}

<INITIAL>{
length RETURN(XKWlength);
accept RETURN(XKWaccept);
long   RETURN(XKWlong);
short  RETURN(XKWshort);
indefinite RETURN(XKWindefinite);
definite   RETURN(XKWdefinite);
}

<textcodec>{
\'{MATCH_CHAR}*\' {
  yylloc.first_line = current_line;
  yylloc.first_column = current_column;
  current_column++;
  bool backslash_flag = false;
  yylval.str = memptystr();
532
533
  size_t len = (size_t)yyleng - 1;
  for (size_t i = 1; i < len; ) {
Elemer Lelik's avatar
Elemer Lelik committed
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
    if (!backslash_flag && (yytext[i] == '\'' || yytext[i] == '"') &&
        yytext[i + 1] == yytext[i]) {
      /* transform '' -> \' and "" -> \" */
      yylval.str = mputc(yylval.str, '\\');
      yylval.str = mputc(yylval.str, yytext[i]);
      current_column += 2;
      i += 2;
    } else if (yytext[i] == '\r' && yytext[i + 1] == '\n') {
      /* handle the CR-LF sequence as a single newline */
      yylval.str = mputstr(yylval.str, "\r\n");
      current_line++;
      current_column = 0;
      backslash_flag = false;
      i += 2;
    } else {
      yylval.str = mputc(yylval.str, yytext[i]);
      if (yytext[i] == '\r' || yytext[i] == '\n') {
	  current_line++;
	  current_column = 0;
      } else current_column++;
      if (backslash_flag) backslash_flag = false;
      else if (yytext[i] == '\\') backslash_flag = true;
      i++;
    }
  }
  current_column++;
  yylloc.last_line = current_line;
  yylloc.last_column = current_column;
  return Xtoken;
}

length RETURN(XLengthToken);
repeatable RETURN(XRepeatToken);
convert RETURN(XConvertToken);
lower_case RETURN(XLowerToken);
upper_case RETURN(XUpperToken);
just RETURN(XJustToken);
left RETURN(XLeftToken);
right RETURN(XRightToken);
center RETURN(XCenterToken);
leading0 RETURN(XLeadingToken);
true RETURN(XTrueToken);
false RETURN(XFalseToken);
case_sensitive RETURN(XSensitivToken);
case_insensitive RETURN(XInSensitivToken);
{RPAREN} {BEGIN(INITIAL);RETURN(*yytext);}
}       /* end <textcodec> */

<INITIAL,textcodec,rawcodec>{
  {IDENTIFIER} {
    yylval.identifier = new Identifier(Identifier::ID_TTCN,
      string(yyleng, yytext));
    RETURN(XIdentifier);
  }
  . RETURN(*yytext);
  <<EOF>> {
    /* we must set the fake length 1 in order to report the single column
     * number of the unexpected EOF in error messages */
    yyleng = 1;
    RETURN(EOF);
  }
}

%%

void free_rawAST_field_list(rawAST_field_list *ptr)
{
  if (ptr != NULL) {
    for (int i = 0; i < ptr->nElements; i++) delete ptr->names[i];
    Free(ptr->names);
    Free(ptr);
  }
}

rawAST_single_tag*
link_rawAST_single_tag(rawAST_single_tag* dst, rawAST_single_tag* src){
    rawAST_single_tag* retv;
    if (src==NULL) return NULL;
    if (dst == src) return dst;
//    if (dst!=NULL) free_rawAST_single_tag(dst);
    ALLOCATE_IF_NULL(retv, dst, rawAST_single_tag);
    retv->fieldName=src->fieldName;
    retv->nElements=src->nElements;
    retv->keyList=src->keyList;
    return retv;
}

void
free_rawAST_single_tag(rawAST_single_tag* spec){
    int i;
    if(spec->fieldName) delete spec->fieldName;
    for (i=0; i<spec->nElements; i++) {
        free_rawAST_tag_field_value(&spec->keyList[i]);
    }
    if(spec->nElements)  Free(spec->keyList);
//    init_rawAST_single_tag(spec);
}

void
free_rawAST_tag_field_value(rawAST_tag_field_value* spec){
    free_rawAST_field_list(spec->keyField);
    Free(spec->value);
    delete spec->v_value;
//    init_rawAST_tag_field_value(spec);
}

rawAST_tag_field_value*
link_rawAST_tag_field_value(rawAST_tag_field_value* dst, rawAST_tag_field_value* src){
    rawAST_tag_field_value* retv;
    if (src==NULL) return NULL;
    if (dst == src) return dst;
    ALLOCATE_IF_NULL(retv, dst, rawAST_tag_field_value);
    retv->keyField = src->keyField;
    retv->value = src->value;
    retv->v_value = src->v_value;
    return retv;
}

rawAST_tag_list*
link_rawAST_tag_list(rawAST_tag_list* dst, rawAST_tag_list* src){
    rawAST_tag_list* retv;
    if (src==NULL) return NULL;
    if (dst == src) return dst;
    ALLOCATE_IF_NULL(retv, dst, rawAST_tag_list);
    retv->nElements=src->nElements;
    retv->tag=src->tag;
    return retv;
}

rawAST_tag_field_value*
init_rawAST_tag_field_value(rawAST_tag_field_value* spec){
    rawAST_tag_field_value* retv;
    ALLOCATE_IF_NULL(retv, spec, rawAST_tag_field_value);
    retv->keyField=NULL;
    retv->value=NULL;
    retv->v_value=NULL;
    return retv;
}

void free_rawAST_tag_list(rawAST_tag_list* spec){
    int i;
    if (spec==NULL) return;
    for (i=0; i<spec->nElements; i++) {
        free_rawAST_single_tag(&spec->tag[i]);
    }
    Free(spec->tag);
//    init_rawAST_tag_list(spec);
}

/* See RawAST.hh */
int parse_rawAST(RawAST *par, TextAST *textpar, XerAttributes *xerpar,
    BerAST* berpar, JsonAST* jsonpar, const Ttcn::AttributeSpec& attrib, 
    int l_multip, const Common::Module* mod, bool &raw_found, bool &text_found,
687
688
    bool &xer_found, bool &ber_found, bool &json_found,
    Common::Type::MessageEncodingType_t par_codec /* = Common::Type::CT_UNDEF */)
Elemer Lelik's avatar
Elemer Lelik committed
689
{
690
691
692
#ifdef PARSER_DEBUG
    rawAST_debug = 1;
#endif
Elemer Lelik's avatar
Elemer Lelik committed
693
694
695
696
697
698
699
    rawstruct=par;
    textstruct=textpar;
    xerstruct = xerpar;
    berstruct = berpar;
    jsonstruct = jsonpar;

    length_multiplier=l_multip;
700
    selected_codec = par_codec;
Elemer Lelik's avatar
Elemer Lelik committed
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
    infile = attrib.get_filename();
    current_line = attrib.get_first_line();
    /* skip the leading " of the attribute value */
    current_column = attrib.get_first_column() + 1;
    mymod=mod;
    raw_f =false;
    text_f=false;
    xer_f =false;
    ber_f =false;
    json_f = false;
    const string& s = attrib.get_spec();
#ifndef NDEBUG
    if (rawAST_debug) {
      fprintf(stderr, "*** raw=[%s]\n", s.c_str());
    }
#endif
    struct yy_buffer_state *flex_buffer = yy_scan_bytes(s.c_str(), s.size());
    if (!flex_buffer) {
      FATAL_ERROR("parse_rawAST(): flex buffer creation failed");
    }
    BEGIN(INITIAL);
    int retn=rawAST_parse();
    yylex_destroy();
724
725
    if(!raw_found) raw_found=raw_f || (legacy_codec_handling &&
      !text_f && !xer_f && !ber_f && !json_f);
Elemer Lelik's avatar
Elemer Lelik committed
726
727
728
729
730
731
732
733
734
735
736
737
738
739
    // if none found, pretend some RAW attribute was found
    if(!text_found) text_found=text_f;
    if(!xer_found) xer_found=xer_f;
    if(!ber_found) ber_found=ber_f;
    if (!json_found) json_found = json_f;

    return retn;
}

void rawAST_error(const char *str)
{
  Location loc(infile, yylloc);
  if (*yytext) {
    // the most recently parsed token is known
Elemer Lelik's avatar
Elemer Lelik committed
740
741
742
743
744
    if (warnings_for_bad_variants) {
      loc.warning("in variant attribute, at or before token `%s': %s", yytext, str);
    } else {
      loc.error("in variant attribute, at or before token `%s': %s", yytext, str);
    }
Elemer Lelik's avatar
Elemer Lelik committed
745
746
  } else {
    // the most recently parsed token is unknown
Elemer Lelik's avatar
Elemer Lelik committed
747
748
749
750
751
    if (warnings_for_bad_variants) {
      loc.warning("in variant attribute: %s", str);
    } else {
      loc.error("in variant attribute: %s", str);
    }
Elemer Lelik's avatar
Elemer Lelik committed
752
753
754
755
756
757
758
759
760
761
  }
}

/*
 Local Variables:
 mode: C
 indent-tabs-mode: nil
 c-basic-offset: 4
 End:
*/