Tokenizer.c 8.6 KB

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