For some reason the while loop is only going through one time, picking up a NUMBER and then exiting. Does anyone have any idea why it isn't lexing the rest of the String? All I had was an input of 1 + 2. Any help is much appreciated!!
public Lexer(String input) throws TokenMismatchException {
tokens = new ArrayList<Token>();
// Lexing logic begins here
StringBuffer tokenPatternsBuffer = new StringBuffer();
for (Type type : Type.values())
tokenPatternsBuffer.append(String.format("|(?<%s>%s)", type.name(), type.pattern));
Pattern tokenPatterns = Pattern.compile(new String(tokenPatternsBuffer.substring(1)));
// Begin matching tokens
Matcher matcher = tokenPatterns.matcher(input.replaceAll(" ", ""));
while (matcher.find()) {
if (matcher.group(Type.NUMBER.name()) != null) {
tokens.add(new Token(Type.NUMBER, matcher.group(Type.NUMBER.name())));
continue;
} else if (matcher.group(Type.OPERATOR.name()) != null) {
tokens.add(new Token(Type.OPERATOR, matcher.group(Type.OPERATOR.name())));
continue;
} else if (matcher.group(Type.UNIT.name()) != null) {
tokens.add(new Token(Type.UNIT, matcher.group(Type.UNIT.name())));
continue;
} else if (matcher.group(Type.PARENTHESES.name()) != null) {
tokens.add(new Token(Type.PARENTHESES, matcher.group(Type.PARENTHESES.name())));
continue;
} else {
throw new TokenMismatchException();
}
}
}
enum Type {
NUMBER("[0-9]+.*[0-9]*"), OPERATOR("[*|/|+|-]"), UNIT("[in|pt]"), PARENTHESES("[(|)]");
public final String pattern;
private Type(String pattern) {
this.pattern = pattern;
}
}