1 #include "DataBoardRepository.h"
3 #include "RecordType.h"
7 #include "IntegerType.h"
9 #include "StringType.h"
10 #include "DoubleType.h"
13 #include "BooleanType.h"
15 #include "OptionalType.h"
16 #include "UnionType.h"
17 #include "VariantType.h"
27 std::string strTypeDefinition::writeOut()
31 r += "type " + name + " = ";
33 r += dataBoard->writeOut();
38 DataBoardRepository::DataBoardRepository()
42 DataBoardRepository::~DataBoardRepository()
44 for(int i = 0; i < (int)dataBoards.size(); ++i)
46 delete dataBoards.at(i).dataBoard;
52 void DataBoardRepository::addDataBoard(char* name, DataType* dataBoard)
54 strTypeDefinition dataBoard_;
56 dataBoard_.name = std::string(name);
57 dataBoard_.dataBoard = dataBoard;
59 dataBoards.push_back(dataBoard_);
62 int DataBoardRepository::count()
64 return (int)dataBoards.size();
67 strTypeDefinition DataBoardRepository::getDataBoard(int i)
69 return dataBoards.at(i);
72 DataType* DataBoardRepository::getDataBoard(std::string name)
74 for(int i = 0; i < (int)dataBoards.size(); ++i)
76 if(dataBoards.at(i).name == name)
78 return dataBoards.at(i).dataBoard;
85 bool DataBoardRepository::open(std::string fileName)
87 pANTLR3_INPUT_STREAM input = antlr3AsciiFileStreamNew((pANTLR3_UINT8)fileName.c_str());
91 ANTLR3_FPRINTF(stderr, "string: %s\n", fileName.c_str());
96 pSimanticsDataLexer lexer = SimanticsDataLexerNew(input);
100 ANTLR3_FPRINTF(stderr, "Unable to create the lexer due to malloc() failure1\n");
101 //exit(ANTLR3_ERR_NOMEM);
106 pANTLR3_COMMON_TOKEN_STREAM tstream = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT, TOKENSOURCE(lexer));//->pLexer->tokSource);
110 ANTLR3_FPRINTF(stderr, "Out of memory trying to allocate token stream\n");
111 //exit(ANTLR3_ERR_NOMEM);
116 pSimanticsDataParser parser = SimanticsDataParserNew(tstream); // CParserNew is generated by ANTLR3
120 ANTLR3_FPRINTF(stderr, "Out of memory trying to allocate parser\n");
121 //exit(ANTLR3_ERR_NOMEM);
126 SimanticsDataParser_typeDefinitions_return astType = parser->typeDefinitions(parser);
128 bool retValue = true;
130 if(lexer->pLexer->rec->state->errorCount == 0)
132 temporaryname t(this);
134 t.parse(astType.tree);
136 //print(astType.tree);
144 parser->free(parser);
145 tstream->free(tstream);
152 void DataBoardRepository::print(pANTLR3_BASE_TREE tree, int indent)
154 for(int i=0;i<indent;++i)
157 std::cout << tree->getText(tree)->chars << " " <<
158 tree->getType(tree) << std::endl;
160 for(int i=0;i<(int)tree->getChildCount(tree);++i)
162 print((pANTLR3_BASE_TREE)tree->getChild(tree, i), indent + 1);
166 const strTypeDefinition* DataBoardRepository::get(std::string name)
168 for(int i = 0; i < (int)dataBoards.size(); ++i)
170 const strTypeDefinition& dataBoard = dataBoards.at(i); // for debug purposes
172 if(dataBoard.name == name)
181 ///////////////////////////////////////////////
183 temporaryname::temporaryname(DataBoardRepository* dataBoardRepository)
185 this->dataBoardRepository = dataBoardRepository;
188 temporaryname::~temporaryname()
192 DataType* temporaryname::parse(pANTLR3_BASE_TREE tree, DataType* parent, int indent)
193 //DataType* DataBoardRepository::parse(pANTLR3_BASE_TREE tree, DataType* parent, int indent)
194 //DataType* parse(pANTLR3_BASE_TREE tree, DataType* parent, int indent)
196 switch(tree->getType(tree)) {
197 case TYPE_DEFINITIONS:
199 for(int i=0;i<(int)tree->getChildCount(tree);++i)
201 parse((pANTLR3_BASE_TREE)tree->getChild(tree, i), parent, indent + 1);
205 case TYPE_DEFINITION:
207 if(tree->getChildCount(tree) == 2)
209 pANTLR3_BASE_TREE id = (pANTLR3_BASE_TREE)tree->getChild(tree, 0);
210 pANTLR3_BASE_TREE type = (pANTLR3_BASE_TREE)tree->getChild(tree, 1);
212 DataType* dataBoard = parse(type, parent, indent+1);
214 if(dataBoard != NULL)
216 dataBoard->setNotBuiltIn();
218 if(dataBoardRepository != NULL)
220 dataBoardRepository->addDataBoard((char*)id->getText(id)->chars, dataBoard);
228 RecordType* record = new RecordType();
230 for(int i=0;i<(int)tree->getChildCount(tree);++i)
232 DataType* childType = parse((pANTLR3_BASE_TREE)tree->getChild(tree, i), record, indent + 1);
234 if(childType != NULL)
236 std::stringstream ss;
244 addToChildren(record, (char*)tupleId.c_str(), childType);
255 RecordType* record = new RecordType();
257 for(int i=0;i<(int)tree->getChildCount(tree);++i)
259 DataType* childType = parse((pANTLR3_BASE_TREE)tree->getChild(tree, i), record, indent + 1);
268 ArrayType* arrayType = new ArrayType();
270 int childCount = tree->getChildCount(tree);
274 DataType* dataType = parse((pANTLR3_BASE_TREE)tree->getChild(tree, 0), parent);
276 arrayType->setComponentType(dataType);
280 Range* range = parseRange(tree, 1, childCount);
282 arrayType->setLength(range);
293 UnionType* unionType = new UnionType();
295 int childCount = tree->getChildCount(tree);
297 for(int i=0;i<(int)tree->getChildCount(tree);++i)
299 pANTLR3_BASE_TREE p = (pANTLR3_BASE_TREE)tree->getChild(tree, i);
301 if(p->getChildCount(p) == 1)
303 pANTLR3_BASE_TREE t = (pANTLR3_BASE_TREE)p->getChild(p, 0);
305 std::string dataTypeName((char*)t->getText(t)->chars);
307 DataType* dataType = NULL;
309 if(dataBoardRepository != NULL)
311 dataType = dataBoardRepository->getDataBoard(dataTypeName);
314 unionType->addComponent(dataTypeName, dataType);
324 pANTLR3_BASE_TREE parameter = NULL;
325 pANTLR3_BASE_TREE type = NULL;
327 if(tree->getChildCount(tree) == 2)
329 parameter = (pANTLR3_BASE_TREE)tree->getChild(tree, 0);
330 type = (pANTLR3_BASE_TREE)tree->getChild(tree, 1);
333 DataType* returnType = NULL;
337 if(type->getType(type) == TYPE_REFERENCE)
339 returnType = parse(type, parent, indent + 1);
341 else if(type->getType(type) == RECORD_TYPE)
343 returnType = parse(type, parent, indent + 1);
345 else if(type->getType(type) == TUPLE_TYPE)
347 returnType = parse(type, parent, indent + 1);
349 else if(type->getType(type) == ARRAY_TYPE)
351 returnType = parse(type, parent, indent + 1);
355 //std::cout << "!!" << std::endl;
356 //std::cout << parameter->getText(parameter)->chars << std::endl;
357 //std::cout << parameter->getType(parameter) << std::endl;
359 //std::cout << type->getText(type)->chars << std::endl;
360 //std::cout << type->getType(type) << std::endl;
364 if(parameter != NULL && returnType != NULL)
366 addToChildren(parent, (char*)parameter->getText(parameter)->chars, returnType);
374 if(tree->getChildCount(tree) >= 1)
376 pANTLR3_BASE_TREE id = (pANTLR3_BASE_TREE)tree->getChild(tree, 0);
378 const char* typeName = (char*)id->getText(id)->chars;
380 bool errFound = false;
382 DataType* returnType = NULL;
384 if(id->getType(id) == ID)
386 if(strEquals(typeName, STR_BOOLEAN) == true)
388 returnType = new BooleanType();
390 else if(strEquals(typeName, STR_INTEGER) == true)
392 returnType = new IntegerType();
394 else if(strEquals(typeName, STR_DOUBLE) == true)
396 returnType = new DoubleType();
398 else if(strEquals(typeName, STR_BYTE) == true)
400 returnType = new ByteType();
402 else if(strEquals(typeName, STR_LONG) == true)
404 returnType = new LongType();
406 else if(strEquals(typeName, STR_FLOAT) == true)
408 // double=>float contradiction on purpose
409 returnType = new DoubleType();
411 else if(strEquals(typeName, STR_STRING) == true)
413 returnType = new StringType();
415 else if(strEquals(typeName, STR_MAP) == true)
417 returnType = new MapType();
419 else if(strEquals(typeName, STR_OPTIONAL) == true)
421 returnType = new OptionalType();
423 else if(strEquals(typeName, STR_VARIANT) == true)
425 returnType = new VariantType();
429 if(dataBoardRepository != NULL)
431 returnType = dataBoardRepository->getDataBoard(typeName);
436 parseParameters(tree, returnType);
438 //if(errFound == true)
440 // std::cout << "!-" << std::endl;
441 // std::cout << "unknown: " << typeName << std::endl << std::endl;
443 // std::cout << parameter->getText(parameter)->chars << std::endl;
444 // std::cout << parameter->getType(parameter) << std::endl;
446 // std::cout << "-!" << std::endl;
452 case TYPE_ANNOTATION:
453 if(tree->getChildCount(tree) == 2)
455 pANTLR3_BASE_TREE id = (pANTLR3_BASE_TREE)tree->getChild(tree, 0);
456 pANTLR3_BASE_TREE parameterValue = (pANTLR3_BASE_TREE)tree->getChild(tree, 1);
458 const char* idName = (char*)id->getText(id)->chars;
460 if(strEquals(idName, STR_UNIT) == true)
462 std::string strValue((char*)parameterValue->getText(parameterValue)->chars);
464 NumberType* numberType = dynamic_cast<NumberType*>(parent);
466 if(numberType != NULL)
468 numberType->setUnit(strValue);
471 //else if(strEquals(typeName, STR_RANGE) == true)
474 else if(strEquals(idName, STR_PATTERN) == true)
476 std::string strValue((char*)parameterValue->getText(parameterValue)->chars);
478 StringType* stringType = dynamic_cast<StringType*>(parent);
480 if(stringType != NULL)
482 stringType->setPattern(strValue);
485 else if(strEquals(idName, STR_MIMETYPE) == true)
487 std::string strValue((char*)parameterValue->getText(parameterValue)->chars);
489 StringType* stringType = dynamic_cast<StringType*>(parent);
491 if(stringType != NULL)
493 stringType->setMimeType(strValue);
496 //else if(strEquals(idName, STR_LENGTH) == true) // range
498 // std::string strValue((char*)parameterValue->getText(parameterValue)->chars);
500 // StringType* stringType = dynamic_cast<StringType*>(parent);
502 // if(stringType != NULL)
504 // stringType->setLength(strValue);
521 Range* temporaryname::parseRange(pANTLR3_BASE_TREE tree, int minPos, int maxPos)
522 //Range* DataBoardRepository::parseRange(pANTLR3_BASE_TREE tree, int minPos, int maxPos)
523 //Range* parseRange(pANTLR3_BASE_TREE tree, int minPos, int maxPos)
525 Range* range = new Range();
527 bool rangeLower = true;
529 bool inclusiveLower = true;
530 bool inclusiveUpper = true;
532 for(int i = minPos;i < maxPos; ++i)
534 pANTLR3_BASE_TREE rangechild = (pANTLR3_BASE_TREE)tree->getChild(tree, i);
536 std::string rangeString((char*)rangechild->getText(rangechild)->chars);
538 if(i == minPos && rangeString == STR_BRACEOPEN)
540 inclusiveLower = false;
542 else if(i == minPos && rangeString == STR_BRACKETOPEN)
544 inclusiveLower = true;
546 else if(i == (maxPos-1) && rangeString == STR_BRACECLOSE)
548 inclusiveUpper = false;
550 else if(i == (maxPos-1) && rangeString == STR_BRACKETCLOSE)
552 inclusiveUpper = true;
554 else if(rangeString == STR_RANGESPLIT)
560 if(rangeLower == true)
562 range->setLower(rangeString);
566 range->setUpper(rangeString);
571 if(inclusiveLower == true)
573 range->getLower()->setInclusive();
575 if(inclusiveUpper == true)
577 range->getUpper()->setInclusive();
580 // split not received => upper = lower
581 if(rangeLower == true)
583 range->setUpper(range->getLower()->getValue());
589 bool temporaryname::addToChildren(DataType* parent, char* name, DataType* children)
590 //bool DataBoardRepository::addToChildren(DataType* parent, char* name, DataType* children)
591 //bool addToChildren(DataType* parent, char* name, DataType* children)
593 //TypeRoot* typeRoot = dynamic_cast<TypeRoot*>(parent);
595 //if(typeRoot != NULL)
597 // typeRoot->addDataBoard(name, children);
602 RecordType* recordType = dynamic_cast<RecordType*>(parent);
604 if(recordType != NULL)
606 recordType->addComponent(name, children);
614 void temporaryname::parseParameters(pANTLR3_BASE_TREE tree, DataType* type)
615 //void DataBoardRepository::parseParameters(pANTLR3_BASE_TREE tree, DataType* type)
616 //void parseParameters(pANTLR3_BASE_TREE tree, DataType* type)
618 if((int)tree->getChildCount(tree) >= 2)
620 if(dynamic_cast<NumberType*>(type) != NULL)
622 NumberType* numberType = (NumberType*)type;
624 int annotationCount = (int)tree->getChildCount(tree);
626 if(annotationCount >= 1)
628 for(int i = 1; i < annotationCount; ++i)
630 pANTLR3_BASE_TREE annotation = (pANTLR3_BASE_TREE)tree->getChild(tree, i);
632 pANTLR3_BASE_TREE unit = (pANTLR3_BASE_TREE)annotation->getChild(annotation, 0);
634 const char* unitName = (char*)unit->getText(unit)->chars;
636 if(strEquals(unitName, STR_UNIT) == true)
638 if((int)annotation->getChildCount(annotation) == 2)
640 pANTLR3_BASE_TREE value = (pANTLR3_BASE_TREE)annotation->getChild(annotation, 1);
642 std::string strValue((char*)value->getText(value)->chars);
644 strValue = removeQuotes(strValue);
646 NumberType* numberType = dynamic_cast<NumberType*>(type);
648 if(numberType != NULL)
650 numberType->setUnit(strValue);
654 else if(strEquals(unitName, STR_RANGE) == true)
656 int c = (int)annotation->getChildCount(annotation);
660 // 6: range;[;n;..;m;]
663 //pANTLR3_BASE_TREE openBracket = (pANTLR3_BASE_TREE)annotation->getChild(annotation, 0);
664 //pANTLR3_BASE_TREE closeBracket = (pANTLR3_BASE_TREE)annotation->getChild(annotation, c-1);
666 //if((char)(openBracket->getText(openBracket)->chars) != '[' &&
667 // (char)(closeBracket->getText(closeBracket)->chars) != ']')
669 Range* range = parseRange(annotation, 1, c);
671 numberType->setRange(range);
678 else if(dynamic_cast<StringType*>(type) != NULL)
680 StringType* stringType = (StringType*)type;
682 for(int i = 1; i < (int)tree->getChildCount(tree); ++i)
684 pANTLR3_BASE_TREE annotation = (pANTLR3_BASE_TREE)tree->getChild(tree, i);
686 pANTLR3_BASE_TREE unit = (pANTLR3_BASE_TREE)annotation->getChild(annotation, 0);
687 pANTLR3_BASE_TREE value = (pANTLR3_BASE_TREE)annotation->getChild(annotation, 1);
689 const char* unitName = (char*)unit->getText(unit)->chars;
691 if(strEquals(unitName, STR_PATTERN) == true)
693 std::string strValue((char*)value->getText(value)->chars);
695 strValue = removeQuotes(strValue);
697 StringType* stringType = dynamic_cast<StringType*>(type);
699 if(stringType != NULL)
701 stringType->setPattern(strValue);
704 else if(strEquals(unitName, STR_MIMETYPE) == true)
706 std::string strValue((char*)value->getText(value)->chars);
708 strValue = removeQuotes(strValue);
710 StringType* stringType = dynamic_cast<StringType*>(type);
712 if(stringType != NULL)
714 stringType->setMimeType(strValue);
717 //else if(strEquals(idName, STR_LENGTH) == true) // range
719 // std::string strValue((char*)parameterValue->getText(parameterValue)->chars);
721 // StringType* stringType = dynamic_cast<StringType*>(parent);
723 // if(stringType != NULL)
725 // stringType->setLength(strValue);
730 else if(dynamic_cast<MapType*>(type) != NULL)
732 MapType* mapType = (MapType*)type;
734 if((int)tree->getChildCount(tree) == 3)
736 pANTLR3_BASE_TREE key = (pANTLR3_BASE_TREE)tree->getChild(tree, 1);
737 pANTLR3_BASE_TREE value = (pANTLR3_BASE_TREE)tree->getChild(tree, 2);
739 DataType* typeKey = parse(key, mapType);
740 DataType* typeValue = parse(value, mapType);
742 mapType->setKeyType(typeKey);
743 mapType->setValueType(typeValue);
746 else if(dynamic_cast<OptionalType*>(type) != NULL)
748 OptionalType* optionalType = (OptionalType*)type;
750 if((int)tree->getChildCount(tree) == 2)
752 pANTLR3_BASE_TREE typeRef = (pANTLR3_BASE_TREE)tree->getChild(tree, 1);
754 DataType* dataType = parse(typeRef, optionalType);
758 optionalType->setComponentType(dataType);
764 //for(int i = 1; i < (int)tree->getChildCount(tree); ++i)
766 // pANTLR3_BASE_TREE parameter = (pANTLR3_BASE_TREE)tree->getChild(tree, i);
768 // parse(parameter, returnType, indent + 1);