#include #include "DataValueRepository.h" #include "DataBoardRepository.h" #include "Value.h" #include "RecordValue.h" #include "StringValue.h" #include "IntegerValue.h" #include "DoubleValue.h" #include "BooleanValue.h" #include "FloatValue.h" #include "LongValue.h" #include "MapValue.h" #include "ArrayValue.h" #include "OptionalValue.h" #include "UnionValue.h" #include "VariantValue.h" #include "RecordType.h" #include "Component.h" #include "BooleanType.h" #include "StringType.h" #include "IntegerType.h" #include "DoubleType.h" #include "FloatType.h" #include "LongType.h" #include "MapType.h" #include "ArrayType.h" #include "OptionalType.h" #include "UnionType.h" #include "VariantType.h" #include "Constants.h" #include #include #include namespace Databoard { namespace Value { std::string strDataValueDefinition::writeOut() const { std::string r; r += name + " : " + type + " = "; if(value != NULL) { r += value->writeOut(); } return r; } DataValueRepository::DataValueRepository() { } DataValueRepository::~DataValueRepository() { for(int i=0;i<(int)values.size();++i) { delete values.at(i).value; } values.size(); } void DataValueRepository::addDataValue(std::string name, std::string type, Value* dataValue) { strDataValueDefinition value_; value_.name = name; value_.type = type; value_.value = dataValue; values.push_back(value_); } const Databoard::Type::strTypeDefinition* DataValueRepository::parseTypeReference(pANTLR3_BASE_TREE tree) { switch(tree->getType(tree)) { case TYPE_REFERENCE: if(tree->getChildCount(tree) == 1) { pANTLR3_BASE_TREE referenceName = (pANTLR3_BASE_TREE)tree->getChild(tree, 0); std::string id((char*)referenceName->getText(referenceName)->chars); const Databoard::Type::strTypeDefinition* dataBoard = dataBoardRepository->get(id); return dataBoard; } break; default: break; } return NULL; } Value* DataValueRepository::parseValues(pANTLR3_BASE_TREE tree, Databoard::Type::DataType* dataBoard, int indent) { if(dynamic_cast(dataBoard) != NULL) { Databoard::Type::OptionalType* optionalType = (Databoard::Type::OptionalType*)dataBoard; Value* value = parseValues(tree, optionalType->getComponentType(), indent + 1); OptionalValue* optionalValue = new OptionalValue(optionalType); optionalValue->setValue(value); return optionalValue; } else if(dynamic_cast(dataBoard) != NULL) { Databoard::Type::StringType* stringType = (Databoard::Type::StringType*)dataBoard; StringValue* stringValue = new StringValue(dataBoard); std::string value((char*)tree->getText(tree)->chars); value = removeQuotes(value); stringValue->setValue(value); return stringValue; } else if(dynamic_cast(dataBoard) != NULL) { Databoard::Type::IntegerType* integerType = (Databoard::Type::IntegerType*)dataBoard; IntegerValue* integerValue = new IntegerValue(dataBoard); std::istringstream iss((char*)tree->getText(tree)->chars); int value; iss >> value; integerValue->setValue(value); return integerValue; } else if(dynamic_cast(dataBoard) != NULL) { Databoard::Type::LongType* longType = (Databoard::Type::LongType*)dataBoard; LongValue* longValue = new LongValue(dataBoard); std::istringstream iss((char*)tree->getText(tree)->chars); long value; iss >> value; longValue->setValue(value); return longValue; } else if(dynamic_cast(dataBoard) != NULL) { Databoard::Type::FloatType* floatType = (Databoard::Type::FloatType*)dataBoard; FloatValue* floatValue = new FloatValue(dataBoard); std::istringstream iss((char*)tree->getText(tree)->chars); float value; iss >> value; floatValue->setValue(value); return floatValue; } else if(dynamic_cast(dataBoard) != NULL) { Databoard::Type::DoubleType* doubleType = (Databoard::Type::DoubleType*)dataBoard; DoubleValue* doubleValue = new DoubleValue(dataBoard); std::istringstream iss((char*)tree->getText(tree)->chars); double value; iss >> value; doubleValue->setValue(value); return doubleValue; } else if(dynamic_cast(dataBoard) != NULL) { if(tree->getType(tree) == TUPLE) { Databoard::Type::RecordType* recordType = (Databoard::Type::RecordType*)dataBoard; RecordValue* recordValue = new RecordValue(recordType); int i2 = (int)tree->getChildCount(tree); int i3 = recordType->count(); if((int)tree->getChildCount(tree) == recordType->count()) { for(int i=0;i<(int)tree->getChildCount(tree);++i) { pANTLR3_BASE_TREE value = (pANTLR3_BASE_TREE)tree->getChild(tree, i); std::string strId((char*)value->getText(value)->chars); Databoard::Type::Component* component = recordType->getComponent(i); if(component != NULL) { Databoard::Type::DataType* dataBoardType = component->getDataBoard(); Value* childValue = parseValues(value, dataBoardType, indent + 1); recordValue->setField(i, childValue); } } return recordValue; } else { std::cout << "mismatch in tuple count: " << (int)tree->getChildCount(tree) << " != " << recordType->count() << std::endl; return NULL; } } else if(tree->getType(tree) == RECORD) { Databoard::Type::RecordType* recordType = (Databoard::Type::RecordType*)dataBoard; RecordValue* recordValue = new RecordValue(recordType); for(int i=0;i<(int)tree->getChildCount(tree);++i) { pANTLR3_BASE_TREE assignment = (pANTLR3_BASE_TREE)tree->getChild(tree, i); if(assignment->getType(assignment) == ASSIGNMENT) { if(assignment->getChildCount(assignment) == 2) { pANTLR3_BASE_TREE id = (pANTLR3_BASE_TREE)assignment->getChild(assignment, 0); pANTLR3_BASE_TREE value = (pANTLR3_BASE_TREE)assignment->getChild(assignment, 1); std::string strId((char*)id->getText(id)->chars); Databoard::Type::Component* component = recordType->getComponent(strId); if(component != NULL) { Databoard::Type::DataType* dataBoardType = component->getDataBoard(); if(dataBoardType != NULL) { Value* childValue = parseValues(value, dataBoardType, indent + 1); int index = recordType->getComponentIndex(strId); recordValue->setField(index, childValue); } } } } } return recordValue; } } else if(dynamic_cast(dataBoard) != NULL) { Databoard::Type::MapType* mapType = (Databoard::Type::MapType*)dataBoard; MapValue* mapValue = new MapValue(mapType); int childCount = tree->getChildCount(tree); if(childCount > 0) { for(int i = 0; i < childCount; ++i) { pANTLR3_BASE_TREE assignment = (pANTLR3_BASE_TREE)tree->getChild(tree, i); if(assignment->getType(assignment) == ASSIGNMENT) { if(assignment->getChildCount(assignment) == 2) { pANTLR3_BASE_TREE key = (pANTLR3_BASE_TREE)assignment->getChild(assignment, 0); pANTLR3_BASE_TREE value = (pANTLR3_BASE_TREE)assignment->getChild(assignment, 1); std::string strKey((char*)key->getText(key)->chars); std::string strValue((char*)value->getText(value)->chars); strKey = removeQuotes(strKey); strValue = removeQuotes(strValue); Value* keyValue = parseValues(key, mapType->getKeyType(), indent + 1); Value* valueValue = parseValues(value, mapType->getValueType(), indent + 1); if(keyValue != NULL && valueValue != NULL) { mapValue->put(keyValue, valueValue); } } } } } return mapValue; } else if(dynamic_cast(dataBoard) != NULL) { Databoard::Type::ArrayType* arrayType = (Databoard::Type::ArrayType*)dataBoard; ArrayValue* arrayValue = new ArrayValue(arrayType); int childCount = tree->getChildCount(tree); if(childCount > 0) { for(int i = 0; i < childCount; ++i) { pANTLR3_BASE_TREE assignment = (pANTLR3_BASE_TREE)tree->getChild(tree, i); Databoard::Type::DataType* dataType = arrayType->getComponentType(); Value* value = parseValues(assignment, dataType, indent + 1); arrayValue->add(value); //Value* keyValue = parseValues(key, dataType, indent + 1); // if(assignment->getType(assignment) == ASSIGNMENT) // { // if(assignment->getChildCount(assignment) == 2) // { // pANTLR3_BASE_TREE key = (pANTLR3_BASE_TREE)assignment->getChild(assignment, 0); // pANTLR3_BASE_TREE value = (pANTLR3_BASE_TREE)assignment->getChild(assignment, 1); // std::string strKey((char*)key->getText(key)->chars); // std::string strValue((char*)value->getText(value)->chars); // strKey = removeQuotes(strKey); // strValue = removeQuotes(strValue); // Value* keyValue = parseValues(key, mapType->getKeyType(), indent + 1); // Value* valueValue = parseValues(value, mapType->getValueType(), indent + 1); // if(keyValue != NULL && valueValue != NULL) // { // mapValue->put(keyValue, valueValue); // } // } // } } } return arrayValue; } else if(dynamic_cast(dataBoard) != NULL) { Databoard::Type::BooleanType* booleanType = (Databoard::Type::BooleanType*)dataBoard; BooleanValue* booleanValue = new BooleanValue(dataBoard); std::string value((char*)tree->getText(tree)->chars); if(value == STR_TRUE) { booleanValue->setValue(true); } else { booleanValue->setValue(false); } return booleanValue; } else if(dynamic_cast(dataBoard) != NULL) { 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); Databoard::Type::UnionType* unionType = (Databoard::Type::UnionType*)dataBoard; UnionValue* unionValue = new UnionValue(dataBoard); std::string strId((char*)id->getText(id)->chars); int tag = unionType->getComponentIndex(strId); Databoard::Type::Component* component = unionType->getComponent(tag); if(component != NULL) { Databoard::Type::DataType* dataType = component->getDataBoard(); if(dataType != NULL) { Databoard::Value::Value* value = parseValues(type, dataType, indent); unionValue->setValue(tag, value); } } return unionValue; } } else if(dynamic_cast(dataBoard) != NULL) { if(tree->getChildCount(tree) == 0) { std::string valueId((char*)tree->getText(tree)->chars); // check the string if(valueId.length() >= 2) { if(valueId.at(0) == '"' && valueId.at(valueId.length()-1) == '"') { valueId = removeQuotes(valueId); Databoard::Type::StringType* stringType = new Databoard::Type::StringType(); stringType->setNotBuiltIn(); Databoard::Value::StringValue* stringValue = new Databoard::Value::StringValue(stringType); stringValue->setValue(valueId); Databoard::Value::VariantValue* variantValue = new VariantValue(dataBoard); variantValue->setValue(stringValue); return variantValue; } } if(strEquals((char*)tree->getText(tree)->chars, STR_TRUE) == true) { Databoard::Type::BooleanType* booleanType = new Databoard::Type::BooleanType(); booleanType->setNotBuiltIn(); Databoard::Value::BooleanValue* booleanValue = new Databoard::Value::BooleanValue(booleanType); booleanValue->setValue(true); Databoard::Value::VariantValue* variantValue = new VariantValue(dataBoard); variantValue->setValue(booleanValue); return variantValue; } else if(strEquals((char*)tree->getText(tree)->chars, STR_FALSE) == true) { Databoard::Type::BooleanType* booleanType = new Databoard::Type::BooleanType(); booleanType->setNotBuiltIn(); Databoard::Value::BooleanValue* booleanValue = new Databoard::Value::BooleanValue(booleanType); booleanValue->setValue(false); Databoard::Value::VariantValue* variantValue = new VariantValue(dataBoard); variantValue->setValue(booleanValue); return variantValue; } return NULL; } else 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); Databoard::Type::VariantType* variantType = (Databoard::Type::VariantType*)dataBoard; Databoard::Type::temporaryname a(NULL); Databoard::Type::DataType* dataType = a.parse(id); Databoard::Value::Value* value = parseValues(type, dataType, indent); Databoard::Value::VariantValue* variantValue = new VariantValue(dataBoard); variantValue->setValue(value); return variantValue; } } else { std::cout << "unknown: " << tree->getType(tree) << std::endl; } return NULL; } Value* DataValueRepository::parse(pANTLR3_BASE_TREE tree, Value* parent, int indent) { switch(tree->getType(tree)) { case VALUE_DEFINITIONS: { for(int i=0;i<(int)tree->getChildCount(tree);++i) { Value* value = parse((pANTLR3_BASE_TREE)tree->getChild(tree, i), parent, indent + 1); } } break; case VALUE_DEFINITION: { if(tree->getChildCount(tree) == 3) { pANTLR3_BASE_TREE id = (pANTLR3_BASE_TREE)tree->getChild(tree, 0); pANTLR3_BASE_TREE type = (pANTLR3_BASE_TREE)tree->getChild(tree, 1); pANTLR3_BASE_TREE values = (pANTLR3_BASE_TREE)tree->getChild(tree, 2); //std::cout << id->getText(id)->chars << " : " << id->getType(id) << std::endl; //std::cout << type->getText(type)->chars << " : " << type->getType(type) << std::endl; //std::cout << values->getText(values)->chars << " : " << values->getType(values) << std::endl; const Databoard::Type::strTypeDefinition* dataBoard = parseTypeReference(type); Value* dataValue = parseValues(values, dataBoard->dataBoard, indent+1); std::string valueId((char*)id->getText(id)->chars); addDataValue(valueId, dataBoard->name, dataValue); //std::cout << "." << std::endl; } } break; default: break; } return NULL; } bool DataValueRepository::open(std::string fileName, Databoard::Type::DataBoardRepository* dataBoardRepository) { pANTLR3_INPUT_STREAM input = antlr3AsciiFileStreamNew((pANTLR3_UINT8)fileName.c_str()); if ( input == NULL ) { ANTLR3_FPRINTF(stderr, "string: %s\n", fileName.c_str()); } pSimanticsDataLexer lexer = SimanticsDataLexerNew(input); if ( lexer == NULL ) { ANTLR3_FPRINTF(stderr, "Unable to create the lexer due to malloc() failure1\n"); exit(ANTLR3_ERR_NOMEM); } 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); } 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); } SimanticsDataParser_valueDefinitions_return astValue = parser->valueDefinitions(parser); this->dataBoardRepository = dataBoardRepository; bool retValue = true; if(lexer->pLexer->rec->state->errorCount == 0) { parse(astValue.tree); //print(astValue.tree); } else { retValue = false; } parser->free(parser); tstream->free(tstream); lexer->free(lexer); input->free(input); return retValue; } Value* DataValueRepository::open(std::string fileName, Databoard::Type::DataType* dataType) { pANTLR3_INPUT_STREAM input = antlr3AsciiFileStreamNew((pANTLR3_UINT8)fileName.c_str()); if ( input == NULL ) { ANTLR3_FPRINTF(stderr, "string: %s\n", fileName.c_str()); } pSimanticsDataLexer lexer = SimanticsDataLexerNew(input); if ( lexer == NULL ) { ANTLR3_FPRINTF(stderr, "Unable to create the lexer due to malloc() failure1\n"); exit(ANTLR3_ERR_NOMEM); } 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); } 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); } SimanticsDataParser_value_return astValue = parser->value(parser); Value* retValue = NULL; dataBoardRepository = NULL; if(lexer->pLexer->rec->state->errorCount == 0) { retValue = parseValues(astValue.tree, dataType, 0); //print(astValue.tree); } else { retValue = false; } parser->free(parser); tstream->free(tstream); lexer->free(lexer); input->free(input); return retValue; } void DataValueRepository::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); } } int DataValueRepository::count() { return (int)values.size(); } const strDataValueDefinition* DataValueRepository::getDataValue(int i) { if(i >= 0 && i < (int)values.size()) { return &(values.at(i)); } else { return NULL; } } const strDataValueDefinition* DataValueRepository::getDataValue(std::string name) { for(int i = 0; i < (int)values.size(); ++i) { if(values.at(i).name == name) { return &(values.at(i)); } } return NULL; } } }