/projects/netbeans-7.3/cnd.modelimpl/test/whitebox/elsa-result-analyser/src/ElsaResultAnalyser/Lexer.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 482 lines · 323 code · 38 blank · 121 comment · 128 complexity · 04ed08b0761041eaf19a970292d22153 MD5 · raw file

  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package ElsaResultAnalyser;
  45. import java.io.BufferedReader;
  46. import java.io.IOException;
  47. import java.util.logging.Level;
  48. import java.util.logging.Logger;
  49. /**
  50. *
  51. * @author nk220367
  52. */
  53. public class Lexer {
  54. BufferedReader input;
  55. Token currentToken;
  56. public Lexer(BufferedReader s) {
  57. input = s;
  58. }
  59. int indent = 0;
  60. public Token getNextToken() {
  61. Token token = new Token();
  62. char c;
  63. c = getSymbol();
  64. boolean calcIndent = false;
  65. while (Character.isSpaceChar(c) || (c == '\n')) {
  66. if (calcIndent) {
  67. indent++;
  68. }
  69. if (c == '\n') {
  70. line++;
  71. row = 0;
  72. calcIndent = true;
  73. indent = 0;
  74. }
  75. c = getSymbol();
  76. }
  77. /* if (c == '/') {
  78. c = GetSymbol();
  79. if (c == '/') {
  80. while (c != '\n' && c != 0) {
  81. c = GetSymbol();
  82. if (c == '\n') {
  83. line++;
  84. }
  85. }
  86. return GetNextToken();
  87. } else {
  88. PutBackSymbol(c);
  89. c = '/';
  90. }
  91. }
  92. */
  93. /* if (c == '{') {
  94. int bn = 1;
  95. while (bn != 0 && c != 0) {
  96. c = GetSymbol();
  97. if (c == '{') {
  98. bn++;
  99. }
  100. if (c == '}') {
  101. bn--;
  102. }
  103. if (c == '\n') {
  104. line++;
  105. }
  106. }
  107. return GetNextToken();
  108. }
  109. */
  110. /* if (c == '[') {
  111. while (c != ']' && c != 0) {
  112. c = GetSymbol();
  113. if (c == '\n') {
  114. line++;
  115. }
  116. }
  117. return GetNextToken();
  118. }
  119. */
  120. /*if (c == '/') {
  121. c = GetSymbol();
  122. if (c == '*') {
  123. while (true) {
  124. c = GetSymbol();
  125. if (c == 0) {
  126. break;
  127. }
  128. if (c == '*') {
  129. c = GetSymbol();
  130. if (c == '/') {
  131. return GetNextToken();
  132. }
  133. }
  134. if (c == '\n') {
  135. line++;
  136. }
  137. }
  138. } else {
  139. PutBackSymbol(c);
  140. c = '/';
  141. }
  142. }*/
  143. token.row = row - 1;
  144. token.line = line;
  145. if (Character.isLetter(c) || (c == '_')) {
  146. token.name += c;
  147. c = getSymbol();
  148. while (Character.isLetter(c) || (c == '_') || Character.isDigit(c)) {
  149. token.name += c;
  150. c = getSymbol();
  151. }
  152. token.type = Token.TT.TOKEN_ID;
  153. } else if (Character.isDigit(c)) {
  154. token.name += c;
  155. c = getSymbol();
  156. while (Character.isDigit(c)) {
  157. token.name += c;
  158. c = getSymbol();
  159. }
  160. token.type = Token.TT.TOKEN_DIGIT;
  161. } else if (c == ':') {
  162. token.name += c;
  163. c = getSymbol();
  164. if (c == ':') {
  165. token.name += c;
  166. c = getSymbol();
  167. }
  168. } else if (c == '(') {
  169. token.name += c;
  170. c = getSymbol();
  171. if (c == ')') {
  172. token.name += c;
  173. c = getSymbol();
  174. }
  175. } else if (c == '[') {
  176. token.name += c;
  177. c = getSymbol();
  178. if (c == ']') {
  179. token.name += c;
  180. c = getSymbol();
  181. }
  182. } else if (c == '+') {
  183. token.name += c;
  184. c = getSymbol();
  185. if (c == '+') {
  186. token.name += c;
  187. c = getSymbol();
  188. }
  189. if (c == '=') {
  190. token.name += c;
  191. c = getSymbol();
  192. }
  193. } else if (c == '-') {
  194. token.name += c;
  195. c = getSymbol();
  196. if (c == '-') {
  197. token.name += c;
  198. c = getSymbol();
  199. }
  200. if (c == '=') {
  201. token.name += c;
  202. c = getSymbol();
  203. }
  204. if (c == '>') {
  205. token.name += c;
  206. c = getSymbol();
  207. if (c == '*') {
  208. token.name += c;
  209. c = getSymbol();
  210. }
  211. }
  212. } else if (c == '=') {
  213. token.name += c;
  214. c = getSymbol();
  215. if (c == '=') {
  216. token.name += c;
  217. c = getSymbol();
  218. }
  219. } else if (c == '.') {
  220. token.name += c;
  221. c = getSymbol();
  222. if (c == '*') {
  223. token.name += c;
  224. c = getSymbol();
  225. }
  226. } else if (c == '|') {
  227. token.name += c;
  228. c = getSymbol();
  229. if (c == '|') {
  230. token.name += c;
  231. c = getSymbol();
  232. }
  233. if (c == '=') {
  234. token.name += c;
  235. c = getSymbol();
  236. }
  237. } else if (c == '&') {
  238. token.name += c;
  239. c = getSymbol();
  240. if (c == '&') {
  241. token.name += c;
  242. c = getSymbol();
  243. }
  244. if (c == '=') {
  245. token.name += c;
  246. c = getSymbol();
  247. }
  248. } else if (c == '>') {
  249. token.name += c;
  250. c = getSymbol();
  251. if (c == '>') {
  252. token.name += c;
  253. c = getSymbol();
  254. if (c == '=') {
  255. token.name += c;
  256. c = getSymbol();
  257. }
  258. }
  259. if (c == '=') {
  260. token.name += c;
  261. c = getSymbol();
  262. }
  263. } else if (c == '<') {
  264. token.name += c;
  265. c = getSymbol();
  266. if (c == '<') {
  267. token.name += c;
  268. c = getSymbol();
  269. if (c == '=') {
  270. token.name += c;
  271. c = getSymbol();
  272. }
  273. }
  274. if (c == '=') {
  275. token.name += c;
  276. c = getSymbol();
  277. }
  278. } else if (c == '!') {
  279. token.name += c;
  280. c = getSymbol();
  281. if (c == '=') {
  282. token.name += c;
  283. c = getSymbol();
  284. }
  285. } else if (c == '?') {
  286. token.name += c;
  287. c = getSymbol();
  288. if (c == ':') {
  289. token.name += c;
  290. c = getSymbol();
  291. }
  292. } else if (c == '*') {
  293. token.name += c;
  294. c = getSymbol();
  295. if (c == '=') {
  296. token.name += c;
  297. c = getSymbol();
  298. }
  299. } else if (c == '/') {
  300. token.name += c;
  301. c = getSymbol();
  302. if (c == '=') {
  303. token.name += c;
  304. c = getSymbol();
  305. }
  306. } else if (c == '%') {
  307. token.name += c;
  308. c = getSymbol();
  309. if (c == '=') {
  310. token.name += c;
  311. c = getSymbol();
  312. }
  313. } else if (c == '^') {
  314. token.name += c;
  315. c = getSymbol();
  316. if (c == '=') {
  317. token.name += c;
  318. c = getSymbol();
  319. }
  320. } else if ((c == '{') ||
  321. (c == '}') ||
  322. (c == ',') ||
  323. (c == '~') ||
  324. (c == '"') ||
  325. (c == ')') ||
  326. (c == ']') ||
  327. (c == '\'') ||
  328. (c == '\\') ||
  329. (c == ';')) {
  330. token.name += c;
  331. c = getSymbol();
  332. } else if (c == 0) {
  333. } else {
  334. System.out.println("Line " + line + " : " + " bad symbol: " + c);
  335. c = getSymbol();
  336. }
  337. putBackSymbol(c);
  338. currentToken = token;
  339. return currentToken;
  340. }
  341. public boolean isEndOfLine() {
  342. char c = getSymbol();
  343. while (Character.isSpaceChar(c) || (c == '\n')) {
  344. if (c == '\n') {
  345. putBackSymbol(c);
  346. return true;
  347. }
  348. c = getSymbol();
  349. }
  350. putBackSymbol(c);
  351. return false;
  352. }
  353. public Token getEndOfString() {
  354. Token token = new Token();
  355. char c = getSymbol();
  356. while (Character.isSpaceChar(c) || (c == '\n')) {
  357. if (c == '\n') {
  358. break;
  359. }
  360. c = getSymbol();
  361. }
  362. while (c != '\n' && c != 0) {
  363. token.name += c;
  364. c = getSymbol();
  365. }
  366. putBackSymbol(c);
  367. return token;
  368. }
  369. public Token getCurrentToken() {
  370. return currentToken;
  371. }
  372. public int getIndent() {
  373. return indent;
  374. }
  375. int line;
  376. int row;
  377. char getSymbol() {
  378. row++;
  379. if (isPutBack) {
  380. isPutBack = false;
  381. return putBack;
  382. }
  383. try {
  384. return (char) input.read();
  385. } catch (IOException ex) {
  386. Logger.getLogger(Lexer.class.getName()).log(Level.SEVERE, null, ex);
  387. return 0;
  388. }
  389. }
  390. boolean isPutBack = false;
  391. char putBack;
  392. void putBackSymbol(char c) {
  393. row--;
  394. if (isPutBack) {
  395. System.out.println("Double putBack!");
  396. }
  397. isPutBack = true;
  398. putBack = c;
  399. }
  400. void seekTo(int line, int colomn) {
  401. this.line = line;
  402. this.row = colomn;
  403. for (int i = 0; i < line - 1; i++) {
  404. try {
  405. input.readLine();
  406. } catch (IOException ex) {
  407. Logger.getLogger(Lexer.class.getName()).log(Level.SEVERE, null, ex);
  408. }
  409. }
  410. for (int i = 0; i < colomn - 1; i++) {
  411. try {
  412. input.read();
  413. } catch (IOException ex) {
  414. Logger.getLogger(Lexer.class.getName()).log(Level.SEVERE, null, ex);
  415. }
  416. }
  417. }
  418. }