gdscript_tokenizer.cpp 37 KB

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