gdscript_tokenizer.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  1. /*************************************************************************/
  2. /* gdscript_tokenizer.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 "gdscript_tokenizer.h"
  31. #include "core/io/marshalls.h"
  32. #include "core/map.h"
  33. #include "core/print_string.h"
  34. #include "gdscript_functions.h"
  35. const char *GDScriptTokenizer::token_names[TK_MAX] = {
  36. "Empty",
  37. "Identifier",
  38. "Constant",
  39. "Self",
  40. "Built-In Type",
  41. "Built-In Func",
  42. "In",
  43. "'=='",
  44. "'!='",
  45. "'<'",
  46. "'<='",
  47. "'>'",
  48. "'>='",
  49. "'and'",
  50. "'or'",
  51. "'not'",
  52. "'+'",
  53. "'-'",
  54. "'*'",
  55. "'/'",
  56. "'%'",
  57. "'<<'",
  58. "'>>'",
  59. "'='",
  60. "'+='",
  61. "'-='",
  62. "'*='",
  63. "'/='",
  64. "'%='",
  65. "'<<='",
  66. "'>>='",
  67. "'&='",
  68. "'|='",
  69. "'^='",
  70. "'&'",
  71. "'|'",
  72. "'^'",
  73. "'~'",
  74. //"Plus Plus",
  75. //"Minus Minus",
  76. "if",
  77. "elif",
  78. "else",
  79. "for",
  80. "while",
  81. "break",
  82. "continue",
  83. "pass",
  84. "return",
  85. "match",
  86. "func",
  87. "class",
  88. "class_name",
  89. "extends",
  90. "is",
  91. "onready",
  92. "tool",
  93. "static",
  94. "export",
  95. "setget",
  96. "const",
  97. "var",
  98. "as",
  99. "void",
  100. "enum",
  101. "preload",
  102. "assert",
  103. "yield",
  104. "signal",
  105. "breakpoint",
  106. "remote",
  107. "master",
  108. "puppet",
  109. "remotesync",
  110. "mastersync",
  111. "puppetsync",
  112. "'['",
  113. "']'",
  114. "'{'",
  115. "'}'",
  116. "'('",
  117. "')'",
  118. "','",
  119. "';'",
  120. "'.'",
  121. "'?'",
  122. "':'",
  123. "'$'",
  124. "'->'",
  125. "'\\n'",
  126. "PI",
  127. "TAU",
  128. "_",
  129. "INF",
  130. "NAN",
  131. "Error",
  132. "EOF",
  133. "Cursor"
  134. };
  135. struct _bit {
  136. Variant::Type type;
  137. const char *text;
  138. };
  139. //built in types
  140. static const _bit _type_list[] = {
  141. //types
  142. { Variant::BOOL, "bool" },
  143. { Variant::INT, "int" },
  144. { Variant::REAL, "float" },
  145. { Variant::STRING, "String" },
  146. { Variant::VECTOR2, "Vector2" },
  147. { Variant::RECT2, "Rect2" },
  148. { Variant::TRANSFORM2D, "Transform2D" },
  149. { Variant::VECTOR3, "Vector3" },
  150. { Variant::AABB, "AABB" },
  151. { Variant::PLANE, "Plane" },
  152. { Variant::QUAT, "Quat" },
  153. { Variant::BASIS, "Basis" },
  154. { Variant::TRANSFORM, "Transform" },
  155. { Variant::COLOR, "Color" },
  156. { Variant::_RID, "RID" },
  157. { Variant::OBJECT, "Object" },
  158. { Variant::STRING_NAME, "StringName" },
  159. { Variant::NODE_PATH, "NodePath" },
  160. { Variant::DICTIONARY, "Dictionary" },
  161. { Variant::CALLABLE, "Callable" },
  162. { Variant::SIGNAL, "Signal" },
  163. { Variant::ARRAY, "Array" },
  164. { Variant::PACKED_BYTE_ARRAY, "PackedByteArray" },
  165. { Variant::PACKED_INT_ARRAY, "PackedIntArray" },
  166. { Variant::PACKED_REAL_ARRAY, "PackedRealArray" },
  167. { Variant::PACKED_STRING_ARRAY, "PackedStringArray" },
  168. { Variant::PACKED_VECTOR2_ARRAY, "PackedVector2Array" },
  169. { Variant::PACKED_VECTOR3_ARRAY, "PackedVector3Array" },
  170. { Variant::PACKED_COLOR_ARRAY, "PackedColorArray" },
  171. { Variant::VARIANT_MAX, NULL },
  172. };
  173. struct _kws {
  174. GDScriptTokenizer::Token token;
  175. const char *text;
  176. };
  177. static const _kws _keyword_list[] = {
  178. //ops
  179. { GDScriptTokenizer::TK_OP_IN, "in" },
  180. { GDScriptTokenizer::TK_OP_NOT, "not" },
  181. { GDScriptTokenizer::TK_OP_OR, "or" },
  182. { GDScriptTokenizer::TK_OP_AND, "and" },
  183. //func
  184. { GDScriptTokenizer::TK_PR_FUNCTION, "func" },
  185. { GDScriptTokenizer::TK_PR_CLASS, "class" },
  186. { GDScriptTokenizer::TK_PR_CLASS_NAME, "class_name" },
  187. { GDScriptTokenizer::TK_PR_EXTENDS, "extends" },
  188. { GDScriptTokenizer::TK_PR_IS, "is" },
  189. { GDScriptTokenizer::TK_PR_ONREADY, "onready" },
  190. { GDScriptTokenizer::TK_PR_TOOL, "tool" },
  191. { GDScriptTokenizer::TK_PR_STATIC, "static" },
  192. { GDScriptTokenizer::TK_PR_EXPORT, "export" },
  193. { GDScriptTokenizer::TK_PR_SETGET, "setget" },
  194. { GDScriptTokenizer::TK_PR_VAR, "var" },
  195. { GDScriptTokenizer::TK_PR_AS, "as" },
  196. { GDScriptTokenizer::TK_PR_VOID, "void" },
  197. { GDScriptTokenizer::TK_PR_PRELOAD, "preload" },
  198. { GDScriptTokenizer::TK_PR_ASSERT, "assert" },
  199. { GDScriptTokenizer::TK_PR_YIELD, "yield" },
  200. { GDScriptTokenizer::TK_PR_SIGNAL, "signal" },
  201. { GDScriptTokenizer::TK_PR_BREAKPOINT, "breakpoint" },
  202. { GDScriptTokenizer::TK_PR_REMOTE, "remote" },
  203. { GDScriptTokenizer::TK_PR_MASTER, "master" },
  204. { GDScriptTokenizer::TK_PR_PUPPET, "puppet" },
  205. { GDScriptTokenizer::TK_PR_REMOTESYNC, "remotesync" },
  206. { GDScriptTokenizer::TK_PR_MASTERSYNC, "mastersync" },
  207. { GDScriptTokenizer::TK_PR_PUPPETSYNC, "puppetsync" },
  208. { GDScriptTokenizer::TK_PR_CONST, "const" },
  209. { GDScriptTokenizer::TK_PR_ENUM, "enum" },
  210. //controlflow
  211. { GDScriptTokenizer::TK_CF_IF, "if" },
  212. { GDScriptTokenizer::TK_CF_ELIF, "elif" },
  213. { GDScriptTokenizer::TK_CF_ELSE, "else" },
  214. { GDScriptTokenizer::TK_CF_FOR, "for" },
  215. { GDScriptTokenizer::TK_CF_WHILE, "while" },
  216. { GDScriptTokenizer::TK_CF_BREAK, "break" },
  217. { GDScriptTokenizer::TK_CF_CONTINUE, "continue" },
  218. { GDScriptTokenizer::TK_CF_RETURN, "return" },
  219. { GDScriptTokenizer::TK_CF_MATCH, "match" },
  220. { GDScriptTokenizer::TK_CF_PASS, "pass" },
  221. { GDScriptTokenizer::TK_SELF, "self" },
  222. { GDScriptTokenizer::TK_CONST_PI, "PI" },
  223. { GDScriptTokenizer::TK_CONST_TAU, "TAU" },
  224. { GDScriptTokenizer::TK_WILDCARD, "_" },
  225. { GDScriptTokenizer::TK_CONST_INF, "INF" },
  226. { GDScriptTokenizer::TK_CONST_NAN, "NAN" },
  227. { GDScriptTokenizer::TK_ERROR, NULL }
  228. };
  229. const char *GDScriptTokenizer::get_token_name(Token p_token) {
  230. ERR_FAIL_INDEX_V(p_token, TK_MAX, "<error>");
  231. return token_names[p_token];
  232. }
  233. bool GDScriptTokenizer::is_token_literal(int p_offset, bool variable_safe) const {
  234. switch (get_token(p_offset)) {
  235. // Can always be literal:
  236. case TK_IDENTIFIER:
  237. case TK_PR_ONREADY:
  238. case TK_PR_TOOL:
  239. case TK_PR_STATIC:
  240. case TK_PR_EXPORT:
  241. case TK_PR_SETGET:
  242. case TK_PR_SIGNAL:
  243. case TK_PR_REMOTE:
  244. case TK_PR_MASTER:
  245. case TK_PR_PUPPET:
  246. case TK_PR_REMOTESYNC:
  247. case TK_PR_MASTERSYNC:
  248. case TK_PR_PUPPETSYNC:
  249. return true;
  250. // Literal for non-variables only:
  251. case TK_BUILT_IN_TYPE:
  252. case TK_BUILT_IN_FUNC:
  253. case TK_OP_IN:
  254. //case TK_OP_NOT:
  255. //case TK_OP_OR:
  256. //case TK_OP_AND:
  257. case TK_PR_CLASS:
  258. case TK_PR_CONST:
  259. case TK_PR_ENUM:
  260. case TK_PR_PRELOAD:
  261. case TK_PR_FUNCTION:
  262. case TK_PR_EXTENDS:
  263. case TK_PR_ASSERT:
  264. case TK_PR_YIELD:
  265. case TK_PR_VAR:
  266. case TK_CF_IF:
  267. case TK_CF_ELIF:
  268. case TK_CF_ELSE:
  269. case TK_CF_FOR:
  270. case TK_CF_WHILE:
  271. case TK_CF_BREAK:
  272. case TK_CF_CONTINUE:
  273. case TK_CF_RETURN:
  274. case TK_CF_MATCH:
  275. case TK_CF_PASS:
  276. case TK_SELF:
  277. case TK_CONST_PI:
  278. case TK_CONST_TAU:
  279. case TK_WILDCARD:
  280. case TK_CONST_INF:
  281. case TK_CONST_NAN:
  282. case TK_ERROR:
  283. return !variable_safe;
  284. case TK_CONSTANT: {
  285. switch (get_token_constant(p_offset).get_type()) {
  286. case Variant::NIL:
  287. case Variant::BOOL:
  288. return true;
  289. default:
  290. return false;
  291. }
  292. }
  293. default:
  294. return false;
  295. }
  296. }
  297. StringName GDScriptTokenizer::get_token_literal(int p_offset) const {
  298. Token token = get_token(p_offset);
  299. switch (token) {
  300. case TK_IDENTIFIER:
  301. return get_token_identifier(p_offset);
  302. case TK_BUILT_IN_TYPE: {
  303. Variant::Type type = get_token_type(p_offset);
  304. int idx = 0;
  305. while (_type_list[idx].text) {
  306. if (type == _type_list[idx].type) {
  307. return _type_list[idx].text;
  308. }
  309. idx++;
  310. }
  311. } break; // Shouldn't get here, stuff happens
  312. case TK_BUILT_IN_FUNC:
  313. return GDScriptFunctions::get_func_name(get_token_built_in_func(p_offset));
  314. case TK_CONSTANT: {
  315. const Variant value = get_token_constant(p_offset);
  316. switch (value.get_type()) {
  317. case Variant::NIL:
  318. return "null";
  319. case Variant::BOOL:
  320. return value ? "true" : "false";
  321. default: {
  322. }
  323. }
  324. }
  325. case TK_OP_AND:
  326. case TK_OP_OR:
  327. break; // Don't get into default, since they can be non-literal
  328. default: {
  329. int idx = 0;
  330. while (_keyword_list[idx].text) {
  331. if (token == _keyword_list[idx].token) {
  332. return _keyword_list[idx].text;
  333. }
  334. idx++;
  335. }
  336. }
  337. }
  338. ERR_FAIL_V_MSG("", "Failed to get token literal.");
  339. }
  340. static bool _is_text_char(CharType c) {
  341. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
  342. }
  343. static bool _is_number(CharType c) {
  344. return (c >= '0' && c <= '9');
  345. }
  346. static bool _is_hex(CharType c) {
  347. return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
  348. }
  349. static bool _is_bin(CharType c) {
  350. return (c == '0' || c == '1');
  351. }
  352. void GDScriptTokenizerText::_make_token(Token p_type) {
  353. TokenData &tk = tk_rb[tk_rb_pos];
  354. tk.type = p_type;
  355. tk.line = line;
  356. tk.col = column;
  357. tk_rb_pos = (tk_rb_pos + 1) % TK_RB_SIZE;
  358. }
  359. void GDScriptTokenizerText::_make_identifier(const StringName &p_identifier) {
  360. TokenData &tk = tk_rb[tk_rb_pos];
  361. tk.type = TK_IDENTIFIER;
  362. tk.identifier = p_identifier;
  363. tk.line = line;
  364. tk.col = column;
  365. tk_rb_pos = (tk_rb_pos + 1) % TK_RB_SIZE;
  366. }
  367. void GDScriptTokenizerText::_make_built_in_func(GDScriptFunctions::Function p_func) {
  368. TokenData &tk = tk_rb[tk_rb_pos];
  369. tk.type = TK_BUILT_IN_FUNC;
  370. tk.func = p_func;
  371. tk.line = line;
  372. tk.col = column;
  373. tk_rb_pos = (tk_rb_pos + 1) % TK_RB_SIZE;
  374. }
  375. void GDScriptTokenizerText::_make_constant(const Variant &p_constant) {
  376. TokenData &tk = tk_rb[tk_rb_pos];
  377. tk.type = TK_CONSTANT;
  378. tk.constant = p_constant;
  379. tk.line = line;
  380. tk.col = column;
  381. tk_rb_pos = (tk_rb_pos + 1) % TK_RB_SIZE;
  382. }
  383. void GDScriptTokenizerText::_make_type(const Variant::Type &p_type) {
  384. TokenData &tk = tk_rb[tk_rb_pos];
  385. tk.type = TK_BUILT_IN_TYPE;
  386. tk.vtype = p_type;
  387. tk.line = line;
  388. tk.col = column;
  389. tk_rb_pos = (tk_rb_pos + 1) % TK_RB_SIZE;
  390. }
  391. void GDScriptTokenizerText::_make_error(const String &p_error) {
  392. error_flag = true;
  393. last_error = p_error;
  394. TokenData &tk = tk_rb[tk_rb_pos];
  395. tk.type = TK_ERROR;
  396. tk.constant = p_error;
  397. tk.line = line;
  398. tk.col = column;
  399. tk_rb_pos = (tk_rb_pos + 1) % TK_RB_SIZE;
  400. }
  401. void GDScriptTokenizerText::_make_newline(int p_indentation, int p_tabs) {
  402. TokenData &tk = tk_rb[tk_rb_pos];
  403. tk.type = TK_NEWLINE;
  404. tk.constant = Vector2(p_indentation, p_tabs);
  405. tk.line = line;
  406. tk.col = column;
  407. tk_rb_pos = (tk_rb_pos + 1) % TK_RB_SIZE;
  408. }
  409. void GDScriptTokenizerText::_advance() {
  410. if (error_flag) {
  411. //parser broke
  412. _make_error(last_error);
  413. return;
  414. }
  415. if (code_pos >= len) {
  416. _make_token(TK_EOF);
  417. return;
  418. }
  419. #define GETCHAR(m_ofs) ((m_ofs + code_pos) >= len ? 0 : _code[m_ofs + code_pos])
  420. #define INCPOS(m_amount) \
  421. { \
  422. code_pos += m_amount; \
  423. column += m_amount; \
  424. }
  425. while (true) {
  426. bool is_string_name = false;
  427. StringMode string_mode = STRING_DOUBLE_QUOTE;
  428. switch (GETCHAR(0)) {
  429. case 0:
  430. _make_token(TK_EOF);
  431. break;
  432. case '\\':
  433. INCPOS(1);
  434. if (GETCHAR(0) == '\r') {
  435. INCPOS(1);
  436. }
  437. if (GETCHAR(0) != '\n') {
  438. _make_error("Expected newline after '\\'.");
  439. return;
  440. }
  441. INCPOS(1);
  442. line++;
  443. while (GETCHAR(0) == ' ' || GETCHAR(0) == '\t') {
  444. INCPOS(1);
  445. }
  446. continue;
  447. case '\t':
  448. case '\r':
  449. case ' ':
  450. INCPOS(1);
  451. continue;
  452. case '#': { // line comment skip
  453. #ifdef DEBUG_ENABLED
  454. String comment;
  455. #endif // DEBUG_ENABLED
  456. while (GETCHAR(0) != '\n') {
  457. #ifdef DEBUG_ENABLED
  458. comment += GETCHAR(0);
  459. #endif // DEBUG_ENABLED
  460. code_pos++;
  461. if (GETCHAR(0) == 0) { //end of file
  462. //_make_error("Unterminated Comment");
  463. _make_token(TK_EOF);
  464. return;
  465. }
  466. }
  467. #ifdef DEBUG_ENABLED
  468. String comment_content = comment.trim_prefix("#").trim_prefix(" ");
  469. if (comment_content.begins_with("warning-ignore:")) {
  470. String code = comment_content.get_slice(":", 1);
  471. warning_skips.push_back(Pair<int, String>(line, code.strip_edges().to_lower()));
  472. } else if (comment_content.begins_with("warning-ignore-all:")) {
  473. String code = comment_content.get_slice(":", 1);
  474. warning_global_skips.insert(code.strip_edges().to_lower());
  475. } else if (comment_content.strip_edges() == "warnings-disable") {
  476. ignore_warnings = true;
  477. }
  478. #endif // DEBUG_ENABLED
  479. FALLTHROUGH;
  480. }
  481. case '\n': {
  482. line++;
  483. INCPOS(1);
  484. bool used_spaces = false;
  485. int tabs = 0;
  486. column = 1;
  487. int i = 0;
  488. while (true) {
  489. if (GETCHAR(i) == ' ') {
  490. i++;
  491. used_spaces = true;
  492. } else if (GETCHAR(i) == '\t') {
  493. if (used_spaces) {
  494. _make_error("Spaces used before tabs on a line");
  495. return;
  496. }
  497. i++;
  498. tabs++;
  499. } else {
  500. break; // not indentation anymore
  501. }
  502. }
  503. _make_newline(i, tabs);
  504. return;
  505. }
  506. case '/': {
  507. switch (GETCHAR(1)) {
  508. case '=': { // diveq
  509. _make_token(TK_OP_ASSIGN_DIV);
  510. INCPOS(1);
  511. } break;
  512. default:
  513. _make_token(TK_OP_DIV);
  514. }
  515. } break;
  516. case '=': {
  517. if (GETCHAR(1) == '=') {
  518. _make_token(TK_OP_EQUAL);
  519. INCPOS(1);
  520. } else
  521. _make_token(TK_OP_ASSIGN);
  522. } break;
  523. case '<': {
  524. if (GETCHAR(1) == '=') {
  525. _make_token(TK_OP_LESS_EQUAL);
  526. INCPOS(1);
  527. } else if (GETCHAR(1) == '<') {
  528. if (GETCHAR(2) == '=') {
  529. _make_token(TK_OP_ASSIGN_SHIFT_LEFT);
  530. INCPOS(1);
  531. } else {
  532. _make_token(TK_OP_SHIFT_LEFT);
  533. }
  534. INCPOS(1);
  535. } else
  536. _make_token(TK_OP_LESS);
  537. } break;
  538. case '>': {
  539. if (GETCHAR(1) == '=') {
  540. _make_token(TK_OP_GREATER_EQUAL);
  541. INCPOS(1);
  542. } else if (GETCHAR(1) == '>') {
  543. if (GETCHAR(2) == '=') {
  544. _make_token(TK_OP_ASSIGN_SHIFT_RIGHT);
  545. INCPOS(1);
  546. } else {
  547. _make_token(TK_OP_SHIFT_RIGHT);
  548. }
  549. INCPOS(1);
  550. } else {
  551. _make_token(TK_OP_GREATER);
  552. }
  553. } break;
  554. case '!': {
  555. if (GETCHAR(1) == '=') {
  556. _make_token(TK_OP_NOT_EQUAL);
  557. INCPOS(1);
  558. } else {
  559. _make_token(TK_OP_NOT);
  560. }
  561. } break;
  562. //case '"' //string - no strings in shader
  563. //case '\'' //string - no strings in shader
  564. case '{':
  565. _make_token(TK_CURLY_BRACKET_OPEN);
  566. break;
  567. case '}':
  568. _make_token(TK_CURLY_BRACKET_CLOSE);
  569. break;
  570. case '[':
  571. _make_token(TK_BRACKET_OPEN);
  572. break;
  573. case ']':
  574. _make_token(TK_BRACKET_CLOSE);
  575. break;
  576. case '(':
  577. _make_token(TK_PARENTHESIS_OPEN);
  578. break;
  579. case ')':
  580. _make_token(TK_PARENTHESIS_CLOSE);
  581. break;
  582. case ',':
  583. _make_token(TK_COMMA);
  584. break;
  585. case ';':
  586. _make_token(TK_SEMICOLON);
  587. break;
  588. case '?':
  589. _make_token(TK_QUESTION_MARK);
  590. break;
  591. case ':':
  592. _make_token(TK_COLON); //for methods maybe but now useless.
  593. break;
  594. case '$':
  595. _make_token(TK_DOLLAR); //for the get_node() shortener
  596. break;
  597. case '^': {
  598. if (GETCHAR(1) == '=') {
  599. _make_token(TK_OP_ASSIGN_BIT_XOR);
  600. INCPOS(1);
  601. } else {
  602. _make_token(TK_OP_BIT_XOR);
  603. }
  604. } break;
  605. case '~':
  606. _make_token(TK_OP_BIT_INVERT);
  607. break;
  608. case '&': {
  609. if (GETCHAR(1) == '&') {
  610. _make_token(TK_OP_AND);
  611. INCPOS(1);
  612. } else if (GETCHAR(1) == '=') {
  613. _make_token(TK_OP_ASSIGN_BIT_AND);
  614. INCPOS(1);
  615. } else {
  616. _make_token(TK_OP_BIT_AND);
  617. }
  618. } break;
  619. case '|': {
  620. if (GETCHAR(1) == '|') {
  621. _make_token(TK_OP_OR);
  622. INCPOS(1);
  623. } else if (GETCHAR(1) == '=') {
  624. _make_token(TK_OP_ASSIGN_BIT_OR);
  625. INCPOS(1);
  626. } else {
  627. _make_token(TK_OP_BIT_OR);
  628. }
  629. } break;
  630. case '*': {
  631. if (GETCHAR(1) == '=') {
  632. _make_token(TK_OP_ASSIGN_MUL);
  633. INCPOS(1);
  634. } else {
  635. _make_token(TK_OP_MUL);
  636. }
  637. } break;
  638. case '+': {
  639. if (GETCHAR(1) == '=') {
  640. _make_token(TK_OP_ASSIGN_ADD);
  641. INCPOS(1);
  642. /*
  643. } else if (GETCHAR(1)=='+') {
  644. _make_token(TK_OP_PLUS_PLUS);
  645. INCPOS(1);
  646. */
  647. } else {
  648. _make_token(TK_OP_ADD);
  649. }
  650. } break;
  651. case '-': {
  652. if (GETCHAR(1) == '=') {
  653. _make_token(TK_OP_ASSIGN_SUB);
  654. INCPOS(1);
  655. } else if (GETCHAR(1) == '>') {
  656. _make_token(TK_FORWARD_ARROW);
  657. INCPOS(1);
  658. } else {
  659. _make_token(TK_OP_SUB);
  660. }
  661. } break;
  662. case '%': {
  663. if (GETCHAR(1) == '=') {
  664. _make_token(TK_OP_ASSIGN_MOD);
  665. INCPOS(1);
  666. } else {
  667. _make_token(TK_OP_MOD);
  668. }
  669. } break;
  670. case '@':
  671. if (CharType(GETCHAR(1)) != '"' && CharType(GETCHAR(1)) != '\'') {
  672. _make_error("Unexpected '@'");
  673. return;
  674. }
  675. INCPOS(1);
  676. is_string_name = true;
  677. FALLTHROUGH;
  678. case '\'':
  679. case '"': {
  680. if (GETCHAR(0) == '\'')
  681. string_mode = STRING_SINGLE_QUOTE;
  682. int i = 1;
  683. if (string_mode == STRING_DOUBLE_QUOTE && GETCHAR(i) == '"' && GETCHAR(i + 1) == '"') {
  684. i += 2;
  685. string_mode = STRING_MULTILINE;
  686. }
  687. String str;
  688. while (true) {
  689. if (CharType(GETCHAR(i)) == 0) {
  690. _make_error("Unterminated String");
  691. return;
  692. } else if (string_mode == STRING_DOUBLE_QUOTE && CharType(GETCHAR(i)) == '"') {
  693. break;
  694. } else if (string_mode == STRING_SINGLE_QUOTE && CharType(GETCHAR(i)) == '\'') {
  695. break;
  696. } else if (string_mode == STRING_MULTILINE && CharType(GETCHAR(i)) == '\"' && CharType(GETCHAR(i + 1)) == '\"' && CharType(GETCHAR(i + 2)) == '\"') {
  697. i += 2;
  698. break;
  699. } else if (string_mode != STRING_MULTILINE && CharType(GETCHAR(i)) == '\n') {
  700. _make_error("Unexpected EOL at String.");
  701. return;
  702. } else if (CharType(GETCHAR(i)) == 0xFFFF) {
  703. //string ends here, next will be TK
  704. i--;
  705. break;
  706. } else if (CharType(GETCHAR(i)) == '\\') {
  707. //escaped characters...
  708. i++;
  709. CharType next = GETCHAR(i);
  710. if (next == 0) {
  711. _make_error("Unterminated String");
  712. return;
  713. }
  714. CharType res = 0;
  715. switch (next) {
  716. case 'a': res = 7; break;
  717. case 'b': res = 8; break;
  718. case 't': res = 9; break;
  719. case 'n': res = 10; break;
  720. case 'v': res = 11; break;
  721. case 'f': res = 12; break;
  722. case 'r': res = 13; break;
  723. case '\'': res = '\''; break;
  724. case '\"': res = '\"'; break;
  725. case '\\': res = '\\'; break;
  726. case '/':
  727. res = '/';
  728. break; //wtf
  729. case 'u': {
  730. // hex number
  731. i += 1;
  732. for (int j = 0; j < 4; j++) {
  733. CharType c = GETCHAR(i + j);
  734. if (c == 0) {
  735. _make_error("Unterminated String");
  736. return;
  737. }
  738. CharType v = 0;
  739. if (c >= '0' && c <= '9') {
  740. v = c - '0';
  741. } else if (c >= 'a' && c <= 'f') {
  742. v = c - 'a';
  743. v += 10;
  744. } else if (c >= 'A' && c <= 'F') {
  745. v = c - 'A';
  746. v += 10;
  747. } else {
  748. _make_error("Malformed hex constant in string");
  749. return;
  750. }
  751. res <<= 4;
  752. res |= v;
  753. }
  754. i += 3;
  755. } break;
  756. default: {
  757. _make_error("Invalid escape sequence");
  758. return;
  759. } break;
  760. }
  761. str += res;
  762. } else {
  763. if (CharType(GETCHAR(i)) == '\n') {
  764. line++;
  765. column = 1;
  766. }
  767. str += CharType(GETCHAR(i));
  768. }
  769. i++;
  770. }
  771. INCPOS(i);
  772. if (is_string_name) {
  773. _make_constant(StringName(str));
  774. } else {
  775. _make_constant(str);
  776. }
  777. } break;
  778. case 0xFFFF: {
  779. _make_token(TK_CURSOR);
  780. } break;
  781. default: {
  782. if (_is_number(GETCHAR(0)) || (GETCHAR(0) == '.' && _is_number(GETCHAR(1)))) {
  783. // parse number
  784. bool period_found = false;
  785. bool exponent_found = false;
  786. bool hexa_found = false;
  787. bool bin_found = false;
  788. bool sign_found = false;
  789. String str;
  790. int i = 0;
  791. while (true) {
  792. if (GETCHAR(i) == '.') {
  793. if (period_found || exponent_found) {
  794. _make_error("Invalid numeric constant at '.'");
  795. return;
  796. } else if (bin_found) {
  797. _make_error("Invalid binary constant at '.'");
  798. return;
  799. } else if (hexa_found) {
  800. _make_error("Invalid hexadecimal constant at '.'");
  801. return;
  802. }
  803. period_found = true;
  804. } else if (GETCHAR(i) == 'x') {
  805. if (hexa_found || bin_found || str.length() != 1 || !((i == 1 && str[0] == '0') || (i == 2 && str[1] == '0' && str[0] == '-'))) {
  806. _make_error("Invalid numeric constant at 'x'");
  807. return;
  808. }
  809. hexa_found = true;
  810. } else if (hexa_found && _is_hex(GETCHAR(i))) {
  811. } else if (!hexa_found && GETCHAR(i) == 'b') {
  812. if (bin_found || str.length() != 1 || !((i == 1 && str[0] == '0') || (i == 2 && str[1] == '0' && str[0] == '-'))) {
  813. _make_error("Invalid numeric constant at 'b'");
  814. return;
  815. }
  816. bin_found = true;
  817. } else if (!hexa_found && GETCHAR(i) == 'e') {
  818. if (exponent_found || bin_found) {
  819. _make_error("Invalid numeric constant at 'e'");
  820. return;
  821. }
  822. exponent_found = true;
  823. } else if (_is_number(GETCHAR(i))) {
  824. //all ok
  825. } else if (bin_found && _is_bin(GETCHAR(i))) {
  826. } else if ((GETCHAR(i) == '-' || GETCHAR(i) == '+') && exponent_found) {
  827. if (sign_found) {
  828. _make_error("Invalid numeric constant at '-'");
  829. return;
  830. }
  831. sign_found = true;
  832. } else if (GETCHAR(i) == '_') {
  833. i++;
  834. continue; // Included for readability, shouldn't be a part of the string
  835. } else
  836. break;
  837. str += CharType(GETCHAR(i));
  838. i++;
  839. }
  840. if (!(_is_number(str[str.length() - 1]) || (hexa_found && _is_hex(str[str.length() - 1])))) {
  841. _make_error("Invalid numeric constant: " + str);
  842. return;
  843. }
  844. INCPOS(i);
  845. if (hexa_found) {
  846. int64_t val = str.hex_to_int64();
  847. _make_constant(val);
  848. } else if (bin_found) {
  849. int64_t val = str.bin_to_int64();
  850. _make_constant(val);
  851. } else if (period_found || exponent_found) {
  852. double val = str.to_double();
  853. _make_constant(val);
  854. } else {
  855. int64_t val = str.to_int64();
  856. _make_constant(val);
  857. }
  858. return;
  859. }
  860. if (GETCHAR(0) == '.') {
  861. //parse period
  862. _make_token(TK_PERIOD);
  863. break;
  864. }
  865. if (_is_text_char(GETCHAR(0))) {
  866. // parse identifier
  867. String str;
  868. str += CharType(GETCHAR(0));
  869. int i = 1;
  870. while (_is_text_char(GETCHAR(i))) {
  871. str += CharType(GETCHAR(i));
  872. i++;
  873. }
  874. bool identifier = false;
  875. if (str == "null") {
  876. _make_constant(Variant());
  877. } else if (str == "true") {
  878. _make_constant(true);
  879. } else if (str == "false") {
  880. _make_constant(false);
  881. } else {
  882. bool found = false;
  883. {
  884. int idx = 0;
  885. while (_type_list[idx].text) {
  886. if (str == _type_list[idx].text) {
  887. _make_type(_type_list[idx].type);
  888. found = true;
  889. break;
  890. }
  891. idx++;
  892. }
  893. }
  894. if (!found) {
  895. //built in func?
  896. for (int j = 0; j < GDScriptFunctions::FUNC_MAX; j++) {
  897. if (str == GDScriptFunctions::get_func_name(GDScriptFunctions::Function(j))) {
  898. _make_built_in_func(GDScriptFunctions::Function(j));
  899. found = true;
  900. break;
  901. }
  902. }
  903. }
  904. if (!found) {
  905. //keyword
  906. int idx = 0;
  907. found = false;
  908. while (_keyword_list[idx].text) {
  909. if (str == _keyword_list[idx].text) {
  910. _make_token(_keyword_list[idx].token);
  911. found = true;
  912. break;
  913. }
  914. idx++;
  915. }
  916. }
  917. if (!found)
  918. identifier = true;
  919. }
  920. if (identifier) {
  921. _make_identifier(str);
  922. }
  923. INCPOS(str.length());
  924. return;
  925. }
  926. _make_error("Unknown character");
  927. return;
  928. } break;
  929. }
  930. INCPOS(1);
  931. break;
  932. }
  933. }
  934. void GDScriptTokenizerText::set_code(const String &p_code) {
  935. code = p_code;
  936. len = p_code.length();
  937. if (len) {
  938. _code = &code[0];
  939. } else {
  940. _code = NULL;
  941. }
  942. code_pos = 0;
  943. line = 1; //it is stand-ar-ized that lines begin in 1 in code..
  944. column = 1; //the same holds for columns
  945. tk_rb_pos = 0;
  946. error_flag = false;
  947. #ifdef DEBUG_ENABLED
  948. ignore_warnings = false;
  949. #endif // DEBUG_ENABLED
  950. last_error = "";
  951. for (int i = 0; i < MAX_LOOKAHEAD + 1; i++)
  952. _advance();
  953. }
  954. GDScriptTokenizerText::Token GDScriptTokenizerText::get_token(int p_offset) const {
  955. ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, TK_ERROR);
  956. ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, TK_ERROR);
  957. int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
  958. return tk_rb[ofs].type;
  959. }
  960. int GDScriptTokenizerText::get_token_line(int p_offset) const {
  961. ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, -1);
  962. ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, -1);
  963. int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
  964. return tk_rb[ofs].line;
  965. }
  966. int GDScriptTokenizerText::get_token_column(int p_offset) const {
  967. ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, -1);
  968. ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, -1);
  969. int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
  970. return tk_rb[ofs].col;
  971. }
  972. const Variant &GDScriptTokenizerText::get_token_constant(int p_offset) const {
  973. ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, tk_rb[0].constant);
  974. ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, tk_rb[0].constant);
  975. int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
  976. ERR_FAIL_COND_V(tk_rb[ofs].type != TK_CONSTANT, tk_rb[0].constant);
  977. return tk_rb[ofs].constant;
  978. }
  979. StringName GDScriptTokenizerText::get_token_identifier(int p_offset) const {
  980. ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, StringName());
  981. ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, StringName());
  982. int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
  983. ERR_FAIL_COND_V(tk_rb[ofs].type != TK_IDENTIFIER, StringName());
  984. return tk_rb[ofs].identifier;
  985. }
  986. GDScriptFunctions::Function GDScriptTokenizerText::get_token_built_in_func(int p_offset) const {
  987. ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, GDScriptFunctions::FUNC_MAX);
  988. ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, GDScriptFunctions::FUNC_MAX);
  989. int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
  990. ERR_FAIL_COND_V(tk_rb[ofs].type != TK_BUILT_IN_FUNC, GDScriptFunctions::FUNC_MAX);
  991. return tk_rb[ofs].func;
  992. }
  993. Variant::Type GDScriptTokenizerText::get_token_type(int p_offset) const {
  994. ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, Variant::NIL);
  995. ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, Variant::NIL);
  996. int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
  997. ERR_FAIL_COND_V(tk_rb[ofs].type != TK_BUILT_IN_TYPE, Variant::NIL);
  998. return tk_rb[ofs].vtype;
  999. }
  1000. int GDScriptTokenizerText::get_token_line_indent(int p_offset) const {
  1001. ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, 0);
  1002. ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, 0);
  1003. int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
  1004. ERR_FAIL_COND_V(tk_rb[ofs].type != TK_NEWLINE, 0);
  1005. return tk_rb[ofs].constant.operator Vector2().x;
  1006. }
  1007. int GDScriptTokenizerText::get_token_line_tab_indent(int p_offset) const {
  1008. ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, 0);
  1009. ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, 0);
  1010. int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
  1011. ERR_FAIL_COND_V(tk_rb[ofs].type != TK_NEWLINE, 0);
  1012. return tk_rb[ofs].constant.operator Vector2().y;
  1013. }
  1014. String GDScriptTokenizerText::get_token_error(int p_offset) const {
  1015. ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, String());
  1016. ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, String());
  1017. int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
  1018. ERR_FAIL_COND_V(tk_rb[ofs].type != TK_ERROR, String());
  1019. return tk_rb[ofs].constant;
  1020. }
  1021. void GDScriptTokenizerText::advance(int p_amount) {
  1022. ERR_FAIL_COND(p_amount <= 0);
  1023. for (int i = 0; i < p_amount; i++)
  1024. _advance();
  1025. }
  1026. //////////////////////////////////////////////////////////////////////////////////////////////////////
  1027. #define BYTECODE_VERSION 13
  1028. Error GDScriptTokenizerBuffer::set_code_buffer(const Vector<uint8_t> &p_buffer) {
  1029. const uint8_t *buf = p_buffer.ptr();
  1030. int total_len = p_buffer.size();
  1031. ERR_FAIL_COND_V(p_buffer.size() < 24 || p_buffer[0] != 'G' || p_buffer[1] != 'D' || p_buffer[2] != 'S' || p_buffer[3] != 'C', ERR_INVALID_DATA);
  1032. int version = decode_uint32(&buf[4]);
  1033. ERR_FAIL_COND_V_MSG(version > BYTECODE_VERSION, ERR_INVALID_DATA, "Bytecode is too recent! Please use a newer engine version.");
  1034. int identifier_count = decode_uint32(&buf[8]);
  1035. int constant_count = decode_uint32(&buf[12]);
  1036. int line_count = decode_uint32(&buf[16]);
  1037. int token_count = decode_uint32(&buf[20]);
  1038. const uint8_t *b = &buf[24];
  1039. total_len -= 24;
  1040. identifiers.resize(identifier_count);
  1041. for (int i = 0; i < identifier_count; i++) {
  1042. int len = decode_uint32(b);
  1043. ERR_FAIL_COND_V(len > total_len, ERR_INVALID_DATA);
  1044. b += 4;
  1045. Vector<uint8_t> cs;
  1046. cs.resize(len);
  1047. for (int j = 0; j < len; j++) {
  1048. cs.write[j] = b[j] ^ 0xb6;
  1049. }
  1050. cs.write[cs.size() - 1] = 0;
  1051. String s;
  1052. s.parse_utf8((const char *)cs.ptr());
  1053. b += len;
  1054. total_len -= len + 4;
  1055. identifiers.write[i] = s;
  1056. }
  1057. constants.resize(constant_count);
  1058. for (int i = 0; i < constant_count; i++) {
  1059. Variant v;
  1060. int len;
  1061. // An object cannot be constant, never decode objects
  1062. Error err = decode_variant(v, b, total_len, &len, false);
  1063. if (err)
  1064. return err;
  1065. b += len;
  1066. total_len -= len;
  1067. constants.write[i] = v;
  1068. }
  1069. ERR_FAIL_COND_V(line_count * 8 > total_len, ERR_INVALID_DATA);
  1070. for (int i = 0; i < line_count; i++) {
  1071. uint32_t token = decode_uint32(b);
  1072. b += 4;
  1073. uint32_t linecol = decode_uint32(b);
  1074. b += 4;
  1075. lines.insert(token, linecol);
  1076. total_len -= 8;
  1077. }
  1078. tokens.resize(token_count);
  1079. for (int i = 0; i < token_count; i++) {
  1080. ERR_FAIL_COND_V(total_len < 1, ERR_INVALID_DATA);
  1081. if ((*b) & TOKEN_BYTE_MASK) { //little endian always
  1082. ERR_FAIL_COND_V(total_len < 4, ERR_INVALID_DATA);
  1083. tokens.write[i] = decode_uint32(b) & ~TOKEN_BYTE_MASK;
  1084. b += 4;
  1085. } else {
  1086. tokens.write[i] = *b;
  1087. b += 1;
  1088. total_len--;
  1089. }
  1090. }
  1091. token = 0;
  1092. return OK;
  1093. }
  1094. Vector<uint8_t> GDScriptTokenizerBuffer::parse_code_string(const String &p_code) {
  1095. Vector<uint8_t> buf;
  1096. Map<StringName, int> identifier_map;
  1097. HashMap<Variant, int, VariantHasher, VariantComparator> constant_map;
  1098. Map<uint32_t, int> line_map;
  1099. Vector<uint32_t> token_array;
  1100. GDScriptTokenizerText tt;
  1101. tt.set_code(p_code);
  1102. int line = -1;
  1103. while (true) {
  1104. if (tt.get_token_line() != line) {
  1105. line = tt.get_token_line();
  1106. line_map[line] = token_array.size();
  1107. }
  1108. uint32_t token = tt.get_token();
  1109. switch (tt.get_token()) {
  1110. case TK_IDENTIFIER: {
  1111. StringName id = tt.get_token_identifier();
  1112. if (!identifier_map.has(id)) {
  1113. int idx = identifier_map.size();
  1114. identifier_map[id] = idx;
  1115. }
  1116. token |= identifier_map[id] << TOKEN_BITS;
  1117. } break;
  1118. case TK_CONSTANT: {
  1119. const Variant &c = tt.get_token_constant();
  1120. if (!constant_map.has(c)) {
  1121. int idx = constant_map.size();
  1122. constant_map[c] = idx;
  1123. }
  1124. token |= constant_map[c] << TOKEN_BITS;
  1125. } break;
  1126. case TK_BUILT_IN_TYPE: {
  1127. token |= tt.get_token_type() << TOKEN_BITS;
  1128. } break;
  1129. case TK_BUILT_IN_FUNC: {
  1130. token |= tt.get_token_built_in_func() << TOKEN_BITS;
  1131. } break;
  1132. case TK_NEWLINE: {
  1133. token |= tt.get_token_line_indent() << TOKEN_BITS;
  1134. } break;
  1135. case TK_ERROR: {
  1136. ERR_FAIL_V(Vector<uint8_t>());
  1137. } break;
  1138. default: {
  1139. }
  1140. };
  1141. token_array.push_back(token);
  1142. if (tt.get_token() == TK_EOF)
  1143. break;
  1144. tt.advance();
  1145. }
  1146. //reverse maps
  1147. Map<int, StringName> rev_identifier_map;
  1148. for (Map<StringName, int>::Element *E = identifier_map.front(); E; E = E->next()) {
  1149. rev_identifier_map[E->get()] = E->key();
  1150. }
  1151. Map<int, Variant> rev_constant_map;
  1152. const Variant *K = NULL;
  1153. while ((K = constant_map.next(K))) {
  1154. rev_constant_map[constant_map[*K]] = *K;
  1155. }
  1156. Map<int, uint32_t> rev_line_map;
  1157. for (Map<uint32_t, int>::Element *E = line_map.front(); E; E = E->next()) {
  1158. rev_line_map[E->get()] = E->key();
  1159. }
  1160. //save header
  1161. buf.resize(24);
  1162. buf.write[0] = 'G';
  1163. buf.write[1] = 'D';
  1164. buf.write[2] = 'S';
  1165. buf.write[3] = 'C';
  1166. encode_uint32(BYTECODE_VERSION, &buf.write[4]);
  1167. encode_uint32(identifier_map.size(), &buf.write[8]);
  1168. encode_uint32(constant_map.size(), &buf.write[12]);
  1169. encode_uint32(line_map.size(), &buf.write[16]);
  1170. encode_uint32(token_array.size(), &buf.write[20]);
  1171. //save identifiers
  1172. for (Map<int, StringName>::Element *E = rev_identifier_map.front(); E; E = E->next()) {
  1173. CharString cs = String(E->get()).utf8();
  1174. int len = cs.length() + 1;
  1175. int extra = 4 - (len % 4);
  1176. if (extra == 4)
  1177. extra = 0;
  1178. uint8_t ibuf[4];
  1179. encode_uint32(len + extra, ibuf);
  1180. for (int i = 0; i < 4; i++) {
  1181. buf.push_back(ibuf[i]);
  1182. }
  1183. for (int i = 0; i < len; i++) {
  1184. buf.push_back(cs[i] ^ 0xb6);
  1185. }
  1186. for (int i = 0; i < extra; i++) {
  1187. buf.push_back(0 ^ 0xb6);
  1188. }
  1189. }
  1190. for (Map<int, Variant>::Element *E = rev_constant_map.front(); E; E = E->next()) {
  1191. int len;
  1192. // Objects cannot be constant, never encode objects
  1193. Error err = encode_variant(E->get(), NULL, len, false);
  1194. ERR_FAIL_COND_V_MSG(err != OK, Vector<uint8_t>(), "Error when trying to encode Variant.");
  1195. int pos = buf.size();
  1196. buf.resize(pos + len);
  1197. encode_variant(E->get(), &buf.write[pos], len, false);
  1198. }
  1199. for (Map<int, uint32_t>::Element *E = rev_line_map.front(); E; E = E->next()) {
  1200. uint8_t ibuf[8];
  1201. encode_uint32(E->key(), &ibuf[0]);
  1202. encode_uint32(E->get(), &ibuf[4]);
  1203. for (int i = 0; i < 8; i++)
  1204. buf.push_back(ibuf[i]);
  1205. }
  1206. for (int i = 0; i < token_array.size(); i++) {
  1207. uint32_t token = token_array[i];
  1208. if (token & ~TOKEN_MASK) {
  1209. uint8_t buf4[4];
  1210. encode_uint32(token_array[i] | TOKEN_BYTE_MASK, &buf4[0]);
  1211. for (int j = 0; j < 4; j++) {
  1212. buf.push_back(buf4[j]);
  1213. }
  1214. } else {
  1215. buf.push_back(token);
  1216. }
  1217. }
  1218. return buf;
  1219. }
  1220. GDScriptTokenizerBuffer::Token GDScriptTokenizerBuffer::get_token(int p_offset) const {
  1221. int offset = token + p_offset;
  1222. if (offset < 0 || offset >= tokens.size())
  1223. return TK_EOF;
  1224. return GDScriptTokenizerBuffer::Token(tokens[offset] & TOKEN_MASK);
  1225. }
  1226. StringName GDScriptTokenizerBuffer::get_token_identifier(int p_offset) const {
  1227. int offset = token + p_offset;
  1228. ERR_FAIL_INDEX_V(offset, tokens.size(), StringName());
  1229. uint32_t identifier = tokens[offset] >> TOKEN_BITS;
  1230. ERR_FAIL_UNSIGNED_INDEX_V(identifier, (uint32_t)identifiers.size(), StringName());
  1231. return identifiers[identifier];
  1232. }
  1233. GDScriptFunctions::Function GDScriptTokenizerBuffer::get_token_built_in_func(int p_offset) const {
  1234. int offset = token + p_offset;
  1235. ERR_FAIL_INDEX_V(offset, tokens.size(), GDScriptFunctions::FUNC_MAX);
  1236. return GDScriptFunctions::Function(tokens[offset] >> TOKEN_BITS);
  1237. }
  1238. Variant::Type GDScriptTokenizerBuffer::get_token_type(int p_offset) const {
  1239. int offset = token + p_offset;
  1240. ERR_FAIL_INDEX_V(offset, tokens.size(), Variant::NIL);
  1241. return Variant::Type(tokens[offset] >> TOKEN_BITS);
  1242. }
  1243. int GDScriptTokenizerBuffer::get_token_line(int p_offset) const {
  1244. int offset = token + p_offset;
  1245. int pos = lines.find_nearest(offset);
  1246. if (pos < 0)
  1247. return -1;
  1248. if (pos >= lines.size())
  1249. pos = lines.size() - 1;
  1250. uint32_t l = lines.getv(pos);
  1251. return l & TOKEN_LINE_MASK;
  1252. }
  1253. int GDScriptTokenizerBuffer::get_token_column(int p_offset) const {
  1254. int offset = token + p_offset;
  1255. int pos = lines.find_nearest(offset);
  1256. if (pos < 0)
  1257. return -1;
  1258. if (pos >= lines.size())
  1259. pos = lines.size() - 1;
  1260. uint32_t l = lines.getv(pos);
  1261. return l >> TOKEN_LINE_BITS;
  1262. }
  1263. int GDScriptTokenizerBuffer::get_token_line_indent(int p_offset) const {
  1264. int offset = token + p_offset;
  1265. ERR_FAIL_INDEX_V(offset, tokens.size(), 0);
  1266. return tokens[offset] >> TOKEN_BITS;
  1267. }
  1268. const Variant &GDScriptTokenizerBuffer::get_token_constant(int p_offset) const {
  1269. int offset = token + p_offset;
  1270. ERR_FAIL_INDEX_V(offset, tokens.size(), nil);
  1271. uint32_t constant = tokens[offset] >> TOKEN_BITS;
  1272. ERR_FAIL_UNSIGNED_INDEX_V(constant, (uint32_t)constants.size(), nil);
  1273. return constants[constant];
  1274. }
  1275. String GDScriptTokenizerBuffer::get_token_error(int p_offset) const {
  1276. ERR_FAIL_V(String());
  1277. }
  1278. void GDScriptTokenizerBuffer::advance(int p_amount) {
  1279. ERR_FAIL_INDEX(p_amount + token, tokens.size());
  1280. token += p_amount;
  1281. }
  1282. GDScriptTokenizerBuffer::GDScriptTokenizerBuffer() {
  1283. token = 0;
  1284. }