Tokenizer.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. package me.hammerle.snuviscript.token;
  2. import java.util.LinkedList;
  3. import me.hammerle.snuviscript.code.Instruction;
  4. import me.hammerle.snuviscript.exceptions.PreScriptException;
  5. public class Tokenizer
  6. {
  7. private final char[] code;
  8. private int line;
  9. private final LinkedList<Token> data;
  10. public Tokenizer(String code)
  11. {
  12. this.code = code.toCharArray();
  13. this.data = new LinkedList<>();
  14. }
  15. private void addToken(TokenType t)
  16. {
  17. data.add(new Token(t, line + 1));
  18. }
  19. private void addToken(TokenType t, Object o)
  20. {
  21. data.add(new Token(t, line + 1, o));
  22. }
  23. public void tokenize()
  24. {
  25. line = 0;
  26. for(int index = 0; index < code.length; index++)
  27. {
  28. if(Character.isLetter(code[index]))
  29. {
  30. int old = index;
  31. index++;
  32. while(index < code.length && (Character.isLetterOrDigit(code[index]) || code[index] == '.' || code[index] == '_'))
  33. {
  34. index++;
  35. }
  36. String s = new String(code, old, index - old);
  37. switch(s)
  38. {
  39. case "if": addToken(TokenType.IF); break;
  40. case "elseif": addToken(TokenType.ELSE_IF); break;
  41. case "else": addToken(TokenType.ELSE); break;
  42. case "for": addToken(TokenType.FOR); break;
  43. case "while": addToken(TokenType.WHILE); break;
  44. case "function": addToken(TokenType.FUNCTION); break;
  45. case "break": addToken(TokenType.BREAK); break;
  46. case "continue": addToken(TokenType.CONTINUE); break;
  47. case "return": addToken(TokenType.RETURN); break;
  48. case "try": addToken(TokenType.TRY); break;
  49. case "catch": addToken(TokenType.CATCH); break;
  50. default:
  51. addToken(TokenType.VAR, s);
  52. }
  53. index--;
  54. }
  55. else if(Character.isDigit(code[index]))
  56. {
  57. int old = index;
  58. index++;
  59. while(index < code.length)
  60. {
  61. switch(code[index])
  62. {
  63. case '0':
  64. case '1':
  65. case '2':
  66. case '3':
  67. case '4':
  68. case '5':
  69. case '6':
  70. case '7':
  71. case '8':
  72. case '9':
  73. {
  74. index++;
  75. continue;
  76. }
  77. case '.':
  78. {
  79. index++;
  80. while(index < code.length && Character.isDigit(code[index]))
  81. {
  82. index++;
  83. }
  84. break;
  85. }
  86. }
  87. break;
  88. }
  89. addToken(TokenType.DOUBLE, Double.parseDouble(new String(code, old, index - old)));
  90. index--;
  91. }
  92. else
  93. {
  94. int startLine = line;
  95. try
  96. {
  97. switch(code[index])
  98. {
  99. case '\n':
  100. {
  101. line++;
  102. break;
  103. }
  104. case '@':
  105. {
  106. int old = index;
  107. index++;
  108. while(index < code.length && (Character.isLetterOrDigit(code[index]) || code[index] == '.' || code[index] == '_'))
  109. {
  110. index++;
  111. }
  112. addToken(TokenType.LABEL, new String(code, old, index - old));
  113. index--;
  114. break;
  115. }
  116. case '"':
  117. {
  118. int old = index + 1;
  119. index++;
  120. while(index < code.length && code[index] != '"')
  121. {
  122. index++;
  123. }
  124. addToken(TokenType.TEXT, new String(code, old, index - old));
  125. break;
  126. }
  127. case '+':
  128. {
  129. switch(code[index + 1])
  130. {
  131. case '+':
  132. addToken(TokenType.INC);
  133. index++;
  134. break;
  135. case '=':
  136. addToken(TokenType.ADD_SET);
  137. index++;
  138. break;
  139. default:
  140. addToken(TokenType.ADD);
  141. }
  142. break;
  143. }
  144. case '-':
  145. {
  146. switch(code[index + 1])
  147. {
  148. case '-':
  149. addToken(TokenType.DEC);
  150. index++;
  151. break;
  152. case '=':
  153. addToken(TokenType.SUB_SET);
  154. index++;
  155. break;
  156. default:
  157. addToken(TokenType.SUB);
  158. }
  159. break;
  160. }
  161. case '*':
  162. {
  163. if(code[index + 1] == '=')
  164. {
  165. addToken(TokenType.MUL_SET);
  166. index++;
  167. }
  168. else
  169. {
  170. addToken(TokenType.MUL);
  171. }
  172. break;
  173. }
  174. case '/':
  175. {
  176. switch(code[index + 1])
  177. {
  178. case '/':
  179. index += 2;
  180. while(code[index] != '\n')
  181. {
  182. index++;
  183. }
  184. index--;
  185. break;
  186. case '*':
  187. index += 2;
  188. while(code[index] != '*' || code[index + 1] != '/')
  189. {
  190. if(code[index] == '\n')
  191. {
  192. line++;
  193. }
  194. index++;
  195. }
  196. index++;
  197. break;
  198. case '=':
  199. addToken(TokenType.DIV_SET);
  200. index++;
  201. break;
  202. default:
  203. addToken(TokenType.DIV);
  204. }
  205. break;
  206. }
  207. case '!':
  208. {
  209. if(code[index + 1] == '=')
  210. {
  211. addToken(TokenType.NOT_EQUAL);
  212. index++;
  213. break;
  214. }
  215. else
  216. {
  217. addToken(TokenType.INVERT);
  218. }
  219. break;
  220. }
  221. case '~':
  222. {
  223. addToken(TokenType.BIT_INVERT);
  224. break;
  225. }
  226. case '%':
  227. {
  228. if(code[index + 1] == '=')
  229. {
  230. addToken(TokenType.MOD_SET);
  231. index++;
  232. }
  233. else
  234. {
  235. addToken(TokenType.MOD);
  236. }
  237. break;
  238. }
  239. case '<':
  240. {
  241. switch(code[index + 1])
  242. {
  243. case '<':
  244. if(code[index + 2] == '=')
  245. {
  246. addToken(TokenType.LEFT_SHIFT_SET);
  247. index += 2;
  248. }
  249. else
  250. {
  251. addToken(TokenType.LEFT_SHIFT);
  252. index++;
  253. }
  254. break;
  255. case '=':
  256. addToken(TokenType.LESS_EQUAL);
  257. index++;
  258. break;
  259. default:
  260. addToken(TokenType.LESS);
  261. }
  262. break;
  263. }
  264. case '>':
  265. {
  266. switch(code[index + 1])
  267. {
  268. case '>':
  269. if(code[index + 2] == '=')
  270. {
  271. addToken(TokenType.RIGHT_SHIFT_SET);
  272. index += 2;
  273. }
  274. else
  275. {
  276. addToken(TokenType.RIGHT_SHIFT);
  277. index++;
  278. }
  279. break;
  280. case '=':
  281. addToken(TokenType.GREATER_EQUAL);
  282. index++;
  283. break;
  284. default:
  285. addToken(TokenType.GREATER);
  286. }
  287. break;
  288. }
  289. case '=':
  290. {
  291. if(code[index + 1] == '=')
  292. {
  293. addToken(TokenType.EQUAL);
  294. index++;
  295. break;
  296. }
  297. else
  298. {
  299. addToken(TokenType.SET);
  300. }
  301. break;
  302. }
  303. case '&':
  304. {
  305. switch(code[index + 1])
  306. {
  307. case '&':
  308. addToken(TokenType.AND);
  309. index++;
  310. break;
  311. case '=':
  312. addToken(TokenType.BIT_AND_SET);
  313. index++;
  314. break;
  315. default:
  316. addToken(TokenType.BIT_AND);
  317. }
  318. break;
  319. }
  320. case '^':
  321. {
  322. if(code[index + 1] == '=')
  323. {
  324. addToken(TokenType.BIT_XOR_SET);
  325. index++;
  326. break;
  327. }
  328. else
  329. {
  330. addToken(TokenType.BIT_XOR);
  331. }
  332. break;
  333. }
  334. case '|':
  335. {
  336. switch(code[index + 1])
  337. {
  338. case '|':
  339. addToken(TokenType.OR);
  340. index++;
  341. break;
  342. case '=':
  343. addToken(TokenType.BIT_OR_SET);
  344. index++;
  345. break;
  346. default:
  347. addToken(TokenType.BIT_OR);
  348. }
  349. break;
  350. }
  351. case ',':
  352. addToken(TokenType.COMMA);
  353. break;
  354. case '(':
  355. addToken(TokenType.OPEN_BRACKET);
  356. break;
  357. case ')':
  358. addToken(TokenType.CLOSE_BRACKET);
  359. break;
  360. case '[':
  361. addToken(TokenType.OPEN_SQUARE_BRACKET);
  362. break;
  363. case ']':
  364. addToken(TokenType.CLOSE_SQUARE_BRACKET);
  365. break;
  366. case '{':
  367. addToken(TokenType.OPEN_CURVED_BRACKET);
  368. break;
  369. case '}':
  370. addToken(TokenType.CLOSE_CURVED_BRACKET);
  371. break;
  372. case ';':
  373. addToken(TokenType.SEMICOLON);
  374. break;
  375. }
  376. }
  377. catch(ArrayIndexOutOfBoundsException ex)
  378. {
  379. throw new PreScriptException("unexpected code end", startLine, line);
  380. }
  381. }
  382. }
  383. addToken(TokenType.END_OF_FILE);
  384. //data.forEach(e -> System.out.println(e));
  385. Parser p = new Parser(data);
  386. for(Instruction in : p.parseTokens())
  387. {
  388. System.out.println(in);
  389. }
  390. }
  391. }