gdscript_tokenizer.cpp 36 KB

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