OpenDDLParser.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. /*-----------------------------------------------------------------------------------------------
  2. The MIT License (MIT)
  3. Copyright (c) 2014-2020 Kim Kulling
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. the Software, and to permit persons to whom the Software is furnished to do so,
  9. subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. -----------------------------------------------------------------------------------------------*/
  19. #include <openddlparser/OpenDDLExport.h>
  20. #include <openddlparser/OpenDDLParser.h>
  21. #include <math.h>
  22. #include <algorithm>
  23. #include <cassert>
  24. #include <iostream>
  25. #include <sstream>
  26. #ifdef _WIN32
  27. #include <windows.h>
  28. #endif // _WIN32
  29. BEGIN_ODDLPARSER_NS
  30. static const char *Version = "0.4.0";
  31. namespace Grammar {
  32. static const char *OpenBracketToken = "{";
  33. static const char *CloseBracketToken = "}";
  34. static const char *OpenPropertyToken = "(";
  35. static const char *ClosePropertyToken = ")";
  36. static const char *OpenArrayToken = "[";
  37. static const char *CloseArrayToken = "]";
  38. static const char *BoolTrue = "true";
  39. static const char *BoolFalse = "false";
  40. static const char *CommaSeparator = ",";
  41. static const char *PrimitiveTypeToken[(size_t)Value::ValueType::ddl_types_max] = {
  42. "bool",
  43. "int8",
  44. "int16",
  45. "int32",
  46. "int64",
  47. "unsigned_int8",
  48. "unsigned_int16",
  49. "unsigned_int32",
  50. "unsigned_int64",
  51. "half",
  52. "float",
  53. "double",
  54. "string",
  55. "ref"
  56. };
  57. } // Namespace Grammar
  58. const char *getTypeToken(Value::ValueType type) {
  59. return Grammar::PrimitiveTypeToken[(size_t)type];
  60. }
  61. static void logInvalidTokenError(char *in, const std::string &exp, OpenDDLParser::logCallback callback) {
  62. if (callback) {
  63. std::string full(in);
  64. std::string part(full.substr(0, 50));
  65. std::stringstream stream;
  66. stream << "Invalid token \"" << *in << "\" "
  67. << "(expected \"" << exp << "\") "
  68. << "in: \"" << part << "\"";
  69. callback(ddl_error_msg, stream.str());
  70. }
  71. }
  72. static bool isIntegerType(Value::ValueType integerType) {
  73. if (integerType != Value::ValueType::ddl_int8 && integerType != Value::ValueType::ddl_int16 &&
  74. integerType != Value::ValueType::ddl_int32 && integerType != Value::ValueType::ddl_int64) {
  75. return false;
  76. }
  77. return true;
  78. }
  79. static bool isUnsignedIntegerType(Value::ValueType integerType) {
  80. if (integerType != Value::ValueType::ddl_unsigned_int8 && integerType != Value::ValueType::ddl_unsigned_int16 &&
  81. integerType != Value::ValueType::ddl_unsigned_int32 && integerType != Value::ValueType::ddl_unsigned_int64) {
  82. return false;
  83. }
  84. return true;
  85. }
  86. static DDLNode *createDDLNode(Text *id, OpenDDLParser *parser) {
  87. if (nullptr == id || nullptr == parser || id->m_buffer == nullptr) {
  88. return nullptr;
  89. }
  90. const std::string type(id->m_buffer);
  91. DDLNode *parent(parser->top());
  92. DDLNode *node = DDLNode::create(type, "", parent);
  93. return node;
  94. }
  95. OpenDDLParser::OpenDDLParser() :
  96. m_logCallback(nullptr),
  97. m_buffer(),
  98. m_stack(),
  99. m_context(nullptr) {
  100. // empty
  101. }
  102. OpenDDLParser::OpenDDLParser(const char *buffer, size_t len) :
  103. m_logCallback(nullptr), m_buffer(), m_context(nullptr) {
  104. if (0 != len) {
  105. setBuffer(buffer, len);
  106. }
  107. }
  108. OpenDDLParser::~OpenDDLParser() {
  109. clear();
  110. }
  111. void OpenDDLParser::logToStream(FILE *f, LogSeverity severity, const std::string &message) {
  112. if (f) {
  113. const char *tag = "none";
  114. switch (severity) {
  115. case ddl_debug_msg: tag = "debug"; break;
  116. case ddl_info_msg: tag = "info"; break;
  117. case ddl_warn_msg: tag = "warn"; break;
  118. case ddl_error_msg: tag = "error"; break;
  119. }
  120. fprintf(f, "OpenDDLParser: (%5s) %s\n", tag, message.c_str());
  121. }
  122. }
  123. OpenDDLParser::logCallback OpenDDLParser::StdLogCallback (FILE *destination) {
  124. using namespace std::placeholders;
  125. return std::bind(logToStream, destination ? destination : stderr, _1, _2);
  126. }
  127. void OpenDDLParser::setLogCallback(logCallback callback) {
  128. // install user-specific log callback; null = no log callback
  129. m_logCallback = callback;
  130. }
  131. OpenDDLParser::logCallback OpenDDLParser::getLogCallback() const {
  132. return m_logCallback;
  133. }
  134. void OpenDDLParser::setBuffer(const char *buffer, size_t len) {
  135. clear();
  136. if (0 == len) {
  137. return;
  138. }
  139. m_buffer.resize(len);
  140. ::memcpy(&m_buffer[0], buffer, len);
  141. }
  142. void OpenDDLParser::setBuffer(const std::vector<char> &buffer) {
  143. clear();
  144. m_buffer.resize(buffer.size());
  145. std::copy(buffer.begin(), buffer.end(), m_buffer.begin());
  146. }
  147. const char *OpenDDLParser::getBuffer() const {
  148. if (m_buffer.empty()) {
  149. return nullptr;
  150. }
  151. return &m_buffer[0];
  152. }
  153. size_t OpenDDLParser::getBufferSize() const {
  154. return m_buffer.size();
  155. }
  156. void OpenDDLParser::clear() {
  157. m_buffer.resize(0);
  158. delete m_context;
  159. m_context = nullptr;
  160. }
  161. bool OpenDDLParser::validate() {
  162. if (m_buffer.empty()) {
  163. return true;
  164. }
  165. if (!isCharacter(m_buffer[0]) && !isNumeric(m_buffer[0])) {
  166. return false;
  167. }
  168. return true;
  169. }
  170. bool OpenDDLParser::parse() {
  171. if (m_buffer.empty()) {
  172. return false;
  173. }
  174. normalizeBuffer(m_buffer);
  175. if (!validate()) {
  176. return false;
  177. }
  178. m_context = new Context;
  179. m_context->m_root = DDLNode::create("root", "", nullptr);
  180. pushNode(m_context->m_root);
  181. // do the main parsing
  182. char *current(&m_buffer[0]);
  183. char *end(&m_buffer[m_buffer.size() - 1] + 1);
  184. size_t pos(current - &m_buffer[0]);
  185. while (pos < m_buffer.size()) {
  186. current = parseNextNode(current, end);
  187. if (current == nullptr) {
  188. return false;
  189. }
  190. pos = current - &m_buffer[0];
  191. }
  192. return true;
  193. }
  194. bool OpenDDLParser::exportContext(Context *ctx, const std::string &filename) {
  195. if (nullptr == ctx) {
  196. return false;
  197. }
  198. OpenDDLExport myExporter;
  199. return myExporter.exportContext(ctx, filename);
  200. }
  201. char *OpenDDLParser::parseNextNode(char *in, char *end) {
  202. in = parseHeader(in, end);
  203. in = parseStructure(in, end);
  204. return in;
  205. }
  206. #ifdef DEBUG_HEADER_NAME
  207. static void dumpId(Identifier *id) {
  208. if (nullptr != id) {
  209. if (nullptr != id->m_text.m_buffer) {
  210. std::cout << id->m_text.m_buffer << std::endl;
  211. }
  212. }
  213. }
  214. #endif
  215. char *OpenDDLParser::parseHeader(char *in, char *end) {
  216. if (nullptr == in || in == end) {
  217. return in;
  218. }
  219. Text *id(nullptr);
  220. in = OpenDDLParser::parseIdentifier(in, end, &id);
  221. #ifdef DEBUG_HEADER_NAME
  222. dumpId(id);
  223. #endif // DEBUG_HEADER_NAME
  224. in = lookForNextToken(in, end);
  225. if (nullptr != id) {
  226. // store the node
  227. DDLNode *node(createDDLNode(id, this));
  228. if (nullptr != node) {
  229. pushNode(node);
  230. } else {
  231. std::cerr << "nullptr returned by creating DDLNode." << std::endl;
  232. }
  233. delete id;
  234. Name *name(nullptr);
  235. in = OpenDDLParser::parseName(in, end, &name);
  236. if (nullptr != name && nullptr != node && nullptr != name->m_id->m_buffer) {
  237. const std::string nodeName(name->m_id->m_buffer);
  238. node->setName(nodeName);
  239. delete name;
  240. }
  241. Property *first(nullptr);
  242. in = lookForNextToken(in, end);
  243. if (in != end && *in == Grammar::OpenPropertyToken[0]) {
  244. in++;
  245. Property *prop(nullptr), *prev(nullptr);
  246. while (in != end && *in != Grammar::ClosePropertyToken[0]) {
  247. in = OpenDDLParser::parseProperty(in, end, &prop);
  248. in = lookForNextToken(in, end);
  249. if(in == end) {
  250. break;
  251. }
  252. if (*in != Grammar::CommaSeparator[0] && *in != Grammar::ClosePropertyToken[0]) {
  253. logInvalidTokenError(in, Grammar::ClosePropertyToken, m_logCallback);
  254. return nullptr;
  255. }
  256. if (nullptr != prop && *in != Grammar::CommaSeparator[0]) {
  257. if (nullptr == first) {
  258. first = prop;
  259. }
  260. if (nullptr != prev) {
  261. prev->m_next = prop;
  262. }
  263. prev = prop;
  264. }
  265. }
  266. if(in != end) {
  267. ++in;
  268. }
  269. }
  270. // set the properties
  271. if (nullptr != first && nullptr != node) {
  272. node->setProperties(first);
  273. }
  274. }
  275. return in;
  276. }
  277. char *OpenDDLParser::parseStructure(char *in, char *end) {
  278. if (nullptr == in || in == end) {
  279. return in;
  280. }
  281. bool error(false);
  282. in = lookForNextToken(in, end);
  283. if (*in == *Grammar::OpenBracketToken) {
  284. // loop over all children ( data and nodes )
  285. do {
  286. in = parseStructureBody(in, end, error);
  287. if (in == nullptr) {
  288. return nullptr;
  289. }
  290. } while (*in != *Grammar::CloseBracketToken);
  291. ++in;
  292. } else {
  293. ++in;
  294. logInvalidTokenError(in, std::string(Grammar::OpenBracketToken), m_logCallback);
  295. error = true;
  296. return nullptr;
  297. }
  298. in = lookForNextToken(in, end);
  299. // pop node from stack after successful parsing
  300. if (!error) {
  301. popNode();
  302. }
  303. return in;
  304. }
  305. static void setNodeValues(DDLNode *currentNode, Value *values) {
  306. if (nullptr != values) {
  307. if (nullptr != currentNode) {
  308. currentNode->setValue(values);
  309. }
  310. }
  311. }
  312. static void setNodeReferences(DDLNode *currentNode, Reference *refs) {
  313. if (nullptr != refs) {
  314. if (nullptr != currentNode) {
  315. currentNode->setReferences(refs);
  316. }
  317. }
  318. }
  319. static void setNodeDataArrayList(DDLNode *currentNode, DataArrayList *dtArrayList) {
  320. if (nullptr != dtArrayList) {
  321. if (nullptr != currentNode) {
  322. currentNode->setDataArrayList(dtArrayList);
  323. }
  324. }
  325. }
  326. char *OpenDDLParser::parseStructureBody(char *in, char *end, bool &error) {
  327. if (!isNumeric(*in) && !isCharacter(*in)) {
  328. ++in;
  329. }
  330. in = lookForNextToken(in, end);
  331. Value::ValueType type(Value::ValueType::ddl_none);
  332. size_t arrayLen(0);
  333. in = OpenDDLParser::parsePrimitiveDataType(in, end, type, arrayLen);
  334. if (Value::ValueType::ddl_none != type) {
  335. // parse a primitive data type
  336. in = lookForNextToken(in, end);
  337. if (*in == Grammar::OpenBracketToken[0]) {
  338. Reference *refs(nullptr);
  339. DataArrayList *dtArrayList(nullptr);
  340. Value *values(nullptr);
  341. if (1 == arrayLen) {
  342. size_t numRefs(0), numValues(0);
  343. in = parseDataList(in, end, type, &values, numValues, &refs, numRefs);
  344. setNodeValues(top(), values);
  345. setNodeReferences(top(), refs);
  346. } else if (arrayLen > 1) {
  347. in = parseDataArrayList(in, end, type, &dtArrayList);
  348. setNodeDataArrayList(top(), dtArrayList);
  349. } else {
  350. std::cerr << "0 for array is invalid." << std::endl;
  351. error = true;
  352. }
  353. }
  354. in = lookForNextToken(in, end);
  355. if (*in != '}') {
  356. logInvalidTokenError(in, std::string(Grammar::CloseBracketToken), m_logCallback);
  357. return nullptr;
  358. } else {
  359. //in++;
  360. }
  361. } else {
  362. // parse a complex data type
  363. in = parseNextNode(in, end);
  364. }
  365. return in;
  366. }
  367. void OpenDDLParser::pushNode(DDLNode *node) {
  368. if (nullptr == node) {
  369. return;
  370. }
  371. m_stack.push_back(node);
  372. }
  373. DDLNode *OpenDDLParser::popNode() {
  374. if (m_stack.empty()) {
  375. return nullptr;
  376. }
  377. DDLNode *topNode(top());
  378. m_stack.pop_back();
  379. return topNode;
  380. }
  381. DDLNode *OpenDDLParser::top() {
  382. if (m_stack.empty()) {
  383. return nullptr;
  384. }
  385. DDLNode *top(m_stack.back());
  386. return top;
  387. }
  388. DDLNode *OpenDDLParser::getRoot() const {
  389. if (nullptr == m_context) {
  390. return nullptr;
  391. }
  392. return m_context->m_root;
  393. }
  394. Context *OpenDDLParser::getContext() const {
  395. return m_context;
  396. }
  397. void OpenDDLParser::normalizeBuffer(std::vector<char> &buffer) {
  398. if (buffer.empty()) {
  399. return;
  400. }
  401. std::vector<char> newBuffer;
  402. const size_t len(buffer.size());
  403. char *end(&buffer[len - 1] + 1);
  404. for (size_t readIdx = 0; readIdx < len; ++readIdx) {
  405. char *c(&buffer[readIdx]);
  406. // check for a comment
  407. if (isCommentOpenTag(c, end)) {
  408. ++readIdx;
  409. while (readIdx < len && !isCommentCloseTag(&buffer[readIdx], end)) {
  410. ++readIdx;
  411. }
  412. ++readIdx;
  413. } else if (!isComment<char>(c, end) && !isNewLine(*c)) {
  414. newBuffer.push_back(buffer[readIdx]);
  415. } else {
  416. if (isComment<char>(c, end)) {
  417. ++readIdx;
  418. // skip the comment and the rest of the line
  419. while (readIdx < len && !isEndofLine(buffer[readIdx])) {
  420. ++readIdx;
  421. }
  422. }
  423. }
  424. }
  425. buffer = newBuffer;
  426. }
  427. char *OpenDDLParser::parseName(char *in, char *end, Name **name) {
  428. *name = nullptr;
  429. if (nullptr == in || in == end) {
  430. return in;
  431. }
  432. // ignore blanks
  433. in = lookForNextToken(in, end);
  434. if (*in != '$' && *in != '%') {
  435. return in;
  436. }
  437. NameType ntype(GlobalName);
  438. if (*in == '%') {
  439. ntype = LocalName;
  440. }
  441. in++;
  442. Name *currentName(nullptr);
  443. Text *id(nullptr);
  444. in = parseIdentifier(in, end, &id);
  445. if (id) {
  446. currentName = new Name(ntype, id);
  447. if (currentName) {
  448. *name = currentName;
  449. }
  450. }
  451. return in;
  452. }
  453. char *OpenDDLParser::parseIdentifier(char *in, char *end, Text **id) {
  454. *id = nullptr;
  455. if (nullptr == in || in == end) {
  456. return in;
  457. }
  458. // ignore blanks
  459. in = lookForNextToken(in, end);
  460. if (in == end) {
  461. return in;
  462. }
  463. // staring with a number is forbidden
  464. if (isNumeric<const char>(*in)) {
  465. return in;
  466. }
  467. // get size of id
  468. size_t idLen(0);
  469. char *start(in);
  470. while ((in != end) && !isSeparator(*in) && !isNewLine(*in) &&
  471. *in != Grammar::OpenPropertyToken[0] &&
  472. *in != Grammar::ClosePropertyToken[0] &&
  473. *in != '$') {
  474. ++in;
  475. ++idLen;
  476. }
  477. const size_t len(idLen);
  478. *id = new Text(start, len);
  479. return in;
  480. }
  481. char *OpenDDLParser::parsePrimitiveDataType(char *in, char *end, Value::ValueType &type, size_t &len) {
  482. type = Value::ValueType::ddl_none;
  483. len = 0;
  484. if (nullptr == in || in == end) {
  485. return in;
  486. }
  487. size_t prim_len(0);
  488. for (size_t i = 0; i < (size_t) Value::ValueType::ddl_types_max; i++) {
  489. prim_len = strlen(Grammar::PrimitiveTypeToken[i]);
  490. if (0 == strncmp(in, Grammar::PrimitiveTypeToken[i], prim_len)) {
  491. type = static_cast<Value::ValueType>(i);
  492. break;
  493. }
  494. }
  495. if (Value::ValueType::ddl_none == type) {
  496. in = lookForNextToken(in, end);
  497. return in;
  498. } else {
  499. in += prim_len;
  500. }
  501. bool ok(true);
  502. if (*in == Grammar::OpenArrayToken[0]) {
  503. ok = false;
  504. ++in;
  505. char *start(in);
  506. while (in != end) {
  507. ++in;
  508. if (*in == Grammar::CloseArrayToken[0]) {
  509. len = ::atoi(start);
  510. ok = true;
  511. ++in;
  512. break;
  513. }
  514. }
  515. } else {
  516. len = 1;
  517. }
  518. if (!ok) {
  519. type = Value::ValueType::ddl_none;
  520. }
  521. return in;
  522. }
  523. char *OpenDDLParser::parseReference(char *in, char *end, std::vector<Name *> &names) {
  524. if (nullptr == in || in == end) {
  525. return in;
  526. }
  527. Name *nextName(nullptr);
  528. in = parseName(in, end, &nextName);
  529. if (nextName) {
  530. names.push_back(nextName);
  531. }
  532. while (Grammar::CommaSeparator[0] == *in) {
  533. in = getNextSeparator(in, end);
  534. if (Grammar::CommaSeparator[0] == *in) {
  535. in = parseName(in, end, &nextName);
  536. if (nextName) {
  537. names.push_back(nextName);
  538. }
  539. } else {
  540. break;
  541. }
  542. }
  543. return in;
  544. }
  545. char *OpenDDLParser::parseBooleanLiteral(char *in, char *end, Value **boolean) {
  546. *boolean = nullptr;
  547. if (nullptr == in || in == end) {
  548. return in;
  549. }
  550. in = lookForNextToken(in, end);
  551. char *start(in);
  552. while (!isSeparator(*in) && in != end) {
  553. ++in;
  554. }
  555. int res = ::strncmp(Grammar::BoolTrue, start, strlen(Grammar::BoolTrue));
  556. if (0 != res) {
  557. res = ::strncmp(Grammar::BoolFalse, start, strlen(Grammar::BoolFalse));
  558. if (0 != res) {
  559. *boolean = nullptr;
  560. return in;
  561. }
  562. *boolean = ValueAllocator::allocPrimData(Value::ValueType::ddl_bool);
  563. (*boolean)->setBool(false);
  564. } else {
  565. *boolean = ValueAllocator::allocPrimData(Value::ValueType::ddl_bool);
  566. (*boolean)->setBool(true);
  567. }
  568. return in;
  569. }
  570. char *OpenDDLParser::parseIntegerLiteral(char *in, char *end, Value **integer, Value::ValueType integerType) {
  571. *integer = nullptr;
  572. if (nullptr == in || in == end) {
  573. return in;
  574. }
  575. if (!(isIntegerType(integerType) || isUnsignedIntegerType(integerType))) {
  576. return in;
  577. }
  578. in = lookForNextToken(in, end);
  579. char *start(in);
  580. while (!isSeparator(*in) && in != end) {
  581. ++in;
  582. }
  583. if (isNumeric(*start)) {
  584. #ifdef OPENDDL_NO_USE_CPP11
  585. const int64 value(atol(start)); // maybe not really 64bit as atoll is but exists without c++11
  586. const uint64 uvalue(strtoul(start, nullptr, 10));
  587. #else
  588. const int64 value(atoll(start));
  589. const uint64 uvalue(strtoull(start, nullptr, 10));
  590. #endif
  591. *integer = ValueAllocator::allocPrimData(integerType);
  592. switch (integerType) {
  593. case Value::ValueType::ddl_int8:
  594. (*integer)->setInt8((int8)value);
  595. break;
  596. case Value::ValueType::ddl_int16:
  597. (*integer)->setInt16((int16)value);
  598. break;
  599. case Value::ValueType::ddl_int32:
  600. (*integer)->setInt32((int32)value);
  601. break;
  602. case Value::ValueType::ddl_int64:
  603. (*integer)->setInt64((int64)value);
  604. break;
  605. case Value::ValueType::ddl_unsigned_int8:
  606. (*integer)->setUnsignedInt8((uint8)uvalue);
  607. break;
  608. case Value::ValueType::ddl_unsigned_int16:
  609. (*integer)->setUnsignedInt16((uint16)uvalue);
  610. break;
  611. case Value::ValueType::ddl_unsigned_int32:
  612. (*integer)->setUnsignedInt32((uint32)uvalue);
  613. break;
  614. case Value::ValueType::ddl_unsigned_int64:
  615. (*integer)->setUnsignedInt64((uint64)uvalue);
  616. break;
  617. default:
  618. break;
  619. }
  620. }
  621. return in;
  622. }
  623. char *OpenDDLParser::parseFloatingLiteral(char *in, char *end, Value **floating, Value::ValueType floatType) {
  624. *floating = nullptr;
  625. if (nullptr == in || in == end) {
  626. return in;
  627. }
  628. in = lookForNextToken(in, end);
  629. char *start(in);
  630. while (!isSeparator(*in) && in != end) {
  631. ++in;
  632. }
  633. // parse the float value
  634. bool ok(false);
  635. if (isHexLiteral(start, end)) {
  636. parseHexaLiteral(start, end, floating);
  637. return in;
  638. }
  639. if (isNumeric(*start)) {
  640. ok = true;
  641. } else {
  642. if (*start == '-') {
  643. if (isNumeric(*(start + 1))) {
  644. ok = true;
  645. }
  646. }
  647. }
  648. if (ok) {
  649. if (floatType == Value::ValueType::ddl_double) {
  650. const double value(atof(start));
  651. *floating = ValueAllocator::allocPrimData(Value::ValueType::ddl_double);
  652. (*floating)->setDouble(value);
  653. } else {
  654. const float value((float)atof(start));
  655. *floating = ValueAllocator::allocPrimData(Value::ValueType::ddl_float);
  656. (*floating)->setFloat(value);
  657. }
  658. }
  659. return in;
  660. }
  661. char *OpenDDLParser::parseStringLiteral(char *in, char *end, Value **stringData) {
  662. *stringData = nullptr;
  663. if (nullptr == in || in == end) {
  664. return in;
  665. }
  666. in = lookForNextToken(in, end);
  667. size_t len(0);
  668. char *start(in);
  669. if (*start == '\"') {
  670. ++start;
  671. ++in;
  672. while (*in != '\"' && in != end) {
  673. ++in;
  674. ++len;
  675. }
  676. *stringData = ValueAllocator::allocPrimData(Value::ValueType::ddl_string, len);
  677. ::strncpy((char *)(*stringData)->m_data, start, len);
  678. (*stringData)->m_data[len] = '\0';
  679. ++in;
  680. }
  681. return in;
  682. }
  683. static void createPropertyWithData(Text *id, Value *primData, Property **prop) {
  684. if (nullptr != primData) {
  685. (*prop) = new Property(id);
  686. (*prop)->m_value = primData;
  687. }
  688. }
  689. char *OpenDDLParser::parseHexaLiteral(char *in, char *end, Value **data) {
  690. *data = nullptr;
  691. if (nullptr == in || in == end) {
  692. return in;
  693. }
  694. in = lookForNextToken(in, end);
  695. if (*in != '0') {
  696. return in;
  697. }
  698. ++in;
  699. if (*in != 'x' && *in != 'X') {
  700. return in;
  701. }
  702. ++in;
  703. bool ok(true);
  704. char *start(in);
  705. int pos(0);
  706. while (!isSeparator(*in) && in != end) {
  707. if ((*in < '0' && *in > '9') || (*in < 'a' && *in > 'f') || (*in < 'A' && *in > 'F')) {
  708. ok = false;
  709. break;
  710. }
  711. ++pos;
  712. ++in;
  713. }
  714. if (!ok) {
  715. return in;
  716. }
  717. int value(0);
  718. while (pos > 0) {
  719. int v = hex2Decimal(*start);
  720. --pos;
  721. value = (value << 4) | v;
  722. ++start;
  723. }
  724. *data = ValueAllocator::allocPrimData(Value::ValueType::ddl_unsigned_int64);
  725. if (nullptr != *data) {
  726. (*data)->setUnsignedInt64(value);
  727. }
  728. return in;
  729. }
  730. char *OpenDDLParser::parseProperty(char *in, char *end, Property **prop) {
  731. *prop = nullptr;
  732. if (nullptr == in || in == end) {
  733. return in;
  734. }
  735. in = lookForNextToken(in, end);
  736. Text *id = nullptr;
  737. in = parseIdentifier(in, end, &id);
  738. if (nullptr != id) {
  739. in = lookForNextToken(in, end);
  740. if (in != end && *in == '=') {
  741. ++in;
  742. in = getNextToken(in, end);
  743. Value *primData(nullptr);
  744. if (isInteger(in, end)) {
  745. in = parseIntegerLiteral(in, end, &primData);
  746. createPropertyWithData(id, primData, prop);
  747. } else if (isFloat(in, end)) {
  748. in = parseFloatingLiteral(in, end, &primData);
  749. createPropertyWithData(id, primData, prop);
  750. } else if (isStringLiteral(*in)) { // string data
  751. in = parseStringLiteral(in, end, &primData);
  752. createPropertyWithData(id, primData, prop);
  753. } else { // reference data
  754. std::vector<Name *> names;
  755. in = parseReference(in, end, names);
  756. if (!names.empty()) {
  757. Reference *ref = new Reference(names.size(), &names[0]);
  758. (*prop) = new Property(id);
  759. (*prop)->m_ref = ref;
  760. }
  761. }
  762. } else {
  763. delete id;
  764. }
  765. }
  766. return in;
  767. }
  768. char *OpenDDLParser::parseDataList(char *in, char *end, Value::ValueType type, Value **data,
  769. size_t &numValues, Reference **refs, size_t &numRefs) {
  770. *data = nullptr;
  771. numValues = numRefs = 0;
  772. if (nullptr == in || in == end) {
  773. return in;
  774. }
  775. in = lookForNextToken(in, end);
  776. if (*in == '{') {
  777. ++in;
  778. Value *current(nullptr), *prev(nullptr);
  779. while ('}' != *in) {
  780. current = nullptr;
  781. in = lookForNextToken(in, end);
  782. if (Value::ValueType::ddl_ref == type) {
  783. std::vector<Name *> names;
  784. in = parseReference(in, end, names);
  785. if (!names.empty()) {
  786. Reference *ref = new Reference(names.size(), &names[0]);
  787. *refs = ref;
  788. numRefs = names.size();
  789. }
  790. } else if (Value::ValueType::ddl_none == type) {
  791. if (isInteger(in, end)) {
  792. in = parseIntegerLiteral(in, end, &current);
  793. } else if (isFloat(in, end)) {
  794. in = parseFloatingLiteral(in, end, &current);
  795. } else if (isStringLiteral(*in)) {
  796. in = parseStringLiteral(in, end, &current);
  797. } else if (isHexLiteral(in, end)) {
  798. in = parseHexaLiteral(in, end, &current);
  799. }
  800. } else {
  801. switch (type) {
  802. case Value::ValueType::ddl_int8:
  803. case Value::ValueType::ddl_int16:
  804. case Value::ValueType::ddl_int32:
  805. case Value::ValueType::ddl_int64:
  806. case Value::ValueType::ddl_unsigned_int8:
  807. case Value::ValueType::ddl_unsigned_int16:
  808. case Value::ValueType::ddl_unsigned_int32:
  809. case Value::ValueType::ddl_unsigned_int64:
  810. in = parseIntegerLiteral(in, end, &current, type);
  811. break;
  812. case Value::ValueType::ddl_half:
  813. case Value::ValueType::ddl_float:
  814. case Value::ValueType::ddl_double:
  815. in = parseFloatingLiteral(in, end, &current, type);
  816. break;
  817. case Value::ValueType::ddl_string:
  818. in = parseStringLiteral(in, end, &current);
  819. break;
  820. default:
  821. break;
  822. }
  823. }
  824. if (nullptr != current) {
  825. if (nullptr == *data) {
  826. *data = current;
  827. prev = current;
  828. } else {
  829. prev->setNext(current);
  830. prev = current;
  831. }
  832. ++numValues;
  833. }
  834. in = getNextSeparator(in, end);
  835. if (',' != *in && Grammar::CloseBracketToken[0] != *in && !isSpace(*in)) {
  836. break;
  837. }
  838. }
  839. ++in;
  840. }
  841. return in;
  842. }
  843. static DataArrayList *createDataArrayList(Value *currentValue, size_t numValues,
  844. Reference *refs, size_t numRefs) {
  845. DataArrayList *dataList(new DataArrayList);
  846. dataList->m_dataList = currentValue;
  847. dataList->m_numItems = numValues;
  848. dataList->m_refs = refs;
  849. dataList->m_numRefs = numRefs;
  850. return dataList;
  851. }
  852. char *OpenDDLParser::parseDataArrayList(char *in, char *end, Value::ValueType type,
  853. DataArrayList **dataArrayList) {
  854. if (nullptr == dataArrayList) {
  855. return in;
  856. }
  857. *dataArrayList = nullptr;
  858. if (nullptr == in || in == end) {
  859. return in;
  860. }
  861. in = lookForNextToken(in, end);
  862. if (*in == Grammar::OpenBracketToken[0]) {
  863. ++in;
  864. Value *currentValue(nullptr);
  865. Reference *refs(nullptr);
  866. DataArrayList *prev(nullptr), *currentDataList(nullptr);
  867. do {
  868. size_t numRefs(0), numValues(0);
  869. currentValue = nullptr;
  870. in = parseDataList(in, end, type, &currentValue, numValues, &refs, numRefs);
  871. if (nullptr != currentValue || 0 != numRefs) {
  872. if (nullptr == prev) {
  873. *dataArrayList = createDataArrayList(currentValue, numValues, refs, numRefs);
  874. prev = *dataArrayList;
  875. } else {
  876. currentDataList = createDataArrayList(currentValue, numValues, refs, numRefs);
  877. if (nullptr != prev) {
  878. prev->m_next = currentDataList;
  879. prev = currentDataList;
  880. }
  881. }
  882. }
  883. } while (Grammar::CommaSeparator[0] == *in && in != end);
  884. in = lookForNextToken(in, end);
  885. ++in;
  886. }
  887. return in;
  888. }
  889. const char *OpenDDLParser::getVersion() {
  890. return Version;
  891. }
  892. END_ODDLPARSER_NS