fix: don`t overlap "close"

This commit is contained in:
2025-12-26 01:03:06 -05:00
parent c2ec9127e8
commit fa44051dec
2 changed files with 24 additions and 23 deletions

View File

@@ -72,11 +72,11 @@ func ParseExpression(i *iterator.Iterator[tokenizer.Token]) (lambda.Expression,
args = append(args, arg)
}
close, closeErr := i.Next()
if closeErr != nil {
return nil, fmt.Errorf("could not parse call terminating parenthesis: %w", closeErr)
} else if close.Type != tokenizer.TokenCloseParen {
return nil, fmt.Errorf("expected call terminating parenthesis, got '%v' (column %v)", close.Value, close.Index)
closing, closingErr := i.Next()
if closingErr != nil {
return nil, fmt.Errorf("could not parse call terminating parenthesis: %w", closingErr)
} else if closing.Type != tokenizer.TokenCloseParen {
return nil, fmt.Errorf("expected call terminating parenthesis, got '%v' (column %v)", closing.Value, closing.Index)
}
// Construction.

View File

@@ -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) {
if err != nil || !isVariable(pop) {
break
}
atom.WriteRune(pop)
if _, err := i.Next(); err != nil {
break
}
}
return &Token{
Index: index,
Type: TokenVariable,
Value: atom,
Value: atom.String(),
}, nil
}
i.Next()
atom += string(pop)
}
}
func GetTokens(input []rune) ([]Token, []error) {