variant_parser.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966
  1. /*************************************************************************/
  2. /* variant_parser.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "variant_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. /* {
  402. Error err = get_token(p_stream,token,line,r_err_str);
  403. if (err)
  404. return err;
  405. }*/
  406. if (token.type == TK_CURLY_BRACKET_OPEN) {
  407. Dictionary d;
  408. Error err = _parse_dictionary(d, p_stream, line, r_err_str, p_res_parser);
  409. if (err)
  410. return err;
  411. value = d;
  412. return OK;
  413. } else if (token.type == TK_BRACKET_OPEN) {
  414. Array a;
  415. Error err = _parse_array(a, p_stream, line, r_err_str, p_res_parser);
  416. if (err)
  417. return err;
  418. value = a;
  419. return OK;
  420. } else if (token.type == TK_IDENTIFIER) {
  421. String id = token.value;
  422. if (id == "true")
  423. value = true;
  424. else if (id == "false")
  425. value = false;
  426. else if (id == "null" || id == "nil")
  427. value = Variant();
  428. else if (id == "inf")
  429. value = Math_INF;
  430. else if (id == "nan")
  431. value = Math_NAN;
  432. else if (id == "Vector2") {
  433. Vector<float> args;
  434. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  435. if (err)
  436. return err;
  437. if (args.size() != 2) {
  438. r_err_str = "Expected 2 arguments for constructor";
  439. }
  440. value = Vector2(args[0], args[1]);
  441. return OK;
  442. } else if (id == "Rect2") {
  443. Vector<float> args;
  444. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  445. if (err)
  446. return err;
  447. if (args.size() != 4) {
  448. r_err_str = "Expected 4 arguments for constructor";
  449. }
  450. value = Rect2(args[0], args[1], args[2], args[3]);
  451. return OK;
  452. } else if (id == "Vector3") {
  453. Vector<float> args;
  454. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  455. if (err)
  456. return err;
  457. if (args.size() != 3) {
  458. r_err_str = "Expected 3 arguments for constructor";
  459. }
  460. value = Vector3(args[0], args[1], args[2]);
  461. return OK;
  462. } else if (id == "Transform2D" || id == "Matrix32") { //compatibility
  463. Vector<float> args;
  464. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  465. if (err)
  466. return err;
  467. if (args.size() != 6) {
  468. r_err_str = "Expected 6 arguments for constructor";
  469. }
  470. Transform2D m;
  471. m[0] = Vector2(args[0], args[1]);
  472. m[1] = Vector2(args[2], args[3]);
  473. m[2] = Vector2(args[4], args[5]);
  474. value = m;
  475. return OK;
  476. } else if (id == "Plane") {
  477. Vector<float> args;
  478. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  479. if (err)
  480. return err;
  481. if (args.size() != 4) {
  482. r_err_str = "Expected 4 arguments for constructor";
  483. }
  484. value = Plane(args[0], args[1], args[2], args[3]);
  485. return OK;
  486. } else if (id == "Quat") {
  487. Vector<float> args;
  488. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  489. if (err)
  490. return err;
  491. if (args.size() != 4) {
  492. r_err_str = "Expected 4 arguments for constructor";
  493. }
  494. value = Quat(args[0], args[1], args[2], args[3]);
  495. return OK;
  496. } else if (id == "AABB" || id == "Rect3") {
  497. Vector<float> args;
  498. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  499. if (err)
  500. return err;
  501. if (args.size() != 6) {
  502. r_err_str = "Expected 6 arguments for constructor";
  503. }
  504. value = AABB(Vector3(args[0], args[1], args[2]), Vector3(args[3], args[4], args[5]));
  505. return OK;
  506. } else if (id == "Basis" || id == "Matrix3") { //compatibility
  507. Vector<float> args;
  508. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  509. if (err)
  510. return err;
  511. if (args.size() != 9) {
  512. r_err_str = "Expected 9 arguments for constructor";
  513. }
  514. value = Basis(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
  515. return OK;
  516. } else if (id == "Transform") {
  517. Vector<float> args;
  518. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  519. if (err)
  520. return err;
  521. if (args.size() != 12) {
  522. r_err_str = "Expected 12 arguments for constructor";
  523. }
  524. 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]));
  525. return OK;
  526. } else if (id == "Color") {
  527. Vector<float> args;
  528. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  529. if (err)
  530. return err;
  531. if (args.size() != 4) {
  532. r_err_str = "Expected 4 arguments for constructor";
  533. }
  534. value = Color(args[0], args[1], args[2], args[3]);
  535. return OK;
  536. } else if (id == "NodePath") {
  537. get_token(p_stream, token, line, r_err_str);
  538. if (token.type != TK_PARENTHESIS_OPEN) {
  539. r_err_str = "Expected '('";
  540. return ERR_PARSE_ERROR;
  541. }
  542. get_token(p_stream, token, line, r_err_str);
  543. if (token.type != TK_STRING) {
  544. r_err_str = "Expected string as argument for NodePath()";
  545. return ERR_PARSE_ERROR;
  546. }
  547. value = NodePath(String(token.value));
  548. get_token(p_stream, token, line, r_err_str);
  549. if (token.type != TK_PARENTHESIS_CLOSE) {
  550. r_err_str = "Expected ')'";
  551. return ERR_PARSE_ERROR;
  552. }
  553. } else if (id == "RID") {
  554. get_token(p_stream, token, line, r_err_str);
  555. if (token.type != TK_PARENTHESIS_OPEN) {
  556. r_err_str = "Expected '('";
  557. return ERR_PARSE_ERROR;
  558. }
  559. get_token(p_stream, token, line, r_err_str);
  560. if (token.type != TK_NUMBER) {
  561. r_err_str = "Expected number as argument";
  562. return ERR_PARSE_ERROR;
  563. }
  564. value = token.value;
  565. get_token(p_stream, token, line, r_err_str);
  566. if (token.type != TK_PARENTHESIS_CLOSE) {
  567. r_err_str = "Expected ')'";
  568. return ERR_PARSE_ERROR;
  569. }
  570. return OK;
  571. } else if (id == "Object") {
  572. get_token(p_stream, token, line, r_err_str);
  573. if (token.type != TK_PARENTHESIS_OPEN) {
  574. r_err_str = "Expected '('";
  575. return ERR_PARSE_ERROR;
  576. }
  577. get_token(p_stream, token, line, r_err_str);
  578. if (token.type != TK_IDENTIFIER) {
  579. r_err_str = "Expected identifier with type of object";
  580. return ERR_PARSE_ERROR;
  581. }
  582. String type = token.value;
  583. Object *obj = ClassDB::instance(type);
  584. if (!obj) {
  585. r_err_str = "Can't instance Object() of type: " + type;
  586. return ERR_PARSE_ERROR;
  587. }
  588. get_token(p_stream, token, line, r_err_str);
  589. if (token.type != TK_COMMA) {
  590. r_err_str = "Expected ',' after object type";
  591. return ERR_PARSE_ERROR;
  592. }
  593. bool at_key = true;
  594. String key;
  595. Token token2;
  596. bool need_comma = false;
  597. while (true) {
  598. if (p_stream->is_eof()) {
  599. r_err_str = "Unexpected End of File while parsing Object()";
  600. return ERR_FILE_CORRUPT;
  601. }
  602. if (at_key) {
  603. Error err = get_token(p_stream, token2, line, r_err_str);
  604. if (err != OK)
  605. return err;
  606. if (token2.type == TK_PARENTHESIS_CLOSE) {
  607. Reference *reference = Object::cast_to<Reference>(obj);
  608. if (reference) {
  609. value = REF(reference);
  610. } else {
  611. value = obj;
  612. }
  613. return OK;
  614. }
  615. if (need_comma) {
  616. if (token2.type != TK_COMMA) {
  617. r_err_str = "Expected '}' or ','";
  618. return ERR_PARSE_ERROR;
  619. } else {
  620. need_comma = false;
  621. continue;
  622. }
  623. }
  624. if (token2.type != TK_STRING) {
  625. r_err_str = "Expected property name as string";
  626. return ERR_PARSE_ERROR;
  627. }
  628. key = token2.value;
  629. err = get_token(p_stream, token2, line, r_err_str);
  630. if (err != OK)
  631. return err;
  632. if (token2.type != TK_COLON) {
  633. r_err_str = "Expected ':'";
  634. return ERR_PARSE_ERROR;
  635. }
  636. at_key = false;
  637. } else {
  638. Error err = get_token(p_stream, token2, line, r_err_str);
  639. if (err != OK)
  640. return err;
  641. Variant v;
  642. err = parse_value(token2, v, p_stream, line, r_err_str, p_res_parser);
  643. if (err)
  644. return err;
  645. obj->set(key, v);
  646. need_comma = true;
  647. at_key = true;
  648. }
  649. }
  650. } else if (id == "Resource" || id == "SubResource" || id == "ExtResource") {
  651. get_token(p_stream, token, line, r_err_str);
  652. if (token.type != TK_PARENTHESIS_OPEN) {
  653. r_err_str = "Expected '('";
  654. return ERR_PARSE_ERROR;
  655. }
  656. if (p_res_parser && id == "Resource" && p_res_parser->func) {
  657. RES res;
  658. Error err = p_res_parser->func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  659. if (err)
  660. return err;
  661. value = res;
  662. return OK;
  663. } else if (p_res_parser && id == "ExtResource" && p_res_parser->ext_func) {
  664. RES res;
  665. Error err = p_res_parser->ext_func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  666. if (err)
  667. return err;
  668. value = res;
  669. return OK;
  670. } else if (p_res_parser && id == "SubResource" && p_res_parser->sub_func) {
  671. RES res;
  672. Error err = p_res_parser->sub_func(p_res_parser->userdata, p_stream, res, line, r_err_str);
  673. if (err)
  674. return err;
  675. value = res;
  676. return OK;
  677. } else {
  678. get_token(p_stream, token, line, r_err_str);
  679. if (token.type == TK_STRING) {
  680. String path = token.value;
  681. RES res = ResourceLoader::load(path);
  682. if (res.is_null()) {
  683. r_err_str = "Can't load resource at path: '" + path + "'.";
  684. return ERR_PARSE_ERROR;
  685. }
  686. get_token(p_stream, token, line, r_err_str);
  687. if (token.type != TK_PARENTHESIS_CLOSE) {
  688. r_err_str = "Expected ')'";
  689. return ERR_PARSE_ERROR;
  690. }
  691. value = res;
  692. return OK;
  693. } else {
  694. r_err_str = "Expected string as argument for Resource().";
  695. return ERR_PARSE_ERROR;
  696. }
  697. }
  698. #ifndef DISABLE_DEPRECATED
  699. } else if (id == "InputEvent") {
  700. get_token(p_stream, token, line, r_err_str);
  701. if (token.type != TK_PARENTHESIS_OPEN) {
  702. r_err_str = "Expected '('";
  703. return ERR_PARSE_ERROR;
  704. }
  705. get_token(p_stream, token, line, r_err_str);
  706. if (token.type != TK_IDENTIFIER) {
  707. r_err_str = "Expected identifier";
  708. return ERR_PARSE_ERROR;
  709. }
  710. String id2 = token.value;
  711. Ref<InputEvent> ie;
  712. if (id2 == "NONE") {
  713. get_token(p_stream, token, line, r_err_str);
  714. if (token.type != TK_PARENTHESIS_CLOSE) {
  715. r_err_str = "Expected ')'";
  716. return ERR_PARSE_ERROR;
  717. }
  718. } else if (id2 == "KEY") {
  719. Ref<InputEventKey> key;
  720. key.instance();
  721. ie = key;
  722. get_token(p_stream, token, line, r_err_str);
  723. if (token.type != TK_COMMA) {
  724. r_err_str = "Expected ','";
  725. return ERR_PARSE_ERROR;
  726. }
  727. get_token(p_stream, token, line, r_err_str);
  728. if (token.type == TK_IDENTIFIER) {
  729. String name = token.value;
  730. key->set_scancode(find_keycode(name));
  731. } else if (token.type == TK_NUMBER) {
  732. key->set_scancode(token.value);
  733. } else {
  734. r_err_str = "Expected string or integer for keycode";
  735. return ERR_PARSE_ERROR;
  736. }
  737. get_token(p_stream, token, line, r_err_str);
  738. if (token.type == TK_COMMA) {
  739. get_token(p_stream, token, line, r_err_str);
  740. if (token.type != TK_IDENTIFIER) {
  741. r_err_str = "Expected identifier with modifier flas";
  742. return ERR_PARSE_ERROR;
  743. }
  744. String mods = token.value;
  745. if (mods.findn("C") != -1)
  746. key->set_control(true);
  747. if (mods.findn("A") != -1)
  748. key->set_alt(true);
  749. if (mods.findn("S") != -1)
  750. key->set_shift(true);
  751. if (mods.findn("M") != -1)
  752. key->set_metakey(true);
  753. get_token(p_stream, token, line, r_err_str);
  754. if (token.type != TK_PARENTHESIS_CLOSE) {
  755. r_err_str = "Expected ')'";
  756. return ERR_PARSE_ERROR;
  757. }
  758. } else if (token.type != TK_PARENTHESIS_CLOSE) {
  759. r_err_str = "Expected ')' or modifier flags.";
  760. return ERR_PARSE_ERROR;
  761. }
  762. } else if (id2 == "MBUTTON") {
  763. Ref<InputEventMouseButton> mb;
  764. mb.instance();
  765. ie = mb;
  766. get_token(p_stream, token, line, r_err_str);
  767. if (token.type != TK_COMMA) {
  768. r_err_str = "Expected ','";
  769. return ERR_PARSE_ERROR;
  770. }
  771. get_token(p_stream, token, line, r_err_str);
  772. if (token.type != TK_NUMBER) {
  773. r_err_str = "Expected button index";
  774. return ERR_PARSE_ERROR;
  775. }
  776. mb->set_button_index(token.value);
  777. get_token(p_stream, token, line, r_err_str);
  778. if (token.type != TK_PARENTHESIS_CLOSE) {
  779. r_err_str = "Expected ')'";
  780. return ERR_PARSE_ERROR;
  781. }
  782. } else if (id2 == "JBUTTON") {
  783. Ref<InputEventJoypadButton> jb;
  784. jb.instance();
  785. ie = jb;
  786. get_token(p_stream, token, line, r_err_str);
  787. if (token.type != TK_COMMA) {
  788. r_err_str = "Expected ','";
  789. return ERR_PARSE_ERROR;
  790. }
  791. get_token(p_stream, token, line, r_err_str);
  792. if (token.type != TK_NUMBER) {
  793. r_err_str = "Expected button index";
  794. return ERR_PARSE_ERROR;
  795. }
  796. jb->set_button_index(token.value);
  797. get_token(p_stream, token, line, r_err_str);
  798. if (token.type != TK_PARENTHESIS_CLOSE) {
  799. r_err_str = "Expected ')'";
  800. return ERR_PARSE_ERROR;
  801. }
  802. } else if (id2 == "JAXIS") {
  803. Ref<InputEventJoypadMotion> jm;
  804. jm.instance();
  805. ie = jm;
  806. get_token(p_stream, token, line, r_err_str);
  807. if (token.type != TK_COMMA) {
  808. r_err_str = "Expected ','";
  809. return ERR_PARSE_ERROR;
  810. }
  811. get_token(p_stream, token, line, r_err_str);
  812. if (token.type != TK_NUMBER) {
  813. r_err_str = "Expected axis index";
  814. return ERR_PARSE_ERROR;
  815. }
  816. jm->set_axis(token.value);
  817. get_token(p_stream, token, line, r_err_str);
  818. if (token.type != TK_COMMA) {
  819. r_err_str = "Expected ',' after axis index";
  820. return ERR_PARSE_ERROR;
  821. }
  822. get_token(p_stream, token, line, r_err_str);
  823. if (token.type != TK_NUMBER) {
  824. r_err_str = "Expected axis sign";
  825. return ERR_PARSE_ERROR;
  826. }
  827. jm->set_axis_value(token.value);
  828. get_token(p_stream, token, line, r_err_str);
  829. if (token.type != TK_PARENTHESIS_CLOSE) {
  830. r_err_str = "Expected ')' for jaxis";
  831. return ERR_PARSE_ERROR;
  832. }
  833. } else {
  834. r_err_str = "Invalid input event type.";
  835. return ERR_PARSE_ERROR;
  836. }
  837. value = ie;
  838. return OK;
  839. #endif
  840. } else if (id == "PoolByteArray" || id == "ByteArray") {
  841. Vector<uint8_t> args;
  842. Error err = _parse_construct<uint8_t>(p_stream, args, line, r_err_str);
  843. if (err)
  844. return err;
  845. PoolVector<uint8_t> arr;
  846. {
  847. int len = args.size();
  848. arr.resize(len);
  849. PoolVector<uint8_t>::Write w = arr.write();
  850. for (int i = 0; i < len; i++) {
  851. w[i] = args[i];
  852. }
  853. }
  854. value = arr;
  855. return OK;
  856. } else if (id == "PoolIntArray" || id == "IntArray") {
  857. Vector<int> args;
  858. Error err = _parse_construct<int>(p_stream, args, line, r_err_str);
  859. if (err)
  860. return err;
  861. PoolVector<int> arr;
  862. {
  863. int len = args.size();
  864. arr.resize(len);
  865. PoolVector<int>::Write w = arr.write();
  866. for (int i = 0; i < len; i++) {
  867. w[i] = int(args[i]);
  868. }
  869. }
  870. value = arr;
  871. return OK;
  872. } else if (id == "PoolRealArray" || id == "FloatArray") {
  873. Vector<float> args;
  874. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  875. if (err)
  876. return err;
  877. PoolVector<float> arr;
  878. {
  879. int len = args.size();
  880. arr.resize(len);
  881. PoolVector<float>::Write w = arr.write();
  882. for (int i = 0; i < len; i++) {
  883. w[i] = args[i];
  884. }
  885. }
  886. value = arr;
  887. return OK;
  888. } else if (id == "PoolStringArray" || id == "StringArray") {
  889. get_token(p_stream, token, line, r_err_str);
  890. if (token.type != TK_PARENTHESIS_OPEN) {
  891. r_err_str = "Expected '('";
  892. return ERR_PARSE_ERROR;
  893. }
  894. Vector<String> cs;
  895. bool first = true;
  896. while (true) {
  897. if (!first) {
  898. get_token(p_stream, token, line, r_err_str);
  899. if (token.type == TK_COMMA) {
  900. //do none
  901. } else if (token.type == TK_PARENTHESIS_CLOSE) {
  902. break;
  903. } else {
  904. r_err_str = "Expected ',' or ')'";
  905. return ERR_PARSE_ERROR;
  906. }
  907. }
  908. get_token(p_stream, token, line, r_err_str);
  909. if (token.type == TK_PARENTHESIS_CLOSE) {
  910. break;
  911. } else if (token.type != TK_STRING) {
  912. r_err_str = "Expected string";
  913. return ERR_PARSE_ERROR;
  914. }
  915. first = false;
  916. cs.push_back(token.value);
  917. }
  918. PoolVector<String> arr;
  919. {
  920. int len = cs.size();
  921. arr.resize(len);
  922. PoolVector<String>::Write w = arr.write();
  923. for (int i = 0; i < len; i++) {
  924. w[i] = cs[i];
  925. }
  926. }
  927. value = arr;
  928. return OK;
  929. } else if (id == "PoolVector2Array" || id == "Vector2Array") {
  930. Vector<float> args;
  931. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  932. if (err)
  933. return err;
  934. PoolVector<Vector2> arr;
  935. {
  936. int len = args.size() / 2;
  937. arr.resize(len);
  938. PoolVector<Vector2>::Write w = arr.write();
  939. for (int i = 0; i < len; i++) {
  940. w[i] = Vector2(args[i * 2 + 0], args[i * 2 + 1]);
  941. }
  942. }
  943. value = arr;
  944. return OK;
  945. } else if (id == "PoolVector3Array" || id == "Vector3Array") {
  946. Vector<float> args;
  947. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  948. if (err)
  949. return err;
  950. PoolVector<Vector3> arr;
  951. {
  952. int len = args.size() / 3;
  953. arr.resize(len);
  954. PoolVector<Vector3>::Write w = arr.write();
  955. for (int i = 0; i < len; i++) {
  956. w[i] = Vector3(args[i * 3 + 0], args[i * 3 + 1], args[i * 3 + 2]);
  957. }
  958. }
  959. value = arr;
  960. return OK;
  961. } else if (id == "PoolColorArray" || id == "ColorArray") {
  962. Vector<float> args;
  963. Error err = _parse_construct<float>(p_stream, args, line, r_err_str);
  964. if (err)
  965. return err;
  966. PoolVector<Color> arr;
  967. {
  968. int len = args.size() / 4;
  969. arr.resize(len);
  970. PoolVector<Color>::Write w = arr.write();
  971. for (int i = 0; i < len; i++) {
  972. w[i] = Color(args[i * 4 + 0], args[i * 4 + 1], args[i * 4 + 2], args[i * 4 + 3]);
  973. }
  974. }
  975. value = arr;
  976. return OK;
  977. } else {
  978. r_err_str = "Unexpected identifier: '" + id + "'.";
  979. return ERR_PARSE_ERROR;
  980. }
  981. return OK;
  982. } else if (token.type == TK_NUMBER) {
  983. value = token.value;
  984. return OK;
  985. } else if (token.type == TK_STRING) {
  986. value = token.value;
  987. return OK;
  988. } else if (token.type == TK_COLOR) {
  989. value = token.value;
  990. return OK;
  991. } else {
  992. r_err_str = "Expected value, got " + String(tk_name[token.type]) + ".";
  993. return ERR_PARSE_ERROR;
  994. }
  995. }
  996. Error VariantParser::_parse_array(Array &array, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  997. Token token;
  998. bool need_comma = false;
  999. while (true) {
  1000. if (p_stream->is_eof()) {
  1001. r_err_str = "Unexpected End of File while parsing array";
  1002. return ERR_FILE_CORRUPT;
  1003. }
  1004. Error err = get_token(p_stream, token, line, r_err_str);
  1005. if (err != OK)
  1006. return err;
  1007. if (token.type == TK_BRACKET_CLOSE) {
  1008. return OK;
  1009. }
  1010. if (need_comma) {
  1011. if (token.type != TK_COMMA) {
  1012. r_err_str = "Expected ','";
  1013. return ERR_PARSE_ERROR;
  1014. } else {
  1015. need_comma = false;
  1016. continue;
  1017. }
  1018. }
  1019. Variant v;
  1020. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  1021. if (err)
  1022. return err;
  1023. array.push_back(v);
  1024. need_comma = true;
  1025. }
  1026. }
  1027. Error VariantParser::_parse_dictionary(Dictionary &object, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) {
  1028. bool at_key = true;
  1029. Variant key;
  1030. Token token;
  1031. bool need_comma = false;
  1032. while (true) {
  1033. if (p_stream->is_eof()) {
  1034. r_err_str = "Unexpected End of File while parsing dictionary";
  1035. return ERR_FILE_CORRUPT;
  1036. }
  1037. if (at_key) {
  1038. Error err = get_token(p_stream, token, line, r_err_str);
  1039. if (err != OK)
  1040. return err;
  1041. if (token.type == TK_CURLY_BRACKET_CLOSE) {
  1042. return OK;
  1043. }
  1044. if (need_comma) {
  1045. if (token.type != TK_COMMA) {
  1046. r_err_str = "Expected '}' or ','";
  1047. return ERR_PARSE_ERROR;
  1048. } else {
  1049. need_comma = false;
  1050. continue;
  1051. }
  1052. }
  1053. err = parse_value(token, key, p_stream, line, r_err_str, p_res_parser);
  1054. if (err)
  1055. return err;
  1056. err = get_token(p_stream, token, line, r_err_str);
  1057. if (err != OK)
  1058. return err;
  1059. if (token.type != TK_COLON) {
  1060. r_err_str = "Expected ':'";
  1061. return ERR_PARSE_ERROR;
  1062. }
  1063. at_key = false;
  1064. } else {
  1065. Error err = get_token(p_stream, token, line, r_err_str);
  1066. if (err != OK)
  1067. return err;
  1068. Variant v;
  1069. err = parse_value(token, v, p_stream, line, r_err_str, p_res_parser);
  1070. if (err)
  1071. return err;
  1072. object[key] = v;
  1073. need_comma = true;
  1074. at_key = true;
  1075. }
  1076. }
  1077. }
  1078. 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) {
  1079. r_tag.fields.clear();
  1080. if (token.type != TK_BRACKET_OPEN) {
  1081. r_err_str = "Expected '['";
  1082. return ERR_PARSE_ERROR;
  1083. }
  1084. if (p_simple_tag) {
  1085. r_tag.name = "";
  1086. r_tag.fields.clear();
  1087. while (true) {
  1088. CharType c = p_stream->get_char();
  1089. if (p_stream->is_eof()) {
  1090. r_err_str = "Unexpected EOF while parsing simple tag";
  1091. return ERR_PARSE_ERROR;
  1092. }
  1093. if (c == ']')
  1094. break;
  1095. r_tag.name += String::chr(c);
  1096. }
  1097. r_tag.name = r_tag.name.strip_edges();
  1098. return OK;
  1099. }
  1100. get_token(p_stream, token, line, r_err_str);
  1101. if (token.type != TK_IDENTIFIER) {
  1102. r_err_str = "Expected identifier (tag name)";
  1103. return ERR_PARSE_ERROR;
  1104. }
  1105. r_tag.name = token.value;
  1106. bool parsing_tag = true;
  1107. while (true) {
  1108. if (p_stream->is_eof()) {
  1109. r_err_str = "Unexpected End of File while parsing tag: " + r_tag.name;
  1110. return ERR_FILE_CORRUPT;
  1111. }
  1112. get_token(p_stream, token, line, r_err_str);
  1113. if (token.type == TK_BRACKET_CLOSE)
  1114. break;
  1115. if (parsing_tag && token.type == TK_PERIOD) {
  1116. r_tag.name += "."; //support tags such as [someprop.Android] for specific platforms
  1117. get_token(p_stream, token, line, r_err_str);
  1118. } else if (parsing_tag && token.type == TK_COLON) {
  1119. r_tag.name += ":"; //support tags such as [someprop.Android] for specific platforms
  1120. get_token(p_stream, token, line, r_err_str);
  1121. } else {
  1122. parsing_tag = false;
  1123. }
  1124. if (token.type != TK_IDENTIFIER) {
  1125. r_err_str = "Expected Identifier";
  1126. return ERR_PARSE_ERROR;
  1127. }
  1128. String id = token.value;
  1129. if (parsing_tag) {
  1130. r_tag.name += id;
  1131. continue;
  1132. }
  1133. get_token(p_stream, token, line, r_err_str);
  1134. if (token.type != TK_EQUAL) {
  1135. return ERR_PARSE_ERROR;
  1136. }
  1137. get_token(p_stream, token, line, r_err_str);
  1138. Variant value;
  1139. Error err = parse_value(token, value, p_stream, line, r_err_str, p_res_parser);
  1140. if (err)
  1141. return err;
  1142. r_tag.fields[id] = value;
  1143. }
  1144. return OK;
  1145. }
  1146. Error VariantParser::parse_tag(Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, ResourceParser *p_res_parser, bool p_simple_tag) {
  1147. Token token;
  1148. get_token(p_stream, token, line, r_err_str);
  1149. if (token.type == TK_EOF) {
  1150. return ERR_FILE_EOF;
  1151. }
  1152. if (token.type != TK_BRACKET_OPEN) {
  1153. r_err_str = "Expected '['";
  1154. return ERR_PARSE_ERROR;
  1155. }
  1156. return _parse_tag(token, p_stream, line, r_err_str, r_tag, p_res_parser, p_simple_tag);
  1157. }
  1158. 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) {
  1159. //assign..
  1160. r_assign = "";
  1161. String what;
  1162. while (true) {
  1163. CharType c;
  1164. if (p_stream->saved) {
  1165. c = p_stream->saved;
  1166. p_stream->saved = 0;
  1167. } else {
  1168. c = p_stream->get_char();
  1169. }
  1170. if (p_stream->is_eof())
  1171. return ERR_FILE_EOF;
  1172. if (c == ';') { //comment
  1173. while (true) {
  1174. CharType ch = p_stream->get_char();
  1175. if (p_stream->is_eof()) {
  1176. return ERR_FILE_EOF;
  1177. }
  1178. if (ch == '\n')
  1179. break;
  1180. }
  1181. continue;
  1182. }
  1183. if (c == '[' && what.length() == 0) {
  1184. //it's a tag!
  1185. p_stream->saved = '['; //go back one
  1186. Error err = parse_tag(p_stream, line, r_err_str, r_tag, p_res_parser, p_simple_tag);
  1187. return err;
  1188. }
  1189. if (c > 32) {
  1190. if (c == '"') { //quoted
  1191. p_stream->saved = '"';
  1192. Token tk;
  1193. Error err = get_token(p_stream, tk, line, r_err_str);
  1194. if (err)
  1195. return err;
  1196. if (tk.type != TK_STRING) {
  1197. r_err_str = "Error reading quoted string";
  1198. return ERR_INVALID_DATA;
  1199. }
  1200. what = tk.value;
  1201. } else if (c != '=') {
  1202. what += String::chr(c);
  1203. } else {
  1204. r_assign = what;
  1205. Token token;
  1206. get_token(p_stream, token, line, r_err_str);
  1207. Error err = parse_value(token, r_value, p_stream, line, r_err_str, p_res_parser);
  1208. return err;
  1209. }
  1210. } else if (c == '\n') {
  1211. line++;
  1212. }
  1213. }
  1214. }
  1215. Error VariantParser::parse(Stream *p_stream, Variant &r_ret, String &r_err_str, int &r_err_line, ResourceParser *p_res_parser) {
  1216. Token token;
  1217. Error err = get_token(p_stream, token, r_err_line, r_err_str);
  1218. if (err)
  1219. return err;
  1220. if (token.type == TK_EOF) {
  1221. return ERR_FILE_EOF;
  1222. }
  1223. return parse_value(token, r_ret, p_stream, r_err_line, r_err_str, p_res_parser);
  1224. }
  1225. //////////////////////////////////////////////////////////////////////////////////
  1226. //////////////////////////////////////////////////////////////////////////////////
  1227. ////////////////////////////////////////////////////////////////////////////////
  1228. static String rtosfix(double p_value) {
  1229. if (p_value == 0.0)
  1230. return "0"; //avoid negative zero (-0) being written, which may annoy git, svn, etc. for changes when they don't exist.
  1231. else
  1232. return rtoss(p_value);
  1233. }
  1234. 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) {
  1235. switch (p_variant.get_type()) {
  1236. case Variant::NIL: {
  1237. p_store_string_func(p_store_string_ud, "null");
  1238. } break;
  1239. case Variant::BOOL: {
  1240. p_store_string_func(p_store_string_ud, p_variant.operator bool() ? "true" : "false");
  1241. } break;
  1242. case Variant::INT: {
  1243. p_store_string_func(p_store_string_ud, itos(p_variant.operator int64_t()));
  1244. } break;
  1245. case Variant::REAL: {
  1246. String s = rtosfix(p_variant.operator real_t());
  1247. if (s != "inf" && s != "nan") {
  1248. if (s.find(".") == -1 && s.find("e") == -1)
  1249. s += ".0";
  1250. }
  1251. p_store_string_func(p_store_string_ud, s);
  1252. } break;
  1253. case Variant::STRING: {
  1254. String str = p_variant;
  1255. str = "\"" + str.c_escape_multiline() + "\"";
  1256. p_store_string_func(p_store_string_ud, str);
  1257. } break;
  1258. case Variant::VECTOR2: {
  1259. Vector2 v = p_variant;
  1260. p_store_string_func(p_store_string_ud, "Vector2( " + rtosfix(v.x) + ", " + rtosfix(v.y) + " )");
  1261. } break;
  1262. case Variant::RECT2: {
  1263. Rect2 aabb = p_variant;
  1264. 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) + " )");
  1265. } break;
  1266. case Variant::VECTOR3: {
  1267. Vector3 v = p_variant;
  1268. p_store_string_func(p_store_string_ud, "Vector3( " + rtosfix(v.x) + ", " + rtosfix(v.y) + ", " + rtosfix(v.z) + " )");
  1269. } break;
  1270. case Variant::PLANE: {
  1271. Plane p = p_variant;
  1272. p_store_string_func(p_store_string_ud, "Plane( " + rtosfix(p.normal.x) + ", " + rtosfix(p.normal.y) + ", " + rtosfix(p.normal.z) + ", " + rtosfix(p.d) + " )");
  1273. } break;
  1274. case Variant::AABB: {
  1275. AABB aabb = p_variant;
  1276. 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) + " )");
  1277. } break;
  1278. case Variant::QUAT: {
  1279. Quat quat = p_variant;
  1280. p_store_string_func(p_store_string_ud, "Quat( " + rtosfix(quat.x) + ", " + rtosfix(quat.y) + ", " + rtosfix(quat.z) + ", " + rtosfix(quat.w) + " )");
  1281. } break;
  1282. case Variant::TRANSFORM2D: {
  1283. String s = "Transform2D( ";
  1284. Transform2D m3 = p_variant;
  1285. for (int i = 0; i < 3; i++) {
  1286. for (int j = 0; j < 2; j++) {
  1287. if (i != 0 || j != 0)
  1288. s += ", ";
  1289. s += rtosfix(m3.elements[i][j]);
  1290. }
  1291. }
  1292. p_store_string_func(p_store_string_ud, s + " )");
  1293. } break;
  1294. case Variant::BASIS: {
  1295. String s = "Basis( ";
  1296. Basis m3 = p_variant;
  1297. for (int i = 0; i < 3; i++) {
  1298. for (int j = 0; j < 3; j++) {
  1299. if (i != 0 || j != 0)
  1300. s += ", ";
  1301. s += rtosfix(m3.elements[i][j]);
  1302. }
  1303. }
  1304. p_store_string_func(p_store_string_ud, s + " )");
  1305. } break;
  1306. case Variant::TRANSFORM: {
  1307. String s = "Transform( ";
  1308. Transform t = p_variant;
  1309. Basis &m3 = t.basis;
  1310. for (int i = 0; i < 3; i++) {
  1311. for (int j = 0; j < 3; j++) {
  1312. if (i != 0 || j != 0)
  1313. s += ", ";
  1314. s += rtosfix(m3.elements[i][j]);
  1315. }
  1316. }
  1317. s = s + ", " + rtosfix(t.origin.x) + ", " + rtosfix(t.origin.y) + ", " + rtosfix(t.origin.z);
  1318. p_store_string_func(p_store_string_ud, s + " )");
  1319. } break;
  1320. // misc types
  1321. case Variant::COLOR: {
  1322. Color c = p_variant;
  1323. p_store_string_func(p_store_string_ud, "Color( " + rtosfix(c.r) + ", " + rtosfix(c.g) + ", " + rtosfix(c.b) + ", " + rtosfix(c.a) + " )");
  1324. } break;
  1325. case Variant::NODE_PATH: {
  1326. String str = p_variant;
  1327. str = "NodePath(\"" + str.c_escape() + "\")";
  1328. p_store_string_func(p_store_string_ud, str);
  1329. } break;
  1330. case Variant::OBJECT: {
  1331. Object *obj = p_variant;
  1332. if (!obj) {
  1333. p_store_string_func(p_store_string_ud, "null");
  1334. break; // don't save it
  1335. }
  1336. RES res = p_variant;
  1337. if (res.is_valid()) {
  1338. //is resource
  1339. String res_text;
  1340. //try external function
  1341. if (p_encode_res_func) {
  1342. res_text = p_encode_res_func(p_encode_res_ud, res);
  1343. }
  1344. //try path because it's a file
  1345. if (res_text == String() && res->get_path().is_resource_file()) {
  1346. //external resource
  1347. String path = res->get_path();
  1348. res_text = "Resource( \"" + path + "\")";
  1349. }
  1350. //could come up with some sort of text
  1351. if (res_text != String()) {
  1352. p_store_string_func(p_store_string_ud, res_text);
  1353. break;
  1354. }
  1355. }
  1356. //store as generic object
  1357. p_store_string_func(p_store_string_ud, "Object(" + obj->get_class() + ",");
  1358. List<PropertyInfo> props;
  1359. obj->get_property_list(&props);
  1360. bool first = true;
  1361. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1362. if (E->get().usage & PROPERTY_USAGE_STORAGE || E->get().usage & PROPERTY_USAGE_SCRIPT_VARIABLE) {
  1363. //must be serialized
  1364. if (first) {
  1365. first = false;
  1366. } else {
  1367. p_store_string_func(p_store_string_ud, ",");
  1368. }
  1369. p_store_string_func(p_store_string_ud, "\"" + E->get().name + "\":");
  1370. write(obj->get(E->get().name), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1371. }
  1372. }
  1373. p_store_string_func(p_store_string_ud, ")\n");
  1374. } break;
  1375. case Variant::DICTIONARY: {
  1376. Dictionary dict = p_variant;
  1377. List<Variant> keys;
  1378. dict.get_key_list(&keys);
  1379. keys.sort();
  1380. p_store_string_func(p_store_string_ud, "{\n");
  1381. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1382. /*
  1383. if (!_check_type(dict[E->get()]))
  1384. continue;
  1385. */
  1386. write(E->get(), p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1387. p_store_string_func(p_store_string_ud, ": ");
  1388. write(dict[E->get()], p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1389. if (E->next())
  1390. p_store_string_func(p_store_string_ud, ",\n");
  1391. }
  1392. p_store_string_func(p_store_string_ud, "\n}");
  1393. } break;
  1394. case Variant::ARRAY: {
  1395. p_store_string_func(p_store_string_ud, "[ ");
  1396. Array array = p_variant;
  1397. int len = array.size();
  1398. for (int i = 0; i < len; i++) {
  1399. if (i > 0)
  1400. p_store_string_func(p_store_string_ud, ", ");
  1401. write(array[i], p_store_string_func, p_store_string_ud, p_encode_res_func, p_encode_res_ud);
  1402. }
  1403. p_store_string_func(p_store_string_ud, " ]");
  1404. } break;
  1405. case Variant::POOL_BYTE_ARRAY: {
  1406. p_store_string_func(p_store_string_ud, "PoolByteArray( ");
  1407. String s;
  1408. PoolVector<uint8_t> data = p_variant;
  1409. int len = data.size();
  1410. PoolVector<uint8_t>::Read r = data.read();
  1411. const uint8_t *ptr = r.ptr();
  1412. for (int i = 0; i < len; i++) {
  1413. if (i > 0)
  1414. p_store_string_func(p_store_string_ud, ", ");
  1415. p_store_string_func(p_store_string_ud, itos(ptr[i]));
  1416. }
  1417. p_store_string_func(p_store_string_ud, " )");
  1418. } break;
  1419. case Variant::POOL_INT_ARRAY: {
  1420. p_store_string_func(p_store_string_ud, "PoolIntArray( ");
  1421. PoolVector<int> data = p_variant;
  1422. int len = data.size();
  1423. PoolVector<int>::Read r = data.read();
  1424. const int *ptr = r.ptr();
  1425. for (int i = 0; i < len; i++) {
  1426. if (i > 0)
  1427. p_store_string_func(p_store_string_ud, ", ");
  1428. p_store_string_func(p_store_string_ud, itos(ptr[i]));
  1429. }
  1430. p_store_string_func(p_store_string_ud, " )");
  1431. } break;
  1432. case Variant::POOL_REAL_ARRAY: {
  1433. p_store_string_func(p_store_string_ud, "PoolRealArray( ");
  1434. PoolVector<real_t> data = p_variant;
  1435. int len = data.size();
  1436. PoolVector<real_t>::Read r = data.read();
  1437. const real_t *ptr = r.ptr();
  1438. for (int i = 0; i < len; i++) {
  1439. if (i > 0)
  1440. p_store_string_func(p_store_string_ud, ", ");
  1441. p_store_string_func(p_store_string_ud, rtosfix(ptr[i]));
  1442. }
  1443. p_store_string_func(p_store_string_ud, " )");
  1444. } break;
  1445. case Variant::POOL_STRING_ARRAY: {
  1446. p_store_string_func(p_store_string_ud, "PoolStringArray( ");
  1447. PoolVector<String> data = p_variant;
  1448. int len = data.size();
  1449. PoolVector<String>::Read r = data.read();
  1450. const String *ptr = r.ptr();
  1451. String s;
  1452. //write_string("\n");
  1453. for (int i = 0; i < len; i++) {
  1454. if (i > 0)
  1455. p_store_string_func(p_store_string_ud, ", ");
  1456. String str = ptr[i];
  1457. p_store_string_func(p_store_string_ud, "\"" + str.c_escape() + "\"");
  1458. }
  1459. p_store_string_func(p_store_string_ud, " )");
  1460. } break;
  1461. case Variant::POOL_VECTOR2_ARRAY: {
  1462. p_store_string_func(p_store_string_ud, "PoolVector2Array( ");
  1463. PoolVector<Vector2> data = p_variant;
  1464. int len = data.size();
  1465. PoolVector<Vector2>::Read r = data.read();
  1466. const Vector2 *ptr = r.ptr();
  1467. for (int i = 0; i < len; i++) {
  1468. if (i > 0)
  1469. p_store_string_func(p_store_string_ud, ", ");
  1470. p_store_string_func(p_store_string_ud, rtosfix(ptr[i].x) + ", " + rtosfix(ptr[i].y));
  1471. }
  1472. p_store_string_func(p_store_string_ud, " )");
  1473. } break;
  1474. case Variant::POOL_VECTOR3_ARRAY: {
  1475. p_store_string_func(p_store_string_ud, "PoolVector3Array( ");
  1476. PoolVector<Vector3> data = p_variant;
  1477. int len = data.size();
  1478. PoolVector<Vector3>::Read r = data.read();
  1479. const Vector3 *ptr = r.ptr();
  1480. for (int i = 0; i < len; i++) {
  1481. if (i > 0)
  1482. p_store_string_func(p_store_string_ud, ", ");
  1483. p_store_string_func(p_store_string_ud, rtosfix(ptr[i].x) + ", " + rtosfix(ptr[i].y) + ", " + rtosfix(ptr[i].z));
  1484. }
  1485. p_store_string_func(p_store_string_ud, " )");
  1486. } break;
  1487. case Variant::POOL_COLOR_ARRAY: {
  1488. p_store_string_func(p_store_string_ud, "PoolColorArray( ");
  1489. PoolVector<Color> data = p_variant;
  1490. int len = data.size();
  1491. PoolVector<Color>::Read r = data.read();
  1492. const Color *ptr = r.ptr();
  1493. for (int i = 0; i < len; i++) {
  1494. if (i > 0)
  1495. p_store_string_func(p_store_string_ud, ", ");
  1496. p_store_string_func(p_store_string_ud, rtosfix(ptr[i].r) + ", " + rtosfix(ptr[i].g) + ", " + rtosfix(ptr[i].b) + ", " + rtosfix(ptr[i].a));
  1497. }
  1498. p_store_string_func(p_store_string_ud, " )");
  1499. } break;
  1500. default: {
  1501. }
  1502. }
  1503. return OK;
  1504. }
  1505. static Error _write_to_str(void *ud, const String &p_string) {
  1506. String *str = (String *)ud;
  1507. (*str) += p_string;
  1508. return OK;
  1509. }
  1510. Error VariantWriter::write_to_string(const Variant &p_variant, String &r_string, EncodeResourceFunc p_encode_res_func, void *p_encode_res_ud) {
  1511. r_string = String();
  1512. return write(p_variant, _write_to_str, &r_string, p_encode_res_func, p_encode_res_ud);
  1513. }