q/src/token/List.go
2025-02-05 11:11:15 +01:00

79 lines
1.3 KiB
Go

package token
import (
"strings"
)
// List is a slice of tokens.
type List []Token
// IndexKind returns the position of a token kind within a token list.
func (list List) IndexKind(kind Kind) int {
for i, token := range list {
if token.Kind == kind {
return i
}
}
return -1
}
// LastIndexKind returns the position of the last token kind within a token list.
func (list List) LastIndexKind(kind Kind) int {
for i := len(list) - 1; i >= 0; i-- {
if list[i].Kind == kind {
return i
}
}
return -1
}
// Split calls the callback function on each set of tokens in a comma separated list.
func (list List) Split(call func(List) error) error {
if len(list) == 0 {
return nil
}
start := 0
groupLevel := 0
for i, t := range list {
switch t.Kind {
case GroupStart, ArrayStart, BlockStart:
groupLevel++
case GroupEnd, ArrayEnd, BlockEnd:
groupLevel--
case Separator:
if groupLevel > 0 {
continue
}
parameter := list[start:i]
err := call(parameter)
if err != nil {
return err
}
start = i + 1
}
}
parameter := list[start:]
return call(parameter)
}
// 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()
}