Tokenizer.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #include <limits.h>
  2. #include <setjmp.h>
  3. #include <stdarg.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "tokenizer/FileReader.h"
  8. #include "tokenizer/Tokenizer.h"
  9. #include "utils/SnuviUtils.h"
  10. #define TOKEN_BUFFER_LENGTH (1024 * 1024)
  11. #define ERROR_LENGTH 256
  12. static FileReader fileReader;
  13. static jmp_buf errorJump;
  14. static Error* error;
  15. static char tokenBuffer[TOKEN_BUFFER_LENGTH];
  16. static int writeIndex = 0;
  17. static int readIndex = 0;
  18. static int16 line = 1;
  19. static void tError(const char* format, ...) {
  20. va_list args;
  21. va_start(args, format);
  22. eInitErrorV(error, line, format, args);
  23. va_end(args);
  24. longjmp(errorJump, 0);
  25. }
  26. static void tAdd(const void* data, int length) {
  27. if(writeIndex + length > TOKEN_BUFFER_LENGTH) {
  28. tError("the token buffer is too small");
  29. }
  30. memcpy(tokenBuffer + writeIndex, data, length);
  31. writeIndex += length;
  32. }
  33. static void tAddToken(Token token) {
  34. unsigned char c = token;
  35. tAdd(&c, 1);
  36. tAdd(&line, sizeof(line));
  37. }
  38. static bool tReadTokens(void* dest, int length) {
  39. if(readIndex + length > writeIndex) {
  40. return true;
  41. }
  42. memcpy(dest, tokenBuffer + readIndex, length);
  43. readIndex += length;
  44. return false;
  45. }
  46. static void tParseLiteral(int c) {
  47. int index = 1;
  48. char buffer[64];
  49. buffer[0] = c;
  50. while(isAllowedInName(frPeek(&fileReader))) {
  51. if(index >= 63) {
  52. tError("literal is too long");
  53. }
  54. buffer[index++] = frRead(&fileReader);
  55. }
  56. buffer[index] = '\0';
  57. if(strcmp(buffer, "return") == 0) {
  58. tAddToken(T_RETURN);
  59. } else if(strcmp(buffer, "if") == 0) {
  60. tAddToken(T_IF);
  61. } else if(strcmp(buffer, "else") == 0) {
  62. tAddToken(T_ELSE);
  63. } else if(strcmp(buffer, "while") == 0) {
  64. tAddToken(T_WHILE);
  65. } else if(strcmp(buffer, "for") == 0) {
  66. tAddToken(T_FOR);
  67. } else if(strcmp(buffer, "break") == 0) {
  68. tAddToken(T_BREAK);
  69. } else if(strcmp(buffer, "continue") == 0) {
  70. tAddToken(T_CONTINUE);
  71. } else if(strcmp(buffer, "int") == 0) {
  72. tAddToken(T_INT);
  73. } else if(strcmp(buffer, "void") == 0) {
  74. tAddToken(T_VOID);
  75. } else if(strcmp(buffer, "float") == 0) {
  76. tAddToken(T_FLOAT);
  77. } else if(strcmp(buffer, "struct") == 0) {
  78. tAddToken(T_STRUCT);
  79. } else if(strcmp(buffer, "new") == 0) {
  80. tAddToken(T_NEW);
  81. } else if(strcmp(buffer, "length") == 0) {
  82. tAddToken(T_LENGTH);
  83. } else if(strcmp(buffer, "true") == 0) {
  84. int32 i = 1;
  85. tAddToken(T_INT_VALUE);
  86. tAdd(&i, sizeof(int32));
  87. } else if(strcmp(buffer, "false") == 0) {
  88. int32 i = 0;
  89. tAddToken(T_INT_VALUE);
  90. tAdd(&i, sizeof(int32));
  91. } else {
  92. tAddToken(T_LITERAL);
  93. tAdd(buffer, index + 1);
  94. }
  95. }
  96. static bool tParseInt(char* s, int length, long long* l) {
  97. *l = 0;
  98. for(int i = 0; i < length; i++) {
  99. if(*l > (LLONG_MAX / 10)) {
  100. return true;
  101. }
  102. *l *= 10;
  103. int digit = s[i] - '0';
  104. if(*l > LLONG_MAX - digit) {
  105. return true;
  106. }
  107. *l += digit;
  108. }
  109. return false;
  110. }
  111. static void tParseNumber(int c) {
  112. int index = 1;
  113. char buffer[64];
  114. buffer[0] = c;
  115. bool point = false;
  116. while(true) {
  117. int c = frPeek(&fileReader);
  118. if(c == '.') {
  119. point = true;
  120. } else if(!isNumber(c)) {
  121. break;
  122. } else if(index >= 63) {
  123. tError("number is too long");
  124. }
  125. buffer[index++] = frRead(&fileReader);
  126. }
  127. buffer[index] = '\0';
  128. if(point) {
  129. char* end = NULL;
  130. float f = strtof(buffer, &end);
  131. if(end[0] != '\0') {
  132. tError("invalid float on line %d", line);
  133. }
  134. tAddToken(T_FLOAT_VALUE);
  135. tAdd(&f, sizeof(float));
  136. } else {
  137. long long l;
  138. if(tParseInt(buffer, index, &l) || l > INT32_MAX) {
  139. tError("invalid int on line %d", line);
  140. }
  141. int32 i = l;
  142. tAddToken(T_INT_VALUE);
  143. tAdd(&i, sizeof(int32));
  144. }
  145. }
  146. static int32 tUnicode(int32 c) {
  147. if(c == '\\') {
  148. switch(frRead(&fileReader)) {
  149. case '"': c = '"'; break;
  150. case '\\': c = '\\'; break;
  151. case 'n': c = '\n'; break;
  152. case 'r': c = '\r'; break;
  153. case 't': c = '\t'; break;
  154. default: tError("unknown escaped character at line %d", line);
  155. }
  156. }
  157. if((c & 0xE0) == 0xC0) {
  158. c = ((c & 0x1F) << 6) | (frRead(&fileReader) & 0x3F);
  159. } else if((c & 0xF0) == 0xE0) {
  160. c = ((c & 0xF) << 12) | ((frRead(&fileReader) & 0x3F) << 6);
  161. c |= frRead(&fileReader) & 0x3F;
  162. } else if((c & 0xF8) == 0xF0) {
  163. c = ((c & 0x7) << 18) | ((frRead(&fileReader) & 0x3F) << 12);
  164. c |= (frRead(&fileReader) & 0x3F) << 6;
  165. c |= frRead(&fileReader) & 0x3F;
  166. }
  167. return c;
  168. }
  169. static void tAddString() {
  170. tAddToken(T_TEXT);
  171. while(true) {
  172. int32 c = frRead(&fileReader);
  173. if(c == '"') {
  174. break;
  175. } else if(c == EOF) {
  176. tError("unclosed string starting at line %d", line);
  177. }
  178. c = tUnicode(c);
  179. tAdd(&c, sizeof(int32));
  180. }
  181. int32 c = 0;
  182. tAdd(&c, sizeof(int32));
  183. }
  184. static void tAddUnicode() {
  185. int32 c = frRead(&fileReader);
  186. c = tUnicode(c);
  187. tAddToken(T_INT_VALUE);
  188. tAdd(&c, sizeof(int32));
  189. if(frRead(&fileReader) != '\'') {
  190. tError("expecting unicode end");
  191. }
  192. }
  193. static void tAddToken2(Token te, Token t) {
  194. if(frReadIf(&fileReader, '=')) {
  195. tAddToken(te);
  196. } else {
  197. tAddToken(t);
  198. }
  199. }
  200. static void tAddToken3(int c, Token tc, Token te, Token t) {
  201. if(frReadIf(&fileReader, c)) {
  202. tAddToken(tc);
  203. } else {
  204. tAddToken2(te, t);
  205. }
  206. }
  207. static void tAddToken4(int c, Token tce, Token tc, Token te, Token t) {
  208. if(frReadIf(&fileReader, c)) {
  209. tAddToken2(tce, tc);
  210. } else {
  211. tAddToken2(te, t);
  212. }
  213. }
  214. static void tLineComment() {
  215. while(true) {
  216. int c = frRead(&fileReader);
  217. if(c == EOF || c == '\n') {
  218. line++;
  219. return;
  220. }
  221. }
  222. }
  223. static void tMultipleLineComment() {
  224. while(true) {
  225. int c = frRead(&fileReader);
  226. if(c == EOF) {
  227. tError("unclosed comment at line %d", line);
  228. } else if(c == '\n') {
  229. line++;
  230. } else if(c == '*' && frReadIf(&fileReader, '/')) {
  231. return;
  232. }
  233. }
  234. }
  235. static void tSlash() {
  236. if(frReadIf(&fileReader, '/')) {
  237. tLineComment();
  238. } else if(frReadIf(&fileReader, '*')) {
  239. tMultipleLineComment();
  240. } else {
  241. tAddToken2(T_DIV_SET, T_DIV);
  242. }
  243. }
  244. static void tParseToken(int c) {
  245. if(isLetter(c)) {
  246. tParseLiteral(c);
  247. return;
  248. } else if(isNumber(c)) {
  249. tParseNumber(c);
  250. return;
  251. }
  252. switch(c) {
  253. case ' ': return;
  254. case '\n': line++; return;
  255. case '+': tAddToken3('+', T_INCREMENT, T_ADD_SET, T_ADD); return;
  256. case '-': tAddToken3('-', T_DECREMENT, T_SUB_SET, T_SUB); return;
  257. case '*': tAddToken2(T_MUL_SET, T_MUL); return;
  258. case '/': tSlash(); return;
  259. case '%': tAddToken2(T_MOD_SET, T_MOD); return;
  260. case '<':
  261. tAddToken4('<', T_LEFT_SHIFT_SET, T_LEFT_SHIFT, T_LESS_EQUAL,
  262. T_LESS);
  263. return;
  264. case '>':
  265. tAddToken4('>', T_RIGHT_SHIFT_SET, T_RIGHT_SHIFT, T_GREATER_EQUAL,
  266. T_GREATER);
  267. return;
  268. case '=': tAddToken2(T_EQUAL, T_SET); return;
  269. case '!': tAddToken2(T_NOT_EQUAL, T_NOT); return;
  270. case '&': tAddToken3('&', T_AND, T_BIT_AND_SET, T_BIT_AND); return;
  271. case '|': tAddToken3('|', T_OR, T_BIT_OR_SET, T_BIT_OR); return;
  272. case '~': tAddToken(T_BIT_NOT); return;
  273. case '^': tAddToken2(T_BIT_XOR_SET, T_BIT_XOR); return;
  274. case ',': tAddToken(T_COMMA); return;
  275. case ';': tAddToken(T_SEMICOLON); return;
  276. case '(': tAddToken(T_OPEN_BRACKET); return;
  277. case ')': tAddToken(T_CLOSE_BRACKET); return;
  278. case '{': tAddToken(T_OPEN_CURVED_BRACKET); return;
  279. case '}': tAddToken(T_CLOSE_CURVED_BRACKET); return;
  280. case '"': tAddString(); return;
  281. case '\'': tAddUnicode(); return;
  282. case '.': tAddToken(T_POINT); return;
  283. case '[': tAddToken(T_OPEN_SQUARE_BRACKET); return;
  284. case ']': tAddToken(T_CLOSE_SQUARE_BRACKET); return;
  285. }
  286. tError("unknown character on line %d: %c", line, c);
  287. }
  288. static void tParseFile() {
  289. readIndex = 0;
  290. writeIndex = 0;
  291. line = 1;
  292. while(true) {
  293. int c = frRead(&fileReader);
  294. if(c == EOF) {
  295. return;
  296. }
  297. tParseToken(c);
  298. }
  299. }
  300. void tTokenize(const char* path, Error* e) {
  301. error = e;
  302. frInit(path, &fileReader, e);
  303. if(!eHasError(e) && !setjmp(errorJump)) {
  304. tParseFile();
  305. }
  306. frDelete(&fileReader);
  307. }
  308. Token tPeekToken() {
  309. if(readIndex >= writeIndex) {
  310. return T_END;
  311. }
  312. return tokenBuffer[readIndex];
  313. }
  314. bool tReadTokenAndLine(Token* t, int16* line) {
  315. if(readIndex >= writeIndex) {
  316. return true;
  317. }
  318. *t = tokenBuffer[readIndex++];
  319. return tReadTokens(line, sizeof(int16));
  320. }
  321. bool tReadInt32(int32* i) {
  322. return tReadTokens(i, sizeof(int32));
  323. }
  324. bool tReadFloat(float* f) {
  325. return tReadTokens(f, sizeof(float));
  326. }
  327. const char* tReadString() {
  328. const char* s = tokenBuffer + readIndex;
  329. while(readIndex <= writeIndex) {
  330. if(tokenBuffer[readIndex++] == '\0') {
  331. return s;
  332. }
  333. }
  334. return NULL;
  335. }
  336. int tGetMarker() {
  337. return readIndex;
  338. }
  339. void tReset(int marker) {
  340. readIndex = marker;
  341. }