gdscript_tokenizer.cpp 36 KB

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