I have trouble with some antlr lexing. I've added a token with a rule:
NUMBER : [0-9]+.?[0-9]*;
WHITESPACE : [ \t\r\n]+ -> skip ;
I test my program using JUnit and if I use the following code:
@Test
public void testWhiteSpaces() {
verifyLexer(" \n7 \t", new String[] {"7"});
}
public void verifyLexer(String input, String[] expectedTokens) {
CharStream stream = new ANTLRInputStream(input);
ExpressionLexer lexer = new ExpressionLexer(stream);
lexer.reportErrorsAsExceptions();
List<? extends Token> actualTokens = lexer.getAllTokens();
assertEquals(expectedTokens.length, actualTokens.size());
for(int i = 0; i < actualTokens.size(); i++) {
String actualToken = actualTokens.get(i).getText();
String expectedToken = expectedTokens[i];
System.out.println(actualToken + "?");
assertEquals(actualToken, expectedToken);
}
The JUnit test fails and it says that the token that it found was "7 " instead "7" I was aiming for. How come? There is no spaces involved in my reg expression for NUMBER token...