2
0

gdscript_tokenizer.cpp 37 KB

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