Reordered tokenizer cases

This commit is contained in:
Eduard Urbach 2024-06-28 21:11:05 +02:00
parent 2520681ad3
commit 445556b64d
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0

View File

@ -24,24 +24,9 @@ func Tokenize(buffer []byte) List {
switch buffer[i] { switch buffer[i] {
// Whitespace // Whitespace
case ' ', '\t': case ' ', '\t':
// Texts // Separator
case '"': case ',':
start := i tokens = append(tokens, Token{Separator, i, separatorBytes})
end := len(buffer)
i++
for i < len(buffer) {
if buffer[i] == '"' {
end = i + 1
i++
break
}
i++
}
tokens = append(tokens, Token{String, start, buffer[start:end]})
continue
// Parentheses start // Parentheses start
case '(': case '(':
@ -67,10 +52,6 @@ func Tokenize(buffer []byte) List {
case ']': case ']':
tokens = append(tokens, Token{ArrayEnd, i, arrayEndBytes}) tokens = append(tokens, Token{ArrayEnd, i, arrayEndBytes})
// Separator
case ',':
tokens = append(tokens, Token{Separator, i, separatorBytes})
// New line // New line
case '\n': case '\n':
tokens = append(tokens, Token{NewLine, i, newLineBytes}) tokens = append(tokens, Token{NewLine, i, newLineBytes})
@ -89,8 +70,27 @@ func Tokenize(buffer []byte) List {
continue continue
// String
case '"':
start := i
end := len(buffer)
i++
for i < len(buffer) {
if buffer[i] == '"' {
end = i + 1
i++
break
}
i++
}
tokens = append(tokens, Token{String, start, buffer[start:end]})
continue
default: default:
// Identifiers // Identifier
if isIdentifierStart(buffer[i]) { if isIdentifierStart(buffer[i]) {
position := i position := i
i++ i++
@ -109,7 +109,7 @@ func Tokenize(buffer []byte) List {
continue continue
} }
// Numbers // Number
if isNumber(buffer[i]) { if isNumber(buffer[i]) {
position := i position := i
i++ i++
@ -122,7 +122,7 @@ func Tokenize(buffer []byte) List {
continue continue
} }
// Operators // Operator
if isOperator(buffer[i]) { if isOperator(buffer[i]) {
position := i position := i
i++ i++
@ -135,7 +135,7 @@ func Tokenize(buffer []byte) List {
continue continue
} }
// Invalid characters // Invalid character
tokens = append(tokens, Token{Invalid, i, buffer[i : i+1]}) tokens = append(tokens, Token{Invalid, i, buffer[i : i+1]})
} }