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