|
1 | | -// package com.company; |
2 | 1 | import java.util.List; |
3 | 2 | import java.util.Optional; |
4 | 3 | import java.util.Collections; |
|
8 | 7 |
|
9 | 8 | public class LexicalAnalyser { |
10 | 9 |
|
11 | | - public static List<Token> analyse(String sourceCode) throws LexicalException { |
12 | | - // Turn the input String into a list of Tokens! |
13 | | - String[] splitList = sourceCode.split(" "); |
| 10 | + public static List<Token> analyse(String sourceCode) throws LexicalException { |
| 11 | + // Turn the input String into a list of Tokens! |
| 12 | + String[] splitList = sourceCode.split(" "); |
14 | 13 | List<String> tokenCase = Arrays.asList(new String[]{ |
15 | | - "\"", "\'", "(", ")", "{", "}", |
16 | | - ";", "+", "-", "*", "/", "%" |
| 14 | + "\"", "'", "(", ")", "{", "}", |
| 15 | + ";", "+", "-", "*", "/", "%" |
17 | 16 | }); |
18 | | - List<String> furtherSplitList = new ArrayList<String>(); |
19 | | - List<Token> tokenList = new ArrayList<Token>(); |
| 17 | + List<String> furtherSplitList = new ArrayList<String>(); |
| 18 | + List<Token> tokenList = new ArrayList<Token>(); |
20 | 19 |
|
21 | | - for (String word : splitList) { |
22 | | - String s = ""; |
23 | | - for (int i = 0; i < word.length(); i++) { |
24 | | - // System.out.println(word.charAt(i)); |
25 | | - String c = Character.toString(word.charAt(i)); |
| 20 | + for (String word : splitList) { |
| 21 | + String s = ""; |
| 22 | + for (int i = 0; i < word.length(); i++) { |
| 23 | + // System.out.println(word.charAt(i)); |
| 24 | + String c = Character.toString(word.charAt(i)); |
26 | 25 | if (tokenCase.contains(c)) { |
27 | 26 | furtherSplitList.add(s); |
28 | 27 | furtherSplitList.add(c); |
29 | 28 | s = ""; |
30 | 29 | } else { |
31 | 30 | s += c; |
32 | 31 | } |
33 | | - } |
34 | | - furtherSplitList.add(s); |
35 | | - } |
36 | | - // System.out.println(furtherSplitList); |
| 32 | + } |
| 33 | + furtherSplitList.add(s); |
| 34 | + } |
| 35 | + // System.out.println(furtherSplitList); |
37 | 36 | for (int i = 0; i < furtherSplitList.size(); i++) { |
38 | 37 | String s = furtherSplitList.get(i); |
39 | | - if (s.length() > 0) { |
40 | | - try { |
41 | | - if (i > 0 && furtherSplitList.get(i-1).matches("\"")) { |
| 38 | + if (s.length() > 0) { |
| 39 | + try { |
| 40 | + if (i > 0 && furtherSplitList.get(i - 1).matches("\"")) { |
42 | 41 | tokenList.add(tokenTypeStringLit(s).get()); |
43 | | - } else if (i > 0 && furtherSplitList.get(i-1).matches("\'")) { |
| 42 | + } else if (i > 0 && furtherSplitList.get(i - 1).matches("'")) { |
44 | 43 | tokenList.add(tokenTypeCharLit(s).get()); |
45 | | - } else if (i < furtherSplitList.size()-1 && furtherSplitList.get(i).matches(".[=]$")) { |
| 44 | + } else if (i < furtherSplitList.size() - 1 && furtherSplitList.get(i).matches(".[=]$")) { |
46 | 45 | // System.out.println(furtherSplitList.get(i)); |
47 | | - if (furtherSplitList.get(i).matches("^[>].*")) { |
| 46 | + if (furtherSplitList.get(i).matches("^[>].*")) { |
48 | 47 | tokenList.add(tokenTypeGE(s).get()); |
49 | | - } else if (furtherSplitList.get(i).matches("^[<].*")) { |
| 48 | + } else if (furtherSplitList.get(i).matches("^[<].*")) { |
50 | 49 | tokenList.add(tokenTypeLE(s).get()); |
51 | | - } else if (furtherSplitList.get(i).matches("^[=].*")) { |
52 | | - tokenList.add(tokenTypeEqual(s).get()); |
53 | 50 | } else if (furtherSplitList.get(i).matches("^[!].*")) { |
54 | | - tokenList.add(tokenTypeNotEqual(s).get()); |
55 | | - } else { |
| 51 | + tokenList.add(tokenTypeNE(s).get()); |
| 52 | + } else if (furtherSplitList.get(i).matches("^[=].*")) { |
56 | 53 | tokenList.add(tokenTypeEqual(s).get()); |
57 | | - } |
58 | | - } |
59 | | - else { tokenList.add(tokenFromString(s).get()); } |
60 | | - } |
61 | | - catch (NoSuchElementException e) { |
62 | | - // tokenList.add(Optional.empty()); |
| 54 | + } else { tokenList.add(tokenTypeEqual(s).get()); } |
| 55 | + } else {tokenList.add(tokenFromString(s).get()); } |
| 56 | + } catch (NoSuchElementException e) { |
| 57 | + // tokenList.add(Optional.empty()); |
63 | 58 | System.out.print("Token not found: " + e + "\n"); |
64 | | - } |
65 | | - catch (Exception e) { |
| 59 | + } catch (Exception e) { |
66 | 60 | System.out.print(e); |
67 | 61 | } |
68 | | - } |
69 | | - } |
70 | | - return tokenList; |
71 | | - } |
| 62 | + } |
| 63 | + } |
| 64 | + return tokenList; |
| 65 | + } |
72 | 66 |
|
73 | 67 | private static Optional<Token> tokenFromString(String t) { |
74 | 68 | Optional<Token.TokenType> type = tokenTypeOf(t); |
@@ -111,8 +105,8 @@ private static Optional<Token> tokenTypeEqual(String t) { |
111 | 105 | return Optional.of(new Token(type.get(), t)); |
112 | 106 | return Optional.empty(); |
113 | 107 | } |
114 | | - |
115 | | - private static Optional<Token> tokenTypeNotEqual(String t) { |
| 108 | + |
| 109 | + private static Optional<Token> tokenTypeNE(String t) { |
116 | 110 | Optional<Token.TokenType> type = Optional.of(Token.TokenType.NEQUAL); |
117 | 111 | if (type.isPresent()) |
118 | 112 | return Optional.of(new Token(type.get(), t)); |
@@ -185,22 +179,17 @@ private static Optional<Token.TokenType> tokenTypeOf(String t) { |
185 | 179 | return Optional.of(Token.TokenType.LT); |
186 | 180 | case ">": |
187 | 181 | return Optional.of(Token.TokenType.GT); |
188 | | - // case "<=": |
189 | | - // return Optional.of(Token.TokenType.LE); |
190 | | - // case ">=": |
191 | | - // return Optional.of(Token.TokenType.GE); |
| 182 | + case "\"": |
| 183 | + return Optional.of(Token.TokenType.DQUOTE); |
| 184 | + case "'": |
| 185 | + return Optional.of(Token.TokenType.SQUOTE); |
192 | 186 | } |
193 | 187 |
|
194 | | - if (t.matches("\"{1}")) |
195 | | - return Optional.of(Token.TokenType.DQUOTE); |
196 | | - if (t.matches("\'{1}")) |
197 | | - return Optional.of(Token.TokenType.SQUOTE); |
198 | 188 | if (t.matches("\\d+")) |
199 | 189 | return Optional.of(Token.TokenType.NUM); |
200 | 190 | if (Character.isAlphabetic(t.charAt(0)) && t.matches("[\\d|\\w]+")) { |
201 | 191 | return Optional.of(Token.TokenType.ID); |
202 | 192 | } |
203 | | - |
204 | 193 | return Optional.empty(); |
205 | 194 | } |
206 | 195 |
|
|
0 commit comments