feat(lexer): Scan dot

This commit is contained in:
Tibo De Peuter 2025-03-27 16:05:39 +01:00
parent f72501fde2
commit 3e80aee0db
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
6 changed files with 67 additions and 33 deletions

5
src/lexer/Error.kt Normal file
View file

@ -0,0 +1,5 @@
package lexer
class Error(message: String, position: LexerPosition) : Exception("""
Error at ${position.line}:${position.column}: $message
""".trimIndent())

View file

@ -1,11 +1,44 @@
package lexer
class Lexer {
fun scan(source: String): List<Token> {
if (source.isEmpty()) {
return listOf(Token(TokenType.EOF, LexerPosition(1, 1, 0)))
import java.util.LinkedList
class Lexer(private val source: String) {
private var tokens: List<Token> = LinkedList()
private val position: LexerPosition = LexerPosition(0, 0, 0)
private var offset: Int = 0
fun scan(): List<Token> {
while (hasNext()) {
val token = scanToken()
tokens += token
}
return emptyList()
tokens += Token(TokenType.EOF, position)
return tokens
}
private fun scanToken(): Token {
val c = peek()
val token = when (c) {
'.' -> Token(TokenType.DOT, position)
else -> throw Error("Unknown symbol: $c", position)
}
offset++
position.column++
return token
}
private fun hasNext(): Boolean {
return offset < source.length
}
private fun peek(): Char {
if (!hasNext()) {
throw Error("Unexpected end of input", position)
}
return source[offset]
}
}

View file

@ -1,5 +1,5 @@
package lexer
class LexerPosition(val line: Int, val column: Int, val length: Int) {
class LexerPosition(val line: Int, var column: Int, val length: Int) {
// Do nothing
}

View file

@ -1,5 +1,7 @@
package lexer
enum class TokenType {
DOT,
EOF
}