elvish/pkg/glob/parse.go
Kurtis Rader 28798ac053 Convert "XXX" to "TODO" comments
I stumbled across a comment that began with "XXX". It was clearly meant as
a "TODO" comment. This changes all such occurrences. However, a few "XXX"
comments are ambiguous and a better prefix might be "WARNING". The "TODO"
prefix at least ensures someone, eventually, looks into the situation
and either rewords the comment or fixes the problem. This change means
everyone can assume searching for "// TODO" will find all such comments
rather than requiring they also know to search for "// XXX".
2020-08-16 16:29:34 +01:00

93 lines
1.4 KiB
Go

package glob
import (
"bytes"
"unicode/utf8"
)
// Parse parses a pattern.
func Parse(s string) Pattern {
segments := []Segment{}
add := func(seg Segment) {
segments = append(segments, seg)
}
p := &parser{s, 0, 0}
rune:
for {
r := p.next()
switch r {
case eof:
break rune
case '?':
add(Wild{Question, false, nil})
case '*':
n := 1
for p.next() == '*' {
n++
}
p.backup()
if n == 1 {
add(Wild{Star, false, nil})
} else {
add(Wild{StarStar, false, nil})
}
case '/':
for p.next() == '/' {
}
p.backup()
add(Slash{})
default:
var literal bytes.Buffer
literal:
for {
switch r {
case '?', '*', '/', eof:
break literal
case '\\':
r = p.next()
if r == eof {
break literal
}
literal.WriteRune(r)
default:
literal.WriteRune(r)
}
r = p.next()
}
p.backup()
add(Literal{literal.String()})
}
}
return Pattern{segments, ""}
}
// TODO(xiaq): Contains duplicate code with parse/parser.go.
type parser struct {
src string
pos int
overEOF int
}
const eof rune = -1
func (ps *parser) next() rune {
if ps.pos == len(ps.src) {
ps.overEOF++
return eof
}
r, s := utf8.DecodeRuneInString(ps.src[ps.pos:])
ps.pos += s
return r
}
func (ps *parser) backup() {
if ps.overEOF > 0 {
ps.overEOF--
return
}
_, s := utf8.DecodeLastRuneInString(ps.src[:ps.pos])
ps.pos -= s
}