gdscript_tokenizer.cpp 37 KB

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