X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fcpp%2FDataBoardTest%2FDataBoard%2FDataBoardRepository.cpp;fp=bundles%2Forg.simantics.databoard%2Fcpp%2FDataBoardTest%2FDataBoard%2FDataBoardRepository.cpp;h=ce36d9312cf3f8344ff8971efb9de330175063e3;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/cpp/DataBoardTest/DataBoard/DataBoardRepository.cpp b/bundles/org.simantics.databoard/cpp/DataBoardTest/DataBoard/DataBoardRepository.cpp new file mode 100644 index 000000000..ce36d9312 --- /dev/null +++ b/bundles/org.simantics.databoard/cpp/DataBoardTest/DataBoard/DataBoardRepository.cpp @@ -0,0 +1,772 @@ +#include "DataBoardRepository.h" + +#include "RecordType.h" +#include "ArrayType.h" +#include "Constants.h" +#include "Component.h" +#include "IntegerType.h" +#include "DataType.h" +#include "StringType.h" +#include "DoubleType.h" +#include "LongType.h" +#include "ByteType.h" +#include "BooleanType.h" +#include "MapType.h" +#include "OptionalType.h" +#include "UnionType.h" +#include "VariantType.h" +#include "Range.h" +#include "Limit.h" + +#include +#include + +namespace Databoard { + namespace Type { + + std::string strTypeDefinition::writeOut() + { + std::string r; + + r += "type " + name + " = "; + + r += dataBoard->writeOut(); + + return r; + } + + DataBoardRepository::DataBoardRepository() + { + } + + DataBoardRepository::~DataBoardRepository() + { + for(int i = 0; i < (int)dataBoards.size(); ++i) + { + delete dataBoards.at(i).dataBoard; + } + + dataBoards.clear(); + } + + void DataBoardRepository::addDataBoard(char* name, DataType* dataBoard) + { + strTypeDefinition dataBoard_; + + dataBoard_.name = std::string(name); + dataBoard_.dataBoard = dataBoard; + + dataBoards.push_back(dataBoard_); + } + + int DataBoardRepository::count() + { + return (int)dataBoards.size(); + } + + strTypeDefinition DataBoardRepository::getDataBoard(int i) + { + return dataBoards.at(i); + } + + DataType* DataBoardRepository::getDataBoard(std::string name) + { + for(int i = 0; i < (int)dataBoards.size(); ++i) + { + if(dataBoards.at(i).name == name) + { + return dataBoards.at(i).dataBoard; + } + } + + return NULL; + } + + bool DataBoardRepository::open(std::string fileName) + { + pANTLR3_INPUT_STREAM input = antlr3AsciiFileStreamNew((pANTLR3_UINT8)fileName.c_str()); + + if ( input == NULL ) + { + ANTLR3_FPRINTF(stderr, "string: %s\n", fileName.c_str()); + + return false; + } + + pSimanticsDataLexer lexer = SimanticsDataLexerNew(input); + + if ( lexer == NULL ) + { + ANTLR3_FPRINTF(stderr, "Unable to create the lexer due to malloc() failure1\n"); + //exit(ANTLR3_ERR_NOMEM); + + return false; + } + + pANTLR3_COMMON_TOKEN_STREAM tstream = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT, TOKENSOURCE(lexer));//->pLexer->tokSource); + + if (tstream == NULL) + { + ANTLR3_FPRINTF(stderr, "Out of memory trying to allocate token stream\n"); + //exit(ANTLR3_ERR_NOMEM); + + return false; + } + + pSimanticsDataParser parser = SimanticsDataParserNew(tstream); // CParserNew is generated by ANTLR3 + + if (parser == NULL) + { + ANTLR3_FPRINTF(stderr, "Out of memory trying to allocate parser\n"); + //exit(ANTLR3_ERR_NOMEM); + + return false; + } + + SimanticsDataParser_typeDefinitions_return astType = parser->typeDefinitions(parser); + + bool retValue = true; + + if(lexer->pLexer->rec->state->errorCount == 0) + { + temporaryname t(this); + + t.parse(astType.tree); + + //print(astType.tree); + } + else + { + retValue = false; + } + + + parser->free(parser); + tstream->free(tstream); + lexer->free(lexer); + input->free(input); + + return retValue; + } + + void DataBoardRepository::print(pANTLR3_BASE_TREE tree, int indent) + { + for(int i=0;igetText(tree)->chars << " " << + tree->getType(tree) << std::endl; + + for(int i=0;i<(int)tree->getChildCount(tree);++i) + { + print((pANTLR3_BASE_TREE)tree->getChild(tree, i), indent + 1); + } + } + + const strTypeDefinition* DataBoardRepository::get(std::string name) + { + for(int i = 0; i < (int)dataBoards.size(); ++i) + { + const strTypeDefinition& dataBoard = dataBoards.at(i); // for debug purposes + + if(dataBoard.name == name) + { + return &dataBoard; + } + } + + return NULL; + } + + /////////////////////////////////////////////// + + temporaryname::temporaryname(DataBoardRepository* dataBoardRepository) + { + this->dataBoardRepository = dataBoardRepository; + } + + temporaryname::~temporaryname() + { + } + + DataType* temporaryname::parse(pANTLR3_BASE_TREE tree, DataType* parent, int indent) + //DataType* DataBoardRepository::parse(pANTLR3_BASE_TREE tree, DataType* parent, int indent) + //DataType* parse(pANTLR3_BASE_TREE tree, DataType* parent, int indent) + { + switch(tree->getType(tree)) { + case TYPE_DEFINITIONS: + { + for(int i=0;i<(int)tree->getChildCount(tree);++i) + { + parse((pANTLR3_BASE_TREE)tree->getChild(tree, i), parent, indent + 1); + } + } + break; + case TYPE_DEFINITION: + { + if(tree->getChildCount(tree) == 2) + { + pANTLR3_BASE_TREE id = (pANTLR3_BASE_TREE)tree->getChild(tree, 0); + pANTLR3_BASE_TREE type = (pANTLR3_BASE_TREE)tree->getChild(tree, 1); + + DataType* dataBoard = parse(type, parent, indent+1); + + if(dataBoard != NULL) + { + dataBoard->setNotBuiltIn(); + + if(dataBoardRepository != NULL) + { + dataBoardRepository->addDataBoard((char*)id->getText(id)->chars, dataBoard); + } + } + } + } + break; + case TUPLE_TYPE: + { + RecordType* record = new RecordType(); + + for(int i=0;i<(int)tree->getChildCount(tree);++i) + { + DataType* childType = parse((pANTLR3_BASE_TREE)tree->getChild(tree, i), record, indent + 1); + + if(childType != NULL) + { + std::stringstream ss; + + ss << i; + + std::string tupleId; + + ss >> tupleId; + + addToChildren(record, (char*)tupleId.c_str(), childType); + } + } + + return record; + } + + break; + + case RECORD_TYPE: + { + RecordType* record = new RecordType(); + + for(int i=0;i<(int)tree->getChildCount(tree);++i) + { + DataType* childType = parse((pANTLR3_BASE_TREE)tree->getChild(tree, i), record, indent + 1); + } + + return record; + } + + break; + case ARRAY_TYPE: + { + ArrayType* arrayType = new ArrayType(); + + int childCount = tree->getChildCount(tree); + + if(childCount >= 1) + { + DataType* dataType = parse((pANTLR3_BASE_TREE)tree->getChild(tree, 0), parent); + + arrayType->setComponentType(dataType); + + if(childCount >= 2) + { + Range* range = parseRange(tree, 1, childCount); + + arrayType->setLength(range); + } + } + + return arrayType; + } + + break; + + case UNION_TYPE: + { + UnionType* unionType = new UnionType(); + + int childCount = tree->getChildCount(tree); + + for(int i=0;i<(int)tree->getChildCount(tree);++i) + { + pANTLR3_BASE_TREE p = (pANTLR3_BASE_TREE)tree->getChild(tree, i); + + if(p->getChildCount(p) == 1) + { + pANTLR3_BASE_TREE t = (pANTLR3_BASE_TREE)p->getChild(p, 0); + + std::string dataTypeName((char*)t->getText(t)->chars); + + DataType* dataType = NULL; + + if(dataBoardRepository != NULL) + { + dataType = dataBoardRepository->getDataBoard(dataTypeName); + } + + unionType->addComponent(dataTypeName, dataType); + } + } + + return unionType; + } + break; + + case TYPE_COMPONENT: + { + pANTLR3_BASE_TREE parameter = NULL; + pANTLR3_BASE_TREE type = NULL; + + if(tree->getChildCount(tree) == 2) + { + parameter = (pANTLR3_BASE_TREE)tree->getChild(tree, 0); + type = (pANTLR3_BASE_TREE)tree->getChild(tree, 1); + } + + DataType* returnType = NULL; + + if(type != NULL) + { + if(type->getType(type) == TYPE_REFERENCE) + { + returnType = parse(type, parent, indent + 1); + } + else if(type->getType(type) == RECORD_TYPE) + { + returnType = parse(type, parent, indent + 1); + } + else if(type->getType(type) == TUPLE_TYPE) + { + returnType = parse(type, parent, indent + 1); + } + else if(type->getType(type) == ARRAY_TYPE) + { + returnType = parse(type, parent, indent + 1); + } + else + { + //std::cout << "!!" << std::endl; + //std::cout << parameter->getText(parameter)->chars << std::endl; + //std::cout << parameter->getType(parameter) << std::endl; + + //std::cout << type->getText(type)->chars << std::endl; + //std::cout << type->getType(type) << std::endl; + } + } + + if(parameter != NULL && returnType != NULL) + { + addToChildren(parent, (char*)parameter->getText(parameter)->chars, returnType); + } + + return returnType; + + break; + } + case TYPE_REFERENCE: + if(tree->getChildCount(tree) >= 1) + { + pANTLR3_BASE_TREE id = (pANTLR3_BASE_TREE)tree->getChild(tree, 0); + + const char* typeName = (char*)id->getText(id)->chars; + + bool errFound = false; + + DataType* returnType = NULL; + + if(id->getType(id) == ID) + { + if(strEquals(typeName, STR_BOOLEAN) == true) + { + returnType = new BooleanType(); + } + else if(strEquals(typeName, STR_INTEGER) == true) + { + returnType = new IntegerType(); + } + else if(strEquals(typeName, STR_DOUBLE) == true) + { + returnType = new DoubleType(); + } + else if(strEquals(typeName, STR_BYTE) == true) + { + returnType = new ByteType(); + } + else if(strEquals(typeName, STR_LONG) == true) + { + returnType = new LongType(); + } + else if(strEquals(typeName, STR_FLOAT) == true) + { + // double=>float contradiction on purpose + returnType = new DoubleType(); + } + else if(strEquals(typeName, STR_STRING) == true) + { + returnType = new StringType(); + } + else if(strEquals(typeName, STR_MAP) == true) + { + returnType = new MapType(); + } + else if(strEquals(typeName, STR_OPTIONAL) == true) + { + returnType = new OptionalType(); + } + else if(strEquals(typeName, STR_VARIANT) == true) + { + returnType = new VariantType(); + } + else + { + if(dataBoardRepository != NULL) + { + returnType = dataBoardRepository->getDataBoard(typeName); + } + } + } + + parseParameters(tree, returnType); + + //if(errFound == true) + //{ + // std::cout << "!-" << std::endl; + // std::cout << "unknown: " << typeName << std::endl << std::endl; + + // std::cout << parameter->getText(parameter)->chars << std::endl; + // std::cout << parameter->getType(parameter) << std::endl; + + // std::cout << "-!" << std::endl; + //} + + return returnType; + } + break; + case TYPE_ANNOTATION: + if(tree->getChildCount(tree) == 2) + { + pANTLR3_BASE_TREE id = (pANTLR3_BASE_TREE)tree->getChild(tree, 0); + pANTLR3_BASE_TREE parameterValue = (pANTLR3_BASE_TREE)tree->getChild(tree, 1); + + const char* idName = (char*)id->getText(id)->chars; + + if(strEquals(idName, STR_UNIT) == true) + { + std::string strValue((char*)parameterValue->getText(parameterValue)->chars); + + NumberType* numberType = dynamic_cast(parent); + + if(numberType != NULL) + { + numberType->setUnit(strValue); + } + } + //else if(strEquals(typeName, STR_RANGE) == true) + //{ + //} + else if(strEquals(idName, STR_PATTERN) == true) + { + std::string strValue((char*)parameterValue->getText(parameterValue)->chars); + + StringType* stringType = dynamic_cast(parent); + + if(stringType != NULL) + { + stringType->setPattern(strValue); + } + } + else if(strEquals(idName, STR_MIMETYPE) == true) + { + std::string strValue((char*)parameterValue->getText(parameterValue)->chars); + + StringType* stringType = dynamic_cast(parent); + + if(stringType != NULL) + { + stringType->setMimeType(strValue); + } + } + //else if(strEquals(idName, STR_LENGTH) == true) // range + //{ + // std::string strValue((char*)parameterValue->getText(parameterValue)->chars); + + // StringType* stringType = dynamic_cast(parent); + + // if(stringType != NULL) + // { + // stringType->setLength(strValue); + // } + //} + } + + break; + case ID: + parent = parent; + break; + + default: + break; + } + + return NULL; + } + + Range* temporaryname::parseRange(pANTLR3_BASE_TREE tree, int minPos, int maxPos) + //Range* DataBoardRepository::parseRange(pANTLR3_BASE_TREE tree, int minPos, int maxPos) + //Range* parseRange(pANTLR3_BASE_TREE tree, int minPos, int maxPos) + { + Range* range = new Range(); + + bool rangeLower = true; + + bool inclusiveLower = true; + bool inclusiveUpper = true; + + for(int i = minPos;i < maxPos; ++i) + { + pANTLR3_BASE_TREE rangechild = (pANTLR3_BASE_TREE)tree->getChild(tree, i); + + std::string rangeString((char*)rangechild->getText(rangechild)->chars); + + if(i == minPos && rangeString == STR_BRACEOPEN) + { + inclusiveLower = false; + } + else if(i == minPos && rangeString == STR_BRACKETOPEN) + { + inclusiveLower = true; + } + else if(i == (maxPos-1) && rangeString == STR_BRACECLOSE) + { + inclusiveUpper = false; + } + else if(i == (maxPos-1) && rangeString == STR_BRACKETCLOSE) + { + inclusiveUpper = true; + } + else if(rangeString == STR_RANGESPLIT) + { + rangeLower = false; + } + else + { + if(rangeLower == true) + { + range->setLower(rangeString); + } + else + { + range->setUpper(rangeString); + } + } + } + + if(inclusiveLower == true) + { + range->getLower()->setInclusive(); + } + if(inclusiveUpper == true) + { + range->getUpper()->setInclusive(); + } + + // split not received => upper = lower + if(rangeLower == true) + { + range->setUpper(range->getLower()->getValue()); + } + + return range; + } + + bool temporaryname::addToChildren(DataType* parent, char* name, DataType* children) + //bool DataBoardRepository::addToChildren(DataType* parent, char* name, DataType* children) + //bool addToChildren(DataType* parent, char* name, DataType* children) + { + //TypeRoot* typeRoot = dynamic_cast(parent); + + //if(typeRoot != NULL) + //{ + // typeRoot->addDataBoard(name, children); + + // return true; + //} + + RecordType* recordType = dynamic_cast(parent); + + if(recordType != NULL) + { + recordType->addComponent(name, children); + + return true; + } + + return false; + } + + void temporaryname::parseParameters(pANTLR3_BASE_TREE tree, DataType* type) + //void DataBoardRepository::parseParameters(pANTLR3_BASE_TREE tree, DataType* type) + //void parseParameters(pANTLR3_BASE_TREE tree, DataType* type) + { + if((int)tree->getChildCount(tree) >= 2) + { + if(dynamic_cast(type) != NULL) + { + NumberType* numberType = (NumberType*)type; + + int annotationCount = (int)tree->getChildCount(tree); + + if(annotationCount >= 1) + { + for(int i = 1; i < annotationCount; ++i) + { + pANTLR3_BASE_TREE annotation = (pANTLR3_BASE_TREE)tree->getChild(tree, i); + + pANTLR3_BASE_TREE unit = (pANTLR3_BASE_TREE)annotation->getChild(annotation, 0); + + const char* unitName = (char*)unit->getText(unit)->chars; + + if(strEquals(unitName, STR_UNIT) == true) + { + if((int)annotation->getChildCount(annotation) == 2) + { + pANTLR3_BASE_TREE value = (pANTLR3_BASE_TREE)annotation->getChild(annotation, 1); + + std::string strValue((char*)value->getText(value)->chars); + + strValue = removeQuotes(strValue); + + NumberType* numberType = dynamic_cast(type); + + if(numberType != NULL) + { + numberType->setUnit(strValue); + } + } + } + else if(strEquals(unitName, STR_RANGE) == true) + { + int c = (int)annotation->getChildCount(annotation); + // possibilities: + // 5: range;[;n;..;] + // 5: range;[;..;m;] + // 6: range;[;n;..;m;] + if(c >= 5) + { + //pANTLR3_BASE_TREE openBracket = (pANTLR3_BASE_TREE)annotation->getChild(annotation, 0); + //pANTLR3_BASE_TREE closeBracket = (pANTLR3_BASE_TREE)annotation->getChild(annotation, c-1); + + //if((char)(openBracket->getText(openBracket)->chars) != '[' && + // (char)(closeBracket->getText(closeBracket)->chars) != ']') + //{ + Range* range = parseRange(annotation, 1, c); + + numberType->setRange(range); + //} + } + } + } + } + } + else if(dynamic_cast(type) != NULL) + { + StringType* stringType = (StringType*)type; + + for(int i = 1; i < (int)tree->getChildCount(tree); ++i) + { + pANTLR3_BASE_TREE annotation = (pANTLR3_BASE_TREE)tree->getChild(tree, i); + + pANTLR3_BASE_TREE unit = (pANTLR3_BASE_TREE)annotation->getChild(annotation, 0); + pANTLR3_BASE_TREE value = (pANTLR3_BASE_TREE)annotation->getChild(annotation, 1); + + const char* unitName = (char*)unit->getText(unit)->chars; + + if(strEquals(unitName, STR_PATTERN) == true) + { + std::string strValue((char*)value->getText(value)->chars); + + strValue = removeQuotes(strValue); + + StringType* stringType = dynamic_cast(type); + + if(stringType != NULL) + { + stringType->setPattern(strValue); + } + } + else if(strEquals(unitName, STR_MIMETYPE) == true) + { + std::string strValue((char*)value->getText(value)->chars); + + strValue = removeQuotes(strValue); + + StringType* stringType = dynamic_cast(type); + + if(stringType != NULL) + { + stringType->setMimeType(strValue); + } + } + //else if(strEquals(idName, STR_LENGTH) == true) // range + //{ + // std::string strValue((char*)parameterValue->getText(parameterValue)->chars); + + // StringType* stringType = dynamic_cast(parent); + + // if(stringType != NULL) + // { + // stringType->setLength(strValue); + // } + //} + } + } + else if(dynamic_cast(type) != NULL) + { + MapType* mapType = (MapType*)type; + + if((int)tree->getChildCount(tree) == 3) + { + pANTLR3_BASE_TREE key = (pANTLR3_BASE_TREE)tree->getChild(tree, 1); + pANTLR3_BASE_TREE value = (pANTLR3_BASE_TREE)tree->getChild(tree, 2); + + DataType* typeKey = parse(key, mapType); + DataType* typeValue = parse(value, mapType); + + mapType->setKeyType(typeKey); + mapType->setValueType(typeValue); + } + } + else if(dynamic_cast(type) != NULL) + { + OptionalType* optionalType = (OptionalType*)type; + + if((int)tree->getChildCount(tree) == 2) + { + pANTLR3_BASE_TREE typeRef = (pANTLR3_BASE_TREE)tree->getChild(tree, 1); + + DataType* dataType = parse(typeRef, optionalType); + + if(dataType != NULL) + { + optionalType->setComponentType(dataType); + } + } + } + } + + //for(int i = 1; i < (int)tree->getChildCount(tree); ++i) + //{ + // pANTLR3_BASE_TREE parameter = (pANTLR3_BASE_TREE)tree->getChild(tree, i); + + // parse(parameter, returnType, indent + 1); + //} + } + } +}