pkg/eval: Inline the use of util.{Throw,Catch} in the compiler.

This commit is contained in:
Qi Xiao 2020-01-12 17:55:17 -05:00
parent c68de40a91
commit 4ef43e0bde
2 changed files with 16 additions and 5 deletions

View File

@ -10,9 +10,9 @@ func NewCompilationError(message string, context diag.Context) error {
Type: compilationErrorType, Message: message, Context: context}
}
// GetCompilationError returns a *diag.Error and true if the given error is a
// GetCompilationError returns a *diag.Error and true if the given value is a
// compilation error. Otherwise it returns nil and false.
func GetCompilationError(e error) (*diag.Error, bool) {
func GetCompilationError(e interface{}) (*diag.Error, bool) {
if e, ok := e.(*diag.Error); ok && e.Type == compilationErrorType {
return e, true
}

View File

@ -8,7 +8,6 @@ import (
"github.com/elves/elvish/pkg/diag"
"github.com/elves/elvish/pkg/parse"
"github.com/elves/elvish/pkg/util"
)
// compiler maintains the set of states needed when compiling a single source
@ -28,7 +27,18 @@ type compiler struct {
func compile(b, g staticNs, n *parse.Chunk, src *Source) (op Op, err error) {
cp := &compiler{b, []staticNs{g}, make(staticNs), 0, 0, src}
defer util.Catch(&err)
defer func() {
r := recover()
if r == nil {
return
} else if e, ok := GetCompilationError(r); ok {
// Save the compilation error and stop the panic.
err = e
} else {
// Resume the panic; it is not supposed to be handled here.
panic(r)
}
}()
return Op{cp.chunkOp(n), src}, nil
}
@ -37,7 +47,8 @@ func (cp *compiler) compiling(n parse.Node) {
}
func (cp *compiler) errorpf(begin, end int, format string, args ...interface{}) {
util.Throw(NewCompilationError(fmt.Sprintf(format, args...),
// The panic is caught by the recover in compile above.
panic(NewCompilationError(fmt.Sprintf(format, args...),
*diag.NewContext(cp.srcMeta.Name, cp.srcMeta.Code, begin, end)))
}