-
Notifications
You must be signed in to change notification settings - Fork 1
/
Splitter.java
144 lines (102 loc) · 2.91 KB
/
Splitter.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
public class Splitter { //removed +262
final int INTEGER = 257;
final int DOUBLE = 258;
final int IDENT = 260;
final int CONSTH = 261;
private Character c = ' ';
File f;
BufferedReader reader;
String[] ar;
ArrayList<String> al;
Token t;
final char END = (char)-1; //used for finding end of input
public Splitter(File file) throws Exception{
f = file;
reader = new BufferedReader(
new InputStreamReader(
new FileInputStream(f),
Charset.forName("UTF-8")));
//can also use hashmap. 0-7,8-19,21-30-34
//<cin> | <cout> | <if> | <assign> | <while>
ar = new String[] {"cin", "cout","if", "while", "assign-holder","int", "char", "else",
";", ",","{", "}", "||", "&&", "!", "|", "^", "&", "~", "<",
"==", "!=", "(", ")",
"<=", "<", ">", ">=", "=",
"+", "-", "*", "/", "%", "<<", ">>"};
al = new ArrayList<String>(Arrays.asList(ar));
}
public Token getToken() throws Exception{
String str="";
int n;
//only if ' '
while(Character.isWhitespace(c)){
int re = reader.read();
c= (char) re;
}
String ch = Character.toString(c); //(can use same string as str)
if(Character.isDigit(c)){
do{
str = str + c;
c=(char) reader.read();
}while(Character.isDigit(c)); //always holds next char*
n = Integer.parseInt(str);
t = new Token(str, INTEGER);
}
//check for decimal
// if (i<len-1 && s.substring(i, i+1).equals(".")){
// //repeat
// i++;
// while(i<len && Character.isDigit(s.charAt(i))){
// i++;
// }
// t = new Token(s.substring(h, i), DOUBLE);
// }
else if(Character.isLetter(c)){
do{
str=str+c;
c=(char) reader.read();
}while(Character.isLetterOrDigit(c));
if(al.contains(str))
t = new Token(str, al.indexOf(str));
else
t = new Token(str, IDENT);
}
//ELSE ITS NOT ABC OR INT SO IT'S A Random CHAR. CHECK WHICH ONE..
else if(al.contains(ch)){
//check for different 2 char possibilities..
c=(char) reader.read();
str=str+ch+c;
if(al.contains(str)){
t = new Token(str, al.indexOf(str));
c=(char) reader.read(); //so as to always keep next char*
}
else{
t = new Token(ch, al.indexOf(ch));
}
}
//also check for "" around strings
//check if end of input
else if(c.equals(END)){
////System.out.println("end of input");
t=new Token("null",500); //or null
}
else{
//invalid character
//System.out.println("token debug: " + ch);
t = new Token("error", 501); //501 error code
}
return t;
//continue with c next time...
}
public void printTokens() throws Exception{
Token tok = getToken();
while(tok.getType()!=500){
//System.out.println(t.getString() + " t:" + t.getType() + "|");
tok = getToken();
}
}
}