variant_parser.cpp 45 KB

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