Tokenizer.c 8.8 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/File.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 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 int32 tUnicode(int32 c) {
  127. if(c == '\\') {
  128. switch(fRead()) {
  129. case '"': c = '"'; break;
  130. case '\\': c = '\\'; break;
  131. case 'n': c = '\n'; break;
  132. case 'r': c = '\r'; break;
  133. case 't': c = '\t'; break;
  134. default: tError("unknown escaped character at line %d", line);
  135. }
  136. }
  137. if((c & 0xE0) == 0xC0) {
  138. c = ((c & 0x1F) << 6) | (fRead() & 0x3F);
  139. } else if((c & 0xF0) == 0xE0) {
  140. c = ((c & 0xF) << 12) | ((fRead() & 0x3F) << 6);
  141. c |= fRead() & 0x3F;
  142. } else if((c & 0xF8) == 0xF0) {
  143. c = ((c & 0x7) << 18) | ((fRead() & 0x3F) << 12);
  144. c |= (fRead() & 0x3F) << 6;
  145. c |= fRead() & 0x3F;
  146. }
  147. return c;
  148. }
  149. static void tAddString() {
  150. tAddToken(T_TEXT);
  151. while(true) {
  152. int32 c = fRead();
  153. if(c == '"') {
  154. break;
  155. } else if(c == EOF) {
  156. tError("unclosed string starting at line %d", line);
  157. }
  158. c = tUnicode(c);
  159. tAdd(&c, sizeof(int32));
  160. }
  161. int32 c = 0;
  162. tAdd(&c, sizeof(int32));
  163. }
  164. static void tAddUnicode() {
  165. int32 c = fRead();
  166. c = tUnicode(c);
  167. tAddToken(T_CONST_INT32);
  168. tAdd(&c, sizeof(int32));
  169. if(fRead() != '\'') {
  170. tError("expecting unicode end");
  171. }
  172. }
  173. static void tAddToken2(Token te, Token t) {
  174. if(fReadIf('=')) {
  175. tAddToken(te);
  176. } else {
  177. tAddToken(t);
  178. }
  179. }
  180. static void tAddToken3(int c, Token tc, Token te, Token t) {
  181. if(fReadIf(c)) {
  182. tAddToken(tc);
  183. } else {
  184. tAddToken2(te, t);
  185. }
  186. }
  187. static void tAddToken4(int c, Token tce, Token tc, Token te, Token t) {
  188. if(fReadIf(c)) {
  189. tAddToken2(tce, tc);
  190. } else {
  191. tAddToken2(te, t);
  192. }
  193. }
  194. static void tAddTokenMinus() {
  195. tAddToken3('-', T_DECREMENT, T_SUB_SET, fReadIf('>') ? T_ARROW : T_SUB);
  196. }
  197. static void tLineComment() {
  198. while(true) {
  199. int c = fRead();
  200. if(c == EOF || c == '\n') {
  201. line++;
  202. return;
  203. }
  204. }
  205. }
  206. static void tMultipleLineComment() {
  207. while(true) {
  208. int c = fRead();
  209. if(c == EOF) {
  210. tError("unclosed comment at line %d", line);
  211. } else if(c == '\n') {
  212. line++;
  213. } else if(c == '*' && fReadIf('/')) {
  214. return;
  215. }
  216. }
  217. }
  218. static void tSlash() {
  219. if(fReadIf('/')) {
  220. tLineComment();
  221. } else if(fReadIf('*')) {
  222. tMultipleLineComment();
  223. } else {
  224. tAddToken2(T_DIV_SET, T_DIV);
  225. }
  226. }
  227. static void tParseToken(int c) {
  228. if(isLetter(c)) {
  229. tParseLiteral(c);
  230. return;
  231. } else if(isNumber(c)) {
  232. tParseNumber(c);
  233. return;
  234. }
  235. switch(c) {
  236. case ' ': return;
  237. case '\n': line++; return;
  238. case '+': tAddToken3('+', T_INCREMENT, T_ADD_SET, T_ADD); return;
  239. case '-': tAddTokenMinus(); return;
  240. case '*': tAddToken2(T_MUL_SET, T_MUL); return;
  241. case '/': tSlash(); return;
  242. case '%': tAddToken2(T_MOD_SET, T_MOD); return;
  243. case '<':
  244. tAddToken4('<', T_LEFT_SHIFT_SET, T_LEFT_SHIFT, T_LESS_EQUAL,
  245. T_LESS);
  246. return;
  247. case '>':
  248. tAddToken4('>', T_RIGHT_SHIFT_SET, T_RIGHT_SHIFT, T_GREATER_EQUAL,
  249. T_GREATER);
  250. return;
  251. case '=': tAddToken2(T_EQUAL, T_SET); return;
  252. case '!': tAddToken2(T_NOT_EQUAL, T_NOT); return;
  253. case '&': tAddToken3('&', T_AND, T_BIT_AND_SET, T_BIT_AND); return;
  254. case '|': tAddToken3('|', T_OR, T_BIT_OR_SET, T_BIT_OR); return;
  255. case '~': tAddToken(T_BIT_NOT); return;
  256. case '^': tAddToken2(T_BIT_XOR_SET, T_BIT_XOR); return;
  257. case ',': tAddToken(T_COMMA); return;
  258. case ';': tAddToken(T_SEMICOLON); return;
  259. case '(': tAddToken(T_OPEN_BRACKET); return;
  260. case ')': tAddToken(T_CLOSE_BRACKET); return;
  261. case '{': tAddToken(T_OPEN_CURVED_BRACKET); return;
  262. case '}': tAddToken(T_CLOSE_CURVED_BRACKET); return;
  263. case '"': tAddString(); return;
  264. case '\'': tAddUnicode(); return;
  265. case '.': tAddToken(T_POINT); return;
  266. case '[': tAddToken(T_OPEN_SQUARE_BRACKET); return;
  267. case ']': tAddToken(T_CLOSE_SQUARE_BRACKET); return;
  268. }
  269. tError("unknown character on line %d: %c", line, c);
  270. }
  271. static void tParseFile() {
  272. readIndex = 0;
  273. writeIndex = 0;
  274. line = 1;
  275. error[0] = '\0';
  276. while(true) {
  277. int c = fRead();
  278. if(c == EOF) {
  279. return;
  280. }
  281. tParseToken(c);
  282. }
  283. }
  284. bool tTokenize(const char* path) {
  285. if(!setjmp(errorJump)) {
  286. fOpen(path, tError);
  287. tParseFile();
  288. }
  289. fClose();
  290. return error[0] != '\0';
  291. }
  292. const char* tGetError() {
  293. return error;
  294. }
  295. int tGetLine() {
  296. return line;
  297. }
  298. void tResetReader() {
  299. readIndex = 0;
  300. }
  301. Token tPeekToken() {
  302. if(readIndex >= writeIndex) {
  303. return T_END;
  304. }
  305. return tokenBuffer[readIndex];
  306. }
  307. Token tReadToken() {
  308. if(readIndex >= writeIndex) {
  309. return T_END;
  310. }
  311. return tokenBuffer[readIndex++];
  312. }
  313. bool tReadInt16(int16* i) {
  314. return tReadTokens(i, sizeof(int16));
  315. }
  316. bool tReadInt32(int32* i) {
  317. return tReadTokens(i, sizeof(int32));
  318. }
  319. bool tReadInt64(int64* i) {
  320. return tReadTokens(i, sizeof(int64));
  321. }
  322. bool tReadFloat(float* f) {
  323. return tReadTokens(f, sizeof(float));
  324. }
  325. const char* tReadString() {
  326. const char* s = tokenBuffer + readIndex;
  327. while(readIndex <= writeIndex) {
  328. if(tokenBuffer[readIndex++] == '\0') {
  329. return s;
  330. }
  331. }
  332. return NULL;
  333. }
  334. int tGetMarker() {
  335. return readIndex;
  336. }
  337. void tReset(int marker) {
  338. readIndex = marker;
  339. }