Tokenizer.c 7.4 KB

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