feat(lexer): Scan alphanumeric
This commit is contained in:
parent
3e80aee0db
commit
e0754650bc
4 changed files with 77 additions and 16 deletions
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue