github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/cmds/core/elvish/eval/must.go (about)

     1  package eval
     2  
     3  import (
     4  	"github.com/u-root/u-root/cmds/core/elvish/parse"
     5  )
     6  
     7  func onePrimary(cn *parse.Compound) *parse.Primary {
     8  	if len(cn.Indexings) == 1 && len(cn.Indexings[0].Indicies) == 0 {
     9  		return cn.Indexings[0].Head
    10  	}
    11  	return nil
    12  }
    13  
    14  func oneString(cn *parse.Compound) (string, bool) {
    15  	pn := onePrimary(cn)
    16  	if pn != nil {
    17  		switch pn.Type {
    18  		case parse.Bareword, parse.SingleQuoted, parse.DoubleQuoted:
    19  			return pn.Value, true
    20  		}
    21  	}
    22  	return "", false
    23  }
    24  
    25  func mustPrimary(cp *compiler, cn *parse.Compound, msg string) *parse.Primary {
    26  	p := onePrimary(cn)
    27  	if p == nil {
    28  		cp.errorpf(cn.Begin(), cn.End(), msg)
    29  	}
    30  	return p
    31  }
    32  
    33  // mustString musts that a Compound contains exactly one Primary of type
    34  // Variable.
    35  func mustString(cp *compiler, cn *parse.Compound, msg string) string {
    36  	s, ok := oneString(cn)
    37  	if !ok {
    38  		cp.errorpf(cn.Begin(), cn.End(), msg)
    39  	}
    40  	return s
    41  }