gdscript_tokenizer.cpp 37 KB

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