variant_parser.cpp 45 KB

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