Skip to content

Commit b7c3cf4

Browse files
committed
completed lexical analyser token return function
1 parent 71dd7c6 commit b7c3cf4

File tree

1 file changed

+64
-13
lines changed

1 file changed

+64
-13
lines changed

Assignment2/src/com/company/LexicalAnalyser.java

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,73 @@
77

88
public class LexicalAnalyser {
99

10-
public static List<Token> analyse(String sourceCode) throws LexicalException {
11-
//Turn the input String into a list of Tokens!
12-
ArrayList<Token> ARList = new ArrayList<Token>();
13-
String[] inputString = 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("\\s+");
13+
List<String> furtherSplitList = new ArrayList<String>();
14+
List<Token> tokenList = new ArrayList<Token>();
1415

15-
for(String input : inputString){
16-
//Optional<Token> inputToken = tokenFromString(input);
17-
//if (inputToken != Optional.empty()){
18-
// Token validToken = Token(input);
19-
// ARList.add(Token);
20-
//}
21-
addToken = new Token();
16+
for (String word : splitList) {
17+
String s = "";
18+
for (int i = 0; i < word.length(); i++) {
19+
// System.out.println(word.charAt(i));
20+
String c = Character.toString(word.charAt(i));
21+
switch (c) {
22+
case "(":
23+
case ")":
24+
case "{":
25+
case "}":
26+
case ";":
27+
// System.out.println(s);
28+
// System.out.println(c);
29+
// if (s != null || s.length() > 0 || s != "") {
30+
// System.out.println(s);
31+
// furtherSplitList.add(s);
32+
// }
33+
furtherSplitList.add(s);
34+
furtherSplitList.add(c);
35+
s = "";
36+
break;
37+
case " ":
38+
i = word.length();
39+
break;
40+
default:
41+
s += c;
42+
}
43+
// if token exists, add s to split list, then add token
44+
// clear string var
45+
}
46+
// if (s != null || s.length() > 0 || s != "") {
47+
// System.out.println(s);
48+
// furtherSplitList.add(s);
49+
// }
50+
furtherSplitList.add(s);
2251
}
2352

24-
return ARList;
25-
}
53+
System.out.println(furtherSplitList);
54+
55+
56+
for(int j = 0; j < furtherSplitList.size(); j++){
57+
58+
}
59+
60+
for (String s : furtherSplitList) {
61+
62+
if(s.length() > 0){
63+
try {
64+
tokenList.add(tokenFromString(s).get());
65+
}
66+
catch (NoSuchElementException e) {
67+
// tokenList.add(Optional.empty());
68+
}
69+
}
70+
//try {
71+
// tokenList.add(LexicalAnalyser.tokenFromString().get())
72+
//}
73+
}
74+
return tokenList;
75+
// return Collections.emptyList();
76+
}
2677

2778
private static Optional<Token> tokenFromString(String t) {
2879
Optional<Token.TokenType> type = tokenTypeOf(t);

0 commit comments

Comments
 (0)