Skip to content

Commit

Permalink
corrects incorrect handling of infix operators after array index
Browse files Browse the repository at this point in the history
  • Loading branch information
uklimaschewski committed Jun 30, 2024
1 parent 2f14ec7 commit 252caef
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
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

0 comments on commit 252caef

Please sign in to comment.