| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "path/filepath" |
| 6 | "regexp" |
| 7 | "regexp/syntax" |
| 8 | "strings" |
| 9 | "sync" |
| 10 | ) |
| 11 | |
| 12 | // A Matcher decides whether some filename matches its set of patterns. |
| 13 | type Matcher interface { |
| 14 | // Match returns whether a filename matches. |
| 15 | Match(name string) bool |
| 16 | // ExcludePrefix returns whether all paths with this prefix cannot match. |
| 17 | // It is allowed to return false negatives but not false positives. |
| 18 | // This is used as an optimization for skipping directory watches with |
| 19 | // inverted matches. |
| 20 | ExcludePrefix(prefix string) bool |
| 21 | String() string |
| 22 | } |
| 23 | |
| 24 | // ParseMatchers combines multiple (possibly inverse) regex and glob patterns |
| 25 | // into a single Matcher. |
| 26 | func ParseMatchers(regexes, inverseRegexes, globs, inverseGlobs []string) (m Matcher, err error) { |
| 27 | var matchers multiMatcher |
| 28 | if len(regexes) == 0 && len(globs) == 0 { |
| 29 | matchers = multiMatcher{matchAll{}} |
| 30 | } |
| 31 | for _, r := range regexes { |
| 32 | regex, err := regexp.Compile(r) |
| 33 | if err != nil { |
| 34 | return nil, err |
| 35 | } |
| 36 | matchers = append(matchers, newRegexMatcher(regex, false)) |
| 37 | } |
| 38 | for _, r := range inverseRegexes { |
| 39 | regex, err := regexp.Compile(r) |
| 40 | if err != nil { |
| 41 | return nil, err |
| 42 | } |
| 43 | matchers = append(matchers, newRegexMatcher(regex, true)) |
| 44 | } |
| 45 | for _, g := range globs { |
| 46 | matchers = append(matchers, &globMatcher{glob: g}) |
| 47 | } |
| 48 | for _, g := range inverseGlobs { |
| 49 | matchers = append(matchers, &globMatcher{ |
| 50 | glob: g, |
| 51 | inverse: true, |
| 52 | }) |
| 53 | } |
| 54 | return matchers, nil |
| 55 | } |
| 56 | |
| 57 | // matchAll is an all-accepting Matcher. |
| 58 | type matchAll struct{} |
| 59 | |
| 60 | func (matchAll) Match(name string) bool { return true } |
| 61 | func (matchAll) ExcludePrefix(prefix string) bool { return false } |
| 62 | func (matchAll) String() string { return "(Implicitly matching all non-excluded files)" } |
| 63 | |
| 64 | type globMatcher struct { |
| 65 | glob string |
| 66 | inverse bool |
| 67 | } |
| 68 | |
| 69 | func (m *globMatcher) Match(name string) bool { |
| 70 | matches, err := filepath.Match(m.glob, name) |
| 71 | if err != nil { |
| 72 | return false |
| 73 | } |
| 74 | return matches != m.inverse |
| 75 | } |
| 76 | |
| 77 | func (m *globMatcher) ExcludePrefix(prefix string) bool { return false } |
| 78 | |
| 79 | func (m *globMatcher) String() string { |
| 80 | s := "Glob" |
| 81 | if m.inverse { |
| 82 | s = "Inverted glob" |
| 83 | } |
| 84 | return fmt.Sprintf("%s match: %q", s, m.glob) |
| 85 | } |
| 86 | |
| 87 | type regexMatcher struct { |
| 88 | regex *regexp.Regexp |
| 89 | inverse bool |
| 90 | |
| 91 | mu *sync.Mutex // protects following |
| 92 | canExcludePrefix bool // This regex has no $, \z, or \b -- see ExcludePrefix |
| 93 | excludeChecked bool |
| 94 | } |
| 95 | |
| 96 | func (m *regexMatcher) Match(name string) bool { |
| 97 | return m.regex.MatchString(name) != m.inverse |
| 98 | } |
| 99 | |
| 100 | func newRegexMatcher(regex *regexp.Regexp, inverse bool) *regexMatcher { |
| 101 | return ®exMatcher{ |
| 102 | regex: regex, |
| 103 | inverse: inverse, |
| 104 | mu: new(sync.Mutex), |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // ExcludePrefix returns whether this matcher cannot possibly match any path |
| 109 | // with a particular prefix. The question is: given a regex r and some prefix p |
| 110 | // which r accepts, is there any string s that has p as a prefix that r does not |
| 111 | // accept? |
| 112 | // |
| 113 | // With a classic regular expression from CS, this can only be the case if r |
| 114 | // ends with $, the end-of-input token (because once the NFA is in an accepting |
| 115 | // state, adding more input will not change that). In Go's regular expressions, |
| 116 | // I think the only way to construct a regex that would not meet this criteria |
| 117 | // is by using zero-width lookahead. There is no arbitrary lookahead in Go, so |
| 118 | // the only zero-width lookahead is provided by $, \z, and \b. For instance, the |
| 119 | // following regular expressions match the "foo", but not "foobar": |
| 120 | // |
| 121 | // foo$ |
| 122 | // foo\b |
| 123 | // (foo$)|(baz$) |
| 124 | // |
| 125 | // Thus, to choose whether we can exclude this prefix, m must be an inverse |
| 126 | // matcher that does not contain the zero-width ops $, \z, and \b. |
| 127 | func (m *regexMatcher) ExcludePrefix(prefix string) bool { |
| 128 | if !m.inverse { |
| 129 | return false |
| 130 | } |
| 131 | if !m.regex.MatchString(prefix) || m.regex.String() == "" { |
| 132 | return false |
| 133 | } |
| 134 | |
| 135 | m.mu.Lock() |
| 136 | defer m.mu.Unlock() |
| 137 | if !m.excludeChecked { |
| 138 | r, err := syntax.Parse(m.regex.String(), syntax.Perl) |
| 139 | if err != nil { |
| 140 | panic("Cannot compile regex, but it was previously compiled!?!") |
| 141 | } |
| 142 | r = r.Simplify() |
| 143 | stack := []*syntax.Regexp{r} |
| 144 | for len(stack) > 0 { |
| 145 | cur := stack[len(stack)-1] |
| 146 | stack = stack[:len(stack)-1] |
| 147 | switch cur.Op { |
| 148 | case syntax.OpEndLine, syntax.OpEndText, syntax.OpWordBoundary: |
| 149 | m.canExcludePrefix = false |
| 150 | goto after |
| 151 | } |
| 152 | if cur.Sub0[0] != nil { |
| 153 | stack = append(stack, cur.Sub0[0]) |
| 154 | } |
| 155 | stack = append(stack, cur.Sub...) |
| 156 | } |
| 157 | m.canExcludePrefix = true |
| 158 | after: |
| 159 | m.excludeChecked = true |
| 160 | } |
| 161 | return m.canExcludePrefix |
| 162 | } |
| 163 | |
| 164 | func (m *regexMatcher) String() string { |
| 165 | s := "Regex" |
| 166 | if m.inverse { |
| 167 | s = "Inverted regex" |
| 168 | } |
| 169 | return fmt.Sprintf("%s match: %q", s, m.regex.String()) |
| 170 | } |
| 171 | |
| 172 | // A multiMatcher returns the logical AND of its sub-matchers. |
| 173 | type multiMatcher []Matcher |
| 174 | |
| 175 | func (m multiMatcher) Match(name string) bool { |
| 176 | for _, matcher := range m { |
| 177 | if !matcher.Match(name) { |
| 178 | return false |
| 179 | } |
| 180 | } |
| 181 | return true |
| 182 | } |
| 183 | |
| 184 | func (m multiMatcher) ExcludePrefix(prefix string) bool { |
| 185 | for _, matcher := range m { |
| 186 | if matcher.ExcludePrefix(prefix) { |
| 187 | return true |
| 188 | } |
| 189 | } |
| 190 | return false |
| 191 | } |
| 192 | |
| 193 | func (m multiMatcher) String() string { |
| 194 | var s []string |
| 195 | for _, matcher := range m { |
| 196 | s = append(s, matcher.String()) |
| 197 | } |
| 198 | return strings.Join(s, "\n") |
| 199 | } |