gdscript_tokenizer.cpp 36 KB

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