2016-01-21 06:57:18 +08:00
|
|
|
package parse
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
"unicode/utf8"
|
2016-02-06 07:08:39 +08:00
|
|
|
|
2016-02-17 02:14:05 +08:00
|
|
|
"github.com/elves/elvish/util"
|
2016-01-21 06:57:18 +08:00
|
|
|
)
|
|
|
|
|
2016-02-03 02:39:44 +08:00
|
|
|
// parser maintains some mutable states of parsing.
|
2016-01-21 06:57:18 +08:00
|
|
|
//
|
|
|
|
// NOTE: The str member is assumed to be valid UF-8.
|
2016-02-03 02:39:44 +08:00
|
|
|
type parser struct {
|
2017-05-30 07:42:38 +08:00
|
|
|
srcName string
|
|
|
|
src string
|
|
|
|
pos int
|
|
|
|
overEOF int
|
|
|
|
cutsets []map[rune]int
|
|
|
|
errors Error
|
2016-01-21 06:57:18 +08:00
|
|
|
}
|
|
|
|
|
2017-05-30 08:10:21 +08:00
|
|
|
// NewParser creates a new parser from a piece of source text and its name.
|
|
|
|
func NewParser(srcname, src string) *parser {
|
|
|
|
return &parser{srcname, src, 0, 0, []map[rune]int{{}}, Error{}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done tells the parser that parsing has completed.
|
|
|
|
func (ps *parser) Done() {
|
|
|
|
if ps.pos != len(ps.src) {
|
|
|
|
ps.error(errUnexpectedRune)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Errors gets the parsing errors after calling one of the parse* functions. If
|
|
|
|
// the return value is not nil, it is always of type Error.
|
|
|
|
func (ps *parser) Errors() error {
|
|
|
|
if len(ps.errors.Entries) > 0 {
|
|
|
|
return &ps.errors
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-08 06:23:16 +08:00
|
|
|
const eof rune = -1
|
2016-01-21 06:57:18 +08:00
|
|
|
|
2016-02-03 02:39:44 +08:00
|
|
|
func (ps *parser) peek() rune {
|
|
|
|
if ps.pos == len(ps.src) {
|
2016-02-08 06:23:16 +08:00
|
|
|
return eof
|
2016-01-21 06:57:18 +08:00
|
|
|
}
|
2016-02-03 02:39:44 +08:00
|
|
|
r, _ := utf8.DecodeRuneInString(ps.src[ps.pos:])
|
2016-02-07 06:17:57 +08:00
|
|
|
if ps.currentCutset()[r] > 0 {
|
2016-02-08 06:23:16 +08:00
|
|
|
return eof
|
2016-02-07 06:17:57 +08:00
|
|
|
}
|
2016-01-21 06:57:18 +08:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2016-02-03 02:39:44 +08:00
|
|
|
func (ps *parser) hasPrefix(prefix string) bool {
|
|
|
|
return strings.HasPrefix(ps.src[ps.pos:], prefix)
|
2016-01-21 06:57:18 +08:00
|
|
|
}
|
|
|
|
|
2016-02-15 03:06:18 +08:00
|
|
|
// findWord looks ahead for [a-z]* that is also a valid compound. If the
|
|
|
|
// lookahead fails, it returns an empty string. It is useful for looking for
|
|
|
|
// command leaders.
|
2016-02-14 22:28:14 +08:00
|
|
|
func (ps *parser) findPossibleLeader() string {
|
|
|
|
rest := ps.src[ps.pos:]
|
|
|
|
i := strings.IndexFunc(rest, func(r rune) bool {
|
|
|
|
return r < 'a' || r > 'z'
|
|
|
|
})
|
|
|
|
if i == -1 {
|
|
|
|
// The whole rest is just one possible leader.
|
|
|
|
return rest
|
|
|
|
}
|
2016-02-15 03:06:18 +08:00
|
|
|
r, _ := utf8.DecodeRuneInString(rest[i:])
|
2016-06-21 23:23:28 +08:00
|
|
|
if startsPrimary(r, false) {
|
2016-02-14 22:28:14 +08:00
|
|
|
return ""
|
|
|
|
}
|
2016-02-15 03:06:18 +08:00
|
|
|
return rest[:i]
|
2016-02-14 22:28:14 +08:00
|
|
|
}
|
|
|
|
|
2016-02-03 02:39:44 +08:00
|
|
|
func (ps *parser) next() rune {
|
|
|
|
if ps.pos == len(ps.src) {
|
2016-02-08 06:23:16 +08:00
|
|
|
ps.overEOF++
|
|
|
|
return eof
|
2016-01-21 06:57:18 +08:00
|
|
|
}
|
2016-02-03 02:39:44 +08:00
|
|
|
r, s := utf8.DecodeRuneInString(ps.src[ps.pos:])
|
2016-02-07 06:17:57 +08:00
|
|
|
if ps.currentCutset()[r] > 0 {
|
2016-02-08 06:23:16 +08:00
|
|
|
return eof
|
2016-02-07 06:17:57 +08:00
|
|
|
}
|
2016-02-03 02:39:44 +08:00
|
|
|
ps.pos += s
|
2016-01-21 06:57:18 +08:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2016-02-03 02:39:44 +08:00
|
|
|
func (ps *parser) backup() {
|
|
|
|
if ps.overEOF > 0 {
|
2016-02-08 06:23:16 +08:00
|
|
|
ps.overEOF--
|
2016-01-26 21:54:24 +08:00
|
|
|
return
|
|
|
|
}
|
2016-02-03 02:39:44 +08:00
|
|
|
_, s := utf8.DecodeLastRuneInString(ps.src[:ps.pos])
|
|
|
|
ps.pos -= s
|
2016-01-21 06:57:18 +08:00
|
|
|
}
|
2016-02-06 07:08:39 +08:00
|
|
|
|
2016-02-14 22:28:14 +08:00
|
|
|
func (ps *parser) advance(c int) {
|
|
|
|
ps.pos += c
|
|
|
|
if ps.pos > len(ps.src) {
|
|
|
|
ps.overEOF = ps.pos - len(ps.src)
|
|
|
|
ps.pos = len(ps.src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-08 08:08:39 +08:00
|
|
|
func (ps *parser) errorp(begin, end int, e error) {
|
2017-01-28 22:39:52 +08:00
|
|
|
ps.errors.Add(e.Error(), util.SourceContext{ps.srcName, ps.src, begin, end, nil})
|
2016-03-08 08:08:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *parser) error(e error) {
|
2016-10-11 20:26:42 +08:00
|
|
|
end := ps.pos
|
|
|
|
if end < len(ps.src) {
|
|
|
|
end++
|
|
|
|
}
|
|
|
|
ps.errorp(ps.pos, end, e)
|
2016-02-06 07:08:39 +08:00
|
|
|
}
|
2016-02-07 00:13:53 +08:00
|
|
|
|
2016-02-07 06:17:57 +08:00
|
|
|
func (ps *parser) pushCutset(rs ...rune) {
|
|
|
|
ps.cutsets = append(ps.cutsets, map[rune]int{})
|
|
|
|
ps.cut(rs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *parser) popCutset() {
|
|
|
|
n := len(ps.cutsets)
|
|
|
|
ps.cutsets[n-1] = nil
|
|
|
|
ps.cutsets = ps.cutsets[:n-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *parser) currentCutset() map[rune]int {
|
|
|
|
return ps.cutsets[len(ps.cutsets)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *parser) cut(rs ...rune) {
|
|
|
|
cutset := ps.currentCutset()
|
|
|
|
for _, r := range rs {
|
|
|
|
cutset[r]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *parser) uncut(rs ...rune) {
|
|
|
|
cutset := ps.currentCutset()
|
|
|
|
for _, r := range rs {
|
|
|
|
cutset[r]--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-07 00:13:53 +08:00
|
|
|
func newError(text string, shouldbe ...string) error {
|
|
|
|
if len(shouldbe) == 0 {
|
|
|
|
return errors.New(text)
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if len(text) > 0 {
|
|
|
|
buf.WriteString(text + ", ")
|
|
|
|
}
|
|
|
|
buf.WriteString("should be " + shouldbe[0])
|
|
|
|
for i, opt := range shouldbe[1:] {
|
|
|
|
if i == len(shouldbe)-2 {
|
|
|
|
buf.WriteString(" or ")
|
|
|
|
} else {
|
|
|
|
buf.WriteString(", ")
|
|
|
|
}
|
|
|
|
buf.WriteString(opt)
|
|
|
|
}
|
|
|
|
return errors.New(buf.String())
|
|
|
|
}
|