forked from u-root/u-root
/
must.go
41 lines (36 loc) · 888 Bytes
/
must.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package eval
import (
"github.com/u-root/u-root/cmds/core/elvish/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
}
func mustPrimary(cp *compiler, cn *parse.Compound, msg string) *parse.Primary {
p := onePrimary(cn)
if p == nil {
cp.errorpf(cn.Begin(), cn.End(), msg)
}
return p
}
// 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.Begin(), cn.End(), msg)
}
return s
}