github.com/NeowayLabs/nash@v0.2.2-0.20200127205349-a227041ffd50/errors/error.go (about)

     1  package errors
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/madlambda/nash/ast"
     7  	"github.com/madlambda/nash/scanner"
     8  )
     9  
    10  type (
    11  	NashError struct {
    12  		reason string
    13  		format string
    14  	}
    15  
    16  	unfinished struct{}
    17  
    18  	unfinishedBlockError struct {
    19  		*NashError
    20  		unfinished
    21  	}
    22  
    23  	unfinishedListError struct {
    24  		*NashError
    25  		unfinished
    26  	}
    27  
    28  	unfinishedCmdError struct {
    29  		*NashError
    30  		unfinished
    31  	}
    32  )
    33  
    34  func NewError(format string, arg ...interface{}) *NashError {
    35  	e := &NashError{}
    36  	e.SetReason(format, arg...)
    37  	return e
    38  }
    39  
    40  func NewEvalError(path string, node ast.Node, format string, arg ...interface{}) *NashError {
    41  	linenum := fmt.Sprintf("%s:%d:%d: ", path, node.Line(), node.Column())
    42  	return NewError(linenum+format, arg...)
    43  }
    44  
    45  func (e *NashError) SetReason(format string, arg ...interface{}) {
    46  	e.reason = fmt.Sprintf(format, arg...)
    47  }
    48  
    49  func (e *NashError) Error() string { return e.reason }
    50  
    51  func (e unfinished) Unfinished() bool { return true }
    52  
    53  func NewUnfinishedBlockError(name string, it scanner.Token) error {
    54  	return &unfinishedBlockError{
    55  		NashError: NewError("%s:%d:%d: Statement's block '{' not finished",
    56  			name, it.Line(), it.Column()),
    57  	}
    58  }
    59  
    60  func NewUnfinishedListError(name string, it scanner.Token) error {
    61  	return &unfinishedListError{
    62  		NashError: NewError("%s:%d:%d: List assignment not finished. Found %v",
    63  			name, it.Line(), it.Column(), it),
    64  	}
    65  }
    66  
    67  func NewUnfinishedCmdError(name string, it scanner.Token) error {
    68  	return &unfinishedCmdError{
    69  		NashError: NewError("%s:%d:%d: Multi-line command not finished. Found %v but expect ')'",
    70  			name, it.Line(), it.Column(), it),
    71  	}
    72  }