gdscript_tokenizer.cpp 37 KB

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