Tokenizer.c 14 KB

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