Documentation
¶
Overview ¶
Package locerr is a small library to make an error with source code location information. It provides a struct to represent a source file, a specific position in code and an error related to specific range or position in source.
It's important to make a good error when compilation or execution errors found. locerr helps it. This library is actually used in some my compiler implementation.
Repository: https://github.com/rhysd/locerr
At first you should gain entire source as *Source instance.
code := `package main func main() { foo := 42 foo := true } ` src := locerr.NewDummySource(code)
You can get *Source instance from file (NewSourceFromFile) or stdin (NewSourceFromStdin) also.
Let's say to find an error at some range in the source.
start := locerr.Pos{ Offset: 41, Line: 6, Column: 2, File: src, } end := locerr.Pos{ Offset: 52, Line: 6, Column: 12, File: src, }
ErrorIn or other factory functions make a new error instance with the range. Error instance implements error interface so it can be handled like other error types.
err := locerr.ErrorIn(start, end, "Found duplicate symbol 'foo'")
Assume that you find additional information (location of variable and its type). Then you can add some notes to the error. Notes can be added by wrapping errors like pkg/errors library.
prev := locerr.Pos{ Offset: 26, Line: 4, Column: 1, File: src, } err = err.NoteAt(prev, "Defined here at first") err = err.NoteAt(prev, "Previously defined as int")
Finally you can see the result! err.Error() gets the error message as string. Note that this is only for non-Windows OS.
fmt.Println(err)
It should output following:
Error: Found duplicate symbol 'foo' (at <dummy>:6:1) Note: Defined here at first (at <dummy>:4:1) Note: Previously defined as int (at <dummy>:4:1) > foo := true
To support Windows, please use PrintToFile() method. It directly writes the error message into given file. This supports Windows and is useful to output from stdout or stderr.
err.PrintToFile(os.Stderr)
Labels such as 'Error:' or 'Notes:' are colorized. Main error message is emphasized with bold font. And source code location information (file name, line and column) is added with gray text. If the error has range information, the error shows code snippet which caused the error at the end of error message
Colorized output can be seen at https://github.com/rhysd/ss/blob/master/locerr/output.png?raw=true
If you have only one position information rather than two, 'start' position and 'end' position, ErrorAt() is available instead of ErrorIn() ErrorAt() takes one Pos instance.
err = ErrorAt(start, "Calling 'foo' with wrong number of argument")
In this case, line snippet is shown in error message. `pos.Line` is used to get line from source text.
fmt.Println(err)
It should output following:
Output: Error: Calling 'foo' with wrong number of argument (at <dummy>:6:7) > foo(true,
Index ¶
- func SetColor(enabled bool)
- type Error
- func ErrorAt(pos Pos, msg string) *Error
- func ErrorIn(start, end Pos, msg string) *Error
- func Errorf(format string, args ...interface{}) *Error
- func ErrorfAt(pos Pos, format string, args ...interface{}) *Error
- func ErrorfIn(start, end Pos, format string, args ...interface{}) *Error
- func NewError(msg string) *Error
- func Note(err error, msg string) *Error
- func NoteAt(pos Pos, err error, msg string) *Error
- func NoteIn(start, end Pos, err error, msg string) *Error
- func Notef(err error, format string, args ...interface{}) *Error
- func NotefAt(pos Pos, err error, format string, args ...interface{}) *Error
- func NotefIn(start, end Pos, err error, format string, args ...interface{}) *Error
- func WithPos(pos Pos, err error) *Error
- func WithRange(start, end Pos, err error) *Error
- func (err *Error) At(pos Pos) *Error
- func (err *Error) Error() string
- func (err *Error) In(start, end Pos) *Error
- func (err *Error) Note(msg string) *Error
- func (err *Error) NoteAt(pos Pos, msg string) *Error
- func (err *Error) Notef(format string, args ...interface{}) *Error
- func (err *Error) NotefAt(pos Pos, format string, args ...interface{}) *Error
- func (err *Error) PrintToFile(f *os.File)
- func (err *Error) WriteMessage(w io.Writer)
- type Pos
- type Source
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Error ¶
Error represents a compilation error with positional information and stacked messages.
func Errorf ¶
Errorf makes locerr.Error instance without source location information following given format.
func Note ¶
Note adds note to the given error. If given error is not locerr.Error, it's converted into locerr.Error.
func NoteAt ¶
NoteAt adds positional information and stack additional message to the original error. If given error is not locerr.Error, it's converted into locerr.Error.
func NoteIn ¶
NoteIn adds range information and stack additional message to the original error. If given error is not locerr.Error, it's converted into locerr.Error.
func Notef ¶
Notef adds note to the given error. Description will be created following given format and arguments. If given error is not locerr.Error, it's converted into locerr.Error.
func NotefAt ¶
NotefAt adds positional information and stack additional formatted message to the original error If given error is not locerr.Error, it's converted into locerr.Error.
func NotefIn ¶
NotefIn adds range information and stack additional formatted message to the original error. If given error is not locerr.Error, it's converted into locerr.Error.
func (*Error) NotefAt ¶
NotefAt stacks the additional formatted message upon current error with poisition.
func (*Error) PrintToFile ¶
PrintToFile prints error message to the given file. This is useful on Windows because Error() does not support colorful string on Windows.
func (*Error) WriteMessage ¶
WriteMessage writes error message to the given writer
type Pos ¶
type Pos struct { // Offset from the beginning of code. Offset int // Line number. Line int // Column number. Column int // File of this position. File *Source }
Pos represents some point in a source code.
type Source ¶
type Source struct { // Path of the file. <stdin> if it is stdin. <dummy> if it is a dummy source. Path string // Code contained in this source. Code []byte // Exists indicates this source exists in filesystem or not. Exists bool }
Source represents Dachs source code file. It may be a file on filesystem, stdin or dummy file.
func NewDummySource ¶
NewDummySource make *Source with passed code. The source is actually does not exist in filesystem (so dummy). This is used for tests.
func NewSourceFromFile ¶
NewSourceFromFile make *Source object from file path.
func NewSourceFromStdin ¶
NewSourceFromStdin make *Source object from stdin. User will need to input source code into stdin.