import java.util.ArrayList;
import java.util.List;
public class Word {
private String content;
private List result;
private int offset;
private int seek;
private int size;
private String token;
private int state = 0;
private StringBuilder builder = new StringBuilder();
public Word(String content) {
this.content = content;
result = null;
offset = seek = 0;
if(content != null) {
size = content.length();
}
}
public List scan() {
result = new ArrayList();
char c;
size = content.length();
for(int i=0;i<size;i++) {
c = content.charAt(i);
offset = seek = i;
switch(getToken(c)) {
case -1:
return null;
//break;
case 0:
break;
case 1:
System.out.println(token);
result.add(token);
break;
}
}
if(builder.length() > 0) {
this.result.add(builder.toString());
builder.delete(0, builder.length());
}
return result;
}
public char prev() {
seek--;
return content.charAt(seek);
}
public char next() {
seek++;
return content.charAt(seek);
}
public int getToken(char c) {
int result = -1;
switch(state) {
case 0:
switch(c) {
case ' ':
case '/r':
case '/n':
case '/t':
result = 0;
break;
case '(':
case ')':
case ';':
state = 0;
result = 1;
token = String.valueOf(c);
break;
case '=':
builder.append(c);
state = 2;
result = 0;
break;
case '+':
case '*':
case '/':
case '-':
builder.append(c);
token = builder.toString();
builder.delete(0, builder.length());
state = 0;
result = 1;
break;
case '/"':
builder.append(c);
state = 3;
result = 0;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
builder.append(c);
state = 1;
result = 0;
break;
}
break;
case 1:
switch(c) {
case ' ':
case '/r':
case '/n':
case '/t':
state = 0;
result = 1;
token = builder.toString();
builder.delete(0, builder.length());
break;
case ')':
state = 0;
result = 1;
token = builder.toString();
this.result.add(token);
builder.delete(0, builder.length());
token = String.valueOf(c);
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
//state = 1;
builder.append(c);
result = 0;
break;
default:
state = 0;
result = 1;
token = builder.toString();
builder.delete(0, builder.length());
builder.append(c);
}
break;
case 2:
switch(c) {
case ' ':
case '/r':
case '/n':
case '/t':
state = 0;
result = 1;
token = builder.toString();
builder.delete(0, builder.length());
break;
case '=':
result = 0;
builder.append(c);
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
//break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
state = 0;
result = 1;
token = builder.toString();
builder.delete(0, builder.length());
builder.append(c);
break;
}
break;
case 3:
switch(c) {
case '/"':
state = 0;
result = 1;
builder.append(c);
token = builder.toString();
builder.delete(0, builder.length());
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
//break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
result = 0;
builder.append(c);
break;
default:
result = 0;
builder.append(c);
}
break;
}
return result;
}
}