gd_tokenizer.cpp 30 KB

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