2017-02-17 08:20:02 +08:00
|
|
|
package eval
|
|
|
|
|
2019-12-24 04:00:59 +08:00
|
|
|
import "github.com/elves/elvish/pkg/parse"
|
2017-02-17 08:20:02 +08:00
|
|
|
|
2018-01-01 01:54:26 +08:00
|
|
|
type errorpfer interface {
|
|
|
|
errorpf(begin, end int, fmt string, args ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// argsWalker is used by builtin special forms to implement argument parsing.
|
2017-02-17 08:20:02 +08:00
|
|
|
type argsWalker struct {
|
2018-01-01 01:54:26 +08:00
|
|
|
cp errorpfer
|
2017-02-17 08:20:02 +08:00
|
|
|
form *parse.Form
|
|
|
|
idx int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *compiler) walkArgs(f *parse.Form) *argsWalker {
|
|
|
|
return &argsWalker{cp, f, 0}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (aw *argsWalker) more() bool {
|
|
|
|
return aw.idx < len(aw.form.Args)
|
|
|
|
}
|
|
|
|
|
2017-02-18 13:25:42 +08:00
|
|
|
func (aw *argsWalker) peek() *parse.Compound {
|
2017-02-17 08:20:02 +08:00
|
|
|
if !aw.more() {
|
2018-10-13 21:07:54 +08:00
|
|
|
aw.cp.errorpf(aw.form.Range().To, aw.form.Range().To, "need more arguments")
|
2017-02-17 08:20:02 +08:00
|
|
|
}
|
2017-02-18 13:25:42 +08:00
|
|
|
return aw.form.Args[aw.idx]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (aw *argsWalker) next() *parse.Compound {
|
|
|
|
n := aw.peek()
|
2017-02-17 08:20:02 +08:00
|
|
|
aw.idx++
|
2017-02-18 13:25:42 +08:00
|
|
|
return n
|
2017-02-17 08:20:02 +08:00
|
|
|
}
|
|
|
|
|
2017-02-18 12:16:21 +08:00
|
|
|
// nextIs returns whether the next argument's source matches the given text. It
|
|
|
|
// also consumes the argument if it is.
|
|
|
|
func (aw *argsWalker) nextIs(text string) bool {
|
|
|
|
if aw.more() && aw.form.Args[aw.idx].SourceText() == text {
|
|
|
|
aw.idx++
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-02-18 13:25:42 +08:00
|
|
|
// nextMustLambda fetches the next argument, raising an error if it is not a
|
|
|
|
// lambda.
|
|
|
|
func (aw *argsWalker) nextMustLambda() *parse.Primary {
|
|
|
|
n := aw.next()
|
|
|
|
if len(n.Indexings) != 1 {
|
2018-10-13 21:07:54 +08:00
|
|
|
aw.cp.errorpf(n.Range().From, n.Range().To, "must be lambda")
|
2017-02-18 13:25:42 +08:00
|
|
|
}
|
|
|
|
if len(n.Indexings[0].Indicies) != 0 {
|
2018-10-13 21:07:54 +08:00
|
|
|
aw.cp.errorpf(n.Range().From, n.Range().To, "must be lambda")
|
2017-02-18 13:25:42 +08:00
|
|
|
}
|
|
|
|
pn := n.Indexings[0].Head
|
|
|
|
if pn.Type != parse.Lambda {
|
2018-10-13 21:07:54 +08:00
|
|
|
aw.cp.errorpf(n.Range().From, n.Range().To, "must be lambda")
|
2017-02-18 13:25:42 +08:00
|
|
|
}
|
|
|
|
return pn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (aw *argsWalker) nextMustLambdaIfAfter(leader string) *parse.Primary {
|
2017-02-18 12:16:21 +08:00
|
|
|
if aw.nextIs(leader) {
|
2017-02-18 13:25:42 +08:00
|
|
|
return aw.nextMustLambda()
|
2017-02-17 08:20:02 +08:00
|
|
|
}
|
2017-02-18 12:16:21 +08:00
|
|
|
return nil
|
2017-02-17 08:20:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (aw *argsWalker) mustEnd() {
|
|
|
|
if aw.more() {
|
2018-10-13 21:07:54 +08:00
|
|
|
aw.cp.errorpf(aw.form.Args[aw.idx].Range().From, aw.form.Range().To, "too many arguments")
|
2017-02-17 08:20:02 +08:00
|
|
|
}
|
|
|
|
}
|