2015-01-31 10:07:48 +00:00
|
|
|
/*-----------------------------------------------------------------------------------------------
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
Copyright (c) 2014-2020 Kim Kulling
|
2015-01-31 10:07:48 +00:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
-----------------------------------------------------------------------------------------------*/
|
2015-10-31 08:43:34 +00:00
|
|
|
#include <openddlparser/OpenDDLExport.h>
|
2020-10-25 09:34:43 +00:00
|
|
|
#include <openddlparser/OpenDDLParser.h>
|
2015-01-31 10:07:48 +00:00
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <algorithm>
|
2015-01-31 10:07:48 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2020-10-25 09:34:43 +00:00
|
|
|
#include <windows.h>
|
2015-01-31 10:07:48 +00:00
|
|
|
#endif // _WIN32
|
|
|
|
|
|
|
|
BEGIN_ODDLPARSER_NS
|
|
|
|
|
2017-06-19 19:07:18 +00:00
|
|
|
static const char *Version = "0.4.0";
|
2015-01-31 10:07:48 +00:00
|
|
|
|
2015-03-09 09:17:23 +00:00
|
|
|
namespace Grammar {
|
2020-10-25 09:34:43 +00:00
|
|
|
|
|
|
|
static const char *OpenBracketToken = "{";
|
|
|
|
static const char *CloseBracketToken = "}";
|
|
|
|
static const char *OpenPropertyToken = "(";
|
|
|
|
static const char *ClosePropertyToken = ")";
|
|
|
|
static const char *OpenArrayToken = "[";
|
|
|
|
static const char *CloseArrayToken = "]";
|
|
|
|
static const char *BoolTrue = "true";
|
|
|
|
static const char *BoolFalse = "false";
|
|
|
|
static const char *CommaSeparator = ",";
|
|
|
|
|
|
|
|
static const char *PrimitiveTypeToken[(size_t)Value::ValueType::ddl_types_max] = {
|
|
|
|
"bool",
|
|
|
|
"int8",
|
|
|
|
"int16",
|
|
|
|
"int32",
|
|
|
|
"int64",
|
|
|
|
"unsigned_int8",
|
|
|
|
"unsigned_int16",
|
|
|
|
"unsigned_int32",
|
|
|
|
"unsigned_int64",
|
|
|
|
"half",
|
|
|
|
"float",
|
|
|
|
"double",
|
|
|
|
"string",
|
|
|
|
"ref"
|
|
|
|
};
|
2015-03-09 09:17:23 +00:00
|
|
|
} // Namespace Grammar
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
const char *getTypeToken(Value::ValueType type) {
|
|
|
|
return Grammar::PrimitiveTypeToken[(size_t)type];
|
2015-10-31 08:43:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
static void logInvalidTokenError(char *in, const std::string &exp, OpenDDLParser::logCallback callback) {
|
2015-01-31 10:07:48 +00:00
|
|
|
std::stringstream stream;
|
2020-10-25 09:34:43 +00:00
|
|
|
stream << "Invalid token \"" << *in << "\""
|
|
|
|
<< " expected \"" << exp << "\"" << std::endl;
|
|
|
|
std::string full(in);
|
|
|
|
std::string part(full.substr(0, 50));
|
2016-01-08 15:46:13 +00:00
|
|
|
stream << part;
|
2020-10-25 09:34:43 +00:00
|
|
|
callback(ddl_error_msg, stream.str());
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
static bool isIntegerType(Value::ValueType integerType) {
|
|
|
|
if (integerType != Value::ValueType::ddl_int8 && integerType != Value::ValueType::ddl_int16 &&
|
|
|
|
integerType != Value::ValueType::ddl_int32 && integerType != Value::ValueType::ddl_int64) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-10-25 09:34:43 +00:00
|
|
|
|
2016-01-08 15:46:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
static bool isUnsignedIntegerType(Value::ValueType integerType) {
|
|
|
|
if (integerType != Value::ValueType::ddl_unsigned_int8 && integerType != Value::ValueType::ddl_unsigned_int16 &&
|
|
|
|
integerType != Value::ValueType::ddl_unsigned_int32 && integerType != Value::ValueType::ddl_unsigned_int64) {
|
2016-01-08 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-01-31 10:07:48 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
static DDLNode *createDDLNode(Text *id, OpenDDLParser *parser) {
|
2020-10-26 20:35:44 +00:00
|
|
|
if (nullptr == id || nullptr == parser || id->m_buffer == nullptr) {
|
2020-10-25 09:34:43 +00:00
|
|
|
return nullptr;
|
2018-07-11 08:10:26 +00:00
|
|
|
}
|
2015-01-31 10:07:48 +00:00
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
const std::string type(id->m_buffer);
|
|
|
|
DDLNode *parent(parser->top());
|
|
|
|
DDLNode *node = DDLNode::create(type, "", parent);
|
|
|
|
|
2015-01-31 10:07:48 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
static void logMessage(LogSeverity severity, const std::string &msg) {
|
2015-01-31 10:07:48 +00:00
|
|
|
std::string log;
|
2020-10-25 09:34:43 +00:00
|
|
|
if (ddl_debug_msg == severity) {
|
2015-01-31 10:07:48 +00:00
|
|
|
log += "Debug:";
|
2020-10-25 09:34:43 +00:00
|
|
|
} else if (ddl_info_msg == severity) {
|
2015-01-31 10:07:48 +00:00
|
|
|
log += "Info :";
|
2020-10-25 09:34:43 +00:00
|
|
|
} else if (ddl_warn_msg == severity) {
|
2015-01-31 10:07:48 +00:00
|
|
|
log += "Warn :";
|
2020-10-25 09:34:43 +00:00
|
|
|
} else if (ddl_error_msg == severity) {
|
2015-01-31 10:07:48 +00:00
|
|
|
log += "Error:";
|
|
|
|
} else {
|
|
|
|
log += "None :";
|
|
|
|
}
|
|
|
|
|
|
|
|
log += msg;
|
|
|
|
std::cout << log;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
OpenDDLParser::OpenDDLParser() :
|
|
|
|
m_logCallback(logMessage),
|
|
|
|
m_buffer(),
|
|
|
|
m_stack(),
|
|
|
|
m_context(nullptr) {
|
2015-01-31 10:07:48 +00:00
|
|
|
// empty
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
OpenDDLParser::OpenDDLParser(const char *buffer, size_t len) :
|
|
|
|
m_logCallback(&logMessage), m_buffer(), m_context(nullptr) {
|
|
|
|
if (0 != len) {
|
|
|
|
setBuffer(buffer, len);
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OpenDDLParser::~OpenDDLParser() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
void OpenDDLParser::setLogCallback(logCallback callback) {
|
|
|
|
if (nullptr != callback) {
|
2015-01-31 10:07:48 +00:00
|
|
|
// install user-specific log callback
|
|
|
|
m_logCallback = callback;
|
|
|
|
} else {
|
|
|
|
// install default log callback
|
|
|
|
m_logCallback = &logMessage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OpenDDLParser::logCallback OpenDDLParser::getLogCallback() const {
|
|
|
|
return m_logCallback;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
void OpenDDLParser::setBuffer(const char *buffer, size_t len) {
|
2015-02-07 11:47:53 +00:00
|
|
|
clear();
|
2020-10-25 09:34:43 +00:00
|
|
|
if (0 == len) {
|
2015-02-07 11:47:53 +00:00
|
|
|
return;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
m_buffer.resize(len);
|
|
|
|
::memcpy(&m_buffer[0], buffer, len);
|
2015-02-07 11:47:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
void OpenDDLParser::setBuffer(const std::vector<char> &buffer) {
|
2015-02-07 11:47:53 +00:00
|
|
|
clear();
|
2020-10-25 09:34:43 +00:00
|
|
|
m_buffer.resize(buffer.size());
|
|
|
|
std::copy(buffer.begin(), buffer.end(), m_buffer.begin());
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
2015-02-07 11:47:53 +00:00
|
|
|
const char *OpenDDLParser::getBuffer() const {
|
2020-10-25 09:34:43 +00:00
|
|
|
if (m_buffer.empty()) {
|
|
|
|
return nullptr;
|
2015-02-07 11:47:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
return &m_buffer[0];
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t OpenDDLParser::getBufferSize() const {
|
2015-02-07 11:47:53 +00:00
|
|
|
return m_buffer.size();
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenDDLParser::clear() {
|
2020-10-25 09:34:43 +00:00
|
|
|
m_buffer.resize(0);
|
|
|
|
if (nullptr != m_context) {
|
2017-06-19 19:07:18 +00:00
|
|
|
delete m_context;
|
2020-10-25 09:34:43 +00:00
|
|
|
m_context = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// DDLNode::releaseNodes();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OpenDDLParser::validate() {
|
|
|
|
if (m_buffer.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isCharacter(m_buffer[0]) && !isNumeric(m_buffer[0])) {
|
|
|
|
return false;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
return true;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool OpenDDLParser::parse() {
|
2020-10-25 09:34:43 +00:00
|
|
|
if (m_buffer.empty()) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-08 15:46:13 +00:00
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
normalizeBuffer(m_buffer);
|
|
|
|
if (!validate()) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-31 10:07:48 +00:00
|
|
|
|
|
|
|
m_context = new Context;
|
2020-10-25 09:34:43 +00:00
|
|
|
m_context->m_root = DDLNode::create("root", "", nullptr);
|
|
|
|
pushNode(m_context->m_root);
|
2015-01-31 10:07:48 +00:00
|
|
|
|
|
|
|
// do the main parsing
|
2020-10-25 09:34:43 +00:00
|
|
|
char *current(&m_buffer[0]);
|
|
|
|
char *end(&m_buffer[m_buffer.size() - 1] + 1);
|
|
|
|
size_t pos(current - &m_buffer[0]);
|
|
|
|
while (pos < m_buffer.size()) {
|
|
|
|
current = parseNextNode(current, end);
|
|
|
|
if (current == nullptr) {
|
2016-01-08 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-10-25 09:34:43 +00:00
|
|
|
pos = current - &m_buffer[0];
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
bool OpenDDLParser::exportContext(Context *ctx, const std::string &filename) {
|
|
|
|
if (nullptr == ctx) {
|
2015-10-01 18:29:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-08 15:46:13 +00:00
|
|
|
|
2015-10-31 08:43:34 +00:00
|
|
|
OpenDDLExport myExporter;
|
2020-10-25 09:34:43 +00:00
|
|
|
return myExporter.exportContext(ctx, filename);
|
2015-10-01 18:29:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
char *OpenDDLParser::parseNextNode(char *in, char *end) {
|
|
|
|
in = parseHeader(in, end);
|
|
|
|
in = parseStructure(in, end);
|
2015-01-31 10:07:48 +00:00
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2016-01-08 15:46:13 +00:00
|
|
|
#ifdef DEBUG_HEADER_NAME
|
2020-10-25 09:34:43 +00:00
|
|
|
static void dumpId(Identifier *id) {
|
|
|
|
if (nullptr != id) {
|
|
|
|
if (nullptr != id->m_text.m_buffer) {
|
2015-10-31 08:43:34 +00:00
|
|
|
std::cout << id->m_text.m_buffer << std::endl;
|
2020-10-25 09:34:43 +00:00
|
|
|
}
|
2015-03-09 09:17:23 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-08 15:46:13 +00:00
|
|
|
#endif
|
2015-03-09 09:17:23 +00:00
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
char *OpenDDLParser::parseHeader(char *in, char *end) {
|
|
|
|
if (nullptr == in || in == end) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
Text *id(nullptr);
|
|
|
|
in = OpenDDLParser::parseIdentifier(in, end, &id);
|
2015-01-31 10:07:48 +00:00
|
|
|
|
2016-01-08 15:46:13 +00:00
|
|
|
#ifdef DEBUG_HEADER_NAME
|
2020-10-25 09:34:43 +00:00
|
|
|
dumpId(id);
|
2015-01-31 10:07:48 +00:00
|
|
|
#endif // DEBUG_HEADER_NAME
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
in = lookForNextToken(in, end);
|
|
|
|
if (nullptr != id) {
|
2015-01-31 10:07:48 +00:00
|
|
|
// store the node
|
2020-10-25 09:34:43 +00:00
|
|
|
DDLNode *node(createDDLNode(id, this));
|
|
|
|
if (nullptr != node) {
|
|
|
|
pushNode(node);
|
2015-01-31 10:07:48 +00:00
|
|
|
} else {
|
|
|
|
std::cerr << "nullptr returned by creating DDLNode." << std::endl;
|
|
|
|
}
|
2017-06-19 19:07:18 +00:00
|
|
|
delete id;
|
2015-01-31 10:07:48 +00:00
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
Name *name(nullptr);
|
|
|
|
in = OpenDDLParser::parseName(in, end, &name);
|
|
|
|
if (nullptr != name && nullptr != node && nullptr != name->m_id->m_buffer) {
|
|
|
|
const std::string nodeName(name->m_id->m_buffer);
|
|
|
|
node->setName(nodeName);
|
|
|
|
delete name;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
2016-02-07 16:58:28 +00:00
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
Property *first(nullptr);
|
|
|
|
in = lookForNextToken(in, end);
|
|
|
|
if (*in == Grammar::OpenPropertyToken[0]) {
|
|
|
|
in++;
|
|
|
|
Property *prop(nullptr), *prev(nullptr);
|
|
|
|
while (*in != Grammar::ClosePropertyToken[0] && in != end) {
|
|
|
|
in = OpenDDLParser::parseProperty(in, end, &prop);
|
|
|
|
in = lookForNextToken(in, end);
|
|
|
|
|
|
|
|
if (*in != Grammar::CommaSeparator[0] && *in != Grammar::ClosePropertyToken[0]) {
|
|
|
|
logInvalidTokenError(in, Grammar::ClosePropertyToken, m_logCallback);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nullptr != prop && *in != Grammar::CommaSeparator[0]) {
|
|
|
|
if (nullptr == first) {
|
|
|
|
first = prop;
|
|
|
|
}
|
|
|
|
if (nullptr != prev) {
|
|
|
|
prev->m_next = prop;
|
|
|
|
}
|
|
|
|
prev = prop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++in;
|
|
|
|
}
|
2017-06-19 19:07:18 +00:00
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
// set the properties
|
|
|
|
if (nullptr != first && nullptr != node) {
|
|
|
|
node->setProperties(first);
|
|
|
|
}
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
char *OpenDDLParser::parseStructure(char *in, char *end) {
|
|
|
|
if (nullptr == in || in == end) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
bool error(false);
|
|
|
|
in = lookForNextToken(in, end);
|
|
|
|
if (*in == *Grammar::OpenBracketToken) {
|
2015-03-20 20:55:19 +00:00
|
|
|
// loop over all children ( data and nodes )
|
2015-03-09 09:17:23 +00:00
|
|
|
do {
|
2020-10-25 09:34:43 +00:00
|
|
|
in = parseStructureBody(in, end, error);
|
|
|
|
if (in == nullptr) {
|
|
|
|
return nullptr;
|
2016-01-08 15:46:13 +00:00
|
|
|
}
|
2020-10-25 09:34:43 +00:00
|
|
|
} while (*in != *Grammar::CloseBracketToken);
|
2017-06-19 19:07:18 +00:00
|
|
|
++in;
|
2015-03-25 18:55:39 +00:00
|
|
|
} else {
|
2017-06-19 19:07:18 +00:00
|
|
|
++in;
|
2020-10-25 09:34:43 +00:00
|
|
|
logInvalidTokenError(in, std::string(Grammar::OpenBracketToken), m_logCallback);
|
|
|
|
error = true;
|
|
|
|
return nullptr;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
2020-10-25 09:34:43 +00:00
|
|
|
in = lookForNextToken(in, end);
|
2016-01-08 15:46:13 +00:00
|
|
|
|
2015-02-10 18:31:23 +00:00
|
|
|
// pop node from stack after successful parsing
|
2020-10-25 09:34:43 +00:00
|
|
|
if (!error) {
|
2015-02-10 18:31:23 +00:00
|
|
|
popNode();
|
|
|
|
}
|
2015-01-31 10:07:48 +00:00
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
static void setNodeValues(DDLNode *currentNode, Value *values) {
|
|
|
|
if (nullptr != values) {
|
|
|
|
if (nullptr != currentNode) {
|
|
|
|
currentNode->setValue(values);
|
2015-03-09 09:17:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
static void setNodeReferences(DDLNode *currentNode, Reference *refs) {
|
|
|
|
if (nullptr != refs) {
|
|
|
|
if (nullptr != currentNode) {
|
|
|
|
currentNode->setReferences(refs);
|
2015-03-09 09:17:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
static void setNodeDataArrayList(DDLNode *currentNode, DataArrayList *dtArrayList) {
|
|
|
|
if (nullptr != dtArrayList) {
|
|
|
|
if (nullptr != currentNode) {
|
|
|
|
currentNode->setDataArrayList(dtArrayList);
|
2015-03-09 09:17:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
char *OpenDDLParser::parseStructureBody(char *in, char *end, bool &error) {
|
|
|
|
if (!isNumeric(*in) && !isCharacter(*in)) {
|
2017-06-19 19:07:18 +00:00
|
|
|
++in;
|
2015-03-09 09:17:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
in = lookForNextToken(in, end);
|
|
|
|
Value::ValueType type(Value::ValueType::ddl_none);
|
|
|
|
size_t arrayLen(0);
|
|
|
|
in = OpenDDLParser::parsePrimitiveDataType(in, end, type, arrayLen);
|
|
|
|
if (Value::ValueType::ddl_none != type) {
|
2015-03-25 18:55:39 +00:00
|
|
|
// parse a primitive data type
|
2020-10-25 09:34:43 +00:00
|
|
|
in = lookForNextToken(in, end);
|
|
|
|
if (*in == Grammar::OpenBracketToken[0]) {
|
|
|
|
Reference *refs(nullptr);
|
|
|
|
DataArrayList *dtArrayList(nullptr);
|
|
|
|
Value *values(nullptr);
|
|
|
|
if (1 == arrayLen) {
|
|
|
|
size_t numRefs(0), numValues(0);
|
|
|
|
in = parseDataList(in, end, type, &values, numValues, &refs, numRefs);
|
|
|
|
setNodeValues(top(), values);
|
|
|
|
setNodeReferences(top(), refs);
|
|
|
|
} else if (arrayLen > 1) {
|
|
|
|
in = parseDataArrayList(in, end, type, &dtArrayList);
|
|
|
|
setNodeDataArrayList(top(), dtArrayList);
|
2015-03-09 09:17:23 +00:00
|
|
|
} else {
|
|
|
|
std::cerr << "0 for array is invalid." << std::endl;
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
in = lookForNextToken(in, end);
|
|
|
|
if (*in != '}') {
|
|
|
|
logInvalidTokenError(in, std::string(Grammar::CloseBracketToken), m_logCallback);
|
|
|
|
return nullptr;
|
2015-03-09 09:17:23 +00:00
|
|
|
} else {
|
|
|
|
//in++;
|
|
|
|
}
|
|
|
|
} else {
|
2015-03-25 18:55:39 +00:00
|
|
|
// parse a complex data type
|
2020-10-25 09:34:43 +00:00
|
|
|
in = parseNextNode(in, end);
|
2015-03-09 09:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
void OpenDDLParser::pushNode(DDLNode *node) {
|
|
|
|
if (nullptr == node) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
m_stack.push_back(node);
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DDLNode *OpenDDLParser::popNode() {
|
2020-10-25 09:34:43 +00:00
|
|
|
if (m_stack.empty()) {
|
|
|
|
return nullptr;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
DDLNode *topNode(top());
|
2015-01-31 10:07:48 +00:00
|
|
|
m_stack.pop_back();
|
|
|
|
return topNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
DDLNode *OpenDDLParser::top() {
|
2020-10-25 09:34:43 +00:00
|
|
|
if (m_stack.empty()) {
|
|
|
|
return nullptr;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
2016-01-08 15:46:13 +00:00
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
DDLNode *top(m_stack.back());
|
2015-01-31 10:07:48 +00:00
|
|
|
return top;
|
|
|
|
}
|
|
|
|
|
|
|
|
DDLNode *OpenDDLParser::getRoot() const {
|
2020-10-25 09:34:43 +00:00
|
|
|
if (nullptr == m_context) {
|
|
|
|
return nullptr;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_context->m_root;
|
|
|
|
}
|
|
|
|
|
|
|
|
Context *OpenDDLParser::getContext() const {
|
|
|
|
return m_context;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
void OpenDDLParser::normalizeBuffer(std::vector<char> &buffer) {
|
|
|
|
if (buffer.empty()) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-07 11:47:53 +00:00
|
|
|
std::vector<char> newBuffer;
|
2020-10-25 09:34:43 +00:00
|
|
|
const size_t len(buffer.size());
|
|
|
|
char *end(&buffer[len - 1] + 1);
|
|
|
|
for (size_t readIdx = 0; readIdx < len; ++readIdx) {
|
|
|
|
char *c(&buffer[readIdx]);
|
2015-01-31 10:07:48 +00:00
|
|
|
// check for a comment
|
2017-06-19 19:07:18 +00:00
|
|
|
if (isCommentOpenTag(c, end)) {
|
|
|
|
++readIdx;
|
|
|
|
while (!isCommentCloseTag(&buffer[readIdx], end)) {
|
|
|
|
++readIdx;
|
|
|
|
}
|
|
|
|
++readIdx;
|
2020-10-25 09:34:43 +00:00
|
|
|
} else if (!isComment<char>(c, end) && !isNewLine(*c)) {
|
|
|
|
newBuffer.push_back(buffer[readIdx]);
|
2015-01-31 10:07:48 +00:00
|
|
|
} else {
|
2020-10-25 09:34:43 +00:00
|
|
|
if (isComment<char>(c, end)) {
|
2015-10-01 18:29:15 +00:00
|
|
|
++readIdx;
|
2015-03-25 18:55:39 +00:00
|
|
|
// skip the comment and the rest of the line
|
2020-10-25 09:34:43 +00:00
|
|
|
while (!isEndofLine(buffer[readIdx])) {
|
2015-10-01 18:29:15 +00:00
|
|
|
++readIdx;
|
2015-03-25 18:55:39 +00:00
|
|
|
}
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-07 11:47:53 +00:00
|
|
|
buffer = newBuffer;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
char *OpenDDLParser::parseName(char *in, char *end, Name **name) {
|
|
|
|
*name = nullptr;
|
|
|
|
if (nullptr == in || in == end) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore blanks
|
2020-10-25 09:34:43 +00:00
|
|
|
in = lookForNextToken(in, end);
|
|
|
|
if (*in != '$' && *in != '%') {
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
NameType ntype(GlobalName);
|
|
|
|
if (*in == '%') {
|
2015-01-31 10:07:48 +00:00
|
|
|
ntype = LocalName;
|
|
|
|
}
|
2016-01-08 15:46:13 +00:00
|
|
|
in++;
|
2020-10-25 09:34:43 +00:00
|
|
|
Name *currentName(nullptr);
|
|
|
|
Text *id(nullptr);
|
|
|
|
in = parseIdentifier(in, end, &id);
|
|
|
|
if (id) {
|
|
|
|
currentName = new Name(ntype, id);
|
2021-01-06 13:52:28 +00:00
|
|
|
*name = currentName;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
2016-01-08 15:46:13 +00:00
|
|
|
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
char *OpenDDLParser::parseIdentifier(char *in, char *end, Text **id) {
|
|
|
|
*id = nullptr;
|
|
|
|
if (nullptr == in || in == end) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore blanks
|
2020-10-25 09:34:43 +00:00
|
|
|
in = lookForNextToken(in, end);
|
2020-10-26 20:35:44 +00:00
|
|
|
if (in == end) {
|
|
|
|
return in;
|
|
|
|
}
|
2016-01-08 15:46:13 +00:00
|
|
|
|
2015-01-31 10:07:48 +00:00
|
|
|
// staring with a number is forbidden
|
2020-10-25 09:34:43 +00:00
|
|
|
if (isNumeric<const char>(*in)) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get size of id
|
2020-10-25 09:34:43 +00:00
|
|
|
size_t idLen(0);
|
|
|
|
char *start(in);
|
|
|
|
while (!isSeparator(*in) &&
|
|
|
|
!isNewLine(*in) && (in != end) &&
|
|
|
|
*in != Grammar::OpenPropertyToken[0] &&
|
|
|
|
*in != Grammar::ClosePropertyToken[0] &&
|
|
|
|
*in != '$') {
|
2015-10-01 18:29:15 +00:00
|
|
|
++in;
|
|
|
|
++idLen;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
2016-01-08 15:46:13 +00:00
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
const size_t len(idLen);
|
|
|
|
*id = new Text(start, len);
|
2015-01-31 10:07:48 +00:00
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
char *OpenDDLParser::parsePrimitiveDataType(char *in, char *end, Value::ValueType &type, size_t &len) {
|
|
|
|
type = Value::ValueType::ddl_none;
|
2015-01-31 10:07:48 +00:00
|
|
|
len = 0;
|
2020-10-25 09:34:43 +00:00
|
|
|
if (nullptr == in || in == end) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
size_t prim_len(0);
|
|
|
|
for (size_t i = 0; i < (size_t) Value::ValueType::ddl_types_max; i++) {
|
|
|
|
prim_len = strlen(Grammar::PrimitiveTypeToken[i]);
|
|
|
|
if (0 == strncmp(in, Grammar::PrimitiveTypeToken[i], prim_len)) {
|
|
|
|
type = static_cast<Value::ValueType>(i);
|
2015-01-31 10:07:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
if (Value::ValueType::ddl_none == type) {
|
|
|
|
in = lookForNextToken(in, end);
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
} else {
|
2015-03-09 09:17:23 +00:00
|
|
|
in += prim_len;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
bool ok(true);
|
|
|
|
if (*in == Grammar::OpenArrayToken[0]) {
|
2015-01-31 10:07:48 +00:00
|
|
|
ok = false;
|
2017-06-19 19:07:18 +00:00
|
|
|
++in;
|
2020-10-25 09:34:43 +00:00
|
|
|
char *start(in);
|
|
|
|
while (in != end) {
|
2017-06-19 19:07:18 +00:00
|
|
|
++in;
|
2020-10-25 09:34:43 +00:00
|
|
|
if (*in == Grammar::CloseArrayToken[0]) {
|
|
|
|
len = ::atoi(start);
|
2015-01-31 10:07:48 +00:00
|
|
|
ok = true;
|
2017-06-19 19:07:18 +00:00
|
|
|
++in;
|
2015-01-31 10:07:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
len = 1;
|
|
|
|
}
|
2020-10-25 09:34:43 +00:00
|
|
|
if (!ok) {
|
|
|
|
type = Value::ValueType::ddl_none;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
char *OpenDDLParser::parseReference(char *in, char *end, std::vector<Name *> &names) {
|
|
|
|
if (nullptr == in || in == end) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
Name *nextName(nullptr);
|
|
|
|
in = parseName(in, end, &nextName);
|
|
|
|
if (nextName) {
|
|
|
|
names.push_back(nextName);
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
2020-10-25 09:34:43 +00:00
|
|
|
while (Grammar::CommaSeparator[0] == *in) {
|
|
|
|
in = getNextSeparator(in, end);
|
|
|
|
if (Grammar::CommaSeparator[0] == *in) {
|
|
|
|
in = parseName(in, end, &nextName);
|
|
|
|
if (nextName) {
|
|
|
|
names.push_back(nextName);
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
char *OpenDDLParser::parseBooleanLiteral(char *in, char *end, Value **boolean) {
|
|
|
|
*boolean = nullptr;
|
|
|
|
if (nullptr == in || in == end) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
in = lookForNextToken(in, end);
|
|
|
|
char *start(in);
|
|
|
|
size_t len(0);
|
|
|
|
while (!isSeparator(*in) && in != end) {
|
2017-06-19 19:07:18 +00:00
|
|
|
++in;
|
|
|
|
++len;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
2017-06-19 19:07:18 +00:00
|
|
|
++len;
|
2020-10-25 09:34:43 +00:00
|
|
|
int res = ::strncmp(Grammar::BoolTrue, start, strlen(Grammar::BoolTrue));
|
|
|
|
if (0 != res) {
|
|
|
|
res = ::strncmp(Grammar::BoolFalse, start, strlen(Grammar::BoolFalse));
|
|
|
|
if (0 != res) {
|
|
|
|
*boolean = nullptr;
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
2020-10-25 09:34:43 +00:00
|
|
|
*boolean = ValueAllocator::allocPrimData(Value::ValueType::ddl_bool);
|
|
|
|
(*boolean)->setBool(false);
|
2015-01-31 10:07:48 +00:00
|
|
|
} else {
|
2020-10-25 09:34:43 +00:00
|
|
|
*boolean = ValueAllocator::allocPrimData(Value::ValueType::ddl_bool);
|
|
|
|
(*boolean)->setBool(true);
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
char *OpenDDLParser::parseIntegerLiteral(char *in, char *end, Value **integer, Value::ValueType integerType) {
|
|
|
|
*integer = nullptr;
|
|
|
|
if (nullptr == in || in == end) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
if (!(isIntegerType(integerType) || isUnsignedIntegerType(integerType))) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
in = lookForNextToken(in, end);
|
|
|
|
char *start(in);
|
|
|
|
while (!isSeparator(*in) && in != end) {
|
2017-06-19 19:07:18 +00:00
|
|
|
++in;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
if (isNumeric(*start)) {
|
2016-01-08 15:46:13 +00:00
|
|
|
#ifdef OPENDDL_NO_USE_CPP11
|
2020-10-25 09:34:43 +00:00
|
|
|
const int64 value(atol(start)); // maybe not really 64bit as atoll is but exists without c++11
|
|
|
|
const uint64 uvalue(strtoul(start, nullptr, 10));
|
2016-01-08 15:46:13 +00:00
|
|
|
#else
|
2020-10-25 09:34:43 +00:00
|
|
|
const int64 value(atoll(start));
|
|
|
|
const uint64 uvalue(strtoull(start, nullptr, 10));
|
2016-01-08 15:46:13 +00:00
|
|
|
#endif
|
2020-10-25 09:34:43 +00:00
|
|
|
*integer = ValueAllocator::allocPrimData(integerType);
|
|
|
|
switch (integerType) {
|
|
|
|
case Value::ValueType::ddl_int8:
|
|
|
|
(*integer)->setInt8((int8)value);
|
2017-06-19 19:07:18 +00:00
|
|
|
break;
|
2020-10-25 09:34:43 +00:00
|
|
|
case Value::ValueType::ddl_int16:
|
|
|
|
(*integer)->setInt16((int16)value);
|
2017-06-19 19:07:18 +00:00
|
|
|
break;
|
2020-10-25 09:34:43 +00:00
|
|
|
case Value::ValueType::ddl_int32:
|
|
|
|
(*integer)->setInt32((int32)value);
|
2017-06-19 19:07:18 +00:00
|
|
|
break;
|
2020-10-25 09:34:43 +00:00
|
|
|
case Value::ValueType::ddl_int64:
|
|
|
|
(*integer)->setInt64((int64)value);
|
2017-06-19 19:07:18 +00:00
|
|
|
break;
|
2020-10-25 09:34:43 +00:00
|
|
|
case Value::ValueType::ddl_unsigned_int8:
|
|
|
|
(*integer)->setUnsignedInt8((uint8)uvalue);
|
2017-06-19 19:07:18 +00:00
|
|
|
break;
|
2020-10-25 09:34:43 +00:00
|
|
|
case Value::ValueType::ddl_unsigned_int16:
|
|
|
|
(*integer)->setUnsignedInt16((uint16)uvalue);
|
2017-06-19 19:07:18 +00:00
|
|
|
break;
|
2020-10-25 09:34:43 +00:00
|
|
|
case Value::ValueType::ddl_unsigned_int32:
|
|
|
|
(*integer)->setUnsignedInt32((uint32)uvalue);
|
2017-06-19 19:07:18 +00:00
|
|
|
break;
|
2020-10-25 09:34:43 +00:00
|
|
|
case Value::ValueType::ddl_unsigned_int64:
|
|
|
|
(*integer)->setUnsignedInt64((uint64)uvalue);
|
2017-06-19 19:07:18 +00:00
|
|
|
break;
|
2015-01-31 10:07:48 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-01-08 15:46:13 +00:00
|
|
|
}
|
2015-01-31 10:07:48 +00:00
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
char *OpenDDLParser::parseFloatingLiteral(char *in, char *end, Value **floating, Value::ValueType floatType) {
|
|
|
|
*floating = nullptr;
|
|
|
|
if (nullptr == in || in == end) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
in = lookForNextToken(in, end);
|
|
|
|
char *start(in);
|
|
|
|
while (!isSeparator(*in) && in != end) {
|
2017-06-19 19:07:18 +00:00
|
|
|
++in;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse the float value
|
2020-10-25 09:34:43 +00:00
|
|
|
bool ok(false);
|
|
|
|
if (isHexLiteral(start, end)) {
|
|
|
|
parseHexaLiteral(start, end, floating);
|
2016-02-07 16:58:28 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
if (isNumeric(*start)) {
|
2015-01-31 10:07:48 +00:00
|
|
|
ok = true;
|
|
|
|
} else {
|
2020-10-25 09:34:43 +00:00
|
|
|
if (*start == '-') {
|
|
|
|
if (isNumeric(*(start + 1))) {
|
2015-01-31 10:07:48 +00:00
|
|
|
ok = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
if (ok) {
|
|
|
|
if (floatType == Value::ValueType::ddl_double) {
|
|
|
|
const double value(atof(start));
|
|
|
|
*floating = ValueAllocator::allocPrimData(Value::ValueType::ddl_double);
|
|
|
|
(*floating)->setDouble(value);
|
2016-01-08 15:46:13 +00:00
|
|
|
} else {
|
2020-10-25 09:34:43 +00:00
|
|
|
const float value((float)atof(start));
|
|
|
|
*floating = ValueAllocator::allocPrimData(Value::ValueType::ddl_float);
|
|
|
|
(*floating)->setFloat(value);
|
2016-01-08 15:46:13 +00:00
|
|
|
}
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
char *OpenDDLParser::parseStringLiteral(char *in, char *end, Value **stringData) {
|
|
|
|
*stringData = nullptr;
|
|
|
|
if (nullptr == in || in == end) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
in = lookForNextToken(in, end);
|
|
|
|
size_t len(0);
|
|
|
|
char *start(in);
|
|
|
|
if (*start == '\"') {
|
2017-06-19 19:07:18 +00:00
|
|
|
++start;
|
|
|
|
++in;
|
2020-10-25 09:34:43 +00:00
|
|
|
while (*in != '\"' && in != end) {
|
2017-06-19 19:07:18 +00:00
|
|
|
++in;
|
|
|
|
++len;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
*stringData = ValueAllocator::allocPrimData(Value::ValueType::ddl_string, len);
|
|
|
|
::strncpy((char *)(*stringData)->m_data, start, len);
|
|
|
|
(*stringData)->m_data[len] = '\0';
|
2017-06-19 19:07:18 +00:00
|
|
|
++in;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
static void createPropertyWithData(Text *id, Value *primData, Property **prop) {
|
|
|
|
if (nullptr != primData) {
|
|
|
|
(*prop) = new Property(id);
|
|
|
|
(*prop)->m_value = primData;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
char *OpenDDLParser::parseHexaLiteral(char *in, char *end, Value **data) {
|
|
|
|
*data = nullptr;
|
|
|
|
if (nullptr == in || in == end) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
in = lookForNextToken(in, end);
|
|
|
|
if (*in != '0') {
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2017-06-19 19:07:18 +00:00
|
|
|
++in;
|
2020-10-25 09:34:43 +00:00
|
|
|
if (*in != 'x' && *in != 'X') {
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2017-06-19 19:07:18 +00:00
|
|
|
++in;
|
2020-10-25 09:34:43 +00:00
|
|
|
bool ok(true);
|
|
|
|
char *start(in);
|
|
|
|
int pos(0);
|
|
|
|
while (!isSeparator(*in) && in != end) {
|
|
|
|
if ((*in < '0' && *in > '9') || (*in < 'a' && *in > 'f') || (*in < 'A' && *in > 'F')) {
|
2015-01-31 10:07:48 +00:00
|
|
|
ok = false;
|
|
|
|
break;
|
|
|
|
}
|
2017-06-19 19:07:18 +00:00
|
|
|
++pos;
|
|
|
|
++in;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
if (!ok) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
int value(0);
|
|
|
|
while (pos > 0) {
|
|
|
|
int v = hex2Decimal(*start);
|
2017-06-19 19:07:18 +00:00
|
|
|
--pos;
|
2020-10-25 09:34:43 +00:00
|
|
|
value = (value << 4) | v;
|
2017-06-19 19:07:18 +00:00
|
|
|
++start;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
*data = ValueAllocator::allocPrimData(Value::ValueType::ddl_unsigned_int64);
|
|
|
|
if (nullptr != *data) {
|
|
|
|
(*data)->setUnsignedInt64(value);
|
2015-04-14 10:04:39 +00:00
|
|
|
}
|
2015-01-31 10:07:48 +00:00
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
char *OpenDDLParser::parseProperty(char *in, char *end, Property **prop) {
|
|
|
|
*prop = nullptr;
|
|
|
|
if (nullptr == in || in == end) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
in = lookForNextToken(in, end);
|
2020-10-26 20:35:44 +00:00
|
|
|
Text *id = nullptr;
|
2020-10-25 09:34:43 +00:00
|
|
|
in = parseIdentifier(in, end, &id);
|
|
|
|
if (nullptr != id) {
|
|
|
|
in = lookForNextToken(in, end);
|
|
|
|
if (*in == '=') {
|
2017-06-19 19:07:18 +00:00
|
|
|
++in;
|
2020-10-25 09:34:43 +00:00
|
|
|
in = getNextToken(in, end);
|
|
|
|
Value *primData(nullptr);
|
|
|
|
if (isInteger(in, end)) {
|
|
|
|
in = parseIntegerLiteral(in, end, &primData);
|
|
|
|
createPropertyWithData(id, primData, prop);
|
|
|
|
} else if (isFloat(in, end)) {
|
|
|
|
in = parseFloatingLiteral(in, end, &primData);
|
|
|
|
createPropertyWithData(id, primData, prop);
|
|
|
|
} else if (isStringLiteral(*in)) { // string data
|
|
|
|
in = parseStringLiteral(in, end, &primData);
|
|
|
|
createPropertyWithData(id, primData, prop);
|
|
|
|
} else { // reference data
|
|
|
|
std::vector<Name *> names;
|
|
|
|
in = parseReference(in, end, names);
|
|
|
|
if (!names.empty()) {
|
|
|
|
Reference *ref = new Reference(names.size(), &names[0]);
|
|
|
|
(*prop) = new Property(id);
|
|
|
|
(*prop)->m_ref = ref;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-19 19:07:18 +00:00
|
|
|
} else {
|
|
|
|
delete id;
|
2016-01-08 15:46:13 +00:00
|
|
|
}
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
char *OpenDDLParser::parseDataList(char *in, char *end, Value::ValueType type, Value **data,
|
|
|
|
size_t &numValues, Reference **refs, size_t &numRefs) {
|
|
|
|
*data = nullptr;
|
2015-03-23 17:46:41 +00:00
|
|
|
numValues = numRefs = 0;
|
2020-10-25 09:34:43 +00:00
|
|
|
if (nullptr == in || in == end) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
in = lookForNextToken(in, end);
|
|
|
|
if (*in == '{') {
|
2017-06-19 19:07:18 +00:00
|
|
|
++in;
|
2020-10-25 09:34:43 +00:00
|
|
|
Value *current(nullptr), *prev(nullptr);
|
|
|
|
while ('}' != *in) {
|
|
|
|
current = nullptr;
|
|
|
|
in = lookForNextToken(in, end);
|
|
|
|
if (Value::ValueType::ddl_ref == type) {
|
|
|
|
std::vector<Name *> names;
|
|
|
|
in = parseReference(in, end, names);
|
|
|
|
if (!names.empty()) {
|
|
|
|
Reference *ref = new Reference(names.size(), &names[0]);
|
2016-02-07 16:58:28 +00:00
|
|
|
*refs = ref;
|
|
|
|
numRefs = names.size();
|
|
|
|
}
|
2020-10-25 09:34:43 +00:00
|
|
|
} else if (Value::ValueType::ddl_none == type) {
|
|
|
|
if (isInteger(in, end)) {
|
|
|
|
in = parseIntegerLiteral(in, end, ¤t);
|
|
|
|
} else if (isFloat(in, end)) {
|
|
|
|
in = parseFloatingLiteral(in, end, ¤t);
|
|
|
|
} else if (isStringLiteral(*in)) {
|
|
|
|
in = parseStringLiteral(in, end, ¤t);
|
|
|
|
} else if (isHexLiteral(in, end)) {
|
|
|
|
in = parseHexaLiteral(in, end, ¤t);
|
2015-10-31 08:43:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-10-25 09:34:43 +00:00
|
|
|
switch (type) {
|
|
|
|
case Value::ValueType::ddl_int8:
|
|
|
|
case Value::ValueType::ddl_int16:
|
|
|
|
case Value::ValueType::ddl_int32:
|
|
|
|
case Value::ValueType::ddl_int64:
|
|
|
|
case Value::ValueType::ddl_unsigned_int8:
|
|
|
|
case Value::ValueType::ddl_unsigned_int16:
|
|
|
|
case Value::ValueType::ddl_unsigned_int32:
|
|
|
|
case Value::ValueType::ddl_unsigned_int64:
|
|
|
|
in = parseIntegerLiteral(in, end, ¤t, type);
|
2016-01-08 15:46:13 +00:00
|
|
|
break;
|
2020-10-25 09:34:43 +00:00
|
|
|
case Value::ValueType::ddl_half:
|
|
|
|
case Value::ValueType::ddl_float:
|
|
|
|
case Value::ValueType::ddl_double:
|
|
|
|
in = parseFloatingLiteral(in, end, ¤t, type);
|
2016-01-08 15:46:13 +00:00
|
|
|
break;
|
2020-10-25 09:34:43 +00:00
|
|
|
case Value::ValueType::ddl_string:
|
|
|
|
in = parseStringLiteral(in, end, ¤t);
|
2016-01-08 15:46:13 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2015-03-09 09:17:23 +00:00
|
|
|
}
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
if (nullptr != current) {
|
|
|
|
if (nullptr == *data) {
|
2015-01-31 10:07:48 +00:00
|
|
|
*data = current;
|
|
|
|
prev = current;
|
|
|
|
} else {
|
2020-10-25 09:34:43 +00:00
|
|
|
prev->setNext(current);
|
2015-01-31 10:07:48 +00:00
|
|
|
prev = current;
|
|
|
|
}
|
2017-06-19 19:07:18 +00:00
|
|
|
++numValues;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
in = getNextSeparator(in, end);
|
|
|
|
if (',' != *in && Grammar::CloseBracketToken[0] != *in && !isSpace(*in)) {
|
2015-01-31 10:07:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-06-19 19:07:18 +00:00
|
|
|
++in;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
static DataArrayList *createDataArrayList(Value *currentValue, size_t numValues,
|
|
|
|
Reference *refs, size_t numRefs) {
|
|
|
|
DataArrayList *dataList(new DataArrayList);
|
2015-03-28 09:50:32 +00:00
|
|
|
dataList->m_dataList = currentValue;
|
|
|
|
dataList->m_numItems = numValues;
|
2020-10-25 09:34:43 +00:00
|
|
|
dataList->m_refs = refs;
|
|
|
|
dataList->m_numRefs = numRefs;
|
2015-03-28 09:50:32 +00:00
|
|
|
|
|
|
|
return dataList;
|
|
|
|
}
|
2016-02-07 16:58:28 +00:00
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
char *OpenDDLParser::parseDataArrayList(char *in, char *end, Value::ValueType type,
|
|
|
|
DataArrayList **dataArrayList) {
|
|
|
|
if (nullptr == dataArrayList) {
|
2016-06-28 11:13:08 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
*dataArrayList = nullptr;
|
|
|
|
if (nullptr == in || in == end) {
|
2015-01-31 10:07:48 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
in = lookForNextToken(in, end);
|
|
|
|
if (*in == Grammar::OpenBracketToken[0]) {
|
2017-06-19 19:07:18 +00:00
|
|
|
++in;
|
2020-10-25 09:34:43 +00:00
|
|
|
Value *currentValue(nullptr);
|
|
|
|
Reference *refs(nullptr);
|
|
|
|
DataArrayList *prev(nullptr), *currentDataList(nullptr);
|
2015-01-31 10:07:48 +00:00
|
|
|
do {
|
2020-10-25 09:34:43 +00:00
|
|
|
size_t numRefs(0), numValues(0);
|
|
|
|
currentValue = nullptr;
|
2016-01-08 15:46:13 +00:00
|
|
|
|
2020-10-25 09:34:43 +00:00
|
|
|
in = parseDataList(in, end, type, ¤tValue, numValues, &refs, numRefs);
|
|
|
|
if (nullptr != currentValue || 0 != numRefs) {
|
|
|
|
if (nullptr == prev) {
|
|
|
|
*dataArrayList = createDataArrayList(currentValue, numValues, refs, numRefs);
|
2016-02-07 16:58:28 +00:00
|
|
|
prev = *dataArrayList;
|
2015-01-31 10:07:48 +00:00
|
|
|
} else {
|
2020-10-25 09:34:43 +00:00
|
|
|
currentDataList = createDataArrayList(currentValue, numValues, refs, numRefs);
|
|
|
|
if (nullptr != prev) {
|
2015-01-31 10:07:48 +00:00
|
|
|
prev->m_next = currentDataList;
|
|
|
|
prev = currentDataList;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-25 09:34:43 +00:00
|
|
|
} while (Grammar::CommaSeparator[0] == *in && in != end);
|
|
|
|
in = lookForNextToken(in, end);
|
2017-06-19 19:07:18 +00:00
|
|
|
++in;
|
2015-01-31 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *OpenDDLParser::getVersion() {
|
|
|
|
return Version;
|
|
|
|
}
|
|
|
|
|
|
|
|
END_ODDLPARSER_NS
|