2016-02-19 05:52:05 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
2016-02-23 09:45:35 +08:00
|
|
|
"errors"
|
2016-02-19 05:52:05 +08:00
|
|
|
"fmt"
|
2017-07-14 08:15:14 +08:00
|
|
|
"reflect"
|
2017-02-03 07:24:08 +08:00
|
|
|
"strings"
|
|
|
|
"unicode"
|
2016-02-19 05:52:05 +08:00
|
|
|
|
2018-01-01 04:31:45 +08:00
|
|
|
"github.com/elves/elvish/eval/types"
|
2016-02-19 05:52:05 +08:00
|
|
|
"github.com/elves/elvish/glob"
|
2017-02-03 07:24:08 +08:00
|
|
|
"github.com/elves/elvish/parse"
|
2016-02-19 05:52:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// GlobPattern is en ephemeral Value generated when evaluating tilde and
|
|
|
|
// wildcards.
|
2017-02-03 05:23:20 +08:00
|
|
|
type GlobPattern struct {
|
|
|
|
glob.Pattern
|
|
|
|
Flags GlobFlag
|
2017-02-16 12:10:31 +08:00
|
|
|
Buts []string
|
2017-02-03 05:23:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type GlobFlag uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
NoMatchOK GlobFlag = 1 << iota
|
|
|
|
)
|
|
|
|
|
|
|
|
func (f GlobFlag) Has(g GlobFlag) bool {
|
|
|
|
return (f & g) == g
|
|
|
|
}
|
2016-02-19 05:52:05 +08:00
|
|
|
|
2016-02-23 09:45:35 +08:00
|
|
|
var (
|
2018-01-21 07:14:06 +08:00
|
|
|
_ types.Value = GlobPattern{}
|
|
|
|
_ types.MultiIndexer = GlobPattern{}
|
2016-02-23 09:45:35 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrMustFollowWildcard = errors.New("must follow wildcard")
|
|
|
|
ErrModifierMustBeString = errors.New("modifier must be string")
|
2017-02-03 05:23:20 +08:00
|
|
|
ErrWildcardNoMatch = errors.New("wildcard has no match")
|
2016-02-23 09:45:35 +08:00
|
|
|
)
|
|
|
|
|
2017-02-03 07:24:08 +08:00
|
|
|
var runeMatchers = map[string]func(rune) bool{
|
|
|
|
"control": unicode.IsControl,
|
|
|
|
"digit": unicode.IsDigit,
|
|
|
|
"graphic": unicode.IsGraphic,
|
|
|
|
"letter": unicode.IsLetter,
|
|
|
|
"lower": unicode.IsDigit,
|
|
|
|
"mark": unicode.IsMark,
|
|
|
|
"number": unicode.IsNumber,
|
|
|
|
"print": unicode.IsPrint,
|
|
|
|
"punct": unicode.IsPunct,
|
|
|
|
"space": unicode.IsSpace,
|
|
|
|
"symbol": unicode.IsSymbol,
|
|
|
|
"title": unicode.IsTitle,
|
|
|
|
"upper": unicode.IsUpper,
|
|
|
|
}
|
|
|
|
|
2016-02-19 05:52:05 +08:00
|
|
|
func (GlobPattern) Kind() string {
|
|
|
|
return "glob-pattern"
|
|
|
|
}
|
|
|
|
|
2017-08-31 01:47:50 +08:00
|
|
|
func (gp GlobPattern) Equal(a interface{}) bool {
|
2017-07-14 08:15:14 +08:00
|
|
|
return reflect.DeepEqual(gp, a)
|
|
|
|
}
|
|
|
|
|
2017-08-31 02:52:27 +08:00
|
|
|
func (gp GlobPattern) Hash() uint32 {
|
|
|
|
// GlobPattern is not a first-class value.
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-02-20 03:11:31 +08:00
|
|
|
func (gp GlobPattern) Repr(int) string {
|
2016-02-19 05:52:05 +08:00
|
|
|
return fmt.Sprintf("<GlobPattern%v>", gp)
|
|
|
|
}
|
|
|
|
|
2018-01-01 04:31:45 +08:00
|
|
|
func (gp GlobPattern) Index(modifiers []types.Value) []types.Value {
|
2016-02-23 09:45:35 +08:00
|
|
|
for _, value := range modifiers {
|
2018-01-02 09:34:09 +08:00
|
|
|
modifierv, ok := value.(types.String)
|
2016-02-23 09:45:35 +08:00
|
|
|
if !ok {
|
|
|
|
throw(ErrModifierMustBeString)
|
|
|
|
}
|
2017-02-03 07:24:08 +08:00
|
|
|
modifier := string(modifierv)
|
|
|
|
switch {
|
|
|
|
case modifier == "nomatch-ok":
|
|
|
|
gp.Flags |= NoMatchOK
|
2017-02-16 12:10:31 +08:00
|
|
|
case strings.HasPrefix(modifier, "but:"):
|
|
|
|
gp.Buts = append(gp.Buts, modifier[len("but:"):])
|
2017-02-03 07:24:08 +08:00
|
|
|
case modifier == "match-hidden":
|
|
|
|
lastSeg := gp.mustGetLastWildSeg()
|
2017-02-03 03:15:42 +08:00
|
|
|
gp.Segments[len(gp.Segments)-1] = glob.Wild{
|
2017-02-03 07:24:08 +08:00
|
|
|
lastSeg.Type, true, lastSeg.Matchers,
|
2017-02-03 03:15:42 +08:00
|
|
|
}
|
2016-02-23 09:45:35 +08:00
|
|
|
default:
|
2017-02-03 07:24:08 +08:00
|
|
|
if matcher, ok := runeMatchers[modifier]; ok {
|
|
|
|
gp.addMatcher(matcher)
|
|
|
|
} else if strings.HasPrefix(modifier, "set:") {
|
|
|
|
set := modifier[len("set:"):]
|
|
|
|
gp.addMatcher(func(r rune) bool {
|
|
|
|
return strings.ContainsRune(set, r)
|
|
|
|
})
|
|
|
|
} else if strings.HasPrefix(modifier, "range:") {
|
|
|
|
rangeExpr := modifier[len("range:"):]
|
|
|
|
badRangeExpr := fmt.Errorf("bad range modifier: %s", parse.Quote(rangeExpr))
|
|
|
|
runes := []rune(rangeExpr)
|
|
|
|
if len(runes) != 3 {
|
|
|
|
throw(badRangeExpr)
|
|
|
|
}
|
|
|
|
from, sep, to := runes[0], runes[1], runes[2]
|
|
|
|
switch sep {
|
|
|
|
case '-':
|
|
|
|
gp.addMatcher(func(r rune) bool {
|
|
|
|
return from <= r && r <= to
|
|
|
|
})
|
|
|
|
case '~':
|
|
|
|
gp.addMatcher(func(r rune) bool {
|
|
|
|
return from <= r && r < to
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
throw(badRangeExpr)
|
|
|
|
}
|
|
|
|
} else {
|
2018-01-01 04:31:45 +08:00
|
|
|
throw(fmt.Errorf("unknown modifier %s", modifierv.Repr(types.NoPretty)))
|
2017-02-03 07:24:08 +08:00
|
|
|
}
|
2016-02-23 09:45:35 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-01 04:31:45 +08:00
|
|
|
return []types.Value{gp}
|
2016-02-23 09:45:35 +08:00
|
|
|
}
|
|
|
|
|
2017-02-03 07:24:08 +08:00
|
|
|
func (gp *GlobPattern) mustGetLastWildSeg() glob.Wild {
|
|
|
|
if len(gp.Segments) == 0 {
|
|
|
|
throw(ErrBadGlobPattern)
|
|
|
|
}
|
|
|
|
if !glob.IsWild(gp.Segments[len(gp.Segments)-1]) {
|
|
|
|
throw(ErrMustFollowWildcard)
|
|
|
|
}
|
|
|
|
return gp.Segments[len(gp.Segments)-1].(glob.Wild)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gp *GlobPattern) addMatcher(matcher func(rune) bool) {
|
|
|
|
lastSeg := gp.mustGetLastWildSeg()
|
|
|
|
gp.Segments[len(gp.Segments)-1] = glob.Wild{
|
|
|
|
lastSeg.Type, lastSeg.MatchHidden,
|
|
|
|
append(lastSeg.Matchers, matcher),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-19 05:52:05 +08:00
|
|
|
func (gp *GlobPattern) append(segs ...glob.Segment) {
|
|
|
|
gp.Segments = append(gp.Segments, segs...)
|
|
|
|
}
|
|
|
|
|
2017-06-27 01:47:19 +08:00
|
|
|
func wildcardToSegment(s string) (glob.Segment, error) {
|
2016-02-19 05:52:05 +08:00
|
|
|
switch s {
|
|
|
|
case "*":
|
2017-06-27 01:47:19 +08:00
|
|
|
return glob.Wild{glob.Star, false, nil}, nil
|
2016-02-19 05:52:05 +08:00
|
|
|
case "**":
|
2017-06-27 01:47:19 +08:00
|
|
|
return glob.Wild{glob.StarStar, false, nil}, nil
|
2016-02-19 05:52:05 +08:00
|
|
|
case "?":
|
2017-06-27 01:47:19 +08:00
|
|
|
return glob.Wild{glob.Question, false, nil}, nil
|
2016-02-19 05:52:05 +08:00
|
|
|
default:
|
2017-06-27 01:47:19 +08:00
|
|
|
return nil, fmt.Errorf("bad wildcard: %q", s)
|
2016-02-19 05:52:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringToSegments(s string) []glob.Segment {
|
|
|
|
segs := []glob.Segment{}
|
|
|
|
for i := 0; i < len(s); {
|
|
|
|
j := i
|
|
|
|
for ; j < len(s) && s[j] != '/'; j++ {
|
|
|
|
}
|
|
|
|
if j > i {
|
2017-02-03 03:15:42 +08:00
|
|
|
segs = append(segs, glob.Literal{s[i:j]})
|
2016-02-19 05:52:05 +08:00
|
|
|
}
|
|
|
|
if j < len(s) {
|
|
|
|
for ; j < len(s) && s[j] == '/'; j++ {
|
|
|
|
}
|
2017-02-03 03:15:42 +08:00
|
|
|
segs = append(segs, glob.Slash{})
|
2016-02-19 05:52:05 +08:00
|
|
|
i = j
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return segs
|
|
|
|
}
|
|
|
|
|
2018-01-01 04:31:45 +08:00
|
|
|
func doGlob(gp GlobPattern, abort <-chan struct{}) []types.Value {
|
2017-02-16 12:10:31 +08:00
|
|
|
but := make(map[string]struct{})
|
|
|
|
for _, s := range gp.Buts {
|
|
|
|
but[s] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2018-01-01 04:31:45 +08:00
|
|
|
vs := make([]types.Value, 0)
|
2017-02-03 05:23:20 +08:00
|
|
|
if !gp.Glob(func(name string) bool {
|
2016-06-29 05:08:15 +08:00
|
|
|
select {
|
|
|
|
case <-abort:
|
2017-05-22 06:54:04 +08:00
|
|
|
logger.Println("glob aborted")
|
2016-06-29 05:08:15 +08:00
|
|
|
return false
|
|
|
|
default:
|
|
|
|
}
|
2017-02-16 12:10:31 +08:00
|
|
|
if _, b := but[name]; !b {
|
2018-01-02 09:34:09 +08:00
|
|
|
vs = append(vs, types.String(name))
|
2017-02-16 12:10:31 +08:00
|
|
|
}
|
2016-06-29 05:08:15 +08:00
|
|
|
return true
|
|
|
|
}) {
|
|
|
|
throw(ErrInterrupted)
|
|
|
|
}
|
2017-02-03 05:23:20 +08:00
|
|
|
if len(vs) == 0 && !gp.Flags.Has(NoMatchOK) {
|
|
|
|
throw(ErrWildcardNoMatch)
|
|
|
|
}
|
2016-02-19 05:52:05 +08:00
|
|
|
return vs
|
|
|
|
}
|