2
0

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. FALLTHROUGH;
  670. case '\'':
  671. case '"': {
  672. if (GETCHAR(0) == '\'')
  673. string_mode = STRING_SINGLE_QUOTE;
  674. int i = 1;
  675. if (string_mode == STRING_DOUBLE_QUOTE && GETCHAR(i) == '"' && GETCHAR(i + 1) == '"') {
  676. i += 2;
  677. string_mode = STRING_MULTILINE;
  678. }
  679. String str;
  680. while (true) {
  681. if (CharType(GETCHAR(i)) == 0) {
  682. _make_error("Unterminated String");
  683. return;
  684. } else if (string_mode == STRING_DOUBLE_QUOTE && CharType(GETCHAR(i)) == '"') {
  685. break;
  686. } else if (string_mode == STRING_SINGLE_QUOTE && CharType(GETCHAR(i)) == '\'') {
  687. break;
  688. } else if (string_mode == STRING_MULTILINE && CharType(GETCHAR(i)) == '\"' && CharType(GETCHAR(i + 1)) == '\"' && CharType(GETCHAR(i + 2)) == '\"') {
  689. i += 2;
  690. break;
  691. } else if (string_mode != STRING_MULTILINE && CharType(GETCHAR(i)) == '\n') {
  692. _make_error("Unexpected EOL at String.");
  693. return;
  694. } else if (CharType(GETCHAR(i)) == 0xFFFF) {
  695. //string ends here, next will be TK
  696. i--;
  697. break;
  698. } else if (CharType(GETCHAR(i)) == '\\') {
  699. //escaped characters...
  700. i++;
  701. CharType next = GETCHAR(i);
  702. if (next == 0) {
  703. _make_error("Unterminated String");
  704. return;
  705. }
  706. CharType res = 0;
  707. switch (next) {
  708. case 'a': res = 7; break;
  709. case 'b': res = 8; break;
  710. case 't': res = 9; break;
  711. case 'n': res = 10; break;
  712. case 'v': res = 11; break;
  713. case 'f': res = 12; break;
  714. case 'r': res = 13; break;
  715. case '\'': res = '\''; break;
  716. case '\"': res = '\"'; break;
  717. case '\\': res = '\\'; break;
  718. case '/':
  719. res = '/';
  720. break; //wtf
  721. case 'u': {
  722. //hexnumbarh - oct is deprecated
  723. i += 1;
  724. for (int j = 0; j < 4; j++) {
  725. CharType c = GETCHAR(i + j);
  726. if (c == 0) {
  727. _make_error("Unterminated String");
  728. return;
  729. }
  730. if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
  731. _make_error("Malformed hex constant in string");
  732. return;
  733. }
  734. CharType v;
  735. if (c >= '0' && c <= '9') {
  736. v = c - '0';
  737. } else if (c >= 'a' && c <= 'f') {
  738. v = c - 'a';
  739. v += 10;
  740. } else if (c >= 'A' && c <= 'F') {
  741. v = c - 'A';
  742. v += 10;
  743. } else {
  744. ERR_PRINT("BUG");
  745. v = 0;
  746. }
  747. res <<= 4;
  748. res |= v;
  749. }
  750. i += 3;
  751. } break;
  752. default: {
  753. _make_error("Invalid escape sequence");
  754. return;
  755. } break;
  756. }
  757. str += res;
  758. } else {
  759. if (CharType(GETCHAR(i)) == '\n') {
  760. line++;
  761. column = 1;
  762. }
  763. str += CharType(GETCHAR(i));
  764. }
  765. i++;
  766. }
  767. INCPOS(i);
  768. if (is_node_path) {
  769. _make_constant(NodePath(str));
  770. } else {
  771. _make_constant(str);
  772. }
  773. } break;
  774. case 0xFFFF: {
  775. _make_token(TK_CURSOR);
  776. } break;
  777. default: {
  778. if (_is_number(GETCHAR(0)) || (GETCHAR(0) == '.' && _is_number(GETCHAR(1)))) {
  779. // parse number
  780. bool period_found = false;
  781. bool exponent_found = false;
  782. bool hexa_found = false;
  783. bool sign_found = false;
  784. String str;
  785. int i = 0;
  786. while (true) {
  787. if (GETCHAR(i) == '.') {
  788. if (period_found || exponent_found) {
  789. _make_error("Invalid numeric constant at '.'");
  790. return;
  791. }
  792. period_found = true;
  793. } else if (GETCHAR(i) == 'x') {
  794. if (hexa_found || str.length() != 1 || !((i == 1 && str[0] == '0') || (i == 2 && str[1] == '0' && str[0] == '-'))) {
  795. _make_error("Invalid numeric constant at 'x'");
  796. return;
  797. }
  798. hexa_found = true;
  799. } else if (!hexa_found && GETCHAR(i) == 'e') {
  800. if (hexa_found || exponent_found) {
  801. _make_error("Invalid numeric constant at 'e'");
  802. return;
  803. }
  804. exponent_found = true;
  805. } else if (_is_number(GETCHAR(i))) {
  806. //all ok
  807. } else if (hexa_found && _is_hex(GETCHAR(i))) {
  808. } else if ((GETCHAR(i) == '-' || GETCHAR(i) == '+') && exponent_found) {
  809. if (sign_found) {
  810. _make_error("Invalid numeric constant at '-'");
  811. return;
  812. }
  813. sign_found = true;
  814. } else if (GETCHAR(i) == '_') {
  815. i++;
  816. continue; // Included for readability, shouldn't be a part of the string
  817. } else
  818. break;
  819. str += CharType(GETCHAR(i));
  820. i++;
  821. }
  822. if (!(_is_number(str[str.length() - 1]) || (hexa_found && _is_hex(str[str.length() - 1])))) {
  823. _make_error("Invalid numeric constant: " + str);
  824. return;
  825. }
  826. INCPOS(i);
  827. if (hexa_found) {
  828. int64_t val = str.hex_to_int64();
  829. _make_constant(val);
  830. } else if (period_found || exponent_found) {
  831. double val = str.to_double();
  832. _make_constant(val);
  833. } else {
  834. int64_t val = str.to_int64();
  835. _make_constant(val);
  836. }
  837. return;
  838. }
  839. if (GETCHAR(0) == '.') {
  840. //parse period
  841. _make_token(TK_PERIOD);
  842. break;
  843. }
  844. if (_is_text_char(GETCHAR(0))) {
  845. // parse identifier
  846. String str;
  847. str += CharType(GETCHAR(0));
  848. int i = 1;
  849. while (_is_text_char(GETCHAR(i))) {
  850. str += CharType(GETCHAR(i));
  851. i++;
  852. }
  853. bool identifier = false;
  854. if (str == "null") {
  855. _make_constant(Variant());
  856. } else if (str == "true") {
  857. _make_constant(true);
  858. } else if (str == "false") {
  859. _make_constant(false);
  860. } else {
  861. bool found = false;
  862. {
  863. int idx = 0;
  864. while (_type_list[idx].text) {
  865. if (str == _type_list[idx].text) {
  866. _make_type(_type_list[idx].type);
  867. found = true;
  868. break;
  869. }
  870. idx++;
  871. }
  872. }
  873. if (!found) {
  874. //built in func?
  875. for (int j = 0; j < GDScriptFunctions::FUNC_MAX; j++) {
  876. if (str == GDScriptFunctions::get_func_name(GDScriptFunctions::Function(j))) {
  877. _make_built_in_func(GDScriptFunctions::Function(j));
  878. found = true;
  879. break;
  880. }
  881. }
  882. }
  883. if (!found) {
  884. //keyword
  885. int idx = 0;
  886. found = false;
  887. while (_keyword_list[idx].text) {
  888. if (str == _keyword_list[idx].text) {
  889. _make_token(_keyword_list[idx].token);
  890. found = true;
  891. break;
  892. }
  893. idx++;
  894. }
  895. }
  896. if (!found)
  897. identifier = true;
  898. }
  899. if (identifier) {
  900. _make_identifier(str);
  901. }
  902. INCPOS(str.length());
  903. return;
  904. }
  905. _make_error("Unknown character");
  906. return;
  907. } break;
  908. }
  909. INCPOS(1);
  910. break;
  911. }
  912. }
  913. void GDScriptTokenizerText::set_code(const String &p_code) {
  914. code = p_code;
  915. len = p_code.length();
  916. if (len) {
  917. _code = &code[0];
  918. } else {
  919. _code = NULL;
  920. }
  921. code_pos = 0;
  922. line = 1; //it is stand-ar-ized that lines begin in 1 in code..
  923. column = 1; //the same holds for columns
  924. tk_rb_pos = 0;
  925. error_flag = false;
  926. #ifdef DEBUG_ENABLED
  927. ignore_warnings = false;
  928. #endif // DEBUG_ENABLED
  929. last_error = "";
  930. for (int i = 0; i < MAX_LOOKAHEAD + 1; i++)
  931. _advance();
  932. }
  933. GDScriptTokenizerText::Token GDScriptTokenizerText::get_token(int p_offset) const {
  934. ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, TK_ERROR);
  935. ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, TK_ERROR);
  936. int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
  937. return tk_rb[ofs].type;
  938. }
  939. int GDScriptTokenizerText::get_token_line(int p_offset) const {
  940. ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, -1);
  941. ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, -1);
  942. int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
  943. return tk_rb[ofs].line;
  944. }
  945. int GDScriptTokenizerText::get_token_column(int p_offset) const {
  946. ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, -1);
  947. ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, -1);
  948. int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
  949. return tk_rb[ofs].col;
  950. }
  951. const Variant &GDScriptTokenizerText::get_token_constant(int p_offset) const {
  952. ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, tk_rb[0].constant);
  953. ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, tk_rb[0].constant);
  954. int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
  955. ERR_FAIL_COND_V(tk_rb[ofs].type != TK_CONSTANT, tk_rb[0].constant);
  956. return tk_rb[ofs].constant;
  957. }
  958. StringName GDScriptTokenizerText::get_token_identifier(int p_offset) const {
  959. ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, StringName());
  960. ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, StringName());
  961. int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
  962. ERR_FAIL_COND_V(tk_rb[ofs].type != TK_IDENTIFIER, StringName());
  963. return tk_rb[ofs].identifier;
  964. }
  965. GDScriptFunctions::Function GDScriptTokenizerText::get_token_built_in_func(int p_offset) const {
  966. ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, GDScriptFunctions::FUNC_MAX);
  967. ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, GDScriptFunctions::FUNC_MAX);
  968. int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
  969. ERR_FAIL_COND_V(tk_rb[ofs].type != TK_BUILT_IN_FUNC, GDScriptFunctions::FUNC_MAX);
  970. return tk_rb[ofs].func;
  971. }
  972. Variant::Type GDScriptTokenizerText::get_token_type(int p_offset) const {
  973. ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, Variant::NIL);
  974. ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, Variant::NIL);
  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_BUILT_IN_TYPE, Variant::NIL);
  977. return tk_rb[ofs].vtype;
  978. }
  979. int GDScriptTokenizerText::get_token_line_indent(int p_offset) const {
  980. ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, 0);
  981. ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, 0);
  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_NEWLINE, 0);
  984. return tk_rb[ofs].constant;
  985. }
  986. String GDScriptTokenizerText::get_token_error(int p_offset) const {
  987. ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, String());
  988. ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, String());
  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_ERROR, String());
  991. return tk_rb[ofs].constant;
  992. }
  993. void GDScriptTokenizerText::advance(int p_amount) {
  994. ERR_FAIL_COND(p_amount <= 0);
  995. for (int i = 0; i < p_amount; i++)
  996. _advance();
  997. }
  998. //////////////////////////////////////////////////////////////////////////////////////////////////////
  999. #define BYTECODE_VERSION 13
  1000. Error GDScriptTokenizerBuffer::set_code_buffer(const Vector<uint8_t> &p_buffer) {
  1001. const uint8_t *buf = p_buffer.ptr();
  1002. int total_len = p_buffer.size();
  1003. 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);
  1004. int version = decode_uint32(&buf[4]);
  1005. if (version > BYTECODE_VERSION) {
  1006. ERR_EXPLAIN("Bytecode is too New! Please use a newer engine version.");
  1007. ERR_FAIL_COND_V(version > BYTECODE_VERSION, ERR_INVALID_DATA);
  1008. }
  1009. int identifier_count = decode_uint32(&buf[8]);
  1010. int constant_count = decode_uint32(&buf[12]);
  1011. int line_count = decode_uint32(&buf[16]);
  1012. int token_count = decode_uint32(&buf[20]);
  1013. const uint8_t *b = &buf[24];
  1014. total_len -= 24;
  1015. identifiers.resize(identifier_count);
  1016. for (int i = 0; i < identifier_count; i++) {
  1017. int len = decode_uint32(b);
  1018. ERR_FAIL_COND_V(len > total_len, ERR_INVALID_DATA);
  1019. b += 4;
  1020. Vector<uint8_t> cs;
  1021. cs.resize(len);
  1022. for (int j = 0; j < len; j++) {
  1023. cs.write[j] = b[j] ^ 0xb6;
  1024. }
  1025. cs.write[cs.size() - 1] = 0;
  1026. String s;
  1027. s.parse_utf8((const char *)cs.ptr());
  1028. b += len;
  1029. total_len -= len + 4;
  1030. identifiers.write[i] = s;
  1031. }
  1032. constants.resize(constant_count);
  1033. for (int i = 0; i < constant_count; i++) {
  1034. Variant v;
  1035. int len;
  1036. // An object cannot be constant, never decode objects
  1037. Error err = decode_variant(v, b, total_len, &len, false);
  1038. if (err)
  1039. return err;
  1040. b += len;
  1041. total_len -= len;
  1042. constants.write[i] = v;
  1043. }
  1044. ERR_FAIL_COND_V(line_count * 8 > total_len, ERR_INVALID_DATA);
  1045. for (int i = 0; i < line_count; i++) {
  1046. uint32_t token = decode_uint32(b);
  1047. b += 4;
  1048. uint32_t linecol = decode_uint32(b);
  1049. b += 4;
  1050. lines.insert(token, linecol);
  1051. total_len -= 8;
  1052. }
  1053. tokens.resize(token_count);
  1054. for (int i = 0; i < token_count; i++) {
  1055. ERR_FAIL_COND_V(total_len < 1, ERR_INVALID_DATA);
  1056. if ((*b) & TOKEN_BYTE_MASK) { //little endian always
  1057. ERR_FAIL_COND_V(total_len < 4, ERR_INVALID_DATA);
  1058. tokens.write[i] = decode_uint32(b) & ~TOKEN_BYTE_MASK;
  1059. b += 4;
  1060. } else {
  1061. tokens.write[i] = *b;
  1062. b += 1;
  1063. total_len--;
  1064. }
  1065. }
  1066. token = 0;
  1067. return OK;
  1068. }
  1069. Vector<uint8_t> GDScriptTokenizerBuffer::parse_code_string(const String &p_code) {
  1070. Vector<uint8_t> buf;
  1071. Map<StringName, int> identifier_map;
  1072. HashMap<Variant, int, VariantHasher, VariantComparator> constant_map;
  1073. Map<uint32_t, int> line_map;
  1074. Vector<uint32_t> token_array;
  1075. GDScriptTokenizerText tt;
  1076. tt.set_code(p_code);
  1077. int line = -1;
  1078. while (true) {
  1079. if (tt.get_token_line() != line) {
  1080. line = tt.get_token_line();
  1081. line_map[line] = token_array.size();
  1082. }
  1083. uint32_t token = tt.get_token();
  1084. switch (tt.get_token()) {
  1085. case TK_IDENTIFIER: {
  1086. StringName id = tt.get_token_identifier();
  1087. if (!identifier_map.has(id)) {
  1088. int idx = identifier_map.size();
  1089. identifier_map[id] = idx;
  1090. }
  1091. token |= identifier_map[id] << TOKEN_BITS;
  1092. } break;
  1093. case TK_CONSTANT: {
  1094. Variant c = tt.get_token_constant();
  1095. if (!constant_map.has(c)) {
  1096. int idx = constant_map.size();
  1097. constant_map[c] = idx;
  1098. }
  1099. token |= constant_map[c] << TOKEN_BITS;
  1100. } break;
  1101. case TK_BUILT_IN_TYPE: {
  1102. token |= tt.get_token_type() << TOKEN_BITS;
  1103. } break;
  1104. case TK_BUILT_IN_FUNC: {
  1105. token |= tt.get_token_built_in_func() << TOKEN_BITS;
  1106. } break;
  1107. case TK_NEWLINE: {
  1108. token |= tt.get_token_line_indent() << TOKEN_BITS;
  1109. } break;
  1110. case TK_ERROR: {
  1111. ERR_FAIL_V(Vector<uint8_t>());
  1112. } break;
  1113. default: {}
  1114. };
  1115. token_array.push_back(token);
  1116. if (tt.get_token() == TK_EOF)
  1117. break;
  1118. tt.advance();
  1119. }
  1120. //reverse maps
  1121. Map<int, StringName> rev_identifier_map;
  1122. for (Map<StringName, int>::Element *E = identifier_map.front(); E; E = E->next()) {
  1123. rev_identifier_map[E->get()] = E->key();
  1124. }
  1125. Map<int, Variant> rev_constant_map;
  1126. const Variant *K = NULL;
  1127. while ((K = constant_map.next(K))) {
  1128. rev_constant_map[constant_map[*K]] = *K;
  1129. }
  1130. Map<int, uint32_t> rev_line_map;
  1131. for (Map<uint32_t, int>::Element *E = line_map.front(); E; E = E->next()) {
  1132. rev_line_map[E->get()] = E->key();
  1133. }
  1134. //save header
  1135. buf.resize(24);
  1136. buf.write[0] = 'G';
  1137. buf.write[1] = 'D';
  1138. buf.write[2] = 'S';
  1139. buf.write[3] = 'C';
  1140. encode_uint32(BYTECODE_VERSION, &buf.write[4]);
  1141. encode_uint32(identifier_map.size(), &buf.write[8]);
  1142. encode_uint32(constant_map.size(), &buf.write[12]);
  1143. encode_uint32(line_map.size(), &buf.write[16]);
  1144. encode_uint32(token_array.size(), &buf.write[20]);
  1145. //save identifiers
  1146. for (Map<int, StringName>::Element *E = rev_identifier_map.front(); E; E = E->next()) {
  1147. CharString cs = String(E->get()).utf8();
  1148. int len = cs.length() + 1;
  1149. int extra = 4 - (len % 4);
  1150. if (extra == 4)
  1151. extra = 0;
  1152. uint8_t ibuf[4];
  1153. encode_uint32(len + extra, ibuf);
  1154. for (int i = 0; i < 4; i++) {
  1155. buf.push_back(ibuf[i]);
  1156. }
  1157. for (int i = 0; i < len; i++) {
  1158. buf.push_back(cs[i] ^ 0xb6);
  1159. }
  1160. for (int i = 0; i < extra; i++) {
  1161. buf.push_back(0 ^ 0xb6);
  1162. }
  1163. }
  1164. for (Map<int, Variant>::Element *E = rev_constant_map.front(); E; E = E->next()) {
  1165. int len;
  1166. // Objects cannot be constant, never encode objects
  1167. Error err = encode_variant(E->get(), NULL, len, false);
  1168. ERR_FAIL_COND_V(err != OK, Vector<uint8_t>());
  1169. int pos = buf.size();
  1170. buf.resize(pos + len);
  1171. encode_variant(E->get(), &buf.write[pos], len, false);
  1172. }
  1173. for (Map<int, uint32_t>::Element *E = rev_line_map.front(); E; E = E->next()) {
  1174. uint8_t ibuf[8];
  1175. encode_uint32(E->key(), &ibuf[0]);
  1176. encode_uint32(E->get(), &ibuf[4]);
  1177. for (int i = 0; i < 8; i++)
  1178. buf.push_back(ibuf[i]);
  1179. }
  1180. for (int i = 0; i < token_array.size(); i++) {
  1181. uint32_t token = token_array[i];
  1182. if (token & ~TOKEN_MASK) {
  1183. uint8_t buf4[4];
  1184. encode_uint32(token_array[i] | TOKEN_BYTE_MASK, &buf4[0]);
  1185. for (int j = 0; j < 4; j++) {
  1186. buf.push_back(buf4[j]);
  1187. }
  1188. } else {
  1189. buf.push_back(token);
  1190. }
  1191. }
  1192. return buf;
  1193. }
  1194. GDScriptTokenizerBuffer::Token GDScriptTokenizerBuffer::get_token(int p_offset) const {
  1195. int offset = token + p_offset;
  1196. if (offset < 0 || offset >= tokens.size())
  1197. return TK_EOF;
  1198. return GDScriptTokenizerBuffer::Token(tokens[offset] & TOKEN_MASK);
  1199. }
  1200. StringName GDScriptTokenizerBuffer::get_token_identifier(int p_offset) const {
  1201. int offset = token + p_offset;
  1202. ERR_FAIL_INDEX_V(offset, tokens.size(), StringName());
  1203. uint32_t identifier = tokens[offset] >> TOKEN_BITS;
  1204. ERR_FAIL_UNSIGNED_INDEX_V(identifier, (uint32_t)identifiers.size(), StringName());
  1205. return identifiers[identifier];
  1206. }
  1207. GDScriptFunctions::Function GDScriptTokenizerBuffer::get_token_built_in_func(int p_offset) const {
  1208. int offset = token + p_offset;
  1209. ERR_FAIL_INDEX_V(offset, tokens.size(), GDScriptFunctions::FUNC_MAX);
  1210. return GDScriptFunctions::Function(tokens[offset] >> TOKEN_BITS);
  1211. }
  1212. Variant::Type GDScriptTokenizerBuffer::get_token_type(int p_offset) const {
  1213. int offset = token + p_offset;
  1214. ERR_FAIL_INDEX_V(offset, tokens.size(), Variant::NIL);
  1215. return Variant::Type(tokens[offset] >> TOKEN_BITS);
  1216. }
  1217. int GDScriptTokenizerBuffer::get_token_line(int p_offset) const {
  1218. int offset = token + p_offset;
  1219. int pos = lines.find_nearest(offset);
  1220. if (pos < 0)
  1221. return -1;
  1222. if (pos >= lines.size())
  1223. pos = lines.size() - 1;
  1224. uint32_t l = lines.getv(pos);
  1225. return l & TOKEN_LINE_MASK;
  1226. }
  1227. int GDScriptTokenizerBuffer::get_token_column(int p_offset) const {
  1228. int offset = token + p_offset;
  1229. int pos = lines.find_nearest(offset);
  1230. if (pos < 0)
  1231. return -1;
  1232. if (pos >= lines.size())
  1233. pos = lines.size() - 1;
  1234. uint32_t l = lines.getv(pos);
  1235. return l >> TOKEN_LINE_BITS;
  1236. }
  1237. int GDScriptTokenizerBuffer::get_token_line_indent(int p_offset) const {
  1238. int offset = token + p_offset;
  1239. ERR_FAIL_INDEX_V(offset, tokens.size(), 0);
  1240. return tokens[offset] >> TOKEN_BITS;
  1241. }
  1242. const Variant &GDScriptTokenizerBuffer::get_token_constant(int p_offset) const {
  1243. int offset = token + p_offset;
  1244. ERR_FAIL_INDEX_V(offset, tokens.size(), nil);
  1245. uint32_t constant = tokens[offset] >> TOKEN_BITS;
  1246. ERR_FAIL_UNSIGNED_INDEX_V(constant, (uint32_t)constants.size(), nil);
  1247. return constants[constant];
  1248. }
  1249. String GDScriptTokenizerBuffer::get_token_error(int p_offset) const {
  1250. ERR_FAIL_V(String());
  1251. }
  1252. void GDScriptTokenizerBuffer::advance(int p_amount) {
  1253. ERR_FAIL_INDEX(p_amount + token, tokens.size());
  1254. token += p_amount;
  1255. }
  1256. GDScriptTokenizerBuffer::GDScriptTokenizerBuffer() {
  1257. token = 0;
  1258. }