Skip to content

Commit

Permalink
fix bug where an identifier starts with a keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
musiKk committed Jul 1, 2024
1 parent 6219c3a commit 6a71583
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/github/musiKk/Tokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,20 @@ interface Pattern {
static class StaticPattern implements Pattern {
String pattern;
TokenType tokenType;
boolean isWord;

public StaticPattern(String pattern, TokenType tokenType) {
this.pattern = pattern;
this.tokenType = tokenType;
this.isWord = pattern.matches("\\w+");
}

@Override
public Optional<Token> match(String programString, int index) {
if (programString.startsWith(pattern, index)) {
if (isWord && index + pattern.length() < programString.length() && Character.isJavaIdentifierPart(programString.charAt(index + pattern.length()))) {
return Optional.empty();
}
return Optional.of(new Token(tokenType, pattern, index, index + pattern.length()));
} else {
return Optional.empty();
Expand Down

0 comments on commit 6a71583

Please sign in to comment.