transcendental-lisp/src/token/TokenFactoryImpl.java

41 lines
1.2 KiB
Java

package token;
import static util.Characters.*;
import file.FilePosition;
import util.Characters;
public class TokenFactoryImpl implements TokenFactory {
public Token createToken(String text, FilePosition position) {
if (text.length() == 0)
throw new EmptyTokenTextException(position);
char firstCharacter = text.charAt(0);
switch (firstCharacter) {
case LEFT_PARENTHESIS:
return new LeftParenthesis(text, position);
case RIGHT_PARENTHESIS:
return new RightParenthesis(text, position);
case SINGLE_QUOTE:
return new QuoteMark(text, position);
case DOUBLE_QUOTE:
return new QuotedString(text, position);
default:
if (Character.isDigit(firstCharacter)) {
return new Number(text, position);
} else if (Characters.isLegalIdentifierCharacter(firstCharacter)) {
return new Identifier(text, position);
}
}
throw new BadCharacterException(text, position);
}
public Token createEOFToken(FilePosition position) {
return new Eof("EOF", position);
}
}