Tokenizer.c 14 KB

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