if (analyse) {
if (peek == ' ' || peek == '\t') {
continue;
}
if (peek == '\n') {
throw new CompileExpressionErrorException("Aviator doesn't support newline expression,index="
+ iterator.getIndex());
}
else {
break;
}
}
else {
char ch = peek;
int index = this.iterator.getIndex();
nextChar();
return new CharToken(ch, index);
}
}
// If it is a digit
if (Character.isDigit(peek) || peek == '.') {
StringBuffer sb = new StringBuffer();
int startIndex = iterator.getIndex();
Number value = 0L;
boolean hasDot = false;
double d = 10.0;
do {
sb.append(peek);
if (peek == '.') {
if (hasDot) {
throw new CompileExpressionErrorException("Illegal Number, index=" + iterator.getIndex());
}
else {
hasDot = true;
value = new Double(value.longValue());
nextChar();
}
}
else {
if (!hasDot) {
value = 10 * value.longValue() + Character.digit(peek, 10);
nextChar();
}
else {
value = value.doubleValue() + Character.digit(peek, 10) / d;
d = d * 10;
nextChar();
}
}
} while (Character.isDigit(peek) || peek == '.');
return new NumberToken(value, sb.toString(), startIndex);
}
// It is a variable
if (Character.isJavaIdentifierStart(peek)) {
int startIndex = iterator.getIndex();
StringBuilder sb = new StringBuilder();
do {
sb.append(peek);
nextChar();
} while (Character.isJavaIdentifierPart(peek) || peek == '.');
String lexeme = sb.toString();
Variable variable = new Variable(lexeme, startIndex);
// If it is a reserved word(true or false)
if (symbolTable.contains(lexeme)) {
return symbolTable.getVariable(lexeme);
}
else {
symbolTable.reserve(lexeme, variable);
return variable;
}
}
if (isBinaryOP(peek)) {
CharToken opToken = new CharToken(peek, iterator.getIndex());
nextChar();
return opToken;
}
// String
if (peek == '"' || peek == '\'') {
char left = peek;
int startIndex = iterator.getIndex();
StringBuilder sb = new StringBuilder();
while ((peek = iterator.next()) != left) {
if (peek == CharacterIterator.DONE) {
throw new CompileExpressionErrorException("Illegal String,start index=" + startIndex);
}
else {
sb.append(peek);
}
}