2016-01-21 06:57:18 +08:00
|
|
|
package parse
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2017-12-04 08:35:43 +08:00
|
|
|
"fmt"
|
2018-10-13 23:02:19 +08:00
|
|
|
"reflect"
|
2016-01-21 06:57:18 +08:00
|
|
|
"strings"
|
|
|
|
"unicode/utf8"
|
2016-02-06 07:08:39 +08:00
|
|
|
|
2018-10-11 00:20:32 +08:00
|
|
|
"github.com/elves/elvish/diag"
|
2016-01-21 06:57:18 +08:00
|
|
|
)
|
|
|
|
|
2018-10-14 00:26:37 +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.
|
2018-10-14 00:26:37 +08:00
|
|
|
type parser struct {
|
2017-05-30 07:42:38 +08:00
|
|
|
srcName string
|
|
|
|
src string
|
|
|
|
pos int
|
|
|
|
overEOF int
|
2018-11-20 07:05:05 +08:00
|
|
|
errors MultiError
|
2016-01-21 06:57:18 +08:00
|
|
|
}
|
|
|
|
|
2018-10-14 00:26:37 +08:00
|
|
|
// newParser creates a new parser from a piece of source text and its name.
|
|
|
|
func newParser(srcname, src string) *parser {
|
2018-11-20 07:05:05 +08:00
|
|
|
return &parser{srcname, src, 0, 0, MultiError{}}
|
2017-05-30 08:10:21 +08:00
|
|
|
}
|
|
|
|
|
2018-10-14 00:26:37 +08:00
|
|
|
func (ps *parser) parse(n Node) parsed {
|
2018-10-13 23:02:19 +08:00
|
|
|
begin := ps.pos
|
2018-10-14 00:08:15 +08:00
|
|
|
n.setFrom(begin)
|
2018-10-13 23:02:19 +08:00
|
|
|
n.parse(ps)
|
2018-10-14 00:08:15 +08:00
|
|
|
n.setTo(ps.pos)
|
2018-10-13 23:02:19 +08:00
|
|
|
n.setSourceText(ps.src[begin:ps.pos])
|
|
|
|
return parsed{n}
|
|
|
|
}
|
|
|
|
|
|
|
|
var nodeType = reflect.TypeOf((*Node)(nil)).Elem()
|
|
|
|
|
|
|
|
type parsed struct {
|
|
|
|
n Node
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p parsed) addAs(ptr interface{}, parent Node) {
|
|
|
|
dst := reflect.ValueOf(ptr).Elem()
|
|
|
|
dst.Set(reflect.ValueOf(p.n)) // *ptr = p.n
|
|
|
|
addChild(parent, p.n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p parsed) addTo(ptr interface{}, parent Node) {
|
|
|
|
dst := reflect.ValueOf(ptr).Elem()
|
|
|
|
dst.Set(reflect.Append(dst, reflect.ValueOf(p.n))) // *ptr = append(*ptr, n)
|
|
|
|
addChild(parent, p.n)
|
|
|
|
}
|
|
|
|
|
2018-10-13 21:37:38 +08:00
|
|
|
// Tells the parser that parsing is done.
|
2018-10-14 00:26:37 +08:00
|
|
|
func (ps *parser) done() {
|
2017-05-30 08:10:21 +08:00
|
|
|
if ps.pos != len(ps.src) {
|
2017-12-04 08:35:43 +08:00
|
|
|
r, _ := utf8.DecodeRuneInString(ps.src[ps.pos:])
|
|
|
|
ps.error(fmt.Errorf("unexpected rune %q", r))
|
2017-05-30 08:10:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 21:37:38 +08:00
|
|
|
// Assembles all parsing errors as one, or returns nil if there were no errors.
|
2018-10-14 00:26:37 +08:00
|
|
|
func (ps *parser) assembleError() error {
|
2017-05-30 08:10:21 +08:00
|
|
|
if len(ps.errors.Entries) > 0 {
|
2018-11-20 07:05:05 +08:00
|
|
|
return ps.errors
|
2017-05-30 08:10:21 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-08 06:23:16 +08:00
|
|
|
const eof rune = -1
|
2016-01-21 06:57:18 +08:00
|
|
|
|
2018-10-14 00:26:37 +08:00
|
|
|
func (ps *parser) peek() rune {
|
2016-02-03 02:39:44 +08:00
|
|
|
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-01-21 06:57:18 +08:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2018-10-14 00:26:37 +08:00
|
|
|
func (ps *parser) hasPrefix(prefix string) bool {
|
2016-02-03 02:39:44 +08:00
|
|
|
return strings.HasPrefix(ps.src[ps.pos:], prefix)
|
2016-01-21 06:57:18 +08:00
|
|
|
}
|
|
|
|
|
2018-10-14 00:26:37 +08:00
|
|
|
func (ps *parser) next() rune {
|
2016-02-03 02:39:44 +08:00
|
|
|
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:])
|
|
|
|
ps.pos += s
|
2016-01-21 06:57:18 +08:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2018-10-14 00:26:37 +08:00
|
|
|
func (ps *parser) backup() {
|
2016-02-03 02:39:44 +08:00
|
|
|
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
|
|
|
|
2018-10-14 00:26:37 +08:00
|
|
|
func (ps *parser) errorp(begin, end int, e error) {
|
2018-10-14 00:17:18 +08:00
|
|
|
ps.errors.add(e.Error(), diag.NewSourceRange(ps.srcName, ps.src, begin, end))
|
2016-03-08 08:08:39 +08:00
|
|
|
}
|
|
|
|
|
2018-10-14 00:26:37 +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
|
|
|
|
|
|
|
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())
|
|
|
|
}
|