variant_parser.cpp 47 KB

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