fix: don`t overlap "close"
This commit is contained in:
@@ -2,11 +2,16 @@ package tokenizer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"git.maximhutz.com/max/lambda/pkg/iterator"
|
||||
)
|
||||
|
||||
func isVariable(r rune) bool {
|
||||
return unicode.IsLetter(r) || unicode.IsNumber(r)
|
||||
}
|
||||
|
||||
func getToken(i *iterator.Iterator[rune]) (*Token, error) {
|
||||
if i.IsDone() {
|
||||
return nil, nil
|
||||
@@ -51,29 +56,25 @@ func getToken(i *iterator.Iterator[rune]) (*Token, error) {
|
||||
}
|
||||
|
||||
// Otherwise, it is an atom.
|
||||
atom := string(letter)
|
||||
atom := strings.Builder{}
|
||||
index := i.Index()
|
||||
for {
|
||||
if i.IsDone() {
|
||||
return &Token{
|
||||
Index: index,
|
||||
Type: TokenVariable,
|
||||
Value: atom,
|
||||
}, nil
|
||||
}
|
||||
|
||||
for !i.IsDone() {
|
||||
pop, err := i.Peek()
|
||||
if err != nil || unicode.IsSpace(pop) || unicode.IsPunct(pop) {
|
||||
return &Token{
|
||||
Index: index,
|
||||
Type: TokenVariable,
|
||||
Value: atom,
|
||||
}, nil
|
||||
if err != nil || !isVariable(pop) {
|
||||
break
|
||||
}
|
||||
|
||||
i.Next()
|
||||
atom += string(pop)
|
||||
atom.WriteRune(pop)
|
||||
if _, err := i.Next(); err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return &Token{
|
||||
Index: index,
|
||||
Type: TokenVariable,
|
||||
Value: atom.String(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func GetTokens(input []rune) ([]Token, []error) {
|
||||
|
||||
Reference in New Issue
Block a user