elvish/pkg/eval/must.go

34 lines
684 B
Go
Raw Normal View History

2016-01-23 01:05:15 +08:00
package eval
import (
2019-12-24 04:00:59 +08:00
"github.com/elves/elvish/pkg/parse"
2016-01-23 01:05:15 +08:00
)
func onePrimary(cn *parse.Compound) *parse.Primary {
2016-02-03 23:36:12 +08:00
if len(cn.Indexings) == 1 && len(cn.Indexings[0].Indicies) == 0 {
return cn.Indexings[0].Head
2016-01-23 01:05:15 +08:00
}
return nil
2016-01-23 01:05:15 +08:00
}
func oneString(cn *parse.Compound) (string, bool) {
pn := onePrimary(cn)
if pn != nil {
switch pn.Type {
case parse.Bareword, parse.SingleQuoted, parse.DoubleQuoted:
return pn.Value, true
}
2016-01-23 01:05:15 +08:00
}
return "", false
2016-01-23 01:05:15 +08:00
}
// mustString musts that a Compound contains exactly one Primary of type
// Variable.
func mustString(cp *compiler, cn *parse.Compound, msg string) string {
s, ok := oneString(cn)
if !ok {
cp.errorpf(cn, msg)
2016-01-23 01:05:15 +08:00
}
return s
2016-01-23 01:05:15 +08:00
}