-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add much better errors when compiling and expression fails
- Loading branch information
Showing
24 changed files
with
482 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package expressions | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
var ( | ||
ErrorUnterminated = errors.New("non-terminated statement in expression") | ||
ErrorEmptyStatement = errors.New("empty statement in expression") | ||
ErrorMissingFunction = errors.New("missing function") | ||
) | ||
|
||
type DetailedError struct { | ||
Err error | ||
Context string | ||
Index int | ||
} | ||
|
||
func (s *DetailedError) Error() string { | ||
return fmt.Sprintf("At `%s` (%d): %v", s.Context, s.Index, s.Err) | ||
} | ||
|
||
func (s *DetailedError) Unwrap() error { | ||
return s.Err | ||
} | ||
|
||
type CompilerErrors struct { | ||
Errors []*DetailedError | ||
Expression string | ||
} | ||
|
||
func (s *CompilerErrors) Error() string { | ||
if len(s.Errors) == 1 { | ||
return s.Errors[0].Error() | ||
} | ||
var sb strings.Builder | ||
sb.WriteString("Compiler Errors in: `") | ||
sb.WriteString(s.Expression) | ||
sb.WriteString("`\n") | ||
for _, e := range s.Errors { | ||
sb.WriteString(" ") | ||
sb.WriteString(e.Error()) | ||
sb.WriteString("\n") | ||
} | ||
return sb.String() | ||
} | ||
|
||
func (s *CompilerErrors) Unwrap() error { | ||
return s.Errors[0] | ||
} | ||
|
||
func (s *CompilerErrors) add(underlying error, context string, offset int) { | ||
s.Errors = append(s.Errors, &DetailedError{underlying, context, offset}) | ||
} | ||
|
||
func (s *CompilerErrors) empty() bool { | ||
return len(s.Errors) == 0 | ||
} | ||
|
||
// Inherit all errors from another compiler error set, and offset the index eg. if nested compile | ||
func (s *CompilerErrors) inherit(other *CompilerErrors, offset int) { | ||
for _, oe := range other.Errors { | ||
s.add(oe.Err, oe.Context, oe.Index+offset) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.