Tokenizer.c 8.5 KB

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