feat(lexer): Scan alphanumeric

This commit is contained in:
Tibo De Peuter 2025-03-27 16:56:11 +01:00
parent 3e80aee0db
commit e0754650bc
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
4 changed files with 77 additions and 16 deletions

View file

@ -9,25 +9,25 @@ class Lexer(private val source: String) {
fun scan(): List<Token> {
while (hasNext()) {
val token = scanToken()
tokens += token
tokens += scanToken()
}
position.length = 0
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)
}
val char: Char = peek()
offset++
position.column++
return token
position.length = 1
return when {
char == '.' -> scanDot()
char.isLetterOrDigit() -> scanAlphanumeric()
else -> throw Error("Unknown symbol: $char", position)
}
}
private fun hasNext(): Boolean {
@ -41,4 +41,27 @@ class Lexer(private val source: String) {
return source[offset]
}
// Scanners
private fun scanDot(): Token {
val token = Token(TokenType.DOT, position)
offset++
position.column++
return token
}
private fun scanAlphanumeric(): Token {
val token = Token(TokenType.ALPHANUMERIC, position)
offset++
position.column++
while (hasNext() && peek().isLetterOrDigit()) {
offset++
position.column++
position.length++
}
return token
}
}