variant_parser.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926
  1. /*************************************************************************/
  2. /* variant_parser.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_parser.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/os/input_event.h"
  33. #include "core/os/keyboard.h"
  34. #include "core/string_buffer.h"
  35. CharType VariantParser::StreamFile::get_char() {
  36. return f->get_8();
  37. }
  38. bool VariantParser::StreamFile::is_utf8() const {
  39. return true;
  40. }
  41. bool VariantParser::StreamFile::is_eof() const {
  42. return f->eof_reached();
  43. }
  44. CharType VariantParser::StreamString::get_char() {
  45. if (pos > s.length()) {
  46. return 0;
  47. } else if (pos == s.length()) {
  48. // You need to try to read again when you have reached the end for EOF to be reported,
  49. // so this works the same as files (like StreamFile does)
  50. pos++;
  51. return 0;
  52. } else {
  53. return s[pos++];
  54. }
  55. }
  56. bool VariantParser::StreamString::is_utf8() const {
  57. return false;
  58. }
  59. bool VariantParser::StreamString::is_eof() const {
  60. return pos > s.length();
  61. }
  62. /////////////////////////////////////////////////////////////////////////////////////////////////
  63. const char *VariantParser::tk_name[TK_MAX] = {
  64. "'{'",
  65. "'}'",
  66. "'['",
  67. "']'",
  68. "'('",
  69. "')'",
  70. "identifier",
  71. "string",
  72. "number",
  73. "color",
  74. "':'",
  75. "','",
  76. "'.'",
  77. "'='",
  78. "EOF",
  79. "ERROR"
  80. };
  81. Error VariantParser::get_token(Stream *p_stream, Token &r_token, int &line, String &r_err_str) {
  82. while (true) {
  83. CharType cchar;
  84. if (p_stream->saved) {
  85. cchar = p_stream->saved;
  86. p_stream->saved = 0;
  87. } else {
  88. cchar = p_stream->get_char();
  89. if (p_stream->is_eof()) {
  90. r_token.type = TK_EOF;
  91. return OK;
  92. }
  93. }
  94. switch (cchar) {
  95. case '\n': {
  96. line++;
  97. break;
  98. };
  99. case 0: {
  100. r_token.type = TK_EOF;
  101. return OK;
  102. } break;
  103. case '{': {
  104. r_token.type = TK_CURLY_BRACKET_OPEN;
  105. return OK;
  106. };
  107. case '}': {
  108. r_token.type = TK_CURLY_BRACKET_CLOSE;
  109. return OK;
  110. };
  111. case '[': {
  112. r_token.type = TK_BRACKET_OPEN;
  113. return OK;
  114. };
  115. case ']': {
  116. r_token.type = TK_BRACKET_CLOSE;
  117. return OK;
  118. };
  119. case '(': {
  120. r_token.type = TK_PARENTHESIS_OPEN;
  121. return OK;
  122. };
  123. case ')': {
  124. r_token.type = TK_PARENTHESIS_CLOSE;
  125. return OK;
  126. };
  127. case ':': {
  128. r_token.type = TK_COLON;
  129. return OK;
  130. };
  131. case ';': {
  132. while (true) {
  133. CharType ch = p_stream->get_char();
  134. if (p_stream->is_eof()) {
  135. r_token.type = TK_EOF;
  136. return OK;
  137. }
  138. if (ch == '\n')
  139. break;
  140. }
  141. break;
  142. };
  143. case ',': {
  144. r_token.type = TK_COMMA;
  145. return OK;
  146. };
  147. case '.': {
  148. r_token.type = TK_PERIOD;
  149. return OK;
  150. };
  151. case '=': {
  152. r_token.type = TK_EQUAL;
  153. return OK;
  154. };
  155. case '#': {
  156. StringBuffer<> color_str;
  157. color_str += '#';
  158. while (true) {
  159. CharType ch = p_stream->get_char();
  160. if (p_stream->is_eof()) {
  161. r_token.type = TK_EOF;
  162. return OK;
  163. } else if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')) {
  164. color_str += ch;
  165. } else {
  166. p_stream->saved = ch;
  167. break;
  168. }
  169. }
  170. r_token.value = Color::html(color_str.as_string());
  171. r_token.type = TK_COLOR;
  172. return OK;
  173. };
  174. case '"': {
  175. String str;
  176. while (true) {
  177. CharType ch = p_stream->get_char();
  178. if (ch == 0) {
  179. r_err_str = "Unterminated String";
  180. r_token.type = TK_ERROR;
  181. return ERR_PARSE_ERROR;
  182. } else if (ch == '"') {
  183. break;
  184. } else if (ch == '\\') {
  185. //escaped characters...
  186. CharType next = p_stream->get_char();
  187. if (next == 0) {
  188. r_err_str = "Unterminated String";
  189. r_token.type = TK_ERROR;
  190. return ERR_PARSE_ERROR;
  191. }
  192. CharType res = 0;
  193. switch (next) {
  194. case 'b': res = 8; break;
  195. case 't': res = 9; break;
  196. case 'n': res = 10; break;
  197. case 'f': res = 12; break;
  198. case 'r': res = 13; break;
  199. case 'u': {
  200. //hexnumbarh - oct is deprecated
  201. for (int j = 0; j < 4; j++) {
  202. CharType c = p_stream->get_char();
  203. if (c == 0) {
  204. r_err_str = "Unterminated String";
  205. r_token.type = TK_ERROR;
  206. return ERR_PARSE_ERROR;
  207. }
  208. if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
  209. r_err_str = "Malformed hex constant in string";
  210. r_token.type = TK_ERROR;
  211. return ERR_PARSE_ERROR;
  212. }
  213. CharType v;
  214. if (c >= '0' && c <= '9') {
  215. v = c - '0';
  216. } else if (c >= 'a' && c <= 'f') {
  217. v = c - 'a';
  218. v += 10;
  219. } else if (c >= 'A' && c <= 'F') {
  220. v = c - 'A';
  221. v += 10;
  222. } else {
  223. ERR_PRINT("BUG");
  224. v = 0;
  225. }
  226. res <<= 4;
  227. res |= v;
  228. }
  229. } break;
  230. //case '\"': res='\"'; break;
  231. //case '\\': res='\\'; break;
  232. //case '/': res='/'; break;
  233. default: {
  234. res = next;
  235. //r_err_str="Invalid escape sequence";
  236. //return ERR_PARSE_ERROR;
  237. } break;
  238. }
  239. str += res;
  240. } else {
  241. if (ch == '\n')
  242. line++;
  243. str += ch;
  244. }
  245. }
  246. if (p_stream->is_utf8()) {
  247. str.parse_utf8(str.ascii(true).get_data());
  248. }
  249. r_token.type = TK_STRING;
  250. r_token.value = str;
  251. return OK;
  252. } break;
  253. default: {
  254. if (cchar <= 32) {
  255. break;
  256. }
  257. if (cchar == '-' || (cchar >= '0' && cchar <= '9')) {
  258. //a number
  259. StringBuffer<> num;
  260. #define READING_SIGN 0
  261. #define READING_INT 1
  262. #define READING_DEC 2
  263. #define READING_EXP 3
  264. #define READING_DONE 4
  265. int reading = READING_INT;
  266. if (cchar == '-') {
  267. num += '-';
  268. cchar = p_stream->get_char();
  269. }
  270. CharType c = cchar;
  271. bool exp_sign = false;
  272. bool exp_beg = false;
  273. bool is_float = false;
  274. while (true) {
  275. switch (reading) {
  276. case READING_INT: {
  277. if (c >= '0' && c <= '9') {
  278. //pass
  279. } else if (c == '.') {
  280. reading = READING_DEC;
  281. is_float = true;
  282. } else if (c == 'e') {
  283. reading = READING_EXP;
  284. is_float = true;
  285. } else {
  286. reading = READING_DONE;
  287. }
  288. } break;
  289. case READING_DEC: {
  290. if (c >= '0' && c <= '9') {
  291. } else if (c == 'e') {
  292. reading = READING_EXP;
  293. } else {
  294. reading = READING_DONE;
  295. }
  296. } break;
  297. case READING_EXP: {
  298. if (c >= '0' && c <= '9') {
  299. exp_beg = true;
  300. } else if ((c == '-' || c == '+') && !exp_sign && !exp_beg) {
  301. exp_sign = true;
  302. } else {
  303. reading = READING_DONE;
  304. }
  305. } break;
  306. }
  307. if (reading == READING_DONE)
  308. break;
  309. num += c;
  310. c = p_stream->get_char();
  311. }
  312. p_stream->saved = c;
  313. r_token.type = TK_NUMBER;
  314. if (is_float)
  315. r_token.value = num.as_double();
  316. else
  317. r_token.value = num.as_int();
  318. return OK;
  319. } else if ((cchar >= 'A' && cchar <= 'Z') || (cchar >= 'a' && cchar <= 'z') || cchar == '_') {
  320. StringBuffer<> id;
  321. bool first = true;
  322. while ((cchar >= 'A' && cchar <= 'Z') || (cchar >= 'a' && cchar <= 'z') || cchar == '_' || (!first && cchar >= '0' && cchar <= '9')) {
  323. id += cchar;
  324. cchar = p_stream->get_char();
  325. first = false;
  326. }
  327. p_stream->saved = cchar;
  328. r_token.type = TK_IDENTIFIER;
  329. r_token.value = id.as_string();
  330. return OK;
  331. } else {
  332. r_err_str = "Unexpected character.";
  333. r_token.type = TK_ERROR;
  334. return ERR_PARSE_ERROR;
  335. }
  336. }
  337. }
  338. }
  339. r_token.type = TK_ERROR;
  340. return ERR_PARSE_ERROR;
  341. }
  342. Error VariantParser::_parse_enginecfg(Stream *p_stream, Vector<String> &strings, int &line, String &r_err_str) {
  343. Token token;
  344. get_token(p_stream, token, line, r_err_str);
  345. if (token.type != TK_PARENTHESIS_OPEN) {
  346. r_err_str = "Expected '(' in old-style project.godot construct";
  347. return ERR_PARSE_ERROR;
  348. }
  349. String accum;
  350. while (true) {
  351. CharType c = p_stream->get_char();
  352. if (p_stream->is_eof()) {
  353. r_err_str = "Unexpected EOF while parsing old-style project.godot construct";
  354. return ERR_PARSE_ERROR;
  355. }
  356. if (c == ',') {
  357. strings.push_back(accum.strip_edges());
  358. accum = String();
  359. } else if (c == ')') {
  360. strings.push_back(accum.strip_edges());
  361. return OK;
  362. } else if (c == '\n') {
  363. line++;
  364. }
  365. }
  366. }
  367. template <class T>
  368. Error VariantParser::_parse_construct(Stream *p_stream, Vector<T> &r_construct, int &line, String &r_err_str) {
  369. Token token;
  370. get_token(p_stream, token, line, r_err_str);
  371. if (token.type != TK_PARENTHESIS_OPEN) {
  372. r_err_str = "Expected '(' in constructor";
  373. return ERR_PARSE_ERROR;
  374. }
  375. bool first = true;
  376. while (true) {
  377. if (!first) {
  378. get_token(p_stream, token, line, r_err_str);
  379. if (token.type == TK_COMMA) {
  380. //do none
  381. } else if (token.type == TK_PARENTHESIS_CLOSE) {
  382. break;
  383. } else {
  384. r_err_str = "Expected ',' or ')' in constructor";
  385. return ERR_PARSE_ERROR;
  386. }
  387. }
  388. get_token(p_stream, token, line, r_err_str);
  389. if (first && token.type == TK_PARENTHESIS_CLOSE) {
  390. break;
  391. } else if (token.type != TK_NUMBER) {
  392. r_err_str = "Expected float in constructor";
  393. return ERR_PARSE_ERROR;
  394. }
  395. r_construct.push_back(token.value);
  396. first = false;
  397. }
  398. return OK;
  399. }
  400. Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  401. if (token.type == TK_CURLY_BRACKET_OPEN) {
  402. Dictionary d;
  403. Error err = _parse_dictionary(d, p_stream, line, r_err_str, p_res_parser);
  404. if (err)
  405. return err;
  406. value = d;
  407. return OK;
  408. } else if (token.type == TK_BRACKET_OPEN) {
  409. Array a;
  410. Error err = _parse_array(a, p_stream, line, r_err_str, p_res_parser);
  411. if (err)
  412. return err;
  413. value = a;
  414. return OK;
  415. } else if (token.type == TK_IDENTIFIER) {
  416. String id = token.value;
  417. if (id == "true")
  418. value = true;
  419. else if (id == "false")
  420. value = false;
  421. else if (id == "null" || id == "nil")
  422. value = Variant();
  423. else if (id == "inf")
  424. value = Math_INF;
  425. else if (id == "nan")
  426. value = Math_NAN;
  427. else if (id == "Vector2") {
  428. Vector<float> args;
  429. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  430. if (err)
  431. return err;
  432. if (args.size() != 2) {
  433. r_err_str = "Expected 2 arguments for constructor";
  434. return ERR_PARSE_ERROR;
  435. }
  436. value = Vector2(args[0], args[1]);
  437. } else if (id == "Rect2") {
  438. Vector<float> args;
  439. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  440. if (err)
  441. return err;
  442. if (args.size() != 4) {
  443. r_err_str = "Expected 4 arguments for constructor";
  444. return ERR_PARSE_ERROR;
  445. }
  446. value = Rect2(args[0], args[1], args[2], args[3]);
  447. } else if (id == "Vector3") {
  448. Vector<float> args;
  449. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  450. if (err)
  451. return err;
  452. if (args.size() != 3) {
  453. r_err_str = "Expected 3 arguments for constructor";
  454. return ERR_PARSE_ERROR;
  455. }
  456. value = Vector3(args[0], args[1], args[2]);
  457. } else if (id == "Transform2D" || id == "Matrix32") { //compatibility
  458. Vector<float> args;
  459. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  460. if (err)
  461. return err;
  462. if (args.size() != 6) {
  463. r_err_str = "Expected 6 arguments for constructor";
  464. return ERR_PARSE_ERROR;
  465. }
  466. Transform2D m;
  467. m[0] = Vector2(args[0], args[1]);
  468. m[1] = Vector2(args[2], args[3]);
  469. m[2] = Vector2(args[4], args[5]);
  470. value = m;
  471. } else if (id == "Plane") {
  472. Vector<float> args;
  473. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  474. if (err)
  475. return err;
  476. if (args.size() != 4) {
  477. r_err_str = "Expected 4 arguments for constructor";
  478. return ERR_PARSE_ERROR;
  479. }
  480. value = Plane(args[0], args[1], args[2], args[3]);
  481. } else if (id == "Quat") {
  482. Vector<float> args;
  483. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  484. if (err)
  485. return err;
  486. if (args.size() != 4) {
  487. r_err_str = "Expected 4 arguments for constructor";
  488. return ERR_PARSE_ERROR;
  489. }
  490. value = Quat(args[0], args[1], args[2], args[3]);
  491. } else if (id == "AABB" || id == "Rect3") {
  492. Vector<float> args;
  493. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  494. if (err)
  495. return err;
  496. if (args.size() != 6) {
  497. r_err_str = "Expected 6 arguments for constructor";
  498. return ERR_PARSE_ERROR;
  499. }
  500. value = AABB(Vector3(args[0], args[1], args[2]), Vector3(args[3], args[4], args[5]));
  501. } else if (id == "Basis" || id == "Matrix3") { //compatibility
  502. Vector<float> args;
  503. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  504. if (err)
  505. return err;
  506. if (args.size() != 9) {
  507. r_err_str = "Expected 9 arguments for constructor";
  508. return ERR_PARSE_ERROR;
  509. }
  510. value = Basis(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
  511. } else if (id == "Transform") {
  512. Vector<float> args;
  513. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  514. if (err)
  515. return err;
  516. if (args.size() != 12) {
  517. r_err_str = "Expected 12 arguments for constructor";
  518. return ERR_PARSE_ERROR;
  519. }
  520. value = Transform(Basis(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]), Vector3(args[9], args[10], args[11]));
  521. } else if (id == "Color") {
  522. Vector<float> args;
  523. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  524. if (err)
  525. return err;
  526. if (args.size() != 4) {
  527. r_err_str = "Expected 4 arguments for constructor";
  528. return ERR_PARSE_ERROR;
  529. }
  530. value = Color(args[0], args[1], args[2], args[3]);
  531. } else if (id == "NodePath") {
  532. get_token(p_stream, token, line, r_err_str);
  533. if (token.type != TK_PARENTHESIS_OPEN) {
  534. r_err_str = "Expected '('";
  535. return ERR_PARSE_ERROR;
  536. }
  537. get_token(p_stream, token, line, r_err_str);
  538. if (token.type != TK_STRING) {
  539. r_err_str = "Expected string as argument for NodePath()";
  540. return ERR_PARSE_ERROR;
  541. }
  542. value = NodePath(String(token.value));
  543. get_token(p_stream, token, line, r_err_str);
  544. if (token.type != TK_PARENTHESIS_CLOSE) {
  545. r_err_str = "Expected ')'";
  546. return ERR_PARSE_ERROR;
  547. }
  548. } else if (id == "RID") {
  549. get_token(p_stream, token, line, r_err_str);
  550. if (token.type != TK_PARENTHESIS_OPEN) {
  551. r_err_str = "Expected '('";
  552. return ERR_PARSE_ERROR;
  553. }
  554. get_token(p_stream, token, line, r_err_str);
  555. if (token.type != TK_NUMBER) {
  556. r_err_str = "Expected number as argument";
  557. return ERR_PARSE_ERROR;
  558. }
  559. value = token.value;
  560. get_token(p_stream, token, line, r_err_str);
  561. if (token.type != TK_PARENTHESIS_CLOSE) {
  562. r_err_str = "Expected ')'";
  563. return ERR_PARSE_ERROR;
  564. }
  565. } else if (id == "Object") {
  566. get_token(p_stream, token, line, r_err_str);
  567. if (token.type != TK_PARENTHESIS_OPEN) {
  568. r_err_str = "Expected '('";
  569. return ERR_PARSE_ERROR;
  570. }
  571. get_token(p_stream, token, line, r_err_str);
  572. if (token.type != TK_IDENTIFIER) {
  573. r_err_str = "Expected identifier with type of object";
  574. return ERR_PARSE_ERROR;
  575. }
  576. String type = token.value;
  577. Object *obj = ClassDB::instance(type);
  578. if (!obj) {
  579. r_err_str = "Can't instance Object() of type: " + type;
  580. return ERR_PARSE_ERROR;
  581. }
  582. REF ref = REF(Object::cast_to<Reference>(obj));
  583. get_token(p_stream, token, line, r_err_str);
  584. if (token.type != TK_COMMA) {
  585. r_err_str = "Expected ',' after object type";
  586. return ERR_PARSE_ERROR;
  587. }
  588. bool at_key = true;
  589. String key;
  590. Token token2;
  591. bool need_comma = false;
  592. while (true) {
  593. if (p_stream->is_eof()) {
  594. r_err_str = "Unexpected End of File while parsing Object()";
  595. return ERR_FILE_CORRUPT;
  596. }
  597. if (at_key) {
  598. Error err = get_token(p_stream, token2, line, r_err_str);
  599. if (err != OK)
  600. return err;
  601. if (token2.type == TK_PARENTHESIS_CLOSE) {
  602. value = ref.is_valid() ? Variant(ref) : Variant(obj);
  603. return OK;
  604. }
  605. if (need_comma) {
  606. if (token2.type != TK_COMMA) {
  607. r_err_str = "Expected '}' or ','";
  608. return ERR_PARSE_ERROR;
  609. } else {
  610. need_comma = false;
  611. continue;
  612. }
  613. }
  614. if (token2.type != TK_STRING) {
  615. r_err_str = "Expected property name as string";
  616. return ERR_PARSE_ERROR;
  617. }
  618. key = token2.value;
  619. err = get_token(p_stream, token2, line, r_err_str);
  620. if (err != OK)
  621. return err;
  622. if (token2.type != TK_COLON) {
  623. r_err_str = "Expected ':'";
  624. return ERR_PARSE_ERROR;
  625. }
  626. at_key = false;
  627. } else {
  628. Error err = get_token(p_stream, token2, line, r_err_str);
  629. if (err != OK)
  630. return err;
  631. Variant v;
  632. err = parse_value(token2, v, p_stream, line, r_err_str, p_res_parser);
  633. if (err)
  634. return err;
  635. obj->set(key, v);
  636. need_comma = true;
  637. at_key = true;
  638. }
  639. }
  640. } else if (id == "Resource" || id == "SubResource" || id == "ExtResource") {
  641. get_token(p_stream, token, line, r_err_str);
  642. if (token.type != TK_PARENTHESIS_OPEN) {
  643. r_err_str = "Expected '('";
  644. return ERR_PARSE_ERROR;
  645. }
  646. if (p_res_parser && id == "Resource" && p_res_parser->func) {
  647. RES res;
  648. Error err = p_res_parser->func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  649. if (err)
  650. return err;
  651. value = res;
  652. } else if (p_res_parser && id == "ExtResource" && p_res_parser->ext_func) {
  653. RES res;
  654. Error err = p_res_parser->ext_func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  655. if (err)
  656. return err;
  657. value = res;
  658. } else if (p_res_parser && id == "SubResource" && p_res_parser->sub_func) {
  659. RES res;
  660. Error err = p_res_parser->sub_func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  661. if (err)
  662. return err;
  663. value = res;
  664. } else {
  665. get_token(p_stream, token, line, r_err_str);
  666. if (token.type == TK_STRING) {
  667. String path = token.value;
  668. RES res = ResourceLoader::load(path);
  669. if (res.is_null()) {
  670. r_err_str = "Can't load resource at path: '" + path + "'.";
  671. return ERR_PARSE_ERROR;
  672. }
  673. get_token(p_stream, token, line, r_err_str);
  674. if (token.type != TK_PARENTHESIS_CLOSE) {
  675. r_err_str = "Expected ')'";
  676. return ERR_PARSE_ERROR;
  677. }
  678. value = res;
  679. } else {
  680. r_err_str = "Expected string as argument for Resource().";
  681. return ERR_PARSE_ERROR;
  682. }
  683. }
  684. #ifndef DISABLE_DEPRECATED
  685. } else if (id == "InputEvent") {
  686. get_token(p_stream, token, line, r_err_str);
  687. if (token.type != TK_PARENTHESIS_OPEN) {
  688. r_err_str = "Expected '('";
  689. return ERR_PARSE_ERROR;
  690. }
  691. get_token(p_stream, token, line, r_err_str);
  692. if (token.type != TK_IDENTIFIER) {
  693. r_err_str = "Expected identifier";
  694. return ERR_PARSE_ERROR;
  695. }
  696. String id2 = token.value;
  697. Ref<InputEvent> ie;
  698. if (id2 == "NONE") {
  699. get_token(p_stream, token, line, r_err_str);
  700. if (token.type != TK_PARENTHESIS_CLOSE) {
  701. r_err_str = "Expected ')'";
  702. return ERR_PARSE_ERROR;
  703. }
  704. } else if (id2 == "KEY") {
  705. Ref<InputEventKey> key;
  706. key.instance();
  707. ie = key;
  708. get_token(p_stream, token, line, r_err_str);
  709. if (token.type != TK_COMMA) {
  710. r_err_str = "Expected ','";
  711. return ERR_PARSE_ERROR;
  712. }
  713. get_token(p_stream, token, line, r_err_str);
  714. if (token.type == TK_IDENTIFIER) {
  715. String name = token.value;
  716. key->set_scancode(find_keycode(name));
  717. } else if (token.type == TK_NUMBER) {
  718. key->set_scancode(token.value);
  719. } else {
  720. r_err_str = "Expected string or integer for keycode";
  721. return ERR_PARSE_ERROR;
  722. }
  723. get_token(p_stream, token, line, r_err_str);
  724. if (token.type == TK_COMMA) {
  725. get_token(p_stream, token, line, r_err_str);
  726. if (token.type != TK_IDENTIFIER) {
  727. r_err_str = "Expected identifier with modifier flas";
  728. return ERR_PARSE_ERROR;
  729. }
  730. String mods = token.value;
  731. if (mods.findn("C") != -1)
  732. key->set_control(true);
  733. if (mods.findn("A") != -1)
  734. key->set_alt(true);
  735. if (mods.findn("S") != -1)
  736. key->set_shift(true);
  737. if (mods.findn("M") != -1)
  738. key->set_metakey(true);
  739. get_token(p_stream, token, line, r_err_str);
  740. if (token.type != TK_PARENTHESIS_CLOSE) {
  741. r_err_str = "Expected ')'";
  742. return ERR_PARSE_ERROR;
  743. }
  744. } else if (token.type != TK_PARENTHESIS_CLOSE) {
  745. r_err_str = "Expected ')' or modifier flags.";
  746. return ERR_PARSE_ERROR;
  747. }
  748. } else if (id2 == "MBUTTON") {
  749. Ref<InputEventMouseButton> mb;
  750. mb.instance();
  751. ie = mb;
  752. get_token(p_stream, token, line, r_err_str);
  753. if (token.type != TK_COMMA) {
  754. r_err_str = "Expected ','";
  755. return ERR_PARSE_ERROR;
  756. }
  757. get_token(p_stream, token, line, r_err_str);
  758. if (token.type != TK_NUMBER) {
  759. r_err_str = "Expected button index";
  760. return ERR_PARSE_ERROR;
  761. }
  762. mb->set_button_index(token.value);
  763. get_token(p_stream, token, line, r_err_str);
  764. if (token.type != TK_PARENTHESIS_CLOSE) {
  765. r_err_str = "Expected ')'";
  766. return ERR_PARSE_ERROR;
  767. }
  768. } else if (id2 == "JBUTTON") {
  769. Ref<InputEventJoypadButton> jb;
  770. jb.instance();
  771. ie = jb;
  772. get_token(p_stream, token, line, r_err_str);
  773. if (token.type != TK_COMMA) {
  774. r_err_str = "Expected ','";
  775. return ERR_PARSE_ERROR;
  776. }
  777. get_token(p_stream, token, line, r_err_str);
  778. if (token.type != TK_NUMBER) {
  779. r_err_str = "Expected button index";
  780. return ERR_PARSE_ERROR;
  781. }
  782. jb->set_button_index(token.value);
  783. get_token(p_stream, token, line, r_err_str);
  784. if (token.type != TK_PARENTHESIS_CLOSE) {
  785. r_err_str = "Expected ')'";
  786. return ERR_PARSE_ERROR;
  787. }
  788. } else if (id2 == "JAXIS") {
  789. Ref<InputEventJoypadMotion> jm;
  790. jm.instance();
  791. ie = jm;
  792. get_token(p_stream, token, line, r_err_str);
  793. if (token.type != TK_COMMA) {
  794. r_err_str = "Expected ','";
  795. return ERR_PARSE_ERROR;
  796. }
  797. get_token(p_stream, token, line, r_err_str);
  798. if (token.type != TK_NUMBER) {
  799. r_err_str = "Expected axis index";
  800. return ERR_PARSE_ERROR;
  801. }
  802. jm->set_axis(token.value);
  803. get_token(p_stream, token, line, r_err_str);
  804. if (token.type != TK_COMMA) {
  805. r_err_str = "Expected ',' after axis index";
  806. return ERR_PARSE_ERROR;
  807. }
  808. get_token(p_stream, token, line, r_err_str);
  809. if (token.type != TK_NUMBER) {
  810. r_err_str = "Expected axis sign";
  811. return ERR_PARSE_ERROR;
  812. }
  813. jm->set_axis_value(token.value);
  814. get_token(p_stream, token, line, r_err_str);
  815. if (token.type != TK_PARENTHESIS_CLOSE) {
  816. r_err_str = "Expected ')' for jaxis";
  817. return ERR_PARSE_ERROR;
  818. }
  819. } else {
  820. r_err_str = "Invalid input event type.";
  821. return ERR_PARSE_ERROR;
  822. }
  823. value = ie;
  824. #endif
  825. } else if (id == "PoolByteArray" || id == "ByteArray") {
  826. Vector<uint8_t> args;
  827. Error err = _parse_construct<uint8_t>(p_stream, args, line, r_err_str);
  828. if (err)
  829. return err;
  830. PoolVector<uint8_t> arr;
  831. {
  832. int len = args.size();
  833. arr.resize(len);
  834. PoolVector<uint8_t>::Write w = arr.write();
  835. for (int i = 0; i < len; i++) {
  836. w[i] = args[i];
  837. }
  838. }
  839. value = arr;
  840. } else if (id == "PoolIntArray" || id == "IntArray") {
  841. Vector<int> args;
  842. Error err = _parse_construct<int>(p_stream, args, line, r_err_str);
  843. if (err)
  844. return err;
  845. PoolVector<int> arr;
  846. {
  847. int len = args.size();
  848. arr.resize(len);
  849. PoolVector<int>::Write w = arr.write();
  850. for (int i = 0; i < len; i++) {
  851. w[i] = int(args[i]);
  852. }
  853. }
  854. value = arr;
  855. } else if (id == "PoolRealArray" || id == "FloatArray") {
  856. Vector<float> args;
  857. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  858. if (err)
  859. return err;
  860. PoolVector<float> arr;
  861. {
  862. int len = args.size();
  863. arr.resize(len);
  864. PoolVector<float>::Write w = arr.write();
  865. for (int i = 0; i < len; i++) {
  866. w[i] = args[i];
  867. }
  868. }
  869. value = arr;
  870. } else if (id == "PoolStringArray" || id == "StringArray") {
  871. get_token(p_stream, token, line, r_err_str);
  872. if (token.type != TK_PARENTHESIS_OPEN) {
  873. r_err_str = "Expected '('";
  874. return ERR_PARSE_ERROR;
  875. }
  876. Vector<String> cs;
  877. bool first = true;
  878. while (true) {
  879. if (!first) {
  880. get_token(p_stream, token, line, r_err_str);
  881. if (token.type == TK_COMMA) {
  882. //do none
  883. } else if (token.type == TK_PARENTHESIS_CLOSE) {
  884. break;
  885. } else {
  886. r_err_str = "Expected ',' or ')'";
  887. return ERR_PARSE_ERROR;
  888. }
  889. }
  890. get_token(p_stream, token, line, r_err_str);
  891. if (token.type == TK_PARENTHESIS_CLOSE) {
  892. break;
  893. } else if (token.type != TK_STRING) {
  894. r_err_str = "Expected string";
  895. return ERR_PARSE_ERROR;
  896. }
  897. first = false;
  898. cs.push_back(token.value);
  899. }
  900. PoolVector<String> arr;
  901. {
  902. int len = cs.size();
  903. arr.resize(len);
  904. PoolVector<String>::Write w = arr.write();
  905. for (int i = 0; i < len; i++) {
  906. w[i] = cs[i];
  907. }
  908. }
  909. value = arr;
  910. } else if (id == "PoolVector2Array" || id == "Vector2Array") {
  911. Vector<float> args;
  912. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  913. if (err)
  914. return err;
  915. PoolVector<Vector2> arr;
  916. {
  917. int len = args.size() / 2;
  918. arr.resize(len);
  919. PoolVector<Vector2>::Write w = arr.write();
  920. for (int i = 0; i < len; i++) {
  921. w[i] = Vector2(args[i * 2 + 0], args[i * 2 + 1]);
  922. }
  923. }
  924. value = arr;
  925. } else if (id == "PoolVector3Array" || id == "Vector3Array") {
  926. Vector<float> args;
  927. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  928. if (err)
  929. return err;
  930. PoolVector<Vector3> arr;
  931. {
  932. int len = args.size() / 3;
  933. arr.resize(len);
  934. PoolVector<Vector3>::Write w = arr.write();
  935. for (int i = 0; i < len; i++) {
  936. w[i] = Vector3(args[i * 3 + 0], args[i * 3 + 1], args[i * 3 + 2]);
  937. }
  938. }
  939. value = arr;
  940. } else if (id == "PoolColorArray" || id == "ColorArray") {
  941. Vector<float> args;
  942. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  943. if (err)
  944. return err;
  945. PoolVector<Color> arr;
  946. {
  947. int len = args.size() / 4;
  948. arr.resize(len);
  949. PoolVector<Color>::Write w = arr.write();
  950. for (int i = 0; i < len; i++) {
  951. w[i] = Color(args[i * 4 + 0], args[i * 4 + 1], args[i * 4 + 2], args[i * 4 + 3]);
  952. }
  953. }
  954. value = arr;
  955. } else {
  956. r_err_str = "Unexpected identifier: '" + id + "'.";
  957. return ERR_PARSE_ERROR;
  958. }
  959. // All above branches end up here unless they had an early return.
  960. return OK;
  961. } else if (token.type == TK_NUMBER) {
  962. value = token.value;
  963. return OK;
  964. } else if (token.type == TK_STRING) {
  965. value = token.value;
  966. return OK;
  967. } else if (token.type == TK_COLOR) {
  968. value = token.value;
  969. return OK;
  970. } else {
  971. r_err_str = "Expected value, got " + String(tk_name[token.type]) + ".";
  972. return ERR_PARSE_ERROR;
  973. }
  974. }
  975. Error VariantParser::_parse_array(Array &array, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  976. Token token;
  977. bool need_comma = false;
  978. while (true) {
  979. if (p_stream->is_eof()) {
  980. r_err_str = "Unexpected End of File while parsing array";
  981. return ERR_FILE_CORRUPT;
  982. }
  983. Error err = get_token(p_stream, token, line, r_err_str);
  984. if (err != OK)
  985. return err;
  986. if (token.type == TK_BRACKET_CLOSE) {
  987. return OK;
  988. }
  989. if (need_comma) {
  990. if (token.type != TK_COMMA) {
  991. r_err_str = "Expected ','";
  992. return ERR_PARSE_ERROR;
  993. } else {
  994. need_comma = false;
  995. continue;
  996. }
  997. }
  998. Variant v;
  999. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  1000. if (err)
  1001. return err;
  1002. array.push_back(v);
  1003. need_comma = true;
  1004. }
  1005. }
  1006. Error VariantParser::_parse_dictionary(Dictionary &object, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  1007. bool at_key = true;
  1008. Variant key;
  1009. Token token;
  1010. bool need_comma = false;
  1011. while (true) {
  1012. if (p_stream->is_eof()) {
  1013. r_err_str = "Unexpected End of File while parsing dictionary";
  1014. return ERR_FILE_CORRUPT;
  1015. }
  1016. if (at_key) {
  1017. Error err = get_token(p_stream, token, line, r_err_str);
  1018. if (err != OK)
  1019. return err;
  1020. if (token.type == TK_CURLY_BRACKET_CLOSE) {
  1021. return OK;
  1022. }
  1023. if (need_comma) {
  1024. if (token.type != TK_COMMA) {
  1025. r_err_str = "Expected '}' or ','";
  1026. return ERR_PARSE_ERROR;
  1027. } else {
  1028. need_comma = false;
  1029. continue;
  1030. }
  1031. }
  1032. err = parse_value(token, key, p_stream, line, r_err_str, p_res_parser);
  1033. if (err)
  1034. return err;
  1035. err = get_token(p_stream, token, line, r_err_str);
  1036. if (err != OK)
  1037. return err;
  1038. if (token.type != TK_COLON) {
  1039. r_err_str = "Expected ':'";
  1040. return ERR_PARSE_ERROR;
  1041. }
  1042. at_key = false;
  1043. } else {
  1044. Error err = get_token(p_stream, token, line, r_err_str);
  1045. if (err != OK)
  1046. return err;
  1047. Variant v;
  1048. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  1049. if (err)
  1050. return err;
  1051. object[key] = v;
  1052. need_comma = true;
  1053. at_key = true;
  1054. }
  1055. }
  1056. }
  1057. Error VariantParser::_parse_tag(Token &token, Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, ResourceParser *p_res_parser, bool p_simple_tag) {
  1058. r_tag.fields.clear();
  1059. if (token.type != TK_BRACKET_OPEN) {
  1060. r_err_str = "Expected '['";
  1061. return ERR_PARSE_ERROR;
  1062. }
  1063. if (p_simple_tag) {
  1064. r_tag.name = "";
  1065. r_tag.fields.clear();
  1066. while (true) {
  1067. CharType c = p_stream->get_char();
  1068. if (p_stream->is_eof()) {
  1069. r_err_str = "Unexpected EOF while parsing simple tag";
  1070. return ERR_PARSE_ERROR;
  1071. }
  1072. if (c == ']')
  1073. break;
  1074. r_tag.name += String::chr(c);
  1075. }
  1076. r_tag.name = r_tag.name.strip_edges();
  1077. return OK;
  1078. }
  1079. get_token(p_stream, token, line, r_err_str);
  1080. if (token.type != TK_IDENTIFIER) {
  1081. r_err_str = "Expected identifier (tag name)";
  1082. return ERR_PARSE_ERROR;
  1083. }
  1084. r_tag.name = token.value;
  1085. bool parsing_tag = true;
  1086. while (true) {
  1087. if (p_stream->is_eof()) {
  1088. r_err_str = "Unexpected End of File while parsing tag: " + r_tag.name;
  1089. return ERR_FILE_CORRUPT;
  1090. }
  1091. get_token(p_stream, token, line, r_err_str);
  1092. if (token.type == TK_BRACKET_CLOSE)
  1093. break;
  1094. if (parsing_tag && token.type == TK_PERIOD) {
  1095. r_tag.name += "."; //support tags such as [someprop.Android] for specific platforms
  1096. get_token(p_stream, token, line, r_err_str);
  1097. } else if (parsing_tag && token.type == TK_COLON) {
  1098. r_tag.name += ":"; //support tags such as [someprop.Android] for specific platforms
  1099. get_token(p_stream, token, line, r_err_str);
  1100. } else {
  1101. parsing_tag = false;
  1102. }
  1103. if (token.type != TK_IDENTIFIER) {
  1104. r_err_str = "Expected Identifier";
  1105. return ERR_PARSE_ERROR;
  1106. }
  1107. String id = token.value;
  1108. if (parsing_tag) {
  1109. r_tag.name += id;
  1110. continue;
  1111. }
  1112. get_token(p_stream, token, line, r_err_str);
  1113. if (token.type != TK_EQUAL) {
  1114. return ERR_PARSE_ERROR;
  1115. }
  1116. get_token(p_stream, token, line, r_err_str);
  1117. Variant value;
  1118. Error err = parse_value(token, value, p_stream, line, r_err_str, p_res_parser);
  1119. if (err)
  1120. return err;
  1121. r_tag.fields[id] = value;
  1122. }
  1123. return OK;
  1124. }
  1125. Error VariantParser::parse_tag(Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, ResourceParser *p_res_parser, bool p_simple_tag) {
  1126. Token token;
  1127. get_token(p_stream, token, line, r_err_str);
  1128. if (token.type == TK_EOF) {
  1129. return ERR_FILE_EOF;
  1130. }
  1131. if (token.type != TK_BRACKET_OPEN) {
  1132. r_err_str = "Expected '['";
  1133. return ERR_PARSE_ERROR;
  1134. }
  1135. return _parse_tag(token, p_stream, line, r_err_str, r_tag, p_res_parser, p_simple_tag);
  1136. }
  1137. Error VariantParser::parse_tag_assign_eof(Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, String &r_assign, Variant &r_value, ResourceParser *p_res_parser, bool p_simple_tag) {
  1138. //assign..
  1139. r_assign = "";
  1140. String what;
  1141. while (true) {
  1142. CharType c;
  1143. if (p_stream->saved) {
  1144. c = p_stream->saved;
  1145. p_stream->saved = 0;
  1146. } else {
  1147. c = p_stream->get_char();
  1148. }
  1149. if (p_stream->is_eof())
  1150. return ERR_FILE_EOF;
  1151. if (c == ';') { //comment
  1152. while (true) {
  1153. CharType ch = p_stream->get_char();
  1154. if (p_stream->is_eof()) {
  1155. return ERR_FILE_EOF;
  1156. }
  1157. if (ch == '\n')
  1158. break;
  1159. }
  1160. continue;
  1161. }
  1162. if (c == '[' && what.length() == 0) {
  1163. //it's a tag!
  1164. p_stream->saved = '['; //go back one
  1165. Error err = parse_tag(p_stream, line, r_err_str, r_tag, p_res_parser, p_simple_tag);
  1166. return err;
  1167. }
  1168. if (c > 32) {
  1169. if (c == '"') { //quoted
  1170. p_stream->saved = '"';
  1171. Token tk;
  1172. Error err = get_token(p_stream, tk, line, r_err_str);
  1173. if (err)
  1174. return err;
  1175. if (tk.type != TK_STRING) {
  1176. r_err_str = "Error reading quoted string";
  1177. return ERR_INVALID_DATA;
  1178. }
  1179. what = tk.value;
  1180. } else if (c != '=') {
  1181. what += String::chr(c);
  1182. } else {
  1183. r_assign = what;
  1184. Token token;
  1185. get_token(p_stream, token, line, r_err_str);
  1186. Error err = parse_value(token, r_value, p_stream, line, r_err_str, p_res_parser);
  1187. return err;
  1188. }
  1189. } else if (c == '\n') {
  1190. line++;
  1191. }
  1192. }
  1193. }
  1194. Error VariantParser::parse(Stream *p_stream, Variant &r_ret, String &r_err_str, int &r_err_line, ResourceParser *p_res_parser) {
  1195. Token token;
  1196. Error err = get_token(p_stream, token, r_err_line, r_err_str);
  1197. if (err)
  1198. return err;
  1199. if (token.type == TK_EOF) {
  1200. return ERR_FILE_EOF;
  1201. }
  1202. return parse_value(token, r_ret, p_stream, r_err_line, r_err_str, p_res_parser);
  1203. }
  1204. //////////////////////////////////////////////////////////////////////////////////
  1205. //////////////////////////////////////////////////////////////////////////////////
  1206. ////////////////////////////////////////////////////////////////////////////////
  1207. static String rtosfix(double p_value) {
  1208. if (p_value == 0.0)
  1209. return "0"; //avoid negative zero (-0) being written, which may annoy git, svn, etc. for changes when they don't exist.
  1210. else
  1211. return rtoss(p_value);
  1212. }
  1213. Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_string_func, void *p_store_string_ud, EncodeResourceFunc p_encode_res_func, void *p_encode_res_ud) {
  1214. switch (p_variant.get_type()) {
  1215. case Variant::NIL: {
  1216. p_store_string_func(p_store_string_ud, "null");
  1217. } break;
  1218. case Variant::BOOL: {
  1219. p_store_string_func(p_store_string_ud, p_variant.operator bool() ? "true" : "false");
  1220. } break;
  1221. case Variant::INT: {
  1222. p_store_string_func(p_store_string_ud, itos(p_variant.operator int64_t()));
  1223. } break;
  1224. case Variant::REAL: {
  1225. String s = rtosfix(p_variant.operator real_t());
  1226. if (s != "inf" && s != "nan") {
  1227. if (s.find(".") == -1 && s.find("e") == -1)
  1228. s += ".0";
  1229. }
  1230. p_store_string_func(p_store_string_ud, s);
  1231. } break;
  1232. case Variant::STRING: {
  1233. String str = p_variant;
  1234. str = "\"" + str.c_escape_multiline() + "\"";
  1235. p_store_string_func(p_store_string_ud, str);
  1236. } break;
  1237. case Variant::VECTOR2: {
  1238. Vector2 v = p_variant;
  1239. p_store_string_func(p_store_string_ud, "Vector2( " + rtosfix(v.x) + ", " + rtosfix(v.y) + " )");
  1240. } break;
  1241. case Variant::RECT2: {
  1242. Rect2 aabb = p_variant;
  1243. p_store_string_func(p_store_string_ud, "Rect2( " + rtosfix(aabb.position.x) + ", " + rtosfix(aabb.position.y) + ", " + rtosfix(aabb.size.x) + ", " + rtosfix(aabb.size.y) + " )");
  1244. } break;
  1245. case Variant::VECTOR3: {
  1246. Vector3 v = p_variant;
  1247. p_store_string_func(p_store_string_ud, "Vector3( " + rtosfix(v.x) + ", " + rtosfix(v.y) + ", " + rtosfix(v.z) + " )");
  1248. } break;
  1249. case Variant::PLANE: {
  1250. Plane p = p_variant;
  1251. p_store_string_func(p_store_string_ud, "Plane( " + rtosfix(p.normal.x) + ", " + rtosfix(p.normal.y) + ", " + rtosfix(p.normal.z) + ", " + rtosfix(p.d) + " )");
  1252. } break;
  1253. case Variant::AABB: {
  1254. AABB aabb = p_variant;
  1255. p_store_string_func(p_store_string_ud, "AABB( " + rtosfix(aabb.position.x) + ", " + rtosfix(aabb.position.y) + ", " + rtosfix(aabb.position.z) + ", " + rtosfix(aabb.size.x) + ", " + rtosfix(aabb.size.y) + ", " + rtosfix(aabb.size.z) + " )");
  1256. } break;
  1257. case Variant::QUAT: {
  1258. Quat quat = p_variant;
  1259. p_store_string_func(p_store_string_ud, "Quat( " + rtosfix(quat.x) + ", " + rtosfix(quat.y) + ", " + rtosfix(quat.z) + ", " + rtosfix(quat.w) + " )");
  1260. } break;
  1261. case Variant::TRANSFORM2D: {
  1262. String s = "Transform2D( ";
  1263. Transform2D m3 = p_variant;
  1264. for (int i = 0; i < 3; i++) {
  1265. for (int j = 0; j < 2; j++) {
  1266. if (i != 0 || j != 0)
  1267. s += ", ";
  1268. s += rtosfix(m3.elements[i][j]);
  1269. }
  1270. }
  1271. p_store_string_func(p_store_string_ud, s + " )");
  1272. } break;
  1273. case Variant::BASIS: {
  1274. String s = "Basis( ";
  1275. Basis m3 = p_variant;
  1276. for (int i = 0; i < 3; i++) {
  1277. for (int j = 0; j < 3; j++) {
  1278. if (i != 0 || j != 0)
  1279. s += ", ";
  1280. s += rtosfix(m3.elements[i][j]);
  1281. }
  1282. }
  1283. p_store_string_func(p_store_string_ud, s + " )");
  1284. } break;
  1285. case Variant::TRANSFORM: {
  1286. String s = "Transform( ";
  1287. Transform t = p_variant;
  1288. Basis &m3 = t.basis;
  1289. for (int i = 0; i < 3; i++) {
  1290. for (int j = 0; j < 3; j++) {
  1291. if (i != 0 || j != 0)
  1292. s += ", ";
  1293. s += rtosfix(m3.elements[i][j]);
  1294. }
  1295. }
  1296. s = s + ", " + rtosfix(t.origin.x) + ", " + rtosfix(t.origin.y) + ", " + rtosfix(t.origin.z);
  1297. p_store_string_func(p_store_string_ud, s + " )");
  1298. } break;
  1299. // misc types
  1300. case Variant::COLOR: {
  1301. Color c = p_variant;
  1302. p_store_string_func(p_store_string_ud, "Color( " + rtosfix(c.r) + ", " + rtosfix(c.g) + ", " + rtosfix(c.b) + ", " + rtosfix(c.a) + " )");
  1303. } break;
  1304. case Variant::NODE_PATH: {
  1305. String str = p_variant;
  1306. str = "NodePath(\"" + str.c_escape() + "\")";
  1307. p_store_string_func(p_store_string_ud, str);
  1308. } break;
  1309. case Variant::OBJECT: {
  1310. Object *obj = p_variant;
  1311. if (!obj) {
  1312. p_store_string_func(p_store_string_ud, "null");
  1313. break; // don't save it
  1314. }
  1315. RES res = p_variant;
  1316. if (res.is_valid()) {
  1317. //is resource
  1318. String res_text;
  1319. //try external function
  1320. if (p_encode_res_func) {
  1321. res_text = p_encode_res_func(p_encode_res_ud, res);
  1322. }
  1323. //try path because it's a file
  1324. if (res_text == String() && res->get_path().is_resource_file()) {
  1325. //external resource
  1326. String path = res->get_path();
  1327. res_text = "Resource( \"" + path + "\")";
  1328. }
  1329. //could come up with some sort of text
  1330. if (res_text != String()) {
  1331. p_store_string_func(p_store_string_ud, res_text);
  1332. break;
  1333. }
  1334. }
  1335. //store as generic object
  1336. p_store_string_func(p_store_string_ud, "Object(" + obj->get_class() + ",");
  1337. List<PropertyInfo> props;
  1338. obj->get_property_list(&props);
  1339. bool first = true;
  1340. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1341. if (E->get().usage & PROPERTY_USAGE_STORAGE || E->get().usage & PROPERTY_USAGE_SCRIPT_VARIABLE) {
  1342. //must be serialized
  1343. if (first) {
  1344. first = false;
  1345. } else {
  1346. p_store_string_func(p_store_string_ud, ",");
  1347. }
  1348. p_store_string_func(p_store_string_ud, "\"" + E->get().name + "\":");
  1349. write(obj->get(E->get().name), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1350. }
  1351. }
  1352. p_store_string_func(p_store_string_ud, ")\n");
  1353. } break;
  1354. case Variant::DICTIONARY: {
  1355. Dictionary dict = p_variant;
  1356. List<Variant> keys;
  1357. dict.get_key_list(&keys);
  1358. keys.sort();
  1359. p_store_string_func(p_store_string_ud, "{\n");
  1360. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1361. /*
  1362. if (!_check_type(dict[E->get()]))
  1363. continue;
  1364. */
  1365. write(E->get(), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1366. p_store_string_func(p_store_string_ud, ": ");
  1367. write(dict[E->get()], p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1368. if (E->next()) {
  1369. p_store_string_func(p_store_string_ud, ",\n");
  1370. } else {
  1371. p_store_string_func(p_store_string_ud, "\n");
  1372. }
  1373. }
  1374. p_store_string_func(p_store_string_ud, "}");
  1375. } break;
  1376. case Variant::ARRAY: {
  1377. p_store_string_func(p_store_string_ud, "[ ");
  1378. Array array = p_variant;
  1379. int len = array.size();
  1380. for (int i = 0; i < len; i++) {
  1381. if (i > 0)
  1382. p_store_string_func(p_store_string_ud, ", ");
  1383. write(array[i], p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1384. }
  1385. p_store_string_func(p_store_string_ud, " ]");
  1386. } break;
  1387. case Variant::POOL_BYTE_ARRAY: {
  1388. p_store_string_func(p_store_string_ud, "PoolByteArray( ");
  1389. String s;
  1390. PoolVector<uint8_t> data = p_variant;
  1391. int len = data.size();
  1392. PoolVector<uint8_t>::Read r = data.read();
  1393. const uint8_t *ptr = r.ptr();
  1394. for (int i = 0; i < len; i++) {
  1395. if (i > 0)
  1396. p_store_string_func(p_store_string_ud, ", ");
  1397. p_store_string_func(p_store_string_ud, itos(ptr[i]));
  1398. }
  1399. p_store_string_func(p_store_string_ud, " )");
  1400. } break;
  1401. case Variant::POOL_INT_ARRAY: {
  1402. p_store_string_func(p_store_string_ud, "PoolIntArray( ");
  1403. PoolVector<int> data = p_variant;
  1404. int len = data.size();
  1405. PoolVector<int>::Read r = data.read();
  1406. const int *ptr = r.ptr();
  1407. for (int i = 0; i < len; i++) {
  1408. if (i > 0)
  1409. p_store_string_func(p_store_string_ud, ", ");
  1410. p_store_string_func(p_store_string_ud, itos(ptr[i]));
  1411. }
  1412. p_store_string_func(p_store_string_ud, " )");
  1413. } break;
  1414. case Variant::POOL_REAL_ARRAY: {
  1415. p_store_string_func(p_store_string_ud, "PoolRealArray( ");
  1416. PoolVector<real_t> data = p_variant;
  1417. int len = data.size();
  1418. PoolVector<real_t>::Read r = data.read();
  1419. const real_t *ptr = r.ptr();
  1420. for (int i = 0; i < len; i++) {
  1421. if (i > 0)
  1422. p_store_string_func(p_store_string_ud, ", ");
  1423. p_store_string_func(p_store_string_ud, rtosfix(ptr[i]));
  1424. }
  1425. p_store_string_func(p_store_string_ud, " )");
  1426. } break;
  1427. case Variant::POOL_STRING_ARRAY: {
  1428. p_store_string_func(p_store_string_ud, "PoolStringArray( ");
  1429. PoolVector<String> data = p_variant;
  1430. int len = data.size();
  1431. PoolVector<String>::Read r = data.read();
  1432. const String *ptr = r.ptr();
  1433. String s;
  1434. //write_string("\n");
  1435. for (int i = 0; i < len; i++) {
  1436. if (i > 0)
  1437. p_store_string_func(p_store_string_ud, ", ");
  1438. String str = ptr[i];
  1439. p_store_string_func(p_store_string_ud, "\"" + str.c_escape() + "\"");
  1440. }
  1441. p_store_string_func(p_store_string_ud, " )");
  1442. } break;
  1443. case Variant::POOL_VECTOR2_ARRAY: {
  1444. p_store_string_func(p_store_string_ud, "PoolVector2Array( ");
  1445. PoolVector<Vector2> data = p_variant;
  1446. int len = data.size();
  1447. PoolVector<Vector2>::Read r = data.read();
  1448. const Vector2 *ptr = r.ptr();
  1449. for (int i = 0; i < len; i++) {
  1450. if (i > 0)
  1451. p_store_string_func(p_store_string_ud, ", ");
  1452. p_store_string_func(p_store_string_ud, rtosfix(ptr[i].x) + ", " + rtosfix(ptr[i].y));
  1453. }
  1454. p_store_string_func(p_store_string_ud, " )");
  1455. } break;
  1456. case Variant::POOL_VECTOR3_ARRAY: {
  1457. p_store_string_func(p_store_string_ud, "PoolVector3Array( ");
  1458. PoolVector<Vector3> data = p_variant;
  1459. int len = data.size();
  1460. PoolVector<Vector3>::Read r = data.read();
  1461. const Vector3 *ptr = r.ptr();
  1462. for (int i = 0; i < len; i++) {
  1463. if (i > 0)
  1464. p_store_string_func(p_store_string_ud, ", ");
  1465. p_store_string_func(p_store_string_ud, rtosfix(ptr[i].x) + ", " + rtosfix(ptr[i].y) + ", " + rtosfix(ptr[i].z));
  1466. }
  1467. p_store_string_func(p_store_string_ud, " )");
  1468. } break;
  1469. case Variant::POOL_COLOR_ARRAY: {
  1470. p_store_string_func(p_store_string_ud, "PoolColorArray( ");
  1471. PoolVector<Color> data = p_variant;
  1472. int len = data.size();
  1473. PoolVector<Color>::Read r = data.read();
  1474. const Color *ptr = r.ptr();
  1475. for (int i = 0; i < len; i++) {
  1476. if (i > 0)
  1477. p_store_string_func(p_store_string_ud, ", ");
  1478. p_store_string_func(p_store_string_ud, rtosfix(ptr[i].r) + ", " + rtosfix(ptr[i].g) + ", " + rtosfix(ptr[i].b) + ", " + rtosfix(ptr[i].a));
  1479. }
  1480. p_store_string_func(p_store_string_ud, " )");
  1481. } break;
  1482. default: {
  1483. }
  1484. }
  1485. return OK;
  1486. }
  1487. static Error _write_to_str(void *ud, const String &p_string) {
  1488. String *str = (String *)ud;
  1489. (*str) += p_string;
  1490. return OK;
  1491. }
  1492. Error VariantWriter::write_to_string(const Variant &p_variant, String &r_string, EncodeResourceFunc p_encode_res_func, void *p_encode_res_ud) {
  1493. r_string = String();
  1494. return write(p_variant, _write_to_str, &r_string, p_encode_res_func, p_encode_res_ud);
  1495. }