gd_tokenizer.cpp 30 KB

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