script_class_parser.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /*************************************************************************/
  2. /* script_class_parser.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "script_class_parser.h"
  31. #include "core/map.h"
  32. #include "core/os/os.h"
  33. #include "../utils/string_utils.h"
  34. const char *ScriptClassParser::token_names[ScriptClassParser::TK_MAX] = {
  35. "[",
  36. "]",
  37. "{",
  38. "}",
  39. ".",
  40. ":",
  41. ",",
  42. "Symbol",
  43. "Identifier",
  44. "String",
  45. "Number",
  46. "<",
  47. ">",
  48. "EOF",
  49. "Error"
  50. };
  51. String ScriptClassParser::get_token_name(ScriptClassParser::Token p_token) {
  52. ERR_FAIL_INDEX_V(p_token, TK_MAX, "<error>");
  53. return token_names[p_token];
  54. }
  55. ScriptClassParser::Token ScriptClassParser::get_token() {
  56. while (true) {
  57. switch (code[idx]) {
  58. case '\n': {
  59. line++;
  60. idx++;
  61. break;
  62. };
  63. case 0: {
  64. return TK_EOF;
  65. } break;
  66. case '{': {
  67. idx++;
  68. return TK_CURLY_BRACKET_OPEN;
  69. };
  70. case '}': {
  71. idx++;
  72. return TK_CURLY_BRACKET_CLOSE;
  73. };
  74. case '[': {
  75. idx++;
  76. return TK_BRACKET_OPEN;
  77. };
  78. case ']': {
  79. idx++;
  80. return TK_BRACKET_CLOSE;
  81. };
  82. case '<': {
  83. idx++;
  84. return TK_OP_LESS;
  85. };
  86. case '>': {
  87. idx++;
  88. return TK_OP_GREATER;
  89. };
  90. case ':': {
  91. idx++;
  92. return TK_COLON;
  93. };
  94. case ',': {
  95. idx++;
  96. return TK_COMMA;
  97. };
  98. case '.': {
  99. idx++;
  100. return TK_PERIOD;
  101. };
  102. case '#': {
  103. //compiler directive
  104. while (code[idx] != '\n' && code[idx] != 0) {
  105. idx++;
  106. }
  107. continue;
  108. } break;
  109. case '/': {
  110. switch (code[idx + 1]) {
  111. case '*': { // block comment
  112. idx += 2;
  113. while (true) {
  114. if (code[idx] == 0) {
  115. error_str = "Unterminated comment";
  116. error = true;
  117. return TK_ERROR;
  118. } else if (code[idx] == '*' && code[idx + 1] == '/') {
  119. idx += 2;
  120. break;
  121. } else if (code[idx] == '\n') {
  122. line++;
  123. }
  124. idx++;
  125. }
  126. } break;
  127. case '/': { // line comment skip
  128. while (code[idx] != '\n' && code[idx] != 0) {
  129. idx++;
  130. }
  131. } break;
  132. default: {
  133. value = "/";
  134. idx++;
  135. return TK_SYMBOL;
  136. }
  137. }
  138. continue; // a comment
  139. } break;
  140. case '\'':
  141. case '"': {
  142. bool verbatim = idx != 0 && code[idx - 1] == '@';
  143. CharType begin_str = code[idx];
  144. idx++;
  145. String tk_string = String();
  146. while (true) {
  147. if (code[idx] == 0) {
  148. error_str = "Unterminated String";
  149. error = true;
  150. return TK_ERROR;
  151. } else if (code[idx] == begin_str) {
  152. if (verbatim && code[idx + 1] == '"') { // `""` is verbatim string's `\"`
  153. idx += 2; // skip next `"` as well
  154. continue;
  155. }
  156. idx += 1;
  157. break;
  158. } else if (code[idx] == '\\' && !verbatim) {
  159. //escaped characters...
  160. idx++;
  161. CharType next = code[idx];
  162. if (next == 0) {
  163. error_str = "Unterminated String";
  164. error = true;
  165. return TK_ERROR;
  166. }
  167. CharType res = 0;
  168. switch (next) {
  169. case 'b': res = 8; break;
  170. case 't': res = 9; break;
  171. case 'n': res = 10; break;
  172. case 'f': res = 12; break;
  173. case 'r':
  174. res = 13;
  175. break;
  176. case '\"': res = '\"'; break;
  177. case '\\':
  178. res = '\\';
  179. break;
  180. default: {
  181. res = next;
  182. } break;
  183. }
  184. tk_string += res;
  185. } else {
  186. if (code[idx] == '\n')
  187. line++;
  188. tk_string += code[idx];
  189. }
  190. idx++;
  191. }
  192. value = tk_string;
  193. return TK_STRING;
  194. } break;
  195. default: {
  196. if (code[idx] <= 32) {
  197. idx++;
  198. break;
  199. }
  200. if ((code[idx] >= 33 && code[idx] <= 47) || (code[idx] >= 58 && code[idx] <= 63) || (code[idx] >= 91 && code[idx] <= 94) || code[idx] == 96 || (code[idx] >= 123 && code[idx] <= 127)) {
  201. value = String::chr(code[idx]);
  202. idx++;
  203. return TK_SYMBOL;
  204. }
  205. if (code[idx] == '-' || (code[idx] >= '0' && code[idx] <= '9')) {
  206. //a number
  207. const CharType *rptr;
  208. double number = String::to_double(&code[idx], &rptr);
  209. idx += (rptr - &code[idx]);
  210. value = number;
  211. return TK_NUMBER;
  212. } else if ((code[idx] == '@' && code[idx + 1] != '"') || code[idx] == '_' || (code[idx] >= 'A' && code[idx] <= 'Z') || (code[idx] >= 'a' && code[idx] <= 'z') || code[idx] > 127) {
  213. String id;
  214. id += code[idx];
  215. idx++;
  216. while (code[idx] == '_' || (code[idx] >= 'A' && code[idx] <= 'Z') || (code[idx] >= 'a' && code[idx] <= 'z') || (code[idx] >= '0' && code[idx] <= '9') || code[idx] > 127) {
  217. id += code[idx];
  218. idx++;
  219. }
  220. value = id;
  221. return TK_IDENTIFIER;
  222. } else if (code[idx] == '@' && code[idx + 1] == '"') {
  223. // begin of verbatim string
  224. idx++;
  225. } else {
  226. error_str = "Unexpected character.";
  227. error = true;
  228. return TK_ERROR;
  229. }
  230. }
  231. }
  232. }
  233. }
  234. Error ScriptClassParser::_skip_generic_type_params() {
  235. Token tk;
  236. while (true) {
  237. tk = get_token();
  238. if (tk == TK_IDENTIFIER) {
  239. tk = get_token();
  240. if (tk == TK_PERIOD) {
  241. while (true) {
  242. tk = get_token();
  243. if (tk != TK_IDENTIFIER) {
  244. error_str = "Expected " + get_token_name(TK_IDENTIFIER) + ", found: " + get_token_name(tk);
  245. error = true;
  246. return ERR_PARSE_ERROR;
  247. }
  248. tk = get_token();
  249. if (tk != TK_PERIOD)
  250. break;
  251. }
  252. }
  253. if (tk == TK_OP_LESS) {
  254. Error err = _skip_generic_type_params();
  255. if (err)
  256. return err;
  257. continue;
  258. } else if (tk == TK_OP_GREATER) {
  259. return OK;
  260. } else if (tk != TK_COMMA) {
  261. error_str = "Unexpected token: " + get_token_name(tk);
  262. error = true;
  263. return ERR_PARSE_ERROR;
  264. }
  265. } else if (tk == TK_OP_LESS) {
  266. error_str = "Expected " + get_token_name(TK_IDENTIFIER) + ", found " + get_token_name(TK_OP_LESS);
  267. error = true;
  268. return ERR_PARSE_ERROR;
  269. } else if (tk == TK_OP_GREATER) {
  270. return OK;
  271. } else {
  272. error_str = "Unexpected token: " + get_token_name(tk);
  273. error = true;
  274. return ERR_PARSE_ERROR;
  275. }
  276. }
  277. }
  278. Error ScriptClassParser::_parse_type_full_name(String &r_full_name) {
  279. Token tk = get_token();
  280. if (tk != TK_IDENTIFIER) {
  281. error_str = "Expected " + get_token_name(TK_IDENTIFIER) + ", found: " + get_token_name(tk);
  282. error = true;
  283. return ERR_PARSE_ERROR;
  284. }
  285. r_full_name += String(value);
  286. if (code[idx] != '.') // We only want to take the next token if it's a period
  287. return OK;
  288. tk = get_token();
  289. CRASH_COND(tk != TK_PERIOD); // Assertion
  290. r_full_name += ".";
  291. return _parse_type_full_name(r_full_name);
  292. }
  293. Error ScriptClassParser::_parse_class_base(Vector<String> &r_base) {
  294. String name;
  295. Error err = _parse_type_full_name(name);
  296. if (err)
  297. return err;
  298. Token tk = get_token();
  299. bool generic = false;
  300. if (tk == TK_OP_LESS) {
  301. Error err = _skip_generic_type_params();
  302. if (err)
  303. return err;
  304. // We don't add it to the base list if it's generic
  305. generic = true;
  306. tk = get_token();
  307. }
  308. if (tk == TK_COMMA) {
  309. Error err = _parse_class_base(r_base);
  310. if (err)
  311. return err;
  312. } else if (tk == TK_IDENTIFIER && String(value) == "where") {
  313. Error err = _parse_type_constraints();
  314. if (err) {
  315. return err;
  316. }
  317. // An open curly bracket was parsed by _parse_type_constraints, so we can exit
  318. } else if (tk == TK_CURLY_BRACKET_OPEN) {
  319. // we are finished when we hit the open curly bracket
  320. } else {
  321. error_str = "Unexpected token: " + get_token_name(tk);
  322. error = true;
  323. return ERR_PARSE_ERROR;
  324. }
  325. if (!generic) {
  326. r_base.push_back(name);
  327. }
  328. return OK;
  329. }
  330. Error ScriptClassParser::_parse_type_constraints() {
  331. Token tk = get_token();
  332. if (tk != TK_IDENTIFIER) {
  333. error_str = "Unexpected token: " + get_token_name(tk);
  334. error = true;
  335. return ERR_PARSE_ERROR;
  336. }
  337. tk = get_token();
  338. if (tk != TK_COLON) {
  339. error_str = "Unexpected token: " + get_token_name(tk);
  340. error = true;
  341. return ERR_PARSE_ERROR;
  342. }
  343. while (true) {
  344. tk = get_token();
  345. if (tk == TK_IDENTIFIER) {
  346. if (String(value) == "where") {
  347. return _parse_type_constraints();
  348. }
  349. tk = get_token();
  350. if (tk == TK_PERIOD) {
  351. while (true) {
  352. tk = get_token();
  353. if (tk != TK_IDENTIFIER) {
  354. error_str = "Expected " + get_token_name(TK_IDENTIFIER) + ", found: " + get_token_name(tk);
  355. error = true;
  356. return ERR_PARSE_ERROR;
  357. }
  358. tk = get_token();
  359. if (tk != TK_PERIOD)
  360. break;
  361. }
  362. }
  363. }
  364. if (tk == TK_COMMA) {
  365. continue;
  366. } else if (tk == TK_IDENTIFIER && String(value) == "where") {
  367. return _parse_type_constraints();
  368. } else if (tk == TK_SYMBOL && String(value) == "(") {
  369. tk = get_token();
  370. if (tk != TK_SYMBOL || String(value) != ")") {
  371. error_str = "Unexpected token: " + get_token_name(tk);
  372. error = true;
  373. return ERR_PARSE_ERROR;
  374. }
  375. } else if (tk == TK_OP_LESS) {
  376. Error err = _skip_generic_type_params();
  377. if (err)
  378. return err;
  379. } else if (tk == TK_CURLY_BRACKET_OPEN) {
  380. return OK;
  381. } else {
  382. error_str = "Unexpected token: " + get_token_name(tk);
  383. error = true;
  384. return ERR_PARSE_ERROR;
  385. }
  386. }
  387. }
  388. Error ScriptClassParser::_parse_namespace_name(String &r_name, int &r_curly_stack) {
  389. Token tk = get_token();
  390. if (tk == TK_IDENTIFIER) {
  391. r_name += String(value);
  392. } else {
  393. error_str = "Unexpected token: " + get_token_name(tk);
  394. error = true;
  395. return ERR_PARSE_ERROR;
  396. }
  397. tk = get_token();
  398. if (tk == TK_PERIOD) {
  399. r_name += ".";
  400. return _parse_namespace_name(r_name, r_curly_stack);
  401. } else if (tk == TK_CURLY_BRACKET_OPEN) {
  402. r_curly_stack++;
  403. return OK;
  404. } else {
  405. error_str = "Unexpected token: " + get_token_name(tk);
  406. error = true;
  407. return ERR_PARSE_ERROR;
  408. }
  409. }
  410. Error ScriptClassParser::parse(const String &p_code) {
  411. code = p_code;
  412. idx = 0;
  413. line = 0;
  414. error_str = String();
  415. error = false;
  416. value = Variant();
  417. classes.clear();
  418. Token tk = get_token();
  419. Map<int, NameDecl> name_stack;
  420. int curly_stack = 0;
  421. int type_curly_stack = 0;
  422. while (!error && tk != TK_EOF) {
  423. if (tk == TK_IDENTIFIER && String(value) == "class") {
  424. tk = get_token();
  425. if (tk == TK_IDENTIFIER) {
  426. String name = value;
  427. int at_level = type_curly_stack;
  428. ClassDecl class_decl;
  429. for (Map<int, NameDecl>::Element *E = name_stack.front(); E; E = E->next()) {
  430. const NameDecl &name_decl = E->value();
  431. if (name_decl.type == NameDecl::NAMESPACE_DECL) {
  432. if (E != name_stack.front())
  433. class_decl.namespace_ += ".";
  434. class_decl.namespace_ += name_decl.name;
  435. } else {
  436. class_decl.name += name_decl.name + ".";
  437. }
  438. }
  439. class_decl.name += name;
  440. class_decl.nested = type_curly_stack > 0;
  441. bool generic = false;
  442. while (true) {
  443. tk = get_token();
  444. if (tk == TK_COLON) {
  445. Error err = _parse_class_base(class_decl.base);
  446. if (err)
  447. return err;
  448. curly_stack++;
  449. type_curly_stack++;
  450. break;
  451. } else if (tk == TK_CURLY_BRACKET_OPEN) {
  452. curly_stack++;
  453. type_curly_stack++;
  454. break;
  455. } else if (tk == TK_OP_LESS && !generic) {
  456. generic = true;
  457. Error err = _skip_generic_type_params();
  458. if (err)
  459. return err;
  460. } else if (tk == TK_IDENTIFIER && String(value) == "where") {
  461. Error err = _parse_type_constraints();
  462. if (err) {
  463. return err;
  464. }
  465. // An open curly bracket was parsed by _parse_type_constraints, so we can exit
  466. curly_stack++;
  467. type_curly_stack++;
  468. break;
  469. } else {
  470. error_str = "Unexpected token: " + get_token_name(tk);
  471. error = true;
  472. return ERR_PARSE_ERROR;
  473. }
  474. }
  475. NameDecl name_decl;
  476. name_decl.name = name;
  477. name_decl.type = NameDecl::CLASS_DECL;
  478. name_stack[at_level] = name_decl;
  479. if (!generic) { // no generics, thanks
  480. classes.push_back(class_decl);
  481. } else if (OS::get_singleton()->is_stdout_verbose()) {
  482. String full_name = class_decl.namespace_;
  483. if (full_name.length())
  484. full_name += ".";
  485. full_name += class_decl.name;
  486. OS::get_singleton()->print(String("Ignoring generic class declaration: " + class_decl.name).utf8());
  487. }
  488. }
  489. } else if (tk == TK_IDENTIFIER && String(value) == "struct") {
  490. String name;
  491. int at_level = type_curly_stack;
  492. while (true) {
  493. tk = get_token();
  494. if (tk == TK_IDENTIFIER && name.empty()) {
  495. name = String(value);
  496. } else if (tk == TK_CURLY_BRACKET_OPEN) {
  497. if (name.empty()) {
  498. error_str = "Expected " + get_token_name(TK_IDENTIFIER) + " after keyword `struct`, found " + get_token_name(TK_CURLY_BRACKET_OPEN);
  499. error = true;
  500. return ERR_PARSE_ERROR;
  501. }
  502. curly_stack++;
  503. type_curly_stack++;
  504. break;
  505. } else if (tk == TK_EOF) {
  506. error_str = "Expected " + get_token_name(TK_CURLY_BRACKET_OPEN) + " after struct decl, found " + get_token_name(TK_EOF);
  507. error = true;
  508. return ERR_PARSE_ERROR;
  509. }
  510. }
  511. NameDecl name_decl;
  512. name_decl.name = name;
  513. name_decl.type = NameDecl::STRUCT_DECL;
  514. name_stack[at_level] = name_decl;
  515. } else if (tk == TK_IDENTIFIER && String(value) == "namespace") {
  516. if (type_curly_stack > 0) {
  517. error_str = "Found namespace nested inside type.";
  518. error = true;
  519. return ERR_PARSE_ERROR;
  520. }
  521. String name;
  522. int at_level = curly_stack;
  523. Error err = _parse_namespace_name(name, curly_stack);
  524. if (err)
  525. return err;
  526. NameDecl name_decl;
  527. name_decl.name = name;
  528. name_decl.type = NameDecl::NAMESPACE_DECL;
  529. name_stack[at_level] = name_decl;
  530. } else if (tk == TK_CURLY_BRACKET_OPEN) {
  531. curly_stack++;
  532. } else if (tk == TK_CURLY_BRACKET_CLOSE) {
  533. curly_stack--;
  534. if (name_stack.has(curly_stack)) {
  535. if (name_stack[curly_stack].type != NameDecl::NAMESPACE_DECL)
  536. type_curly_stack--;
  537. name_stack.erase(curly_stack);
  538. }
  539. }
  540. tk = get_token();
  541. }
  542. if (!error && tk == TK_EOF && curly_stack > 0) {
  543. error_str = "Reached EOF with missing close curly brackets.";
  544. error = true;
  545. }
  546. if (error)
  547. return ERR_PARSE_ERROR;
  548. return OK;
  549. }
  550. Error ScriptClassParser::parse_file(const String &p_filepath) {
  551. String source;
  552. Error ferr = read_all_file_utf8(p_filepath, source);
  553. if (ferr != OK) {
  554. if (ferr == ERR_INVALID_DATA) {
  555. ERR_EXPLAIN("File '" + p_filepath + "' contains invalid unicode (utf-8), so it was not loaded. Please ensure that scripts are saved in valid utf-8 unicode.");
  556. }
  557. ERR_FAIL_V(ferr);
  558. }
  559. return parse(source);
  560. }
  561. String ScriptClassParser::get_error() {
  562. return error_str;
  563. }
  564. Vector<ScriptClassParser::ClassDecl> ScriptClassParser::get_classes() {
  565. return classes;
  566. }