gdscript_tokenizer_buffer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /**************************************************************************/
  2. /* gdscript_tokenizer_buffer.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_buffer.h"
  31. #include "core/io/marshalls.h"
  32. #define TOKENIZER_VERSION 100
  33. int GDScriptTokenizerBuffer::_token_to_binary(const Token &p_token, Vector<uint8_t> &r_buffer, int p_start, HashMap<StringName, uint32_t> &r_identifiers_map, HashMap<Variant, uint32_t, VariantHasher, VariantComparator> &r_constants_map) {
  34. int pos = p_start;
  35. int token_type = p_token.type & TOKEN_MASK;
  36. switch (p_token.type) {
  37. case GDScriptTokenizer::Token::ANNOTATION:
  38. case GDScriptTokenizer::Token::IDENTIFIER: {
  39. // Add identifier to map.
  40. int identifier_pos;
  41. StringName id = p_token.get_identifier();
  42. if (r_identifiers_map.has(id)) {
  43. identifier_pos = r_identifiers_map[id];
  44. } else {
  45. identifier_pos = r_identifiers_map.size();
  46. r_identifiers_map[id] = identifier_pos;
  47. }
  48. token_type |= identifier_pos << TOKEN_BITS;
  49. } break;
  50. case GDScriptTokenizer::Token::ERROR:
  51. case GDScriptTokenizer::Token::LITERAL: {
  52. // Add literal to map.
  53. int constant_pos;
  54. if (r_constants_map.has(p_token.literal)) {
  55. constant_pos = r_constants_map[p_token.literal];
  56. } else {
  57. constant_pos = r_constants_map.size();
  58. r_constants_map[p_token.literal] = constant_pos;
  59. }
  60. token_type |= constant_pos << TOKEN_BITS;
  61. } break;
  62. default:
  63. break;
  64. }
  65. // Encode token.
  66. int token_len;
  67. if (token_type & TOKEN_MASK) {
  68. token_len = 8;
  69. r_buffer.resize(pos + token_len);
  70. encode_uint32(token_type | TOKEN_BYTE_MASK, &r_buffer.write[pos]);
  71. pos += 4;
  72. } else {
  73. token_len = 5;
  74. r_buffer.resize(pos + token_len);
  75. r_buffer.write[pos] = token_type;
  76. pos++;
  77. }
  78. encode_uint32(p_token.start_line, &r_buffer.write[pos]);
  79. return token_len;
  80. }
  81. GDScriptTokenizer::Token GDScriptTokenizerBuffer::_binary_to_token(const uint8_t *p_buffer) {
  82. Token token;
  83. const uint8_t *b = p_buffer;
  84. uint32_t token_type = decode_uint32(b);
  85. token.type = (Token::Type)(token_type & TOKEN_MASK);
  86. if (token_type & TOKEN_BYTE_MASK) {
  87. b += 4;
  88. } else {
  89. b++;
  90. }
  91. token.start_line = decode_uint32(b);
  92. token.end_line = token.start_line;
  93. token.literal = token.get_name();
  94. if (token.type == Token::CONST_NAN) {
  95. token.literal = String("NAN"); // Special case since name and notation are different.
  96. }
  97. switch (token.type) {
  98. case GDScriptTokenizer::Token::ANNOTATION:
  99. case GDScriptTokenizer::Token::IDENTIFIER: {
  100. // Get name from map.
  101. int identifier_pos = token_type >> TOKEN_BITS;
  102. if (unlikely(identifier_pos >= identifiers.size())) {
  103. Token error;
  104. error.type = Token::ERROR;
  105. error.literal = "Identifier index out of bounds.";
  106. return error;
  107. }
  108. token.literal = identifiers[identifier_pos];
  109. } break;
  110. case GDScriptTokenizer::Token::ERROR:
  111. case GDScriptTokenizer::Token::LITERAL: {
  112. // Get literal from map.
  113. int constant_pos = token_type >> TOKEN_BITS;
  114. if (unlikely(constant_pos >= constants.size())) {
  115. Token error;
  116. error.type = Token::ERROR;
  117. error.literal = "Constant index out of bounds.";
  118. return error;
  119. }
  120. token.literal = constants[constant_pos];
  121. } break;
  122. default:
  123. break;
  124. }
  125. return token;
  126. }
  127. Error GDScriptTokenizerBuffer::set_code_buffer(const Vector<uint8_t> &p_buffer) {
  128. const uint8_t *buf = p_buffer.ptr();
  129. int total_len = p_buffer.size();
  130. 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);
  131. int version = decode_uint32(&buf[4]);
  132. ERR_FAIL_COND_V_MSG(version > TOKENIZER_VERSION, ERR_INVALID_DATA, "Binary GDScript is too recent! Please use a newer engine version.");
  133. uint32_t identifier_count = decode_uint32(&buf[8]);
  134. uint32_t constant_count = decode_uint32(&buf[12]);
  135. uint32_t token_line_count = decode_uint32(&buf[16]);
  136. uint32_t token_count = decode_uint32(&buf[20]);
  137. const uint8_t *b = &buf[24];
  138. total_len -= 24;
  139. identifiers.resize(identifier_count);
  140. for (uint32_t i = 0; i < identifier_count; i++) {
  141. uint32_t len = decode_uint32(b);
  142. total_len -= 4;
  143. ERR_FAIL_COND_V((len * 4u) > (uint32_t)total_len, ERR_INVALID_DATA);
  144. b += 4;
  145. Vector<uint32_t> cs;
  146. cs.resize(len);
  147. for (uint32_t j = 0; j < len; j++) {
  148. uint8_t tmp[4];
  149. for (uint32_t k = 0; k < 4; k++) {
  150. tmp[k] = b[j * 4 + k] ^ 0xb6;
  151. }
  152. cs.write[j] = decode_uint32(tmp);
  153. }
  154. String s(reinterpret_cast<const char32_t *>(cs.ptr()), len);
  155. b += len * 4;
  156. total_len -= len * 4;
  157. identifiers.write[i] = s;
  158. }
  159. constants.resize(constant_count);
  160. for (uint32_t i = 0; i < constant_count; i++) {
  161. Variant v;
  162. int len;
  163. Error err = decode_variant(v, b, total_len, &len, false);
  164. if (err) {
  165. return err;
  166. }
  167. b += len;
  168. total_len -= len;
  169. constants.write[i] = v;
  170. }
  171. for (uint32_t i = 0; i < token_line_count; i++) {
  172. ERR_FAIL_COND_V(total_len < 8, ERR_INVALID_DATA);
  173. uint32_t token_index = decode_uint32(b);
  174. b += 4;
  175. uint32_t line = decode_uint32(b);
  176. b += 4;
  177. total_len -= 8;
  178. token_lines[token_index] = line;
  179. }
  180. for (uint32_t i = 0; i < token_line_count; i++) {
  181. ERR_FAIL_COND_V(total_len < 8, ERR_INVALID_DATA);
  182. uint32_t token_index = decode_uint32(b);
  183. b += 4;
  184. uint32_t column = decode_uint32(b);
  185. b += 4;
  186. total_len -= 8;
  187. token_columns[token_index] = column;
  188. }
  189. tokens.resize(token_count);
  190. for (uint32_t i = 0; i < token_count; i++) {
  191. int token_len = 5;
  192. if ((*b) & TOKEN_BYTE_MASK) {
  193. token_len = 8;
  194. }
  195. ERR_FAIL_COND_V(total_len < token_len, ERR_INVALID_DATA);
  196. Token token = _binary_to_token(b);
  197. b += token_len;
  198. ERR_FAIL_INDEX_V(token.type, Token::TK_MAX, ERR_INVALID_DATA);
  199. tokens.write[i] = token;
  200. total_len -= token_len;
  201. }
  202. ERR_FAIL_COND_V(total_len > 0, ERR_INVALID_DATA);
  203. return OK;
  204. }
  205. Vector<uint8_t> GDScriptTokenizerBuffer::parse_code_string(const String &p_code) {
  206. Vector<uint8_t> buf;
  207. HashMap<StringName, uint32_t> identifier_map;
  208. HashMap<Variant, uint32_t, VariantHasher, VariantComparator> constant_map;
  209. Vector<uint8_t> token_buffer;
  210. HashMap<uint32_t, uint32_t> token_lines;
  211. HashMap<uint32_t, uint32_t> token_columns;
  212. GDScriptTokenizerText tokenizer;
  213. tokenizer.set_source_code(p_code);
  214. tokenizer.set_multiline_mode(true); // Ignore whitespace tokens.
  215. Token current = tokenizer.scan();
  216. int token_pos = 0;
  217. int last_token_line = 0;
  218. int token_counter = 0;
  219. while (current.type != Token::TK_EOF) {
  220. int token_len = _token_to_binary(current, token_buffer, token_pos, identifier_map, constant_map);
  221. token_pos += token_len;
  222. if (token_counter > 0 && current.start_line > last_token_line) {
  223. token_lines[token_counter] = current.start_line;
  224. token_columns[token_counter] = current.start_column;
  225. }
  226. last_token_line = current.end_line;
  227. current = tokenizer.scan();
  228. token_counter++;
  229. }
  230. // Reverse maps.
  231. Vector<StringName> rev_identifier_map;
  232. rev_identifier_map.resize(identifier_map.size());
  233. for (const KeyValue<StringName, uint32_t> &E : identifier_map) {
  234. rev_identifier_map.write[E.value] = E.key;
  235. }
  236. Vector<Variant> rev_constant_map;
  237. rev_constant_map.resize(constant_map.size());
  238. for (const KeyValue<Variant, uint32_t> &E : constant_map) {
  239. rev_constant_map.write[E.value] = E.key;
  240. }
  241. HashMap<uint32_t, uint32_t> rev_token_lines;
  242. for (const KeyValue<uint32_t, uint32_t> &E : token_lines) {
  243. rev_token_lines[E.value] = E.key;
  244. }
  245. // Remove continuation lines from map.
  246. for (int line : tokenizer.get_continuation_lines()) {
  247. if (rev_token_lines.has(line + 1)) {
  248. token_lines.erase(rev_token_lines[line + 1]);
  249. token_columns.erase(rev_token_lines[line + 1]);
  250. }
  251. }
  252. // Save header.
  253. buf.resize(24);
  254. buf.write[0] = 'G';
  255. buf.write[1] = 'D';
  256. buf.write[2] = 'S';
  257. buf.write[3] = 'C';
  258. encode_uint32(TOKENIZER_VERSION, &buf.write[4]);
  259. encode_uint32(identifier_map.size(), &buf.write[8]);
  260. encode_uint32(constant_map.size(), &buf.write[12]);
  261. encode_uint32(token_lines.size(), &buf.write[16]);
  262. encode_uint32(token_counter, &buf.write[20]);
  263. int buf_pos = 24;
  264. // Save identifiers.
  265. for (const StringName &id : rev_identifier_map) {
  266. String s = id.operator String();
  267. int len = s.length();
  268. buf.resize(buf_pos + (len + 1) * 4);
  269. encode_uint32(len, &buf.write[buf_pos]);
  270. buf_pos += 4;
  271. for (int i = 0; i < len; i++) {
  272. uint8_t tmp[4];
  273. encode_uint32(s[i], tmp);
  274. for (int b = 0; b < 4; b++) {
  275. buf.write[buf_pos + b] = tmp[b] ^ 0xb6;
  276. }
  277. buf_pos += 4;
  278. }
  279. }
  280. // Save constants.
  281. for (const Variant &v : rev_constant_map) {
  282. int len;
  283. // Objects cannot be constant, never encode objects.
  284. Error err = encode_variant(v, nullptr, len, false);
  285. ERR_FAIL_COND_V_MSG(err != OK, Vector<uint8_t>(), "Error when trying to encode Variant.");
  286. buf.resize(buf_pos + len);
  287. encode_variant(v, &buf.write[buf_pos], len, false);
  288. buf_pos += len;
  289. }
  290. // Save lines and columns.
  291. buf.resize(buf_pos + token_lines.size() * 16);
  292. for (const KeyValue<uint32_t, uint32_t> &e : token_lines) {
  293. encode_uint32(e.key, &buf.write[buf_pos]);
  294. buf_pos += 4;
  295. encode_uint32(e.value, &buf.write[buf_pos]);
  296. buf_pos += 4;
  297. }
  298. for (const KeyValue<uint32_t, uint32_t> &e : token_columns) {
  299. encode_uint32(e.key, &buf.write[buf_pos]);
  300. buf_pos += 4;
  301. encode_uint32(e.value, &buf.write[buf_pos]);
  302. buf_pos += 4;
  303. }
  304. // Store tokens.
  305. buf.append_array(token_buffer);
  306. return buf;
  307. }
  308. int GDScriptTokenizerBuffer::get_cursor_line() const {
  309. return 0;
  310. }
  311. int GDScriptTokenizerBuffer::get_cursor_column() const {
  312. return 0;
  313. }
  314. void GDScriptTokenizerBuffer::set_cursor_position(int p_line, int p_column) {
  315. }
  316. void GDScriptTokenizerBuffer::set_multiline_mode(bool p_state) {
  317. multiline_mode = p_state;
  318. }
  319. bool GDScriptTokenizerBuffer::is_past_cursor() const {
  320. return false;
  321. }
  322. void GDScriptTokenizerBuffer::push_expression_indented_block() {
  323. indent_stack_stack.push_back(indent_stack);
  324. }
  325. void GDScriptTokenizerBuffer::pop_expression_indented_block() {
  326. ERR_FAIL_COND(indent_stack_stack.size() == 0);
  327. indent_stack = indent_stack_stack.back()->get();
  328. indent_stack_stack.pop_back();
  329. }
  330. GDScriptTokenizer::Token GDScriptTokenizerBuffer::scan() {
  331. // Add final newline.
  332. if (current >= tokens.size() && !last_token_was_newline) {
  333. Token newline;
  334. newline.type = Token::NEWLINE;
  335. newline.start_line = current_line;
  336. newline.end_line = current_line;
  337. last_token_was_newline = true;
  338. return newline;
  339. }
  340. // Resolve pending indentation change.
  341. if (pending_indents > 0) {
  342. pending_indents--;
  343. Token indent;
  344. indent.type = Token::INDENT;
  345. indent.start_line = current_line;
  346. indent.end_line = current_line;
  347. return indent;
  348. } else if (pending_indents < 0) {
  349. pending_indents++;
  350. Token dedent;
  351. dedent.type = Token::DEDENT;
  352. dedent.start_line = current_line;
  353. dedent.end_line = current_line;
  354. return dedent;
  355. }
  356. if (current >= tokens.size()) {
  357. if (!indent_stack.is_empty()) {
  358. pending_indents -= indent_stack.size();
  359. indent_stack.clear();
  360. return scan();
  361. }
  362. Token eof;
  363. eof.type = Token::TK_EOF;
  364. return eof;
  365. };
  366. if (!last_token_was_newline && token_lines.has(current)) {
  367. current_line = token_lines[current];
  368. uint32_t current_column = token_columns[current];
  369. // Check if there's a need to indent/dedent.
  370. if (!multiline_mode) {
  371. uint32_t previous_indent = 0;
  372. if (!indent_stack.is_empty()) {
  373. previous_indent = indent_stack.back()->get();
  374. }
  375. if (current_column - 1 > previous_indent) {
  376. pending_indents++;
  377. indent_stack.push_back(current_column - 1);
  378. } else {
  379. while (current_column - 1 < previous_indent) {
  380. pending_indents--;
  381. indent_stack.pop_back();
  382. if (indent_stack.is_empty()) {
  383. break;
  384. }
  385. previous_indent = indent_stack.back()->get();
  386. }
  387. }
  388. Token newline;
  389. newline.type = Token::NEWLINE;
  390. newline.start_line = current_line;
  391. newline.end_line = current_line;
  392. last_token_was_newline = true;
  393. return newline;
  394. }
  395. }
  396. last_token_was_newline = false;
  397. Token token = tokens[current++];
  398. return token;
  399. }