gdscript_tokenizer.cpp 36 KB

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