This repository has been archived on 2025-09-23. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
2025LogProg-project-GhentPr.../src/lexer/Lexer.kt

44 lines
983 B
Kotlin

package lexer
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
}
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]
}
}