2015-12-24 22:30:20 +03:00
|
|
|
package glob
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
2015-12-25 19:40:36 +03:00
|
|
|
var eof rune = 0
|
2015-12-24 22:30:20 +03:00
|
|
|
|
|
|
|
type stateFn func(*lexer) stateFn
|
|
|
|
|
|
|
|
type itemType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
item_eof itemType = iota
|
|
|
|
item_error
|
|
|
|
item_text
|
|
|
|
item_any
|
|
|
|
item_single
|
|
|
|
item_range_open
|
|
|
|
item_range_not
|
2015-12-25 19:40:36 +03:00
|
|
|
item_range_lo
|
2015-12-24 22:30:20 +03:00
|
|
|
item_range_minus
|
2015-12-25 19:40:36 +03:00
|
|
|
item_range_hi
|
|
|
|
item_range_chars
|
2015-12-24 22:30:20 +03:00
|
|
|
item_range_close
|
|
|
|
)
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
case item_any:
|
|
|
|
return "any"
|
|
|
|
|
|
|
|
case item_single:
|
|
|
|
return "single"
|
|
|
|
|
|
|
|
case item_range_open:
|
|
|
|
return "range_open"
|
|
|
|
|
|
|
|
case item_range_not:
|
|
|
|
return "range_not"
|
|
|
|
|
|
|
|
case item_range_lo:
|
|
|
|
return "range_lo"
|
|
|
|
|
|
|
|
case item_range_minus:
|
|
|
|
return "range_minus"
|
|
|
|
|
|
|
|
case item_range_hi:
|
|
|
|
return "range_hi"
|
|
|
|
|
|
|
|
case item_range_chars:
|
|
|
|
return "range_chars"
|
|
|
|
|
|
|
|
case item_range_close:
|
|
|
|
return "range_close"
|
|
|
|
|
|
|
|
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 {
|
2015-12-25 21:08:54 +03:00
|
|
|
return fmt.Sprintf("%v<%s>", i.t, i.s)
|
2015-12-25 19:40:36 +03:00
|
|
|
}
|
|
|
|
|
2015-12-24 22:30:20 +03:00
|
|
|
type lexer struct {
|
|
|
|
input string
|
|
|
|
start int
|
|
|
|
pos int
|
|
|
|
width int
|
|
|
|
runes int
|
2015-12-25 19:40:36 +03:00
|
|
|
state stateFn
|
2015-12-24 22:30:20 +03:00
|
|
|
items chan item
|
|
|
|
}
|
|
|
|
|
2015-12-25 19:40:36 +03:00
|
|
|
func newLexer(source string) *lexer {
|
|
|
|
l := &lexer{
|
|
|
|
input: source,
|
|
|
|
state: lexText,
|
|
|
|
items: make(chan item, 5),
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2015-12-24 22:30:20 +03:00
|
|
|
func (l *lexer) run() {
|
|
|
|
for state := lexText; state != nil; {
|
|
|
|
state = state(l)
|
|
|
|
}
|
|
|
|
close(l.items)
|
|
|
|
}
|
|
|
|
|
2015-12-25 19:40:36 +03:00
|
|
|
func (l *lexer) read() (r rune) {
|
2015-12-24 22:30:20 +03:00
|
|
|
if l.pos >= len(l.input) {
|
|
|
|
return eof
|
|
|
|
}
|
|
|
|
|
2015-12-25 19:40:36 +03:00
|
|
|
r, l.width = utf8.DecodeRuneInString(l.input[l.pos:])
|
2015-12-24 22:30:20 +03:00
|
|
|
l.pos += l.width
|
|
|
|
l.runes++
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *lexer) unread() {
|
|
|
|
l.pos -= l.width
|
|
|
|
l.runes--
|
|
|
|
}
|
|
|
|
|
2015-12-26 12:14:30 +03:00
|
|
|
func (l *lexer) reset() {
|
|
|
|
l.pos = l.start
|
2015-12-25 19:40:36 +03:00
|
|
|
l.runes = 0
|
|
|
|
}
|
|
|
|
|
2015-12-24 22:30:20 +03:00
|
|
|
func (l *lexer) ignore() {
|
|
|
|
l.start = l.pos
|
|
|
|
l.runes = 0
|
|
|
|
}
|
|
|
|
|
2015-12-25 19:40:36 +03:00
|
|
|
func (l *lexer) lookahead() rune {
|
2015-12-24 22:30:20 +03:00
|
|
|
r := l.read()
|
|
|
|
l.unread()
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *lexer) accept(valid string) bool {
|
|
|
|
if strings.IndexRune(valid, l.read()) != -1 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
l.unread()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *lexer) acceptAll(valid string) {
|
|
|
|
for strings.IndexRune(valid, l.read()) != -1 {
|
|
|
|
}
|
|
|
|
l.unread()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *lexer) emit(t itemType) {
|
|
|
|
l.items <- item{t, l.input[l.start:l.pos]}
|
|
|
|
l.start = l.pos
|
|
|
|
l.runes = 0
|
|
|
|
l.width = 0
|
|
|
|
}
|
|
|
|
|
2015-12-26 12:14:30 +03:00
|
|
|
func (l *lexer) emitMaybe(t itemType) {
|
2015-12-24 22:30:20 +03:00
|
|
|
if l.pos > l.start {
|
|
|
|
l.emit(t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *lexer) errorf(format string, args ...interface{}) {
|
2015-12-25 19:40:36 +03:00
|
|
|
l.items <- item{item_error, fmt.Sprintf(format, args...)}
|
2015-12-24 22:30:20 +03:00
|
|
|
}
|
|
|
|
|
2015-12-25 19:40:36 +03:00
|
|
|
func (l *lexer) nextItem() item {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case item := <-l.items:
|
|
|
|
return item
|
|
|
|
default:
|
|
|
|
if l.state == nil {
|
|
|
|
return item{t: item_eof}
|
|
|
|
}
|
2015-12-24 22:30:20 +03:00
|
|
|
|
2015-12-25 19:40:36 +03:00
|
|
|
l.state = l.state(l)
|
|
|
|
}
|
|
|
|
}
|
2015-12-24 22:30:20 +03:00
|
|
|
|
2015-12-25 19:40:36 +03:00
|
|
|
panic("something went wrong")
|
2015-12-24 22:30:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func lexText(l *lexer) stateFn {
|
|
|
|
for {
|
2015-12-25 19:40:36 +03:00
|
|
|
c := l.read()
|
|
|
|
if c == eof {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c {
|
2015-12-24 22:30:20 +03:00
|
|
|
case escape:
|
|
|
|
if l.read() == eof {
|
|
|
|
l.errorf("unclosed '%s' character", string(escape))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case single:
|
2015-12-25 19:40:36 +03:00
|
|
|
l.unread()
|
2015-12-26 12:14:30 +03:00
|
|
|
l.emitMaybe(item_text)
|
2015-12-24 22:30:20 +03:00
|
|
|
return lexSingle
|
|
|
|
case any:
|
2015-12-25 19:40:36 +03:00
|
|
|
l.unread()
|
2015-12-26 12:14:30 +03:00
|
|
|
l.emitMaybe(item_text)
|
2015-12-24 22:30:20 +03:00
|
|
|
return lexAny
|
|
|
|
case range_open:
|
2015-12-25 19:40:36 +03:00
|
|
|
l.unread()
|
2015-12-26 12:14:30 +03:00
|
|
|
l.emitMaybe(item_text)
|
2015-12-24 22:30:20 +03:00
|
|
|
return lexRangeOpen
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.pos > l.start {
|
|
|
|
l.emit(item_text)
|
|
|
|
}
|
|
|
|
|
|
|
|
l.emit(item_eof)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func lexInsideRange(l *lexer) stateFn {
|
|
|
|
for {
|
2015-12-25 19:40:36 +03:00
|
|
|
c := l.read()
|
|
|
|
if c == eof {
|
|
|
|
l.errorf("unclosed range construction")
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-24 22:30:20 +03:00
|
|
|
|
2015-12-25 19:40:36 +03:00
|
|
|
switch c {
|
2015-12-24 22:30:20 +03:00
|
|
|
case inside_range_not:
|
|
|
|
// only first char makes sense
|
2015-12-26 12:14:30 +03:00
|
|
|
if l.pos-l.width == l.start {
|
2015-12-24 22:30:20 +03:00
|
|
|
l.emit(item_range_not)
|
|
|
|
}
|
|
|
|
|
|
|
|
case inside_range_minus:
|
2015-12-26 12:14:30 +03:00
|
|
|
if l.runes != 2 {
|
2015-12-25 19:40:36 +03:00
|
|
|
l.errorf("unexpected length of lo char inside range")
|
2015-12-24 22:30:20 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-26 12:14:30 +03:00
|
|
|
l.reset()
|
2015-12-25 19:40:36 +03:00
|
|
|
return lexRangeHiLo
|
2015-12-24 22:30:20 +03:00
|
|
|
|
|
|
|
case range_close:
|
2015-12-25 19:40:36 +03:00
|
|
|
l.unread()
|
2015-12-26 12:14:30 +03:00
|
|
|
l.emitMaybe(item_range_chars)
|
2015-12-24 22:30:20 +03:00
|
|
|
return lexRangeClose
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func lexAny(l *lexer) stateFn {
|
|
|
|
l.pos += 1
|
|
|
|
l.emit(item_any)
|
|
|
|
return lexText
|
|
|
|
}
|
|
|
|
|
|
|
|
func lexRangeHiLo(l *lexer) stateFn {
|
2015-12-25 21:08:54 +03:00
|
|
|
start := l.start
|
|
|
|
|
2015-12-25 19:40:36 +03:00
|
|
|
for {
|
|
|
|
c := l.read()
|
|
|
|
if c == eof {
|
|
|
|
l.errorf("unexpected end of input")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c {
|
|
|
|
case inside_range_minus:
|
2015-12-26 12:14:30 +03:00
|
|
|
if l.runes != 1 {
|
2015-12-25 21:08:54 +03:00
|
|
|
l.errorf("unexpected length of range: single character expected before minus")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-25 19:40:36 +03:00
|
|
|
l.emit(item_range_minus)
|
2015-12-24 22:30:20 +03:00
|
|
|
|
2015-12-25 19:40:36 +03:00
|
|
|
case range_close:
|
|
|
|
l.unread()
|
2015-12-25 21:08:54 +03:00
|
|
|
|
2015-12-26 12:14:30 +03:00
|
|
|
if l.runes != 1 {
|
2015-12-25 21:08:54 +03:00
|
|
|
l.errorf("unexpected length of range: single character expected before close")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
l.emit(item_range_hi)
|
2015-12-25 19:40:36 +03:00
|
|
|
return lexRangeClose
|
|
|
|
|
|
|
|
default:
|
2015-12-25 21:08:54 +03:00
|
|
|
if start != l.start {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-12-26 12:14:30 +03:00
|
|
|
if l.runes != 1 {
|
2015-12-25 21:08:54 +03:00
|
|
|
l.errorf("unexpected length of range: single character expected at the begining")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
l.emit(item_range_lo)
|
2015-12-25 19:40:36 +03:00
|
|
|
}
|
|
|
|
}
|
2015-12-24 22:30:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func lexSingle(l *lexer) stateFn {
|
|
|
|
l.pos += 1
|
|
|
|
l.emit(item_single)
|
|
|
|
return lexText
|
|
|
|
}
|
|
|
|
|
2015-12-25 19:40:36 +03:00
|
|
|
func lexRangeOpen(l *lexer) stateFn {
|
|
|
|
l.pos += 1
|
|
|
|
l.emit(item_range_open)
|
|
|
|
return lexInsideRange
|
|
|
|
}
|
|
|
|
|
2015-12-24 22:30:20 +03:00
|
|
|
func lexRangeClose(l *lexer) stateFn {
|
|
|
|
l.pos += 1
|
|
|
|
l.emit(item_range_close)
|
|
|
|
return lexText
|
|
|
|
}
|