Tokenizer.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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(2, 3) void tError(int codeLine, 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, codeLine, 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(__LINE__, "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(__LINE__, "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(__LINE__, "invalid number on line %d", line);
  122. }
  123. l *= 10;
  124. if(!isNumber(s[i])) {
  125. tError(__LINE__, "invalid character in number '%c' on line %d",
  126. s[i], line);
  127. }
  128. int digit = s[i] - '0';
  129. if(l > LLONG_MAX - digit) {
  130. tError(__LINE__, "invalid number on line %d", line);
  131. }
  132. l += digit;
  133. }
  134. return l;
  135. }
  136. static void tParseNumber(const char* buffer) {
  137. long long l = tParseInt(buffer);
  138. if(tReadSingleIf('.')) {
  139. FileToken* t = tNextToken();
  140. if(t->type != FT_LITERAL) {
  141. tError(__LINE__,
  142. "expected literal after comma of number on line %d", line);
  143. }
  144. long long comma = tParseInt(t->literal);
  145. float f = (float)comma;
  146. while(f > 1.0f) {
  147. f /= 10.0f;
  148. }
  149. f += (float)l;
  150. tAddToken(T_FLOAT_VALUE);
  151. tAdd(&f, sizeof(float));
  152. } else {
  153. if(l > INT32_MAX) {
  154. tError(__LINE__, "invalid int on line %d", line);
  155. }
  156. int32 i = (int)l;
  157. tAddToken(T_INT_VALUE);
  158. tAdd(&i, sizeof(int32));
  159. }
  160. }
  161. static int32 tNextUnicodePart(void) {
  162. FileToken* t = tNextToken();
  163. if(t->type == FT_SINGLE) {
  164. return t->single;
  165. } else if(t->type == FT_LITERAL) {
  166. int length = (int)strlen(t->literal);
  167. if(length != 1) {
  168. tError(__LINE__, "unicode literal has wrong length %d", length);
  169. }
  170. return t->literal[0];
  171. } else {
  172. tError(__LINE__, "cannot read next unicode character part on line %d",
  173. line);
  174. return 0;
  175. }
  176. }
  177. static int32 tUnicode(int32 c) {
  178. if(c == '\\') {
  179. switch(tNextUnicodePart()) {
  180. case '"': c = '"'; break;
  181. case '\\': c = '\\'; break;
  182. case 'n': c = '\n'; break;
  183. case 'r': c = '\r'; break;
  184. case 't': c = '\t'; break;
  185. default:
  186. tError(__LINE__, "unknown escaped character at line %d", line);
  187. }
  188. }
  189. if((c & 0xE0) == 0xC0) {
  190. c = ((c & 0x1F) << 6) | (tNextUnicodePart() & 0x3F);
  191. } else if((c & 0xF0) == 0xE0) {
  192. c = ((c & 0xF) << 12) | ((tNextUnicodePart() & 0x3F) << 6);
  193. c |= tNextUnicodePart() & 0x3F;
  194. } else if((c & 0xF8) == 0xF0) {
  195. c = ((c & 0x7) << 18) | ((tNextUnicodePart() & 0x3F) << 12);
  196. c |= (tNextUnicodePart() & 0x3F) << 6;
  197. c |= tNextUnicodePart() & 0x3F;
  198. }
  199. return c;
  200. }
  201. static int32 tReadNextPart(const char* s, int* index, int length) {
  202. if(*index >= length) {
  203. tError(__LINE__, "missing escape character");
  204. }
  205. return s[(*index)++];
  206. }
  207. static int32 tStringUnicode(const char* s, int* index, int length) {
  208. int32 c = s[(*index)++];
  209. if(c == '\\') {
  210. switch(tReadNextPart(s, index, length)) {
  211. case '"': c = '"'; break;
  212. case '\\': c = '\\'; break;
  213. case 'n': c = '\n'; break;
  214. case 'r': c = '\r'; break;
  215. case 't': c = '\t'; break;
  216. default:
  217. tError(__LINE__, "unknown escaped character at line %d", line);
  218. }
  219. }
  220. if((c & 0xE0) == 0xC0) {
  221. c = ((c & 0x1F) << 6) | (tReadNextPart(s, index, length) & 0x3F);
  222. } else if((c & 0xF0) == 0xE0) {
  223. c = ((c & 0xF) << 12) | ((tReadNextPart(s, index, length) & 0x3F) << 6);
  224. c |= tReadNextPart(s, index, length) & 0x3F;
  225. } else if((c & 0xF8) == 0xF0) {
  226. c = ((c & 0x7) << 18) |
  227. ((tReadNextPart(s, index, length) & 0x3F) << 12);
  228. c |= (tReadNextPart(s, index, length) & 0x3F) << 6;
  229. c |= tReadNextPart(s, index, length) & 0x3F;
  230. }
  231. return c;
  232. }
  233. static void tAddString(void) {
  234. tAddToken(T_TEXT);
  235. FileToken* t = tNextToken();
  236. if(t->type == FT_SINGLE) {
  237. if(t->single != '"') {
  238. tError(__LINE__, "unexpected single '%d'", t->single);
  239. }
  240. } else if(t->type == FT_LITERAL) {
  241. int length = (int)strlen(t->literal);
  242. const char* s = t->literal;
  243. int index = 0;
  244. while(index < length) {
  245. int32 c = tStringUnicode(s, &index, length);
  246. tAdd(&c, sizeof(int32));
  247. }
  248. if(!tReadSingleIf('"')) {
  249. tError(__LINE__, "unclosed string");
  250. }
  251. } else {
  252. tError(__LINE__, "unexpected string file token %u", t->type);
  253. }
  254. int32 c = 0;
  255. tAdd(&c, sizeof(int32));
  256. }
  257. static void tAddUnicode(void) {
  258. FileToken* t = tNextToken();
  259. if(t->type == FT_LITERAL) {
  260. int length = (int)strlen(t->literal);
  261. if(length != 1) {
  262. tError(__LINE__, "invalid character on line %d", line);
  263. }
  264. int32 c = t->literal[0];
  265. tAddToken(T_INT_VALUE);
  266. tAdd(&c, sizeof(int32));
  267. } else if(t->type == FT_SINGLE) {
  268. int32 c = tUnicode(t->single);
  269. tAddToken(T_INT_VALUE);
  270. tAdd(&c, sizeof(int32));
  271. } else {
  272. tError(__LINE__, "invalid character on line %d", line);
  273. }
  274. if(!tReadSingleIf('\'')) {
  275. tError(__LINE__, "expecting unicode end");
  276. }
  277. }
  278. static void tAddToken2(Token te, Token t) {
  279. if(tReadSingleIf('=')) {
  280. tAddToken(te);
  281. } else {
  282. tAddToken(t);
  283. }
  284. }
  285. static void tAddToken3(int c, Token tc, Token te, Token t) {
  286. if(tReadSingleIf(c)) {
  287. tAddToken(tc);
  288. } else {
  289. tAddToken2(te, t);
  290. }
  291. }
  292. static void tAddToken4(int c, Token tce, Token tc, Token te, Token t) {
  293. if(tReadSingleIf(c)) {
  294. tAddToken2(tce, tc);
  295. } else {
  296. tAddToken2(te, t);
  297. }
  298. }
  299. static void tParseToken(FileToken* t) {
  300. switch(t->type) {
  301. case FT_PATH: {
  302. if(includeStackIndex >= INCLUDE_STACK_LENGTH) {
  303. tError(__LINE__, "include stack overflow");
  304. }
  305. includeStack[includeStackIndex].path = t->literal;
  306. includeStack[includeStackIndex].line = line;
  307. includeStackIndex++;
  308. line = 1;
  309. tAddToken(T_OPEN_PATH);
  310. tAdd(t->literal, (int)strlen(t->literal) + 1);
  311. return;
  312. }
  313. case FT_END_PATH: {
  314. if(includeStackIndex <= 0) {
  315. tError(__LINE__, "include stack underflow");
  316. }
  317. includeStackIndex--;
  318. line = includeStack[includeStackIndex].line;
  319. tAddToken(T_CLOSE_PATH);
  320. const char* path = includeStackIndex > 0
  321. ? includeStack[includeStackIndex - 1].path
  322. : "path not set";
  323. tAdd(path, (int)strlen(path) + 1);
  324. return;
  325. }
  326. case FT_NEWLINE: line++; return;
  327. case FT_LITERAL: {
  328. const char* buffer = t->literal;
  329. if(isLetter(buffer[0])) {
  330. tParseLiteral(buffer);
  331. } else if(isNumber(buffer[0])) {
  332. tParseNumber(buffer);
  333. } else {
  334. tError(__LINE__, "invalid literal string '%s'", buffer);
  335. }
  336. return;
  337. }
  338. case FT_SPACE: return;
  339. case FT_SINGLE: {
  340. int c = t->single;
  341. switch(c) {
  342. case ' ': return;
  343. case '+':
  344. tAddToken3('+', T_INCREMENT, T_ADD_SET, T_ADD);
  345. return;
  346. case '-':
  347. tAddToken3('-', T_DECREMENT, T_SUB_SET, T_SUB);
  348. return;
  349. case '*': tAddToken2(T_MUL_SET, T_MUL); return;
  350. case '/': tAddToken2(T_DIV_SET, T_DIV); return;
  351. case '%': tAddToken2(T_MOD_SET, T_MOD); return;
  352. case '<':
  353. tAddToken4('<', T_LEFT_SHIFT_SET, T_LEFT_SHIFT,
  354. T_LESS_EQUAL, T_LESS);
  355. return;
  356. case '>':
  357. tAddToken4('>', T_RIGHT_SHIFT_SET, T_RIGHT_SHIFT,
  358. T_GREATER_EQUAL, T_GREATER);
  359. return;
  360. case '=': tAddToken2(T_EQUAL, T_SET); return;
  361. case '!': tAddToken2(T_NOT_EQUAL, T_NOT); return;
  362. case '&':
  363. tAddToken3('&', T_AND, T_BIT_AND_SET, T_BIT_AND);
  364. return;
  365. case '|': tAddToken3('|', T_OR, T_BIT_OR_SET, T_BIT_OR); return;
  366. case '~': tAddToken(T_BIT_NOT); return;
  367. case '^': tAddToken2(T_BIT_XOR_SET, T_BIT_XOR); return;
  368. case ',': tAddToken(T_COMMA); return;
  369. case ';': tAddToken(T_SEMICOLON); return;
  370. case '(': tAddToken(T_OPEN_BRACKET); return;
  371. case ')': tAddToken(T_CLOSE_BRACKET); return;
  372. case '{': tAddToken(T_OPEN_CURVED_BRACKET); return;
  373. case '}': tAddToken(T_CLOSE_CURVED_BRACKET); return;
  374. case '"': tAddString(); return;
  375. case '\'': tAddUnicode(); return;
  376. case '.': tAddToken(T_POINT); return;
  377. case '[': tAddToken(T_OPEN_SQUARE_BRACKET); return;
  378. case ']': tAddToken(T_CLOSE_SQUARE_BRACKET); return;
  379. }
  380. if(isprint(c)) {
  381. tError(__LINE__, "unknown character on line %d: %c", line, c);
  382. } else {
  383. tError(__LINE__, "unknown character on line %d: %d", line,
  384. (int)c);
  385. }
  386. }
  387. }
  388. }
  389. static void tParseFile(void) {
  390. readIndex = 0;
  391. writeIndex = 0;
  392. line = 1;
  393. fileTokenIndex = 0;
  394. while(fileTokenIndex < fileTokens.length) {
  395. tParseToken(fileTokens.tokens + fileTokenIndex++);
  396. }
  397. }
  398. void tTokenize(const char* path, Error* e) {
  399. error = e;
  400. ftInit(path, &fileTokens, e);
  401. if(!eHasError(e) && !setjmp(errorJump)) {
  402. tParseFile();
  403. }
  404. if(eHasError(e)) {
  405. ftPrint(&fileTokens);
  406. }
  407. includeStackIndex = 0;
  408. ftDelete(&fileTokens);
  409. }
  410. Token tPeekToken(void) {
  411. if(readIndex >= writeIndex) {
  412. return T_END;
  413. }
  414. return (Token)tokenBuffer[readIndex];
  415. }
  416. bool tReadTokenAndLine(Token* t, int16* l) {
  417. if(readIndex >= writeIndex) {
  418. return true;
  419. }
  420. *t = (Token)tokenBuffer[readIndex++];
  421. return tReadTokens(l, sizeof(int16));
  422. }
  423. bool tReadInt32(int32* i) {
  424. return tReadTokens(i, sizeof(int32));
  425. }
  426. bool tReadFloat(float* f) {
  427. return tReadTokens(f, sizeof(float));
  428. }
  429. const char* tReadString(void) {
  430. const char* s = tokenBuffer + readIndex;
  431. while(readIndex <= writeIndex) {
  432. if(tokenBuffer[readIndex++] == '\0') {
  433. return s;
  434. }
  435. }
  436. return NULL;
  437. }
  438. int tGetMarker(void) {
  439. return readIndex;
  440. }
  441. void tReset(int marker) {
  442. readIndex = marker;
  443. }