2020-11-24 18:27:59 +00:00
|
|
|
/*
|
|
|
|
Serene --- Yet an other Lisp
|
|
|
|
|
|
|
|
Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package core
|
|
|
|
|
2020-12-16 18:57:54 +00:00
|
|
|
// Error implementations:
|
|
|
|
// * `IError` is the main interface to represent errors.
|
|
|
|
// * `Error` struct is an expression itself.
|
|
|
|
// * `IError` and any implementation of it has to implement `ILocatable`
|
|
|
|
// so we can point to the exact location of the error.
|
|
|
|
// * We have to use `IError` everywhere and avoid using Golangs errors
|
|
|
|
// since IError is an expression itself.
|
|
|
|
//
|
|
|
|
// TODOs:
|
|
|
|
// * Make errors stackable, so different pieces of code can stack related
|
|
|
|
// errors on top of each other so user can track them through the code
|
|
|
|
// * Errors should contain a help message as well to give some hints to the
|
|
|
|
// user about how to fix the problem. Something similar to Rust's error
|
|
|
|
// messages
|
|
|
|
// * Integrate the call stack with IError
|
|
|
|
|
2020-11-24 18:27:59 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"serene-lang.org/bootstrap/pkg/ast"
|
2021-01-01 19:13:49 +00:00
|
|
|
"serene-lang.org/bootstrap/pkg/errors"
|
2020-11-24 18:27:59 +00:00
|
|
|
)
|
|
|
|
|
2021-01-02 21:04:35 +00:00
|
|
|
type ErrType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
SyntaxError ErrType = iota
|
|
|
|
SemanticError
|
|
|
|
RuntimeError
|
|
|
|
)
|
|
|
|
|
2021-01-03 23:42:49 +00:00
|
|
|
func (e ErrType) String() string {
|
|
|
|
return [...]string{"Syntax Error", "Semantic Error", "Runtime Error"}[e]
|
|
|
|
}
|
|
|
|
|
2020-12-16 18:57:54 +00:00
|
|
|
// IError defines the necessary functionality of the internal errors.
|
2020-11-24 18:27:59 +00:00
|
|
|
type IError interface {
|
2020-12-16 18:57:54 +00:00
|
|
|
// In order to point to a specific point in the input
|
2020-11-24 18:27:59 +00:00
|
|
|
ast.ILocatable
|
2020-12-16 18:57:54 +00:00
|
|
|
|
|
|
|
// We want errors to be printable by the `print` family
|
2020-12-24 16:28:12 +00:00
|
|
|
IRepresentable
|
2020-11-24 18:27:59 +00:00
|
|
|
IDebuggable
|
2020-12-16 18:57:54 +00:00
|
|
|
|
2021-01-02 21:04:35 +00:00
|
|
|
GetErrType() ErrType
|
2021-01-01 19:13:49 +00:00
|
|
|
GetDescription() *string
|
2020-12-30 17:50:00 +00:00
|
|
|
GetStackTrace() *TraceBack
|
2020-12-16 18:57:54 +00:00
|
|
|
// To wrap Golan rrrrors
|
2020-12-05 11:13:49 +00:00
|
|
|
WithError(err error) IError
|
2020-12-16 18:57:54 +00:00
|
|
|
|
|
|
|
// Some errors might doesn't have any node available to them
|
|
|
|
// at the creation time. SetNode allows us to the the appropriate
|
|
|
|
// node later in time.
|
2020-12-12 20:28:32 +00:00
|
|
|
SetNode(n *Node)
|
2020-11-24 18:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Error struct {
|
|
|
|
Node
|
2021-01-02 21:04:35 +00:00
|
|
|
errtype ErrType
|
2021-01-01 19:13:49 +00:00
|
|
|
errno errors.Errno
|
2020-12-05 11:13:49 +00:00
|
|
|
WrappedErr error
|
|
|
|
msg string
|
2020-12-30 17:50:00 +00:00
|
|
|
trace *TraceBack
|
2020-11-24 18:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Error) String() string {
|
|
|
|
return e.msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Error) ToDebugStr() string {
|
2020-12-05 11:13:49 +00:00
|
|
|
_, isInternalErr := e.WrappedErr.(*Error)
|
|
|
|
if isInternalErr {
|
|
|
|
return fmt.Sprintf("%s:\n\t%s", e.msg, e.WrappedErr.(*Error).ToDebugStr())
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s:\n\t%s", e.msg, e.WrappedErr.Error())
|
|
|
|
}
|
|
|
|
|
2021-01-02 21:04:35 +00:00
|
|
|
func (e *Error) GetErrType() ErrType {
|
|
|
|
return e.errtype
|
|
|
|
}
|
|
|
|
|
2020-12-05 11:13:49 +00:00
|
|
|
func (e *Error) WithError(err error) IError {
|
|
|
|
e.WrappedErr = err
|
|
|
|
return e
|
2020-11-24 18:27:59 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 20:28:32 +00:00
|
|
|
func (e *Error) SetNode(n *Node) {
|
|
|
|
e.Node = *n
|
|
|
|
}
|
|
|
|
|
2020-12-05 10:07:04 +00:00
|
|
|
func (e *Error) Error() string {
|
|
|
|
return e.msg
|
|
|
|
}
|
|
|
|
|
2020-12-30 17:50:00 +00:00
|
|
|
func (e *Error) GetStackTrace() *TraceBack {
|
|
|
|
return e.trace
|
|
|
|
}
|
|
|
|
|
2021-01-01 19:13:49 +00:00
|
|
|
func (e *Error) GetDescription() *string {
|
|
|
|
desc, ok := errors.ErrorsDescription[e.errno]
|
|
|
|
if ok {
|
|
|
|
return &desc
|
|
|
|
}
|
|
|
|
|
|
|
|
desc = errors.ErrorsDescription[0]
|
|
|
|
return &desc
|
|
|
|
}
|
|
|
|
|
2020-12-12 20:28:32 +00:00
|
|
|
func MakePlainError(msg string) IError {
|
2020-11-24 18:27:59 +00:00
|
|
|
return &Error{
|
|
|
|
msg: msg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-30 17:50:00 +00:00
|
|
|
// MakeError creates an Error which points to the given IExpr `e` as
|
2020-12-16 18:57:54 +00:00
|
|
|
// the root of the error.
|
2020-12-30 17:50:00 +00:00
|
|
|
func MakeError(rt *Runtime, e IExpr, msg string) IError {
|
2021-01-02 21:04:35 +00:00
|
|
|
frame := MakeFrame(e, rt.Stack.GetCurrentFn(), 1)
|
|
|
|
trace := append(*rt.Stack.ToTraceBack(), frame)
|
2020-11-25 19:19:48 +00:00
|
|
|
|
2020-11-24 18:27:59 +00:00
|
|
|
return &Error{
|
2021-01-02 21:04:35 +00:00
|
|
|
Node: MakeNodeFromExpr(e),
|
|
|
|
errtype: RuntimeError,
|
|
|
|
msg: msg,
|
|
|
|
trace: &trace,
|
2020-11-24 18:27:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 21:04:35 +00:00
|
|
|
func MakeSyntaxErrorf(n Node, msg string, a ...interface{}) IError {
|
2020-11-24 18:27:59 +00:00
|
|
|
return &Error{
|
2021-01-02 21:04:35 +00:00
|
|
|
Node: n,
|
|
|
|
errtype: SyntaxError,
|
|
|
|
msg: fmt.Sprintf(msg, a...),
|
2020-11-24 18:27:59 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-01 19:13:49 +00:00
|
|
|
|
|
|
|
func MakeSemanticError(rt *Runtime, e IExpr, errno errors.Errno, msg string) IError {
|
|
|
|
currentFn := rt.Stack.GetCurrentFn()
|
|
|
|
|
|
|
|
frames := &[]*Frame{
|
|
|
|
MakeFrame(e, currentFn, 1),
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Error{
|
2021-01-02 21:04:35 +00:00
|
|
|
Node: MakeNodeFromExpr(e),
|
|
|
|
errtype: SemanticError,
|
|
|
|
errno: errno,
|
|
|
|
msg: msg,
|
|
|
|
trace: frames,
|
2021-01-01 19:13:49 +00:00
|
|
|
}
|
|
|
|
}
|