Tokenizer.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #include <ctype.h>
  2. #include <limits.h>
  3. #include <setjmp.h>
  4. #include <stdarg.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "tokenizer/FileTokens.h"
  9. #include "tokenizer/Tokenizer.h"
  10. #include "utils/SnuviUtils.h"
  11. #define TOKEN_BUFFER_LENGTH (1024 * 1024)
  12. #define INCLUDE_STACK_LENGTH 32
  13. static FileTokens fileTokens;
  14. static int fileTokenIndex;
  15. static jmp_buf errorJump;
  16. static Error* error = NULL;
  17. typedef struct {
  18. const char* path;
  19. int16 line;
  20. char padding[sizeof(const char*) - sizeof(int16)];
  21. } IncludeEntry;
  22. static IncludeEntry includeStack[INCLUDE_STACK_LENGTH];
  23. static int includeStackIndex = 0;
  24. static char tokenBuffer[TOKEN_BUFFER_LENGTH];
  25. static int writeIndex = 0;
  26. static int readIndex = 0;
  27. static int16 line = 1;
  28. static void tError(const char* format, ...) {
  29. va_list args;
  30. va_start(args, format);
  31. const char* path = includeStackIndex > 0
  32. ? includeStack[includeStackIndex - 1].path
  33. : "path not set";
  34. eInitErrorV(error, path, line, format, args);
  35. for(int i = includeStackIndex - 2; i >= 0; i--) {
  36. eAddPath(error, includeStack[i].path);
  37. }
  38. va_end(args);
  39. longjmp(errorJump, 0);
  40. }
  41. static void tAdd(const void* data, int length) {
  42. if(writeIndex + length > TOKEN_BUFFER_LENGTH) {
  43. tError("the token buffer is too small");
  44. }
  45. memcpy(tokenBuffer + writeIndex, data, (size_t)length);
  46. writeIndex += length;
  47. }
  48. static void tAddToken(Token token) {
  49. unsigned char c = token;
  50. tAdd(&c, 1);
  51. tAdd(&line, sizeof(line));
  52. }
  53. static bool tReadTokens(void* dest, int length) {
  54. if(readIndex + length > writeIndex) {
  55. return true;
  56. }
  57. memcpy(dest, tokenBuffer + readIndex, (size_t)length);
  58. readIndex += length;
  59. return false;
  60. }
  61. static FileToken* tNextToken(void) {
  62. if(fileTokenIndex >= fileTokens.length) {
  63. tError("unexpected end of file");
  64. }
  65. return fileTokens.tokens + fileTokenIndex++;
  66. }
  67. static bool tReadSingleIf(int c) {
  68. if(fileTokenIndex >= fileTokens.length ||
  69. fileTokens.tokens[fileTokenIndex].type != FT_SINGLE ||
  70. fileTokens.tokens[fileTokenIndex].single != c) {
  71. return false;
  72. }
  73. fileTokenIndex++;
  74. return true;
  75. }
  76. static void tParseLiteral(const char* buffer) {
  77. if(strcmp(buffer, "return") == 0) {
  78. tAddToken(T_RETURN);
  79. } else if(strcmp(buffer, "if") == 0) {
  80. tAddToken(T_IF);
  81. } else if(strcmp(buffer, "else") == 0) {
  82. tAddToken(T_ELSE);
  83. } else if(strcmp(buffer, "while") == 0) {
  84. tAddToken(T_WHILE);
  85. } else if(strcmp(buffer, "for") == 0) {
  86. tAddToken(T_FOR);
  87. } else if(strcmp(buffer, "break") == 0) {
  88. tAddToken(T_BREAK);
  89. } else if(strcmp(buffer, "continue") == 0) {
  90. tAddToken(T_CONTINUE);
  91. } else if(strcmp(buffer, "int") == 0) {
  92. tAddToken(T_INT);
  93. } else if(strcmp(buffer, "void") == 0) {
  94. tAddToken(T_VOID);
  95. } else if(strcmp(buffer, "float") == 0) {
  96. tAddToken(T_FLOAT);
  97. } else if(strcmp(buffer, "struct") == 0) {
  98. tAddToken(T_STRUCT);
  99. } else if(strcmp(buffer, "new") == 0) {
  100. tAddToken(T_NEW);
  101. } else if(strcmp(buffer, "length") == 0) {
  102. tAddToken(T_LENGTH);
  103. } else if(strcmp(buffer, "true") == 0) {
  104. int32 i = 1;
  105. tAddToken(T_INT_VALUE);
  106. tAdd(&i, sizeof(int32));
  107. } else if(strcmp(buffer, "false") == 0) {
  108. int32 i = 0;
  109. tAddToken(T_INT_VALUE);
  110. tAdd(&i, sizeof(int32));
  111. } else {
  112. tAddToken(T_LITERAL);
  113. tAdd(buffer, (int)strlen(buffer) + 1);
  114. }
  115. }
  116. static long long tParseInt(const char* s) {
  117. long long l = 0;
  118. for(int i = 0; s[i] != '\0'; i++) {
  119. if(l > (LLONG_MAX / 10)) {
  120. tError("invalid number on line %d", line);
  121. }
  122. l *= 10;
  123. if(!isNumber(s[i])) {
  124. tError("invalid character in number '%c' on line %d", s[i], line);
  125. }
  126. int digit = s[i] - '0';
  127. if(l > LLONG_MAX - digit) {
  128. tError("invalid number on line %d", line);
  129. }
  130. l += digit;
  131. }
  132. return l;
  133. }
  134. static void tParseNumber(const char* buffer) {
  135. long long l = tParseInt(buffer);
  136. if(tReadSingleIf('.')) {
  137. FileToken* t = tNextToken();
  138. if(t->type != FT_LITERAL) {
  139. tError("expected literal after comma of number on line %d", line);
  140. }
  141. long long comma = tParseInt(t->literal);
  142. float f = (float)comma;
  143. while(f > 1.0f) {
  144. f /= 10.0f;
  145. }
  146. f += (float)l;
  147. tAddToken(T_FLOAT_VALUE);
  148. tAdd(&f, sizeof(float));
  149. } else {
  150. if(l > INT32_MAX) {
  151. tError("invalid int on line %d", line);
  152. }
  153. int32 i = (int)l;
  154. tAddToken(T_INT_VALUE);
  155. tAdd(&i, sizeof(int32));
  156. }
  157. }
  158. static int32 tNextUnicodePart(void) {
  159. FileToken* t = tNextToken();
  160. if(t->type == FT_SINGLE) {
  161. return t->single;
  162. } else if(t->type == FT_LITERAL) {
  163. int length = (int)strlen(t->literal);
  164. if(length != 1) {
  165. tError("unicode literal has wrong length %d", length);
  166. }
  167. return t->literal[0];
  168. } else {
  169. tError("cannot read next unicode character part on line %d", line);
  170. return 0;
  171. }
  172. }
  173. static int32 tUnicode(int32 c) {
  174. if(c == '\\') {
  175. switch(tNextUnicodePart()) {
  176. case '"': c = '"'; break;
  177. case '\\': c = '\\'; break;
  178. case 'n': c = '\n'; break;
  179. case 'r': c = '\r'; break;
  180. case 't': c = '\t'; break;
  181. default: tError("unknown escaped character at line %d", line);
  182. }
  183. }
  184. if((c & 0xE0) == 0xC0) {
  185. c = ((c & 0x1F) << 6) | (tNextUnicodePart() & 0x3F);
  186. } else if((c & 0xF0) == 0xE0) {
  187. c = ((c & 0xF) << 12) | ((tNextUnicodePart() & 0x3F) << 6);
  188. c |= tNextUnicodePart() & 0x3F;
  189. } else if((c & 0xF8) == 0xF0) {
  190. c = ((c & 0x7) << 18) | ((tNextUnicodePart() & 0x3F) << 12);
  191. c |= (tNextUnicodePart() & 0x3F) << 6;
  192. c |= tNextUnicodePart() & 0x3F;
  193. }
  194. return c;
  195. }
  196. static int32 tReadNextPart(const char* s, int* index, int length) {
  197. if(*index >= length) {
  198. tError("missing escape character");
  199. }
  200. return s[(*index)++];
  201. }
  202. static int32 tStringUnicode(const char* s, int* index, int length) {
  203. int32 c = s[(*index)++];
  204. if(c == '\\') {
  205. switch(tReadNextPart(s, index, length)) {
  206. case '"': c = '"'; break;
  207. case '\\': c = '\\'; break;
  208. case 'n': c = '\n'; break;
  209. case 'r': c = '\r'; break;
  210. case 't': c = '\t'; break;
  211. default: tError("unknown escaped character at line %d", line);
  212. }
  213. }
  214. if((c & 0xE0) == 0xC0) {
  215. c = ((c & 0x1F) << 6) | (tReadNextPart(s, index, length) & 0x3F);
  216. } else if((c & 0xF0) == 0xE0) {
  217. c = ((c & 0xF) << 12) | ((tReadNextPart(s, index, length) & 0x3F) << 6);
  218. c |= tReadNextPart(s, index, length) & 0x3F;
  219. } else if((c & 0xF8) == 0xF0) {
  220. c = ((c & 0x7) << 18) |
  221. ((tReadNextPart(s, index, length) & 0x3F) << 12);
  222. c |= (tReadNextPart(s, index, length) & 0x3F) << 6;
  223. c |= tReadNextPart(s, index, length) & 0x3F;
  224. }
  225. return c;
  226. }
  227. static void tAddString(void) {
  228. tAddToken(T_TEXT);
  229. FileToken* t = tNextToken();
  230. if(t->type == FT_SINGLE) {
  231. if(t->single != '"') {
  232. tError("unexpected single '%d'", t->single);
  233. }
  234. } else if(t->type == FT_LITERAL) {
  235. int length = (int)strlen(t->literal);
  236. const char* s = t->literal;
  237. int index = 0;
  238. while(index < length) {
  239. int32 c = tStringUnicode(s, &index, length);
  240. tAdd(&c, sizeof(int32));
  241. }
  242. if(!tReadSingleIf('"')) {
  243. tError("unclosed string");
  244. }
  245. } else {
  246. tError("unexpected string file token %d", t);
  247. }
  248. int32 c = 0;
  249. tAdd(&c, sizeof(int32));
  250. }
  251. static void tAddUnicode(void) {
  252. FileToken* t = tNextToken();
  253. if(t->type == FT_LITERAL) {
  254. int length = (int)strlen(t->literal);
  255. if(length != 1) {
  256. tError("invalid character on line %d", line);
  257. }
  258. int32 c = t->literal[0];
  259. tAddToken(T_INT_VALUE);
  260. tAdd(&c, sizeof(int32));
  261. } else if(t->type == FT_SINGLE) {
  262. int32 c = tUnicode(t->single);
  263. tAddToken(T_INT_VALUE);
  264. tAdd(&c, sizeof(int32));
  265. } else {
  266. tError("invalid character on line %d", line);
  267. }
  268. if(!tReadSingleIf('\'')) {
  269. tError("expecting unicode end");
  270. }
  271. }
  272. static void tAddToken2(Token te, Token t) {
  273. if(tReadSingleIf('=')) {
  274. tAddToken(te);
  275. } else {
  276. tAddToken(t);
  277. }
  278. }
  279. static void tAddToken3(int c, Token tc, Token te, Token t) {
  280. if(tReadSingleIf(c)) {
  281. tAddToken(tc);
  282. } else {
  283. tAddToken2(te, t);
  284. }
  285. }
  286. static void tAddToken4(int c, Token tce, Token tc, Token te, Token t) {
  287. if(tReadSingleIf(c)) {
  288. tAddToken2(tce, tc);
  289. } else {
  290. tAddToken2(te, t);
  291. }
  292. }
  293. static void tParseToken(FileToken* t) {
  294. switch(t->type) {
  295. case FT_PATH: {
  296. if(includeStackIndex >= INCLUDE_STACK_LENGTH) {
  297. tError("include stack overflow");
  298. }
  299. includeStack[includeStackIndex].path = t->literal;
  300. includeStack[includeStackIndex].line = line;
  301. includeStackIndex++;
  302. line = 1;
  303. tAddToken(T_OPEN_PATH);
  304. tAdd(t->literal, (int)strlen(t->literal) + 1);
  305. return;
  306. }
  307. case FT_END_PATH: {
  308. if(includeStackIndex <= 0) {
  309. tError("include stack underflow");
  310. }
  311. includeStackIndex--;
  312. line = includeStack[includeStackIndex].line;
  313. tAddToken(T_CLOSE_PATH);
  314. const char* path = includeStackIndex > 0
  315. ? includeStack[includeStackIndex - 1].path
  316. : "path not set";
  317. tAdd(path, (int)strlen(path) + 1);
  318. return;
  319. }
  320. case FT_NEWLINE: line++; return;
  321. case FT_LITERAL: {
  322. const char* buffer = t->literal;
  323. if(isLetter(buffer[0])) {
  324. tParseLiteral(buffer);
  325. } else if(isNumber(buffer[0])) {
  326. tParseNumber(buffer);
  327. } else {
  328. tError("invalid literal string '%s'", buffer);
  329. }
  330. return;
  331. }
  332. case FT_SPACE: return;
  333. case FT_SINGLE: {
  334. int c = t->single;
  335. switch(c) {
  336. case ' ': return;
  337. case '+':
  338. tAddToken3('+', T_INCREMENT, T_ADD_SET, T_ADD);
  339. return;
  340. case '-':
  341. tAddToken3('-', T_DECREMENT, T_SUB_SET, T_SUB);
  342. return;
  343. case '*': tAddToken2(T_MUL_SET, T_MUL); return;
  344. case '/': tAddToken2(T_DIV_SET, T_DIV); return;
  345. case '%': tAddToken2(T_MOD_SET, T_MOD); return;
  346. case '<':
  347. tAddToken4('<', T_LEFT_SHIFT_SET, T_LEFT_SHIFT,
  348. T_LESS_EQUAL, T_LESS);
  349. return;
  350. case '>':
  351. tAddToken4('>', T_RIGHT_SHIFT_SET, T_RIGHT_SHIFT,
  352. T_GREATER_EQUAL, T_GREATER);
  353. return;
  354. case '=': tAddToken2(T_EQUAL, T_SET); return;
  355. case '!': tAddToken2(T_NOT_EQUAL, T_NOT); return;
  356. case '&':
  357. tAddToken3('&', T_AND, T_BIT_AND_SET, T_BIT_AND);
  358. return;
  359. case '|': tAddToken3('|', T_OR, T_BIT_OR_SET, T_BIT_OR); return;
  360. case '~': tAddToken(T_BIT_NOT); return;
  361. case '^': tAddToken2(T_BIT_XOR_SET, T_BIT_XOR); return;
  362. case ',': tAddToken(T_COMMA); return;
  363. case ';': tAddToken(T_SEMICOLON); return;
  364. case '(': tAddToken(T_OPEN_BRACKET); return;
  365. case ')': tAddToken(T_CLOSE_BRACKET); return;
  366. case '{': tAddToken(T_OPEN_CURVED_BRACKET); return;
  367. case '}': tAddToken(T_CLOSE_CURVED_BRACKET); return;
  368. case '"': tAddString(); return;
  369. case '\'': tAddUnicode(); return;
  370. case '.': tAddToken(T_POINT); return;
  371. case '[': tAddToken(T_OPEN_SQUARE_BRACKET); return;
  372. case ']': tAddToken(T_CLOSE_SQUARE_BRACKET); return;
  373. }
  374. if(isprint(c)) {
  375. tError("unknown character on line %d: %c", line, c);
  376. } else {
  377. tError("unknown character on line %d: %d", line, (int)c);
  378. }
  379. }
  380. }
  381. }
  382. static void tParseFile(void) {
  383. readIndex = 0;
  384. writeIndex = 0;
  385. line = 1;
  386. fileTokenIndex = 0;
  387. while(fileTokenIndex < fileTokens.length) {
  388. tParseToken(fileTokens.tokens + fileTokenIndex++);
  389. }
  390. }
  391. void tTokenize(const char* path, Error* e) {
  392. error = e;
  393. ftInit(path, &fileTokens, e);
  394. if(!eHasError(e) && !setjmp(errorJump)) {
  395. tParseFile();
  396. }
  397. if(eHasError(e)) {
  398. ftPrint(&fileTokens);
  399. }
  400. includeStackIndex = 0;
  401. ftDelete(&fileTokens);
  402. }
  403. Token tPeekToken(void) {
  404. if(readIndex >= writeIndex) {
  405. return T_END;
  406. }
  407. return (Token)tokenBuffer[readIndex];
  408. }
  409. bool tReadTokenAndLine(Token* t, int16* l) {
  410. if(readIndex >= writeIndex) {
  411. return true;
  412. }
  413. *t = (Token)tokenBuffer[readIndex++];
  414. return tReadTokens(l, sizeof(int16));
  415. }
  416. bool tReadInt32(int32* i) {
  417. return tReadTokens(i, sizeof(int32));
  418. }
  419. bool tReadFloat(float* f) {
  420. return tReadTokens(f, sizeof(float));
  421. }
  422. const char* tReadString(void) {
  423. const char* s = tokenBuffer + readIndex;
  424. while(readIndex <= writeIndex) {
  425. if(tokenBuffer[readIndex++] == '\0') {
  426. return s;
  427. }
  428. }
  429. return NULL;
  430. }
  431. int tGetMarker(void) {
  432. return readIndex;
  433. }
  434. void tReset(int marker) {
  435. readIndex = marker;
  436. }