Tokenizer.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include <limits.h>
  2. #include <stdarg.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "Utils.h"
  7. #include "tokenizer/File.h"
  8. #include "tokenizer/Tokenizer.h"
  9. #define TOKEN_BUFFER_LENGTH (1024 * 1024)
  10. #define ERROR_LENGTH 256
  11. static char tokenBuffer[TOKEN_BUFFER_LENGTH];
  12. static int writeIndex = 0;
  13. static int readIndex = 0;
  14. static int16 line = 1;
  15. static char error[ERROR_LENGTH] = {'\0'};
  16. static void tError(const char* format, ...) {
  17. va_list args;
  18. va_start(args, format);
  19. vsnprintf(error, ERROR_LENGTH, format, args);
  20. va_end(args);
  21. }
  22. static bool tAdd(const void* data, int length) {
  23. if(writeIndex + length > TOKEN_BUFFER_LENGTH) {
  24. tError("the token buffer is too small");
  25. return false;
  26. }
  27. memcpy(tokenBuffer + writeIndex, data, length);
  28. writeIndex += length;
  29. return true;
  30. }
  31. static bool tAddToken(Token token) {
  32. unsigned char c = token;
  33. return tAdd(&c, 1) && tAdd(&line, sizeof(line));
  34. }
  35. static bool tReadTokens(void* dest, int length) {
  36. if(readIndex + length > writeIndex) {
  37. return false;
  38. }
  39. memcpy(dest, tokenBuffer + readIndex, length);
  40. readIndex += length;
  41. return true;
  42. }
  43. static bool tParseLiteral(int c) {
  44. int index = 1;
  45. char buffer[64];
  46. buffer[0] = c;
  47. while(isLetter(fPeek())) {
  48. if(index >= 63) {
  49. tError("literal is too long");
  50. return false;
  51. }
  52. buffer[index++] = fRead();
  53. }
  54. buffer[index] = '\0';
  55. Token t = tFromName(buffer);
  56. if(t != T_END) {
  57. return tAddToken(t);
  58. }
  59. return tAddToken(T_LITERAL) && tAdd(buffer, index + 1);
  60. }
  61. static bool tParseNumber(int c) {
  62. int index = 1;
  63. char buffer[64];
  64. buffer[0] = c;
  65. bool point = false;
  66. while(true) {
  67. int c = fPeek();
  68. if(c == '.') {
  69. point = true;
  70. } else if(!isNumber(c)) {
  71. break;
  72. } else if(index >= 63) {
  73. tError("number is too long");
  74. return false;
  75. }
  76. buffer[index++] = fRead();
  77. }
  78. buffer[index] = '\0';
  79. if(point) {
  80. char* end = NULL;
  81. float f = strtof(buffer, &end);
  82. if(end[0] != '\0') {
  83. tError("invalid float on line %d", line);
  84. return false;
  85. }
  86. return tAddToken(T_FLOAT) && tAdd(&f, sizeof(float));
  87. } else {
  88. char* end = NULL;
  89. long l = strtol(buffer, &end, 10);
  90. if(end[0] != '\0' || l > INT_MAX) {
  91. tError("invalid int on line %d", line);
  92. return false;
  93. }
  94. int i = l;
  95. return tAddToken(T_INT) && tAdd(&i, sizeof(int));
  96. }
  97. }
  98. static bool tAddString() {
  99. if(!tAddToken(T_TEXT)) {
  100. return false;
  101. }
  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:
  111. tError("unknown escaped character at line %d", line);
  112. return false;
  113. }
  114. } else if(c == EOF) {
  115. tError("unclosed string starting at line %d", line);
  116. return false;
  117. }
  118. if(!tAdd(&c, 1)) {
  119. return false;
  120. }
  121. }
  122. char c = '\0';
  123. return tAdd(&c, 1);
  124. }
  125. static bool tAddToken2(Token te, Token t) {
  126. return fReadIf('=') ? tAddToken(te) : tAddToken(t);
  127. }
  128. static bool tAddToken3(int c, Token tc, Token te, Token t) {
  129. return fReadIf(c) ? tAddToken(tc) : tAddToken2(te, t);
  130. }
  131. static bool tAddToken4(int c, Token tce, Token tc, Token te, Token t) {
  132. return fReadIf(c) ? tAddToken2(tce, tc) : tAddToken2(te, t);
  133. }
  134. static bool tParseToken() {
  135. int c = fRead();
  136. if(c == EOF) {
  137. return false;
  138. } else if(isLetter(c)) {
  139. return tParseLiteral(c);
  140. } else if(isNumber(c)) {
  141. return tParseNumber(c);
  142. }
  143. switch(c) {
  144. case ' ': return true;
  145. case '\n': line++; return true;
  146. case '+': return tAddToken3('+', T_INCREMENT, T_ADD_SET, T_ADD);
  147. case '-': return tAddToken3('-', T_DECREMENT, T_SUB_SET, T_SUB);
  148. case '*': return tAddToken2(T_MUL_SET, T_MUL);
  149. case '/': return tAddToken2(T_DIV_SET, T_DIV);
  150. case '%': return tAddToken2(T_MOD_SET, T_MOD);
  151. case '<':
  152. return tAddToken4('<', T_LEFT_SHIFT_SET, T_LEFT_SHIFT, T_LESS_EQUAL,
  153. T_LESS);
  154. case '>':
  155. return tAddToken4('>', T_RIGHT_SHIFT_SET, T_RIGHT_SHIFT,
  156. T_GREATER_EQUAL, T_GREATER);
  157. case '=': return tAddToken2(T_EQUAL, T_SET);
  158. case '!': return tAddToken2(T_NOT_EQUAL, T_NOT);
  159. case '&': return tAddToken3('&', T_AND, T_BIT_AND_SET, T_BIT_AND);
  160. case '|': return tAddToken3('|', T_OR, T_BIT_OR_SET, T_BIT_OR);
  161. case '~': return tAddToken(T_BIT_NOT);
  162. case '^': return tAddToken2(T_BIT_XOR_SET, T_BIT_XOR);
  163. case ',': return tAddToken(T_COMMA);
  164. case ';': return tAddToken(T_SEMICOLON);
  165. case '(': return tAddToken(T_OPEN_BRACKET);
  166. case ')': return tAddToken(T_CLOSE_BRACKET);
  167. case '{': return tAddToken(T_OPEN_CURVED_BRACKET);
  168. case '}': return tAddToken(T_CLOSE_CURVED_BRACKET);
  169. case '"': return tAddString();
  170. case '.': return tAddToken(T_POINT);
  171. case '[': return tAddToken(T_OPEN_SQUARE_BRACKET);
  172. case ']': return tAddToken(T_CLOSE_SQUARE_BRACKET);
  173. }
  174. tError("unknown character on line %d: %c", line, c);
  175. return false;
  176. }
  177. static void tParseFile() {
  178. readIndex = 0;
  179. writeIndex = 0;
  180. line = 1;
  181. error[0] = '\0';
  182. while(tParseToken()) {
  183. }
  184. }
  185. bool tTokenize(const char* path) {
  186. if(fOpen(path)) {
  187. tError("cannot read file '%s'", path);
  188. return true;
  189. }
  190. tParseFile();
  191. fClose();
  192. return error[0] != '\0';
  193. }
  194. const char* tGetError() {
  195. return error;
  196. }
  197. void tResetReader() {
  198. readIndex = 0;
  199. }
  200. Token tPeekToken() {
  201. if(readIndex >= writeIndex) {
  202. return T_END;
  203. }
  204. return tokenBuffer[readIndex];
  205. }
  206. Token tReadToken() {
  207. if(readIndex >= writeIndex) {
  208. return T_END;
  209. }
  210. return tokenBuffer[readIndex++];
  211. }
  212. bool tReadInt(int* i) {
  213. if(tReadTokens(i, sizeof(int))) {
  214. return true;
  215. }
  216. return false;
  217. }
  218. bool tReadInt16(int16* i) {
  219. if(tReadTokens(i, sizeof(int16))) {
  220. return true;
  221. }
  222. return false;
  223. }
  224. bool tReadFloat(float* f) {
  225. if(tReadTokens(f, sizeof(float))) {
  226. return true;
  227. }
  228. return false;
  229. }
  230. const char* tReadString(int* length) {
  231. *length = 0;
  232. const char* s = tokenBuffer + readIndex;
  233. while(readIndex <= writeIndex) {
  234. (*length)++;
  235. if(tokenBuffer[readIndex++] == '\0') {
  236. return s;
  237. }
  238. }
  239. return NULL;
  240. }