mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-11-28 07:21:21 +08:00
34 lines
684 B
Go
34 lines
684 B
Go
package eval
|
|
|
|
import (
|
|
"github.com/elves/elvish/pkg/parse"
|
|
)
|
|
|
|
func onePrimary(cn *parse.Compound) *parse.Primary {
|
|
if len(cn.Indexings) == 1 && len(cn.Indexings[0].Indicies) == 0 {
|
|
return cn.Indexings[0].Head
|
|
}
|
|
return nil
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
return s
|
|
}
|