Simplified tokenizer

This commit is contained in:
Eduard Urbach 2025-02-02 11:11:59 +01:00
parent dd6ebd9cd4
commit 25f6928f40
Signed by: eduard
GPG key ID: 49226B848C78F6C8
9 changed files with 300 additions and 261 deletions

46
src/token/identifier.go Normal file
View file

@ -0,0 +1,46 @@
package token
// identifier handles all tokens that qualify as an identifier.
func identifier(tokens List, buffer []byte, i Position) (List, Position) {
position := i
i++
for i < Position(len(buffer)) && isIdentifier(buffer[i]) {
i++
}
identifier := buffer[position:i]
kind := Identifier
switch string(identifier) {
case "assert":
kind = Assert
case "if":
kind = If
case "else":
kind = Else
case "import":
kind = Import
case "loop":
kind = Loop
case "return":
kind = Return
case "switch":
kind = Switch
}
tokens = append(tokens, Token{Kind: kind, Position: position, Length: Length(len(identifier))})
return tokens, i
}
func isIdentifier(c byte) bool {
return isLetter(c) || isDigit(c) || c == '_'
}
func isIdentifierStart(c byte) bool {
return isLetter(c) || c == '_'
}
func isLetter(c byte) bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
}