2016-04-02 21:03:24 +02:00
|
|
|
package glob
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2016-05-28 06:43:54 +02:00
|
|
|
"github.com/gobwas/glob/runes"
|
2016-04-02 21:03:24 +02:00
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
char_any = '*'
|
2016-05-12 09:11:16 +02:00
|
|
|
char_comma = ','
|
2016-04-02 21:03:24 +02:00
|
|
|
char_single = '?'
|
|
|
|
char_escape = '\\'
|
|
|
|
char_range_open = '['
|
|
|
|
char_range_close = ']'
|
|
|
|
char_terms_open = '{'
|
|
|
|
char_terms_close = '}'
|
|
|
|
char_range_not = '!'
|
|
|
|
char_range_between = '-'
|
|
|
|
)
|
|
|
|
|
|
|
|
var specials = []byte{
|
|
|
|
char_any,
|
|
|
|
char_single,
|
|
|
|
char_escape,
|
|
|
|
char_range_open,
|
|
|
|
char_range_close,
|
|
|
|
char_terms_open,
|
|
|
|
char_terms_close,
|
|
|
|
}
|
|
|
|
|
|
|
|
func special(c byte) bool {
|
|
|
|
return bytes.IndexByte(specials, c) != -1
|
|
|
|
}
|
|
|
|
|
|
|
|
type itemType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
item_eof itemType = iota
|
|
|
|
item_error
|
|
|
|
item_text
|
|
|
|
item_char
|
|
|
|
item_any
|
|
|
|
item_super
|
|
|
|
item_single
|
|
|
|
item_not
|
|
|
|
item_separator
|
|
|
|
item_range_open
|
|
|
|
item_range_close
|
|
|
|
item_range_lo
|
|
|
|
item_range_hi
|
|
|
|
item_range_between
|
|
|
|
item_terms_open
|
|
|
|
item_terms_close
|
|
|
|
)
|
|
|
|
|
|
|
|
func (i itemType) String() string {
|
|
|
|
switch i {
|
|
|
|
case item_eof:
|
|
|
|
return "eof"
|
|
|
|
|
|
|
|
case item_error:
|
|
|
|
return "error"
|
|
|
|
|
|
|
|
case item_text:
|
|
|
|
return "text"
|
|
|
|
|
|
|
|
case item_char:
|
|
|
|
return "char"
|
|
|
|
|
|
|
|
case item_any:
|
|
|
|
return "any"
|
|
|
|
|
|
|
|
case item_super:
|
|
|
|
return "super"
|
|
|
|
|
|
|
|
case item_single:
|
|
|
|
return "single"
|
|
|
|
|
|
|
|
case item_not:
|
|
|
|
return "not"
|
|
|
|
|
|
|
|
case item_separator:
|
|
|
|
return "separator"
|
|
|
|
|
|
|
|
case item_range_open:
|
|
|
|
return "range_open"
|
|
|
|
|
|
|
|
case item_range_close:
|
|
|
|
return "range_close"
|
|
|
|
|
|
|
|
case item_range_lo:
|
|
|
|
return "range_lo"
|
|
|
|
|
|
|
|
case item_range_hi:
|
|
|
|
return "range_hi"
|
|
|
|
|
|
|
|
case item_range_between:
|
|
|
|
return "range_between"
|
|
|
|
|
|
|
|
case item_terms_open:
|
|
|
|
return "terms_open"
|
|
|
|
|
|
|
|
case item_terms_close:
|
|
|
|
return "terms_close"
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "undef"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type item struct {
|
|
|
|
t itemType
|
|
|
|
s string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i item) String() string {
|
2016-05-28 06:43:54 +02:00
|
|
|
return fmt.Sprintf("%v<%q>", i.t, i.s)
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
type stubLexer struct {
|
|
|
|
Items []item
|
|
|
|
pos int
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
func (s *stubLexer) nextItem() (ret item) {
|
|
|
|
if s.pos == len(s.Items) {
|
|
|
|
return item{item_eof, ""}
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
2016-05-28 06:43:54 +02:00
|
|
|
ret = s.Items[s.pos]
|
|
|
|
s.pos++
|
|
|
|
return
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
type items []item
|
2016-04-02 21:03:24 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
func (i *items) shift() (ret item) {
|
|
|
|
ret = (*i)[0]
|
|
|
|
copy(*i, (*i)[1:])
|
|
|
|
*i = (*i)[:len(*i)-1]
|
|
|
|
return
|
|
|
|
}
|
2016-04-02 21:03:24 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
func (i *items) push(v item) {
|
|
|
|
*i = append(*i, v)
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
func (i *items) empty() bool {
|
|
|
|
return len(*i) == 0
|
|
|
|
}
|
2016-04-02 21:03:24 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
var eof rune = 0
|
2016-04-02 21:03:24 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
type lexer struct {
|
|
|
|
data string
|
|
|
|
pos int
|
|
|
|
err error
|
2016-04-02 21:03:24 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
items items
|
|
|
|
termsLevel int
|
2016-04-02 21:03:24 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
lastRune rune
|
|
|
|
lastRuneSize int
|
|
|
|
hasRune bool
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
func newLexer(source string) *lexer {
|
|
|
|
l := &lexer{
|
|
|
|
data: source,
|
|
|
|
items: items(make([]item, 0, 4)),
|
|
|
|
}
|
|
|
|
return l
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
func (l *lexer) peek() (r rune, w int) {
|
|
|
|
if l.pos == len(l.data) {
|
|
|
|
return eof, 0
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
r, w = utf8.DecodeRuneInString(l.data[l.pos:])
|
|
|
|
if r == utf8.RuneError {
|
|
|
|
l.errorf("could not read rune")
|
|
|
|
r = eof
|
|
|
|
w = 0
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
2016-05-28 06:43:54 +02:00
|
|
|
|
|
|
|
return
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
func (l *lexer) read() rune {
|
|
|
|
if l.hasRune {
|
|
|
|
l.hasRune = false
|
|
|
|
l.seek(l.lastRuneSize)
|
|
|
|
return l.lastRune
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
2016-05-28 06:43:54 +02:00
|
|
|
|
|
|
|
r, s := l.peek()
|
|
|
|
l.seek(s)
|
|
|
|
|
|
|
|
l.lastRune = r
|
|
|
|
l.lastRuneSize = s
|
|
|
|
|
|
|
|
return r
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
func (l *lexer) seek(w int) {
|
|
|
|
l.pos += w
|
2016-05-12 09:11:16 +02:00
|
|
|
}
|
2016-04-02 21:03:24 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
func (l *lexer) unread() {
|
|
|
|
if l.hasRune {
|
|
|
|
l.errorf("could not unread rune")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
l.seek(-l.lastRuneSize)
|
|
|
|
l.hasRune = true
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
func (l *lexer) errorf(f string, v ...interface{}) {
|
|
|
|
l.err = fmt.Errorf(f, v...)
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
2016-05-12 09:11:16 +02:00
|
|
|
func (l *lexer) inTerms() bool {
|
2016-05-28 06:43:54 +02:00
|
|
|
return l.termsLevel > 0
|
2016-05-12 09:11:16 +02:00
|
|
|
}
|
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
func (l *lexer) termsEnter() {
|
|
|
|
l.termsLevel++
|
|
|
|
}
|
2016-04-02 21:03:24 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
func (l *lexer) termsLeave() {
|
|
|
|
l.termsLevel--
|
|
|
|
}
|
2016-04-02 21:03:24 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
func (l *lexer) nextItem() item {
|
|
|
|
if l.err != nil {
|
|
|
|
return item{item_error, l.err.Error()}
|
|
|
|
}
|
|
|
|
if !l.items.empty() {
|
|
|
|
return l.items.shift()
|
|
|
|
}
|
2016-04-02 21:03:24 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
l.fetchItem()
|
|
|
|
return l.nextItem()
|
|
|
|
}
|
2016-05-12 09:11:16 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
var inTextBreakers = []rune{char_single, char_any, char_range_open, char_terms_open}
|
|
|
|
var inTermsBreakers = append(inTextBreakers, char_terms_close, char_comma)
|
2016-04-02 21:03:24 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
func (l *lexer) fetchItem() {
|
|
|
|
r := l.read()
|
|
|
|
switch {
|
|
|
|
case r == eof:
|
|
|
|
l.items.push(item{item_eof, ""})
|
2016-04-02 21:03:24 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
case r == char_terms_open:
|
|
|
|
l.termsEnter()
|
|
|
|
l.items.push(item{item_terms_open, string(r)})
|
2016-04-02 21:03:24 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
case r == char_comma && l.inTerms():
|
|
|
|
l.items.push(item{item_separator, string(r)})
|
2016-04-02 21:03:24 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
case r == char_terms_close && l.inTerms():
|
|
|
|
l.items.push(item{item_terms_close, string(r)})
|
|
|
|
l.termsLeave()
|
2016-04-02 21:03:24 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
case r == char_range_open:
|
|
|
|
l.items.push(item{item_range_open, string(r)})
|
|
|
|
l.fetchRange()
|
2016-05-12 09:11:16 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
case r == char_single:
|
|
|
|
l.items.push(item{item_single, string(r)})
|
2016-05-12 09:11:16 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
case r == char_any:
|
|
|
|
if l.read() == char_any {
|
|
|
|
l.items.push(item{item_super, string(r) + string(r)})
|
|
|
|
} else {
|
2016-05-12 09:11:16 +02:00
|
|
|
l.unread()
|
2016-05-28 06:43:54 +02:00
|
|
|
l.items.push(item{item_any, string(r)})
|
|
|
|
}
|
2016-05-12 09:11:16 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
default:
|
|
|
|
l.unread()
|
2016-05-12 09:11:16 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
var breakers []rune
|
|
|
|
if l.inTerms() {
|
|
|
|
breakers = inTermsBreakers
|
|
|
|
} else {
|
|
|
|
breakers = inTextBreakers
|
2016-05-12 09:11:16 +02:00
|
|
|
}
|
2016-05-28 06:43:54 +02:00
|
|
|
l.fetchText(breakers)
|
2016-05-12 09:11:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
func (l *lexer) fetchRange() {
|
|
|
|
var wantHi bool
|
|
|
|
var wantClose bool
|
|
|
|
var seenNot bool
|
2016-04-02 21:03:24 +02:00
|
|
|
for {
|
2016-05-28 06:43:54 +02:00
|
|
|
r := l.read()
|
|
|
|
if r == eof {
|
|
|
|
l.errorf("unexpected end of input")
|
|
|
|
return
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
if wantClose {
|
|
|
|
if r != char_range_close {
|
|
|
|
l.errorf("expected close range character")
|
|
|
|
} else {
|
|
|
|
l.items.push(item{item_range_close, string(r)})
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
2016-05-28 06:43:54 +02:00
|
|
|
return
|
|
|
|
}
|
2016-04-02 21:03:24 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
if wantHi {
|
|
|
|
l.items.push(item{item_range_hi, string(r)})
|
|
|
|
wantClose = true
|
|
|
|
continue
|
|
|
|
}
|
2016-04-02 21:03:24 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
if !seenNot && r == char_range_not {
|
|
|
|
l.items.push(item{item_not, string(r)})
|
|
|
|
seenNot = true
|
|
|
|
continue
|
|
|
|
}
|
2016-05-12 09:11:16 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
if n, w := l.peek(); n == char_range_between {
|
|
|
|
l.seek(w)
|
|
|
|
l.items.push(item{item_range_lo, string(r)})
|
|
|
|
l.items.push(item{item_range_between, string(n)})
|
|
|
|
wantHi = true
|
|
|
|
continue
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
2016-05-28 06:43:54 +02:00
|
|
|
|
|
|
|
l.unread() // unread first peek and fetch as text
|
|
|
|
l.fetchText([]rune{char_range_close})
|
|
|
|
wantClose = true
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
func (l *lexer) fetchText(breakers []rune) {
|
|
|
|
var data []rune
|
|
|
|
var escaped bool
|
2016-04-02 21:03:24 +02:00
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
reading:
|
2016-04-02 21:03:24 +02:00
|
|
|
for {
|
2016-05-28 06:43:54 +02:00
|
|
|
r := l.read()
|
|
|
|
if r == eof {
|
|
|
|
break
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
if !escaped {
|
|
|
|
if r == char_escape {
|
|
|
|
escaped = true
|
2016-04-02 21:03:24 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
if runes.IndexRune(breakers, r) != -1 {
|
|
|
|
l.unread()
|
|
|
|
break reading
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
escaped = false
|
|
|
|
data = append(data, r)
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
|
|
|
|
2016-05-28 06:43:54 +02:00
|
|
|
if len(data) > 0 {
|
|
|
|
l.items.push(item{item_text, string(data)})
|
2016-04-02 21:03:24 +02:00
|
|
|
}
|
|
|
|
}
|