package expression import ( "git.akyoto.dev/cli/q/src/build/token" ) // NewList generates a list of expressions from comma separated parameters. func NewList(tokens token.List) []*Expression { var list []*Expression EachParameter(tokens, func(parameter token.List) error { expression := Parse(parameter) list = append(list, expression) return nil }) return list } // EachParameter calls the callback function on each parameter in a comma separated list. func EachParameter(tokens token.List, call func(token.List) error) error { start := 0 groupLevel := 0 for i, t := range tokens { switch t.Kind { case token.GroupStart, token.ArrayStart, token.BlockStart: groupLevel++ case token.GroupEnd, token.ArrayEnd, token.BlockEnd: groupLevel-- case token.Separator: if groupLevel > 0 { continue } parameter := tokens[start:i] err := call(parameter) if err != nil { return err } start = i + 1 } } if start != len(tokens) { parameter := tokens[start:] return call(parameter) } return nil }