From 7efeb9c56fe6ff62b9fc9f7a106915aca2112b7a Mon Sep 17 00:00:00 2001 From: Qi Xiao Date: Fri, 19 Feb 2016 17:46:21 +0100 Subject: [PATCH] Remove ContextualError. --- edit/editor.go | 6 ++--- eval/eval.go | 4 +-- main.go | 2 -- util/contextual_error.go | 55 ---------------------------------------- util/pos_error.go | 26 ++++++++++++++++++- 5 files changed, 28 insertions(+), 65 deletions(-) delete mode 100644 util/contextual_error.go diff --git a/edit/editor.go b/edit/editor.go index 3df10629..cfcedeee 100644 --- a/edit/editor.go +++ b/edit/editor.go @@ -138,8 +138,8 @@ func (ed *Editor) refresh(fullRefresh bool, tips bool) error { if tips && !atEnd(err, len(src)) { ed.addTip("compiler error: %s", err) } - if err, ok := err.(*util.ContextualError); ok { - p := err.Pos() + if err, ok := err.(*util.PosError); ok { + p := err.Begin for i, token := range ed.tokens { if token.Node.Begin() <= p && p < token.Node.End() { ed.tokens[i].MoreStyle += styleForCompilerError @@ -160,8 +160,6 @@ func (ed *Editor) refresh(fullRefresh bool, tips bool) error { func atEnd(e error, n int) bool { switch e := e.(type) { - case *util.ContextualError: - return e.Pos() == n case *util.PosError: return e.Begin == n case *util.Errors: diff --git a/eval/eval.go b/eval/eval.go index 50eb2b2e..85e5d811 100644 --- a/eval/eval.go +++ b/eval/eval.go @@ -185,9 +185,7 @@ func (ec *EvalCtx) PCall(f Caller, args []Value) (ex error) { // errorf stops the ec.eval immediately by panicking with a diagnostic message. // The panic is supposed to be caught by ec.eval. func (ec *EvalCtx) errorf(p int, format string, args ...interface{}) { - throw(util.NewContextualError( - fmt.Sprintf("%s (%s)", ec.name, ec.context), "error", - ec.text, p, format, args...)) + throw(&util.PosError{p, p, fmt.Errorf(format, args...)}) } // SourceText evaluates a chunk of elvish source. diff --git a/main.go b/main.go index ac0eac78..ed2eb552 100644 --- a/main.go +++ b/main.go @@ -225,8 +225,6 @@ func printError(err error) { return } switch err := err.(type) { - case *util.ContextualError: - fmt.Print(err.Pprint()) case *util.Errors: for _, e := range err.Errors { printError(e) diff --git a/util/contextual_error.go b/util/contextual_error.go deleted file mode 100644 index 133ddd8d..00000000 --- a/util/contextual_error.go +++ /dev/null @@ -1,55 +0,0 @@ -package util - -import ( - "bytes" - "fmt" - "strings" -) - -// ContextualError is an error associated with a particular point in a source -// text. -type ContextualError struct { - srcname string - title string - pos int - line string - lineno int - colno int - msg string -} - -// NewContextualError creates a new ContextualError. -func NewContextualError(srcname, title, text string, pos int, format string, args ...interface{}) *ContextualError { - lineno, colno, line := FindContext(text, pos) - return &ContextualError{srcname, title, pos, line, lineno, colno, fmt.Sprintf(format, args...)} -} - -func (e *ContextualError) Pos() int { - return e.pos -} - -// Error implements the error interface. It returns a compact representation of -// the error. -func (e *ContextualError) Error() string { - return fmt.Sprintf("%s:%d:%d %s:%s", e.srcname, e.lineno, e.colno, e.title, e.msg) -} - -// Pprint pretty-prints a ContextualError with the error position, the affected -// line an arrow pointing to the point of error and the error message, with -// colors. -func (e *ContextualError) Pprint() string { - buf := new(bytes.Buffer) - // Position info - fmt.Fprintf(buf, "\033[1m%s:%d:%d: ", e.srcname, e.lineno+1, e.colno+1) - // "error:" - fmt.Fprintf(buf, "\033[31m%s: ", e.title) - // Message - fmt.Fprintf(buf, "\033[m\033[1m%s\033[m\n", e.msg) - // Context: line - // TODO Handle long lines - fmt.Fprintf(buf, "%s\n", e.line) - // Context: arrow - // TODO Handle multi-width characters - fmt.Fprintf(buf, "%s\033[32;1m^\033[m\n", strings.Repeat(" ", e.colno)) - return buf.String() -} diff --git a/util/pos_error.go b/util/pos_error.go index a3f6d76c..5d61b7d0 100644 --- a/util/pos_error.go +++ b/util/pos_error.go @@ -1,6 +1,10 @@ package util -import "fmt" +import ( + "bytes" + "fmt" + "strings" +) // PosError is an error associated with a position range. type PosError struct { @@ -12,3 +16,23 @@ type PosError struct { func (pe *PosError) Error() string { return fmt.Sprintf("%d-%d: %s", pe.Begin, pe.End, pe.Err.Error()) } + +// Pprint pretty-prints a PosError with a header indicating the source and type +// of the error, the error text and the affected line with an additional line +// that points an arrow at the affected column. +func (pe *PosError) Pprint(srcname, errtype, src string) string { + lineno, colno, line := FindContext(src, pe.Begin) + + buf := new(bytes.Buffer) + // Source and type of the error + fmt.Fprintf(buf, "\033[1m%s:%d:%d: \033[31m%s:", srcname, lineno+1, colno+1, errtype) + // Message + fmt.Fprintf(buf, "\033[m\033[1m%s\033[m\n", pe.Err.Error()) + // Affected line + // TODO Handle long lines + fmt.Fprintf(buf, "%s\n", line) + // Column indicator + // TODO Handle multi-width characters + fmt.Fprintf(buf, "%s\033[32;1m^\033[m\n", strings.Repeat(" ", colno)) + return buf.String() +}