Skip to content

Commit

Permalink
Runtime error trace format (#111)
Browse files Browse the repository at this point in the history
* Runtime error format

* Parser and Compiler error formats updated
  • Loading branch information
geseq authored and d5 committed Feb 22, 2019
1 parent f265f17 commit 6dd573c
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 148 deletions.
10 changes: 3 additions & 7 deletions cmd/tengo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,27 +201,23 @@ func runREPL(in io.Reader, out io.Writer) {
srcFile := fileSet.AddFile("repl", -1, len(line))
file, err := parser.ParseFile(srcFile, []byte(line), nil)
if err != nil {
_, _ = fmt.Fprintf(out, "error: %s\n", err.Error())
_, _ = fmt.Fprintln(out, err.Error())
continue
}

file = addPrints(file)

c := compiler.NewCompiler(srcFile, symbolTable, constants, nil, nil)
if err := c.Compile(file); err != nil {
_, _ = fmt.Fprintf(out, "Compilation error:\n %s\n", err.Error())
_, _ = fmt.Fprintln(out, err.Error())
continue
}

bytecode := c.Bytecode()

machine := runtime.NewVM(bytecode, globals, nil)
if err != nil {
_, _ = fmt.Fprintf(out, "VM error:\n %s\n", err.Error())
continue
}
if err := machine.Run(); err != nil {
_, _ = fmt.Fprintf(out, "Execution error:\n %s\n", err.Error())
_, _ = fmt.Fprintln(out, err.Error())
continue
}

Expand Down
16 changes: 8 additions & 8 deletions compiler/compiler_error_report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package compiler_test
import "testing"

func TestCompilerErrorReport(t *testing.T) {
expectError(t, `import("user1")`, "test:1:1: module file read error: open user1.tengo: no such file or directory")
expectError(t, `import("user1")`, "Compile Error: module file read error: open user1.tengo: no such file or directory\n\tat test:1:1")

expectError(t, `a = 1`, "test:1:1: unresolved reference 'a'")
expectError(t, `a, b := 1, 2`, "test:1:1: tuple assignment not allowed")
expectError(t, `a = 1`, "Compile Error: unresolved reference 'a'\n\tat test:1:1")
expectError(t, `a, b := 1, 2`, "Compile Error: tuple assignment not allowed\n\tat test:1:1")
expectError(t, `a.b := 1`, "not allowed with selector")
expectError(t, `a:=1; a:=3`, "test:1:7: 'a' redeclared in this block")
expectError(t, `a:=1; a:=3`, "Compile Error: 'a' redeclared in this block\n\tat test:1:7")

expectError(t, `return 5`, "test:1:1: return not allowed outside function")
expectError(t, `func() { break }`, "test:1:10: break not allowed outside loop")
expectError(t, `func() { continue }`, "test:1:10: continue not allowed outside loop")
expectError(t, `func() { export 5 }`, "test:1:10: export not allowed inside function")
expectError(t, `return 5`, "Compile Error: return not allowed outside function\n\tat test:1:1")
expectError(t, `func() { break }`, "Compile Error: break not allowed outside loop\n\tat test:1:10")
expectError(t, `func() { continue }`, "Compile Error: continue not allowed outside loop\n\tat test:1:10")
expectError(t, `func() { export 5 }`, "Compile Error: export not allowed inside function\n\tat test:1:10")
}
2 changes: 1 addition & 1 deletion compiler/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ type Error struct {

func (e *Error) Error() string {
filePos := e.fileSet.Position(e.node.Pos())
return fmt.Sprintf("%s: %s", filePos, e.error.Error())
return fmt.Sprintf("Compile Error: %s\n\tat %s", e.error.Error(), filePos)
}
10 changes: 7 additions & 3 deletions compiler/parser/error.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package parser

import "github.com/d5/tengo/compiler/source"
import (
"fmt"

"github.com/d5/tengo/compiler/source"
)

// Error represents a parser error.
type Error struct {
Expand All @@ -10,8 +14,8 @@ type Error struct {

func (e Error) Error() string {
if e.Pos.Filename != "" || e.Pos.IsValid() {
return e.Pos.String() + ": " + e.Msg
return fmt.Sprintf("Parse Error: %s\n\tat %s", e.Msg, e.Pos)
}

return e.Msg
return fmt.Sprintf("Parse Error: %s", e.Msg)
}
2 changes: 1 addition & 1 deletion compiler/parser/error_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ func TestErrorList_Sort(t *testing.T) {
list.Add(source.FilePos{Offset: 30, Line: 3, Column: 10}, "error 3")
list.Add(source.FilePos{Offset: 10, Line: 1, Column: 10}, "error 1")
list.Sort()
assert.Equal(t, "1:10: error 1 (and 2 more errors)", list.Error())
assert.Equal(t, "Parse Error: error 1\n\tat 1:10 (and 2 more errors)", list.Error())
}
2 changes: 1 addition & 1 deletion compiler/parser/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import (

func TestError_Error(t *testing.T) {
err := &parser.Error{Pos: source.FilePos{Offset: 10, Line: 1, Column: 10}, Msg: "test"}
assert.Equal(t, "1:10: test", err.Error())
assert.Equal(t, "Parse Error: test\n\tat 1:10", err.Error())
}
Loading

0 comments on commit 6dd573c

Please sign in to comment.