Tokenizer.c 12 KB

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