gdscript_tokenizer.cpp 37 KB

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