2
0

JsonParser.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * JsonParser.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "JsonParser.h"
  12. #include "JsonFormatException.h"
  13. #include "../TextOperations.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. JsonParser::JsonParser(const char * inputString, size_t stringSize, const JsonParsingSettings & settings):
  16. input(inputString, stringSize),
  17. settings(settings),
  18. lineCount(1),
  19. lineStart(0),
  20. pos(0)
  21. {
  22. }
  23. JsonNode JsonParser::parse(const std::string & fileName)
  24. {
  25. JsonNode root;
  26. if (input.size() == 0)
  27. {
  28. error("File is empty", false);
  29. }
  30. else
  31. {
  32. if (!TextOperations::isValidUnicodeString(&input[0], input.size()))
  33. error("Not a valid UTF-8 file", false);
  34. extractValue(root);
  35. extractWhitespace(false);
  36. //Warn if there are any non-whitespace symbols left
  37. if (pos < input.size())
  38. error("Not all file was parsed!", true);
  39. }
  40. if (!errors.empty())
  41. {
  42. logMod->warn("File %s is not a valid JSON file!", fileName);
  43. logMod->warn(errors);
  44. }
  45. return root;
  46. }
  47. bool JsonParser::isValid()
  48. {
  49. return errors.empty();
  50. }
  51. bool JsonParser::extractSeparator()
  52. {
  53. if (!extractWhitespace())
  54. return false;
  55. if ( input[pos] !=':')
  56. return error("Separator expected");
  57. pos++;
  58. return true;
  59. }
  60. bool JsonParser::extractValue(JsonNode &node)
  61. {
  62. if (!extractWhitespace())
  63. return false;
  64. switch (input[pos])
  65. {
  66. case '\"': return extractString(node);
  67. case 'n' : return extractNull(node);
  68. case 't' : return extractTrue(node);
  69. case 'f' : return extractFalse(node);
  70. case '{' : return extractStruct(node);
  71. case '[' : return extractArray(node);
  72. case '-' : return extractFloat(node);
  73. default:
  74. {
  75. if (input[pos] >= '0' && input[pos] <= '9')
  76. return extractFloat(node);
  77. return error("Value expected!");
  78. }
  79. }
  80. }
  81. bool JsonParser::extractWhitespace(bool verbose)
  82. {
  83. while (true)
  84. {
  85. while(pos < input.size() && static_cast<ui8>(input[pos]) <= ' ')
  86. {
  87. if (input[pos] == '\n')
  88. {
  89. lineCount++;
  90. lineStart = pos+1;
  91. }
  92. pos++;
  93. }
  94. if (pos >= input.size() || input[pos] != '/')
  95. break;
  96. if (settings.mode == JsonParsingSettings::JsonFormatMode::JSON)
  97. error("Comments are not permitted in json!", true);
  98. pos++;
  99. if (pos == input.size())
  100. break;
  101. if (input[pos] == '/')
  102. pos++;
  103. else
  104. error("Comments must consist of two slashes!", true);
  105. while (pos < input.size() && input[pos] != '\n')
  106. pos++;
  107. }
  108. if (pos >= input.size() && verbose)
  109. return error("Unexpected end of file!");
  110. return true;
  111. }
  112. bool JsonParser::extractEscaping(std::string &str)
  113. {
  114. switch(input[pos])
  115. {
  116. break; case '\"': str += '\"';
  117. break; case '\\': str += '\\';
  118. break; case 'b': str += '\b';
  119. break; case 'f': str += '\f';
  120. break; case 'n': str += '\n';
  121. break; case 'r': str += '\r';
  122. break; case 't': str += '\t';
  123. break; case '/': str += '/';
  124. break; default: return error("Unknown escape sequence!", true);
  125. }
  126. return true;
  127. }
  128. bool JsonParser::extractString(std::string &str)
  129. {
  130. if (input[pos] != '\"')
  131. return error("String expected!");
  132. pos++;
  133. size_t first = pos;
  134. while (pos != input.size())
  135. {
  136. if (input[pos] == '\"') // Correct end of string
  137. {
  138. str.append( &input[first], pos-first);
  139. pos++;
  140. return true;
  141. }
  142. if (input[pos] == '\\') // Escaping
  143. {
  144. str.append( &input[first], pos-first);
  145. pos++;
  146. if (pos == input.size())
  147. break;
  148. extractEscaping(str);
  149. first = pos + 1;
  150. }
  151. if (input[pos] == '\n') // end-of-line
  152. {
  153. str.append( &input[first], pos-first);
  154. return error("Closing quote not found!", true);
  155. }
  156. if(static_cast<unsigned char>(input[pos]) < ' ') // control character
  157. {
  158. str.append( &input[first], pos-first);
  159. first = pos+1;
  160. error("Illegal character in the string!", true);
  161. }
  162. pos++;
  163. }
  164. return error("Unterminated string!");
  165. }
  166. bool JsonParser::extractString(JsonNode &node)
  167. {
  168. std::string str;
  169. if (!extractString(str))
  170. return false;
  171. node.setType(JsonNode::JsonType::DATA_STRING);
  172. node.String() = str;
  173. return true;
  174. }
  175. bool JsonParser::extractLiteral(const std::string &literal)
  176. {
  177. if (literal.compare(0, literal.size(), &input[pos], literal.size()) != 0)
  178. {
  179. while (pos < input.size() && ((input[pos]>'a' && input[pos]<'z')
  180. || (input[pos]>'A' && input[pos]<'Z')))
  181. pos++;
  182. return error("Unknown literal found", true);
  183. }
  184. pos += literal.size();
  185. return true;
  186. }
  187. bool JsonParser::extractNull(JsonNode &node)
  188. {
  189. if (!extractLiteral("null"))
  190. return false;
  191. node.clear();
  192. return true;
  193. }
  194. bool JsonParser::extractTrue(JsonNode &node)
  195. {
  196. if (!extractLiteral("true"))
  197. return false;
  198. node.Bool() = true;
  199. return true;
  200. }
  201. bool JsonParser::extractFalse(JsonNode &node)
  202. {
  203. if (!extractLiteral("false"))
  204. return false;
  205. node.Bool() = false;
  206. return true;
  207. }
  208. bool JsonParser::extractStruct(JsonNode &node)
  209. {
  210. node.setType(JsonNode::JsonType::DATA_STRUCT);
  211. pos++;
  212. if (!extractWhitespace())
  213. return false;
  214. //Empty struct found
  215. if (input[pos] == '}')
  216. {
  217. pos++;
  218. return true;
  219. }
  220. while (true)
  221. {
  222. if (!extractWhitespace())
  223. return false;
  224. bool overrideFlag = false;
  225. std::string key;
  226. if (!extractString(key))
  227. return false;
  228. if (key.find('#') != std::string::npos)
  229. {
  230. // split key string into actual key and meta-flags
  231. std::vector<std::string> keyAndFlags;
  232. boost::split(keyAndFlags, key, boost::is_any_of("#"));
  233. key = keyAndFlags[0];
  234. for(int i = 1; i < keyAndFlags.size(); i++)
  235. {
  236. if (keyAndFlags[i] == "override")
  237. overrideFlag = true;
  238. else
  239. error("Encountered unknown flag #" + keyAndFlags[i], true);
  240. }
  241. }
  242. if (node.Struct().find(key) != node.Struct().end())
  243. error("Duplicate element encountered!", true);
  244. if (!extractSeparator())
  245. return false;
  246. if (!extractElement(node.Struct()[key], '}'))
  247. return false;
  248. node.Struct()[key].setOverrideFlag(overrideFlag);
  249. if (input[pos] == '}')
  250. {
  251. pos++;
  252. return true;
  253. }
  254. }
  255. }
  256. bool JsonParser::extractArray(JsonNode &node)
  257. {
  258. pos++;
  259. node.setType(JsonNode::JsonType::DATA_VECTOR);
  260. if (!extractWhitespace())
  261. return false;
  262. //Empty array found
  263. if (input[pos] == ']')
  264. {
  265. pos++;
  266. return true;
  267. }
  268. while (true)
  269. {
  270. //NOTE: currently 50% of time is this vector resizing.
  271. //May be useful to use list during parsing and then swap() all items to vector
  272. node.Vector().resize(node.Vector().size()+1);
  273. if (!extractElement(node.Vector().back(), ']'))
  274. return false;
  275. if (input[pos] == ']')
  276. {
  277. pos++;
  278. return true;
  279. }
  280. }
  281. }
  282. bool JsonParser::extractElement(JsonNode &node, char terminator)
  283. {
  284. if (!extractValue(node))
  285. return false;
  286. if (!extractWhitespace())
  287. return false;
  288. bool comma = (input[pos] == ',');
  289. if (comma )
  290. {
  291. pos++;
  292. if (!extractWhitespace())
  293. return false;
  294. }
  295. if (input[pos] == terminator)
  296. {
  297. if (comma)
  298. error("Extra comma found!", true);
  299. return true;
  300. }
  301. if (!comma)
  302. error("Comma expected!", true);
  303. return true;
  304. }
  305. bool JsonParser::extractFloat(JsonNode &node)
  306. {
  307. assert(input[pos] == '-' || (input[pos] >= '0' && input[pos] <= '9'));
  308. bool negative=false;
  309. double result=0;
  310. si64 integerPart = 0;
  311. bool isFloat = false;
  312. if (input[pos] == '-')
  313. {
  314. pos++;
  315. negative = true;
  316. }
  317. if (input[pos] < '0' || input[pos] > '9')
  318. return error("Number expected!");
  319. //Extract integer part
  320. while (input[pos] >= '0' && input[pos] <= '9')
  321. {
  322. integerPart = integerPart*10+(input[pos]-'0');
  323. pos++;
  324. }
  325. result = static_cast<double>(integerPart);
  326. if (input[pos] == '.')
  327. {
  328. //extract fractional part
  329. isFloat = true;
  330. pos++;
  331. double fractMult = 0.1;
  332. if (input[pos] < '0' || input[pos] > '9')
  333. return error("Decimal part expected!");
  334. while (input[pos] >= '0' && input[pos] <= '9')
  335. {
  336. result = result + fractMult*(input[pos]-'0');
  337. fractMult /= 10;
  338. pos++;
  339. }
  340. }
  341. if(input[pos] == 'e')
  342. {
  343. //extract exponential part
  344. pos++;
  345. isFloat = true;
  346. bool powerNegative = false;
  347. double power = 0;
  348. if(input[pos] == '-')
  349. {
  350. pos++;
  351. powerNegative = true;
  352. }
  353. else if(input[pos] == '+')
  354. {
  355. pos++;
  356. }
  357. if (input[pos] < '0' || input[pos] > '9')
  358. return error("Exponential part expected!");
  359. while (input[pos] >= '0' && input[pos] <= '9')
  360. {
  361. power = power*10 + (input[pos]-'0');
  362. pos++;
  363. }
  364. if(powerNegative)
  365. power = -power;
  366. result *= std::pow(10, power);
  367. }
  368. if(isFloat)
  369. {
  370. if(negative)
  371. result = -result;
  372. node.setType(JsonNode::JsonType::DATA_FLOAT);
  373. node.Float() = result;
  374. }
  375. else
  376. {
  377. if(negative)
  378. integerPart = -integerPart;
  379. node.setType(JsonNode::JsonType::DATA_INTEGER);
  380. node.Integer() = integerPart;
  381. }
  382. return true;
  383. }
  384. bool JsonParser::error(const std::string &message, bool warning)
  385. {
  386. if (settings.strict)
  387. throw JsonFormatException(message);
  388. std::ostringstream stream;
  389. std::string type(warning?" warning: ":" error: ");
  390. stream << "At line " << lineCount << ", position "<<pos-lineStart
  391. << type << message <<"\n";
  392. errors += stream.str();
  393. return warning;
  394. }
  395. VCMI_LIB_NAMESPACE_END