-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.java
82 lines (73 loc) · 2.24 KB
/
utils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.util.Scanner; // Import the Scanner class to read text files
import java.util.ArrayList;
class InputSource {
private String source;
private int counter = -1;
public InputSource(String source , boolean isFile) {
this.source = source;
if(isFile) {if(this.source.charAt(source.length()-1) != ' ') { this.source += ' '; this.source += ' ';this.source += ' '; this.source += ' '; } }
}
public boolean isEOF() { return (counter == source.length()-1); }
public void retract() { counter -= 1; }
public char getCurrChar() { return source.charAt(counter); }
public char nextChar() { counter += 1; return source.charAt(counter); }
}
class LexSource {
private ArrayList<token> tokens;
private int counter = -1;
public LexSource(ArrayList<token> tokens) {
this.tokens = tokens;
}
public String nextToken() {
counter += 1;
token t = tokens.get(counter);
switch(t.tokenName) {
case OO:
case EOF:
case AO: {
return t.lexeme;
}
}
String tokenType = t.tokenName.toString().toLowerCase();
return tokenType;
}
public String getCurrentToken() {
token t = tokens.get(counter);
switch(t.tokenName) {
case OO:
case EOF:
case AO: {
return t.lexeme;
}
}
String tokenType = t.tokenName.toString().toLowerCase();
return tokenType;
}
public String getCurrentTokenLexeme() {
token t= tokens.get(counter);
return t.lexeme;
}
}
public class utils {
public static String CharToString(char c) {
String s = "";
s += c;
return s;
}
public static String readfile(String pathToFile) {
String res = "";
try {
File obj = new File(pathToFile);
Scanner reader = new Scanner(obj);
while(reader.hasNextLine()) {
res += "\n"; //Add new line literally
res += reader.nextLine();
} reader.close();
} catch(FileNotFoundException e) {
System.out.println("Error Occured: \n");
e.printStackTrace();
} return res;
}
}