Tokenizer.c 9.7 KB

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