Tokenizer.c 7.9 KB

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