variant_parser.cpp 47 KB

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