Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

corrects incorrect handling of infix operators after array index #488

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/com/ezylang/evalex/parser/Tokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ private boolean infixOperatorAllowed() {
case STRING_LITERAL:
case POSTFIX_OPERATOR:
case NUMBER_LITERAL:
case ARRAY_CLOSE:
return true;
default:
return false;
Expand Down
84 changes: 84 additions & 0 deletions src/test/java/com/ezylang/evalex/parser/TokenizerArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,90 @@ void testArrayNested() throws ParseException {
new Token(14, "]", TokenType.ARRAY_CLOSE));
}

@Test
void testArrayEquals() throws ParseException {
assertAllTokensParsedCorrectly(
"a[1] = 2",
new Token(1, "a", TokenType.VARIABLE_OR_CONSTANT),
new Token(2, "[", TokenType.ARRAY_OPEN),
new Token(3, "1", TokenType.NUMBER_LITERAL),
new Token(4, "]", TokenType.ARRAY_CLOSE),
new Token(6, "=", TokenType.INFIX_OPERATOR),
new Token(8, "2", TokenType.NUMBER_LITERAL));
}

@Test
void testArrayGreaterThan() throws ParseException {
assertAllTokensParsedCorrectly(
"a[1] > 2",
new Token(1, "a", TokenType.VARIABLE_OR_CONSTANT),
new Token(2, "[", TokenType.ARRAY_OPEN),
new Token(3, "1", TokenType.NUMBER_LITERAL),
new Token(4, "]", TokenType.ARRAY_CLOSE),
new Token(6, ">", TokenType.INFIX_OPERATOR),
new Token(8, "2", TokenType.NUMBER_LITERAL));
}

@Test
void testArrayLessThan() throws ParseException {
assertAllTokensParsedCorrectly(
"a[1] < 2",
new Token(1, "a", TokenType.VARIABLE_OR_CONSTANT),
new Token(2, "[", TokenType.ARRAY_OPEN),
new Token(3, "1", TokenType.NUMBER_LITERAL),
new Token(4, "]", TokenType.ARRAY_CLOSE),
new Token(6, "<", TokenType.INFIX_OPERATOR),
new Token(8, "2", TokenType.NUMBER_LITERAL));
}

@Test
void testArrayNotEquals() throws ParseException {
assertAllTokensParsedCorrectly(
"a[1] != 2",
new Token(1, "a", TokenType.VARIABLE_OR_CONSTANT),
new Token(2, "[", TokenType.ARRAY_OPEN),
new Token(3, "1", TokenType.NUMBER_LITERAL),
new Token(4, "]", TokenType.ARRAY_CLOSE),
new Token(6, "!=", TokenType.INFIX_OPERATOR),
new Token(9, "2", TokenType.NUMBER_LITERAL));
}

@Test
void testArrayEqualsEquals() throws ParseException {
assertAllTokensParsedCorrectly(
"a[1] == 2",
new Token(1, "a", TokenType.VARIABLE_OR_CONSTANT),
new Token(2, "[", TokenType.ARRAY_OPEN),
new Token(3, "1", TokenType.NUMBER_LITERAL),
new Token(4, "]", TokenType.ARRAY_CLOSE),
new Token(6, "==", TokenType.INFIX_OPERATOR),
new Token(9, "2", TokenType.NUMBER_LITERAL));
}

@Test
void testArrayGreaterEqualsThan() throws ParseException {
assertAllTokensParsedCorrectly(
"a[1] >= 2",
new Token(1, "a", TokenType.VARIABLE_OR_CONSTANT),
new Token(2, "[", TokenType.ARRAY_OPEN),
new Token(3, "1", TokenType.NUMBER_LITERAL),
new Token(4, "]", TokenType.ARRAY_CLOSE),
new Token(6, ">=", TokenType.INFIX_OPERATOR),
new Token(9, "2", TokenType.NUMBER_LITERAL));
}

@Test
void testArrayLessEqualsThan() throws ParseException {
assertAllTokensParsedCorrectly(
"a[1] <= 2",
new Token(1, "a", TokenType.VARIABLE_OR_CONSTANT),
new Token(2, "[", TokenType.ARRAY_OPEN),
new Token(3, "1", TokenType.NUMBER_LITERAL),
new Token(4, "]", TokenType.ARRAY_CLOSE),
new Token(6, "<=", TokenType.INFIX_OPERATOR),
new Token(9, "2", TokenType.NUMBER_LITERAL));
}

@Test
void testMissingClosingArray() {
assertThatThrownBy(() -> new Tokenizer("a[2+4", configuration).parse())
Expand Down