Skip to content

Commit

Permalink
Fix bug with case-insensitive strings
Browse files Browse the repository at this point in the history
  • Loading branch information
boxbeam committed Mar 10, 2022
1 parent 94ebd0e commit b28e1a7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/redempt/redlex/token/StringToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import redempt.redlex.data.TokenType;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -57,6 +58,12 @@ public int maxLength() {

@Override
public List<Character> calcFirstCharacters() {
if (!caseSensitive && string.length() > 0) {
List<Character> chars = new ArrayList<>();
chars.add(string.charAt(0));
chars.add(Character.toUpperCase(string.charAt(0)));
return chars;
}
return Collections.singletonList(string.length() == 0 ? null : string.charAt(0));
}

Expand Down
6 changes: 6 additions & 0 deletions test/java/redempt/redlex/test/RedLexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ public void caseInsensitiveTest() {
assertDoesNotThrow(() -> lexer.tokenize("aBc"));
assertDoesNotThrow(() -> lexer.tokenize("Abc"));
assertDoesNotThrow(() -> lexer.tokenize("ABC"));

Lexer lexer2 = BNFParser.createLexer("root ::= \"abc\"");
assertDoesNotThrow(() -> lexer2.tokenize("abc"));
assertThrows(LexException.class, () -> lexer2.tokenize("Abc"));
assertThrows(LexException.class, () -> lexer2.tokenize("aBc"));
assertThrows(LexException.class, () -> lexer2.tokenize("aBC"));
}

}

0 comments on commit b28e1a7

Please sign in to comment.