variant_construct_string.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*************************************************************************/
  2. /* variant_construct_string.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "variant.h"
  31. class VariantConstruct {
  32. enum TokenType {
  33. TK_CURLY_BRACKET_OPEN,
  34. TK_CURLY_BRACKET_CLOSE,
  35. TK_BRACKET_OPEN,
  36. TK_BRACKET_CLOSE,
  37. TK_IDENTIFIER,
  38. TK_STRING,
  39. TK_NUMBER,
  40. TK_COLON,
  41. TK_COMMA,
  42. TK_EOF,
  43. TK_MAX
  44. };
  45. enum Expecting {
  46. EXPECT_OBJECT,
  47. EXPECT_OBJECT_KEY,
  48. EXPECT_COLON,
  49. EXPECT_OBJECT_VALUE,
  50. };
  51. struct Token {
  52. TokenType type;
  53. Variant value;
  54. };
  55. static const char *tk_name[TK_MAX];
  56. static String _print_var(const Variant &p_var);
  57. static Error _get_token(const CharType *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str);
  58. static Error _parse_value(Variant &value, Token &token, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str, Variant::ObjectConstruct *p_construct, void *p_ud);
  59. static Error _parse_array(Array &array, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str, Variant::ObjectConstruct *p_construct, void *p_ud);
  60. static Error _parse_dict(Dictionary &object, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str, Variant::ObjectConstruct *p_construct, void *p_ud);
  61. public:
  62. static Error parse(const String &p_string, Variant &r_ret, String &r_err_str, int &r_err_line, Variant::ObjectConstruct *p_construct, void *p_ud);
  63. };
  64. const char *VariantConstruct::tk_name[TK_MAX] = {
  65. "'{'",
  66. "'}'",
  67. "'['",
  68. "']'",
  69. "identifier",
  70. "string",
  71. "number",
  72. "':'",
  73. "','",
  74. "EOF",
  75. };
  76. Error VariantConstruct::_get_token(const CharType *p_str, int &idx, int p_len, Token &r_token, int &line, String &r_err_str) {
  77. while (true) {
  78. switch (p_str[idx]) {
  79. case '\n': {
  80. line++;
  81. idx++;
  82. break;
  83. };
  84. case 0: {
  85. r_token.type = TK_EOF;
  86. return OK;
  87. } break;
  88. case '{': {
  89. r_token.type = TK_CURLY_BRACKET_OPEN;
  90. idx++;
  91. return OK;
  92. };
  93. case '}': {
  94. r_token.type = TK_CURLY_BRACKET_CLOSE;
  95. idx++;
  96. return OK;
  97. };
  98. case '[': {
  99. r_token.type = TK_BRACKET_OPEN;
  100. idx++;
  101. return OK;
  102. };
  103. case ']': {
  104. r_token.type = TK_BRACKET_CLOSE;
  105. idx++;
  106. return OK;
  107. };
  108. case ':': {
  109. r_token.type = TK_COLON;
  110. idx++;
  111. return OK;
  112. };
  113. case ',': {
  114. r_token.type = TK_COMMA;
  115. idx++;
  116. return OK;
  117. };
  118. case '"': {
  119. idx++;
  120. String str;
  121. while (true) {
  122. if (p_str[idx] == 0) {
  123. r_err_str = "Unterminated String";
  124. return ERR_PARSE_ERROR;
  125. } else if (p_str[idx] == '"') {
  126. idx++;
  127. break;
  128. } else if (p_str[idx] == '\\') {
  129. //escaped characters...
  130. idx++;
  131. CharType next = p_str[idx];
  132. if (next == 0) {
  133. r_err_str = "Unterminated String";
  134. return ERR_PARSE_ERROR;
  135. }
  136. CharType res = 0;
  137. switch (next) {
  138. case 'b': res = 8; break;
  139. case 't': res = 9; break;
  140. case 'n': res = 10; break;
  141. case 'f': res = 12; break;
  142. case 'r': res = 13; break;
  143. case '\"': res = '\"'; break;
  144. case '\\': res = '\\'; break;
  145. case '/': res = '/'; break;
  146. case 'u': {
  147. //hexnumbarh - oct is deprecated
  148. for (int j = 0; j < 4; j++) {
  149. CharType c = p_str[idx + j + 1];
  150. if (c == 0) {
  151. r_err_str = "Unterminated String";
  152. return ERR_PARSE_ERROR;
  153. }
  154. if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
  155. r_err_str = "Malformed hex constant in string";
  156. return ERR_PARSE_ERROR;
  157. }
  158. CharType v;
  159. if (c >= '0' && c <= '9') {
  160. v = c - '0';
  161. } else if (c >= 'a' && c <= 'f') {
  162. v = c - 'a';
  163. v += 10;
  164. } else if (c >= 'A' && c <= 'F') {
  165. v = c - 'A';
  166. v += 10;
  167. } else {
  168. ERR_PRINT("BUG");
  169. v = 0;
  170. }
  171. res <<= 4;
  172. res |= v;
  173. }
  174. idx += 4; //will add at the end anyway
  175. } break;
  176. default: {
  177. r_err_str = "Invalid escape sequence";
  178. return ERR_PARSE_ERROR;
  179. } break;
  180. }
  181. str += res;
  182. } else {
  183. if (p_str[idx] == '\n')
  184. line++;
  185. str += p_str[idx];
  186. }
  187. idx++;
  188. }
  189. r_token.type = TK_STRING;
  190. r_token.value = str;
  191. return OK;
  192. } break;
  193. default: {
  194. if (p_str[idx] <= 32) {
  195. idx++;
  196. break;
  197. }
  198. if (p_str[idx] == '-' || (p_str[idx] >= '0' && p_str[idx] <= '9')) {
  199. //a number
  200. const CharType *rptr;
  201. double number = String::to_double(&p_str[idx], &rptr);
  202. idx += (rptr - &p_str[idx]);
  203. r_token.type = TK_NUMBER;
  204. r_token.value = number;
  205. return OK;
  206. } else if ((p_str[idx] >= 'A' && p_str[idx] <= 'Z') || (p_str[idx] >= 'a' && p_str[idx] <= 'z')) {
  207. String id;
  208. while ((p_str[idx] >= 'A' && p_str[idx] <= 'Z') || (p_str[idx] >= 'a' && p_str[idx] <= 'z')) {
  209. id += p_str[idx];
  210. idx++;
  211. }
  212. r_token.type = TK_IDENTIFIER;
  213. r_token.value = id;
  214. return OK;
  215. } else {
  216. r_err_str = "Unexpected character.";
  217. return ERR_PARSE_ERROR;
  218. }
  219. }
  220. }
  221. }
  222. return ERR_PARSE_ERROR;
  223. }
  224. Error VariantConstruct::_parse_value(Variant &value, Token &token, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str, Variant::ObjectConstruct *p_construct, void *p_ud) {
  225. if (token.type == TK_CURLY_BRACKET_OPEN) {
  226. Dictionary d;
  227. Error err = _parse_dict(d, p_str, index, p_len, line, r_err_str, p_construct, p_ud);
  228. if (err)
  229. return err;
  230. value = d;
  231. return OK;
  232. } else if (token.type == TK_BRACKET_OPEN) {
  233. Array a;
  234. Error err = _parse_array(a, p_str, index, p_len, line, r_err_str, p_construct, p_ud);
  235. if (err)
  236. return err;
  237. value = a;
  238. return OK;
  239. } else if (token.type == TK_IDENTIFIER) {
  240. String id = token.value;
  241. if (id == "true")
  242. value = true;
  243. else if (id == "false")
  244. value = false;
  245. else if (id == "null")
  246. value = Variant();
  247. else {
  248. r_err_str = "Expected 'true','false' or 'null', got '" + id + "'.";
  249. return ERR_PARSE_ERROR;
  250. }
  251. return OK;
  252. } else if (token.type == TK_NUMBER) {
  253. value = token.value;
  254. return OK;
  255. } else if (token.type == TK_STRING) {
  256. value = token.value;
  257. return OK;
  258. } else {
  259. r_err_str = "Expected value, got " + String(tk_name[token.type]) + ".";
  260. return ERR_PARSE_ERROR;
  261. }
  262. return ERR_PARSE_ERROR;
  263. }
  264. Error VariantConstruct::_parse_array(Array &array, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str, Variant::ObjectConstruct *p_construct, void *p_ud) {
  265. Token token;
  266. bool need_comma = false;
  267. while (index < p_len) {
  268. Error err = _get_token(p_str, index, p_len, token, line, r_err_str);
  269. if (err != OK)
  270. return err;
  271. if (token.type == TK_BRACKET_CLOSE) {
  272. return OK;
  273. }
  274. if (need_comma) {
  275. if (token.type != TK_COMMA) {
  276. r_err_str = "Expected ','";
  277. return ERR_PARSE_ERROR;
  278. } else {
  279. need_comma = false;
  280. continue;
  281. }
  282. }
  283. Variant v;
  284. err = _parse_value(v, token, p_str, index, p_len, line, r_err_str, p_construct, p_ud);
  285. if (err)
  286. return err;
  287. array.push_back(v);
  288. need_comma = true;
  289. }
  290. return OK;
  291. }
  292. Error VariantConstruct::_parse_dict(Dictionary &dict, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str, Variant::ObjectConstruct *p_construct, void *p_ud) {
  293. bool at_key = true;
  294. Variant key;
  295. Token token;
  296. bool need_comma = false;
  297. while (index < p_len) {
  298. if (at_key) {
  299. Error err = _get_token(p_str, index, p_len, token, line, r_err_str);
  300. if (err != OK)
  301. return err;
  302. if (token.type == TK_CURLY_BRACKET_CLOSE) {
  303. return OK;
  304. }
  305. if (need_comma) {
  306. if (token.type != TK_COMMA) {
  307. r_err_str = "Expected '}' or ','";
  308. return ERR_PARSE_ERROR;
  309. } else {
  310. need_comma = false;
  311. continue;
  312. }
  313. }
  314. err = _parse_value(key, token, p_str, index, p_len, line, r_err_str, p_construct, p_ud);
  315. if (err != OK)
  316. return err;
  317. err = _get_token(p_str, index, p_len, token, line, r_err_str);
  318. if (err != OK)
  319. return err;
  320. if (token.type != TK_COLON) {
  321. r_err_str = "Expected ':'";
  322. return ERR_PARSE_ERROR;
  323. }
  324. at_key = false;
  325. } else {
  326. Error err = _get_token(p_str, index, p_len, token, line, r_err_str);
  327. if (err != OK)
  328. return err;
  329. Variant v;
  330. err = _parse_value(v, token, p_str, index, p_len, line, r_err_str, p_construct, p_ud);
  331. if (err)
  332. return err;
  333. dict[key] = v;
  334. need_comma = true;
  335. at_key = true;
  336. }
  337. }
  338. return OK;
  339. }
  340. Error VariantConstruct::parse(const String &p_string, Variant &r_ret, String &r_err_str, int &r_err_line, Variant::ObjectConstruct *p_construct, void *p_ud) {
  341. const CharType *str = p_string.ptr();
  342. int idx = 0;
  343. int len = p_string.length();
  344. Token token;
  345. r_err_line = 0;
  346. String aux_key;
  347. Error err = _get_token(str, idx, len, token, r_err_line, r_err_str);
  348. if (err)
  349. return err;
  350. return _parse_value(r_ret, token, str, idx, len, r_err_line, r_err_str, p_construct, p_ud);
  351. }