gdscript_tokenizer.cpp 37 KB

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