Added more tests

This commit is contained in:
2024-08-03 23:05:09 +02:00
parent dbf416d45b
commit 57ee65abd1
9 changed files with 77 additions and 2 deletions

View File

@ -54,5 +54,9 @@ func EachInstruction(body token.List, call func(token.List) error) error {
}
}
if start != len(body) {
return call(body[start:])
}
return nil
}

View File

@ -63,7 +63,13 @@ func parseKeyword(tokens token.List, source []byte, nodes AST) (Node, error) {
return nil, errors.New(errors.MissingBlockEnd, nil, tokens[len(tokens)-1].End())
}
cases, err := parseSwitch(tokens[blockStart+1:blockEnd], source)
body := tokens[blockStart+1 : blockEnd]
if len(body) == 0 {
return nil, errors.New(errors.EmptySwitch, nil, tokens[0].Position)
}
cases, err := parseSwitch(body, source)
return &Switch{Cases: cases}, err
default:

View File

@ -1,6 +1,7 @@
package errors
var (
EmptySwitch = &Base{"Empty switch"}
ExpectedIfBeforeElse = &Base{"Expected an 'if' block before 'else'"}
InvalidNumber = &Base{"Invalid number"}
InvalidExpression = &Base{"Invalid expression"}

View File

@ -1,5 +1,9 @@
package token
import (
"strings"
)
// List is a slice of tokens.
type List []Token
@ -24,3 +28,14 @@ func (list List) LastIndexKind(kind Kind) int {
return -1
}
// Text returns the concatenated token text.
func (list List) Text(source []byte) string {
tmp := strings.Builder{}
for _, t := range list {
tmp.WriteString(t.Text(source))
}
return tmp.String()
}