1 /******************************************************************************
2 * Copyright (c) 2000-2015 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
7 ******************************************************************************/
9 %option never-interactive
14 #include "../../common/memory.h"
17 #include "../Setting.hh"
18 #include "../Value.hh"
21 #include "../XerAttributes.hh"
22 #include "rawASTspec.h"
23 // the next one is the header generated from the .y
24 #include "rawAST.tab.hh"
25 #include "AST_ttcn3.hh"
26 #include "Attributes.hh"
29 using namespace Common;
31 #define ALLOCATE_IF_NULL(retv, oldptr, ptype) \
33 retv = (ptype*)Malloc(sizeof(ptype)); \
37 const Common::Module *mymod;
40 XerAttributes *xerstruct;
48 int length_multiplier;
50 extern void rawAST_error(const char *str); /* defined in this file */
51 extern int rawAST_parse(); /* in rawAST.tab.cc, generated from rawAST.y */
53 //rawAST_full_spec rawAST_result;
54 //rawAST_encoding_spec rawAST_partial_result;
56 extern int rawAST_debug;
58 #define yylval rawAST_lval
59 #define yylloc rawAST_lloc
61 extern YYLTYPE yylloc;
63 /* always points to the first character of the regexp to be recognized */
64 static int current_line, current_column;
66 static void update_lloc()
68 yylloc.first_line = current_line;
69 yylloc.first_column = current_column;
70 current_column += yyleng;
71 yylloc.last_line = current_line;
72 yylloc.last_column = current_column;
75 #define RETURN(retv) update_lloc(); return retv
81 LINECOMMENT "//"[^\r\n]*
88 FLOAT ({NUMBER}\.[0-9]+)|((({NUMBER}(\.[0-9]+)?)|(\.[0-9]+))[Ee][+-]?{NUMBER})
89 NUMBER 0|([1-9][0-9]*)
90 IDENTIFIER [A-Za-z][A-Za-z0-9_]*
92 CHAR [^\"\\]|\\[^\"]|{DQUOTE}{DQUOTE}
93 MATCH_CHAR [^'\\]|"''"|\\(.|\n)
96 /* start conditions (x: exclusive) */
105 int comment_caller = INITIAL;
107 <INITIAL,textcodec,rawcodec>{ /* all but ccomment */
109 yylloc.first_line = current_line;
110 yylloc.first_column = current_column;
111 current_column += yyleng;
112 comment_caller = YY_START;
116 {WHITESPACE}|{LINECOMMENT} current_column += yyleng;
118 } /* end <all but ccomment> */
120 <*>{NEWLINE} current_line++; current_column = 0;
123 "*/" current_column += yyleng; BEGIN(comment_caller);
126 yylloc.last_line = current_line;
127 yylloc.last_column = current_column;
128 Location loc(infile, yylloc);
129 loc.error("Unterminated block comment in variant attribute");
134 <INITIAL,textcodec,rawcodec>{NUMBER} {
136 Location loc(infile, yylloc);
137 yylval.intval = string2Int(yytext, loc);
145 Location loc(infile, yylloc);
146 yylval.floatval = string2Real(yytext, loc);
149 true { yylval.boolval = true;
150 RETURN(XBooleanConst);}
151 false { yylval.boolval = false;
152 RETURN(XBooleanConst);}
153 none { yylval.verdictval = Value::Verdict_NONE;
154 RETURN(XVerdictConst);}
155 pass { yylval.verdictval = Value::Verdict_PASS;
156 RETURN(XVerdictConst);}
157 inconc { yylval.verdictval = Value::Verdict_INCONC;
158 RETURN(XVerdictConst);}
159 fail { yylval.verdictval = Value::Verdict_FAIL;
160 RETURN(XVerdictConst);}
161 error { yylval.verdictval = Value::Verdict_ERROR;
162 RETURN(XVerdictConst);}
163 null RETURN(XNullKeyword);
164 NULL RETURN(XNULLKeyword);
165 omit RETURN(XOmitKeyword);
167 {DQUOTE}{CHAR}*{DQUOTE} {
168 yylloc.first_line = current_line;
169 /* we have to cheat because parse_charstring_value() expects one quotation
170 * mark character as delimiter, but {DQUOTE} matches two characters */
171 yylloc.first_column = current_column + 1;
173 bool backslash_flag = false;
174 yylval.str = memptystr();
175 for (size_t i = 2; i < yyleng - 2; ) {
176 if (!backslash_flag && (yytext[i] == '\\' || yytext[i] == '"') &&
177 yytext[i + 1] == '"' &&
178 (yytext[i + 2] == '\\' || yytext[i + 2] == '"') &&
179 yytext[i + 3] == '"') {
180 /* transform embedded escaped quotes: \"\" or """" -> "" */
181 /* we must use the octal notation to keep the column numbers in synch */
182 yylval.str = mputstr(yylval.str, "\\042");
185 } else if (yytext[i] == '\r' && yytext[i + 1] == '\n') {
186 /* handle the CR-LF sequence as a single newline */
187 yylval.str = mputstr(yylval.str, "\r\n");
190 backslash_flag = false;
193 yylval.str = mputc(yylval.str, yytext[i]);
194 if (yytext[i] == '\r' || yytext[i] == '\n') {
197 } else current_column++;
198 if (backslash_flag) backslash_flag = false;
199 else if (yytext[i] == '\\') backslash_flag = true;
204 yylloc.last_line = current_line;
205 yylloc.last_column = current_column - 1;
210 yytext[yyleng - 2] = '\0';
211 yylval.str = mcopystr(yytext + 1);
215 '[^\']*'B { /* a properly delimited bit string with incorrect content */
216 /* the backslash in the character class is redundant, but helps editors
217 * get the syntax highlighting right */
219 rawAST_error("invalid bitstring value");
220 yylval.str = memptystr();
225 yytext[yyleng - 2] = '\0';
226 yylval.str = mcopystr(yytext + 1);
230 '[^\']*'H { /* a properly delimited hex string with incorrect content */
232 rawAST_error("invalid hexstring value");
233 yylval.str = memptystr();
238 yytext[yyleng - 2] = '\0';
239 yylval.str = mcopystr(yytext + 1);
243 '[^\']*'O { /* a properly delimited octet string with incorrect content */
245 rawAST_error("invalid octetstring value");
246 yylval.str = memptystr();
251 '[^\']*' { /* A string delimited by a pair of ' */
252 yytext[yyleng - 1] = '\0';
253 yylval.str = mcopystr(yytext + 1);
257 } /* end <rawcodec,INITIAL> */
259 /* RAW encoder keywords */
260 PADDING { BEGIN(rawcodec); RETURN(XPaddingKeyword); }
261 PREPADDING { BEGIN(rawcodec); RETURN(XPrePaddingKeyword); }
262 PADDING_PATTERN { BEGIN(rawcodec); RETURN(XPaddingPatternKeyword); }
263 PADDALL RETURN(XPaddAllKeyword);
264 FIELDORDER { BEGIN(rawcodec); RETURN(XFieldOrderKeyword); }
265 EXTENSION_BIT { BEGIN(rawcodec); RETURN(XExtensionBitKeyword); }
266 EXTENSION_BIT_GROUP { BEGIN(rawcodec); RETURN(XExtensionBitGroupKeyword); }
267 LENGTHTO { BEGIN(rawcodec); RETURN(XLengthToKeyword); }
268 POINTERTO { BEGIN(rawcodec); RETURN(XPointerToKeyword); }
269 UNIT { BEGIN(rawcodec); RETURN(XUnitKeyword); }
270 PTRUNIT { BEGIN(rawcodec); RETURN(XPtrUnitKeyword); }
271 REPEATABLE { BEGIN(rawcodec); RETURN(XRepeatableKeyword); }
272 PTROFFSET { BEGIN(rawcodec); RETURN(XPtrOffsetKeyword); }
273 LENGTHINDEX { BEGIN(rawcodec); RETURN(XLengthIndexKeyword); }
274 TAG { BEGIN(rawcodec); RETURN(XTagKeyword); }
275 CROSSTAG { BEGIN(rawcodec); RETURN(XCrossTagKeyword); }
276 PRESENCE { BEGIN(rawcodec); RETURN(XPresenceKeyword); }
277 FIELDLENGTH { BEGIN(rawcodec); RETURN(XFieldLengthKeyword); }
278 FORMAT { BEGIN(rawcodec); RETURN(XFieldLengthKeyword); }
279 ALIGN { BEGIN(rawcodec); RETURN(XAlignKeyword); }
280 BYTEORDER { BEGIN(rawcodec); RETURN(XByteOrderKeyword); }
281 COMP { BEGIN(rawcodec); RETURN(XCompKeyword); }
282 BITORDER { BEGIN(rawcodec); RETURN(XBitOrderKeyword); }
283 BITORDERINFIELD { BEGIN(rawcodec); RETURN(XBitOrderInFieldKeyword); }
284 BITORDERINOCTET { BEGIN(rawcodec); RETURN(XBitOrderInOctetKeyword); }
285 HEXORDER { BEGIN(rawcodec); RETURN(XHexOrderKeyword); }
286 TOPLEVEL { BEGIN(rawcodec); RETURN(XToplevelKeyword); }
287 IntX { RETURN(XIntXKeyword); }
288 bit { RETURN(XBitKeyword); }
289 unsigned { RETURN(XUnsignedKeyword); }
292 yes { yylval.enumval = XDEFYES; RETURN(XYes); }
293 no { yylval.enumval = XDEFNO; RETURN(XNo); }
294 reverse { yylval.enumval = XDEFREVERSE; RETURN (XReverse); }
295 msb { yylval.enumval = XDEFMSB; RETURN(XMsb); }
296 lsb { yylval.enumval = XDEFLSB; RETURN(XLsb); }
297 bits { yylval.enumval = 1; RETURN(XBits); }
298 bit { yylval.enumval = 1; RETURN(XBits); }
299 octets { yylval.enumval = 8; RETURN(XOctets); }
300 octet { yylval.enumval = 8; RETURN(XOctets); }
301 nibble { yylval.enumval = 4; RETURN(XOctets); }
302 word16 { yylval.enumval = 16; RETURN(XOctets); }
303 dword32 { yylval.enumval = 32; RETURN(XOctets); }
304 elements { yylval.enumval = -1; RETURN(XOctets); }
305 variable { yylval.intval = 0; RETURN(XNumber);}
306 IEEE754[ ]double { yylval.intval = 64; RETURN(XNumber);}
307 IEEE754[ ]float { yylval.intval = 32; RETURN(XNumber);}
308 left { yylval.enumval = XDEFLEFT; RETURN(XLeft); }
309 right { yylval.enumval = XDEFRIGHT; RETURN(XRight); }
310 nosign { yylval.enumval = XDEFUNSIGNED; RETURN(XUnsigned); }
311 2scompl { yylval.enumval = XDEFCOMPL; RETURN(XCompl); }
312 signbit { yylval.enumval = XDEFSIGNBIT; RETURN(XSignbit); }
313 first { yylval.enumval = XDEFFIRST; RETURN(XFirst); }
314 last { yylval.enumval = XDEFLAST; RETURN(XLast); }
315 low { yylval.enumval = XDEFLOW; RETURN(XLow); }
316 high { yylval.enumval = XDEFHIGH; RETURN(XHigh); }
317 OTHERWISE RETURN(XOtherwise);
319 {RPAREN} {BEGIN(INITIAL);RETURN(*yytext);}
321 } /* end <rawcodec> */
323 /* TEXT codec starters */
324 BEGIN { BEGIN(textcodec);RETURN(XBeginKeyword);}
325 END { BEGIN(textcodec);RETURN(XEndKeyword);}
326 SEPARATOR { BEGIN(textcodec);RETURN(XSeparatorKeyword);}
327 TEXT_CODING { BEGIN(textcodec);RETURN(XCodingKeyword); }
331 /* XER attributes are not as well delimited like TEXT/RAW,
332 * so no start condition for them (it would be difficult to determine
333 * when to return to INITIAL) */
335 /* First, the "starter" attributes */
336 abstract RETURN(XKWabstract);
337 anyAttributes RETURN(XKWanyAttributes);
338 anyElement RETURN(XKWanyElement);
339 attribute RETURN(XKWattribute);
340 attributeFormQualified RETURN(XKWattributeFormQualified);
341 block RETURN(XKWblock);
342 controlNamespace RETURN(XKWcontrolNamespace);
343 defaultForEmpty RETURN(XKWdefaultForEmpty);
344 element RETURN(XKWelement);
345 elementFormQualified RETURN(XKWelementFormQualified);
346 embedValues RETURN(XKWembedValues);
347 form RETURN(XKWform);
348 list RETURN(XKWlist);
349 name RETURN(XKWname);
350 namespace RETURN(XKWnamespace);
352 text RETURN(XKWtext);
353 untagged RETURN(XKWuntagged);
354 useNil RETURN(XKWuseNil);
355 useNumber RETURN(XKWuseNumber);
356 useOrder RETURN(XKWuseOrder);
357 useType RETURN(XKWuseType);
358 useUnion RETURN(XKWuseUnion);
359 whiteSpace RETURN(XKWwhiteSpace);
366 capitalized RETURN(XKWcapitalized);
367 uncapitalized RETURN(XKWuncapitalized);
368 lowercased RETURN(XKWlowercased);
369 uppercased RETURN(XKWuppercased);
371 qualified RETURN(XKWqualified);
372 unqualified RETURN(XKWunqualified); /* ASN uses ABSENT */
373 except RETURN(XKWexcept);
374 from RETURN(XKWfrom);
375 prefix RETURN(XKWprefix);
376 /* whitespace actions */
377 preserve RETURN(XKWpreserve);
378 collapse RETURN(XKWcollapse);
379 replace RETURN(XKWreplace);
382 string RETURN(XSDstring);
383 normalizedString RETURN(XSDnormalizedString);
384 token RETURN(XSDtoken);
385 Name RETURN(XSDName);
386 NMTOKEN RETURN(XSDNMTOKEN);
387 NCName RETURN(XSDNCName);
389 IDREF RETURN(XSDIDREF);
390 ENTITY RETURN(XSDENTITY);
391 hexBinary RETURN(XSDhexBinary);
392 base64Binary RETURN(XSDbase64Binary);
393 anyURI RETURN(XSDanyURI);
394 language RETURN(XSDlanguage);
395 integer RETURN(XSDinteger);
396 positiveInteger RETURN(XSDpositiveInteger);
397 nonPositiveInteger RETURN(XSDnonPositiveInteger);
398 negativeInteger RETURN(XSDnegativeInteger);
399 nonNegativeInteger RETURN(XSDnonNegativeInteger);
400 /* already taken by BER: long RETURN(XSDlong);*/
401 unsignedLong RETURN(XSDunsignedLong);
403 unsignedInt RETURN(XSDunsignedInt);
404 /* already taken by BER: short RETURN(XSDshort);*/
405 unsignedShort RETURN(XSDunsignedShort);
406 byte RETURN(XSDbyte);
407 unsignedByte RETURN(XSDunsignedByte);
408 decimal RETURN(XSDdecimal);
409 float RETURN(XSDfloat);
410 double RETURN(XSDdouble);
411 duration RETURN(XSDduration);
412 dateTime RETURN(XSDdateTime);
413 time RETURN(XSDtime);
414 date RETURN(XSDdate);
415 gYearMonth RETURN(XSDgYearMonth);
416 gYear RETURN(XSDgYear);
417 gMonthDay RETURN(XSDgMonthDay);
418 gDay RETURN(XSDgDay);
419 gMonth RETURN(XSDgMonth);
420 NMTOKENS RETURN(XSDNMTOKENS);
421 IDREFS RETURN(XSDIDREFS);
422 ENTITIES RETURN(XSDENTITIES);
423 QName RETURN(XSDQName);
424 boolean RETURN(XSDboolean);
426 anySimpleType RETURN(XSDanySimpleType);
427 anyType RETURN(XSDanyType);
429 } /* end XER keywords in INITIAL */
432 JSON { BEGIN(jsoncodec); RETURN(XKWjson); }
435 [: \t] RETURN(*yytext);
436 omit RETURN(XKWomit);
438 null RETURN(XKWnull);
439 name RETURN(XKWname);
440 value RETURN(XKWvalue);
441 default RETURN(XKWdefault);
442 extend RETURN(XKWextend);
443 metainfo RETURN(XKWmetainfo);
445 unbound RETURN(XKWunbound);
446 [(] { BEGIN(jsonvalue); RETURN(XJsonValueStart); }
447 {IDENTIFIER} { yylval.str = mcopystr(yytext); RETURN(XAliasToken); }
448 [^: \t] { rawAST_error("invalid JSON token"); }
452 [\\][\\][)] { /* \\) -> \) would not work with the other rules */
453 yylval.str = mcopystr("\\)");
454 RETURN(XJsonValueSegment);
456 [\\]. { /* \) -> ), all others are treated literally */
457 if (yytext[1] == ')') yylval.str = mcopystr(")");
458 else yylval.str = mcopystr(yytext);
459 RETURN(XJsonValueSegment);
461 [)] { BEGIN(jsoncodec); RETURN(XJsonValueEnd); }
462 [\"][\"] { yylval.str = mcopystr("\\\""); RETURN(XJsonValueSegment); }
463 [^\"\\)]+ { yylval.str = mcopystr(yytext); RETURN(XJsonValueSegment); }
467 length RETURN(XKWlength);
468 accept RETURN(XKWaccept);
469 long RETURN(XKWlong);
470 short RETURN(XKWshort);
471 indefinite RETURN(XKWindefinite);
472 definite RETURN(XKWdefinite);
477 yylloc.first_line = current_line;
478 yylloc.first_column = current_column;
480 bool backslash_flag = false;
481 yylval.str = memptystr();
482 for (size_t i = 1; i < yyleng - 1; ) {
483 if (!backslash_flag && (yytext[i] == '\'' || yytext[i] == '"') &&
484 yytext[i + 1] == yytext[i]) {
485 /* transform '' -> \' and "" -> \" */
486 yylval.str = mputc(yylval.str, '\\');
487 yylval.str = mputc(yylval.str, yytext[i]);
490 } else if (yytext[i] == '\r' && yytext[i + 1] == '\n') {
491 /* handle the CR-LF sequence as a single newline */
492 yylval.str = mputstr(yylval.str, "\r\n");
495 backslash_flag = false;
498 yylval.str = mputc(yylval.str, yytext[i]);
499 if (yytext[i] == '\r' || yytext[i] == '\n') {
502 } else current_column++;
503 if (backslash_flag) backslash_flag = false;
504 else if (yytext[i] == '\\') backslash_flag = true;
509 yylloc.last_line = current_line;
510 yylloc.last_column = current_column;
514 length RETURN(XLengthToken);
515 repeatable RETURN(XRepeatToken);
516 convert RETURN(XConvertToken);
517 lower_case RETURN(XLowerToken);
518 upper_case RETURN(XUpperToken);
519 just RETURN(XJustToken);
520 left RETURN(XLeftToken);
521 right RETURN(XRightToken);
522 center RETURN(XCenterToken);
523 leading0 RETURN(XLeadingToken);
524 true RETURN(XTrueToken);
525 false RETURN(XFalseToken);
526 case_sensitive RETURN(XSensitivToken);
527 case_insensitive RETURN(XInSensitivToken);
528 {RPAREN} {BEGIN(INITIAL);RETURN(*yytext);}
529 } /* end <textcodec> */
531 <INITIAL,textcodec,rawcodec>{
533 yylval.identifier = new Identifier(Identifier::ID_TTCN,
534 string(yyleng, yytext));
539 /* we must set the fake length 1 in order to report the single column
540 * number of the unexpected EOF in error messages */
548 void free_rawAST_field_list(rawAST_field_list *ptr)
551 for (int i = 0; i < ptr->nElements; i++) delete ptr->names[i];
558 link_rawAST_single_tag(rawAST_single_tag* dst, rawAST_single_tag* src){
559 rawAST_single_tag* retv;
560 if (src==NULL) return NULL;
561 if (dst == src) return dst;
562 // if (dst!=NULL) free_rawAST_single_tag(dst);
563 ALLOCATE_IF_NULL(retv, dst, rawAST_single_tag);
564 retv->fieldName=src->fieldName;
565 retv->nElements=src->nElements;
566 retv->keyList=src->keyList;
571 free_rawAST_single_tag(rawAST_single_tag* spec){
573 if(spec->fieldName) delete spec->fieldName;
574 for (i=0; i<spec->nElements; i++) {
575 free_rawAST_tag_field_value(&spec->keyList[i]);
577 if(spec->nElements) Free(spec->keyList);
578 // init_rawAST_single_tag(spec);
582 free_rawAST_tag_field_value(rawAST_tag_field_value* spec){
583 free_rawAST_field_list(spec->keyField);
585 delete spec->v_value;
586 // init_rawAST_tag_field_value(spec);
589 rawAST_tag_field_value*
590 link_rawAST_tag_field_value(rawAST_tag_field_value* dst, rawAST_tag_field_value* src){
591 rawAST_tag_field_value* retv;
592 if (src==NULL) return NULL;
593 if (dst == src) return dst;
594 ALLOCATE_IF_NULL(retv, dst, rawAST_tag_field_value);
595 retv->keyField = src->keyField;
596 retv->value = src->value;
597 retv->v_value = src->v_value;
602 link_rawAST_tag_list(rawAST_tag_list* dst, rawAST_tag_list* src){
603 rawAST_tag_list* retv;
604 if (src==NULL) return NULL;
605 if (dst == src) return dst;
606 ALLOCATE_IF_NULL(retv, dst, rawAST_tag_list);
607 retv->nElements=src->nElements;
612 rawAST_tag_field_value*
613 init_rawAST_tag_field_value(rawAST_tag_field_value* spec){
614 rawAST_tag_field_value* retv;
615 ALLOCATE_IF_NULL(retv, spec, rawAST_tag_field_value);
622 void free_rawAST_tag_list(rawAST_tag_list* spec){
624 if (spec==NULL) return;
625 for (i=0; i<spec->nElements; i++) {
626 free_rawAST_single_tag(&spec->tag[i]);
629 // init_rawAST_tag_list(spec);
633 int parse_rawAST(RawAST *par, TextAST *textpar, XerAttributes *xerpar,
634 BerAST* berpar, JsonAST* jsonpar, const Ttcn::AttributeSpec& attrib,
635 int l_multip, const Common::Module* mod, bool &raw_found, bool &text_found,
636 bool &xer_found, bool &ber_found, bool &json_found)
642 jsonstruct = jsonpar;
644 length_multiplier=l_multip;
645 infile = attrib.get_filename();
646 current_line = attrib.get_first_line();
647 /* skip the leading " of the attribute value */
648 current_column = attrib.get_first_column() + 1;
655 const string& s = attrib.get_spec();
658 fprintf(stderr, "*** raw=[%s]\n", s.c_str());
661 struct yy_buffer_state *flex_buffer = yy_scan_bytes(s.c_str(), s.size());
663 FATAL_ERROR("parse_rawAST(): flex buffer creation failed");
666 int retn=rawAST_parse();
668 if(!raw_found) raw_found=raw_f || (!text_f && !xer_f && !ber_f && !json_f);
669 // if none found, pretend some RAW attribute was found
670 if(!text_found) text_found=text_f;
671 if(!xer_found) xer_found=xer_f;
672 if(!ber_found) ber_found=ber_f;
673 if (!json_found) json_found = json_f;
678 void rawAST_error(const char *str)
680 Location loc(infile, yylloc);
682 // the most recently parsed token is known
683 loc.warning("in variant attribute, at or before token `%s': %s", yytext, str);
685 // the most recently parsed token is unknown
686 loc.warning("in variant attribute: %s", str);
693 indent-tabs-mode: nil