gdscript_tokenizer.cpp 37 KB

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