From b924fc5cb14d89c267616d4c47a8d999c4ae6013 Mon Sep 17 00:00:00 2001 From: vighnesh153 Date: Fri, 9 Feb 2024 07:29:55 +0530 Subject: [PATCH] feat: add lexing for *= --- .../interpreters/javalang/common/lexer/Lexer.kt | 15 ++++++++++++++- .../javalang/common/lexer/LexerTest.kt | 11 +++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/jvm-tools/interpreters/languages/java-lang-common/src/main/kotlin/interpreters/javalang/common/lexer/Lexer.kt b/jvm-tools/interpreters/languages/java-lang-common/src/main/kotlin/interpreters/javalang/common/lexer/Lexer.kt index d2e6316a..de093441 100644 --- a/jvm-tools/interpreters/languages/java-lang-common/src/main/kotlin/interpreters/javalang/common/lexer/Lexer.kt +++ b/jvm-tools/interpreters/languages/java-lang-common/src/main/kotlin/interpreters/javalang/common/lexer/Lexer.kt @@ -78,7 +78,20 @@ fun Lexer.nextToken(): Token { } } - '*' -> t = Token(tokenType = TokenType.ASTERISK, tokenLiteral = TokenType.ASTERISK.value) + '*' -> { + val peek = peekCharacter() + t = when (peek) { + '=' -> { + readNextCharacter() + Token(tokenType = TokenType.ASTERISK_EQUALS, tokenLiteral = TokenType.ASTERISK_EQUALS.value) + } + + else -> { + Token(tokenType = TokenType.ASTERISK, tokenLiteral = TokenType.ASTERISK.value) + } + } + } + '/' -> t = Token(tokenType = TokenType.FORWARD_SLASH, tokenLiteral = TokenType.FORWARD_SLASH.value) '\\' -> t = Token(tokenType = TokenType.BACK_SLASH, tokenLiteral = TokenType.BACK_SLASH.value) '%' -> t = Token(tokenType = TokenType.MODULUS, tokenLiteral = TokenType.MODULUS.value) diff --git a/jvm-tools/interpreters/languages/java-lang-common/src/test/kotlin/interpreters/javalang/common/lexer/LexerTest.kt b/jvm-tools/interpreters/languages/java-lang-common/src/test/kotlin/interpreters/javalang/common/lexer/LexerTest.kt index 5f8236d4..b3e3cf25 100644 --- a/jvm-tools/interpreters/languages/java-lang-common/src/test/kotlin/interpreters/javalang/common/lexer/LexerTest.kt +++ b/jvm-tools/interpreters/languages/java-lang-common/src/test/kotlin/interpreters/javalang/common/lexer/LexerTest.kt @@ -26,6 +26,8 @@ b += c + ++d x -- y-= z++ - --a +a *=b*= c = d + """.trimIndent() val expectedTokens = listOf( @@ -133,6 +135,15 @@ y-= z++ - --a ExpectedToken(id = 53, tokenType = TokenType.DECREMENT, tokenLiteral = "--"), ExpectedToken(id = 54, tokenType = TokenType.IDENTIFIER, tokenLiteral = "a"), + // a *=b*= c = d + ExpectedToken(id = 55, tokenType = TokenType.IDENTIFIER, tokenLiteral = "a"), + ExpectedToken(id = 56, tokenType = TokenType.ASTERISK_EQUALS, tokenLiteral = "*="), + ExpectedToken(id = 57, tokenType = TokenType.IDENTIFIER, tokenLiteral = "b"), + ExpectedToken(id = 58, tokenType = TokenType.ASTERISK_EQUALS, tokenLiteral = "*="), + ExpectedToken(id = 59, tokenType = TokenType.IDENTIFIER, tokenLiteral = "c"), + ExpectedToken(id = 60, tokenType = TokenType.EQUALS, tokenLiteral = "="), + ExpectedToken(id = 61, tokenType = TokenType.IDENTIFIER, tokenLiteral = "d"), + // eof ExpectedToken(id = -1, tokenType = Token.EOF.tokenType, tokenLiteral = Token.EOF.tokenLiteral), )