locerr: github.com/rhysd/locerr Index | Files | Directories

package locerr

import "github.com/rhysd/locerr"

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

Package Files

doc.go error.go position.go source.go

func SetColor Uses

func SetColor(enabled bool)

SetColor controls font should be colorful or not.

type Error Uses

type Error struct {
    Start    Pos
    End      Pos
    Messages []string
}

Error represents a compilation error with positional information and stacked messages.

func ErrorAt Uses

func ErrorAt(pos Pos, msg string) *Error

ErrorAt makes a new compilation error with the position.

func ErrorIn Uses

func ErrorIn(start, end Pos, msg string) *Error

ErrorIn makes a new compilation error with the range.

func Errorf Uses

func Errorf(format string, args ...interface{}) *Error

Errorf makes locerr.Error instance without source location information following given format.

func ErrorfAt Uses

func ErrorfAt(pos Pos, format string, args ...interface{}) *Error

ErrorfAt makes a new compilation error with the position and formatted message.

func ErrorfIn Uses

func ErrorfIn(start, end Pos, format string, args ...interface{}) *Error

ErrorfIn makes a new compilation error with the range and formatted message.

func NewError Uses

func NewError(msg string) *Error

NewError makes locerr.Error instance without source location information.

func Note Uses

func Note(err error, msg string) *Error

Note adds note to the given error. If given error is not locerr.Error, it's converted into locerr.Error.

func NoteAt Uses

func NoteAt(pos Pos, err error, msg string) *Error

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 Uses

func NoteIn(start, end Pos, err error, msg string) *Error

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 Uses

func Notef(err error, format string, args ...interface{}) *Error

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 Uses

func NotefAt(pos Pos, err error, format string, args ...interface{}) *Error

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 Uses

func NotefIn(start, end Pos, err error, format string, args ...interface{}) *Error

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 WithPos Uses

func WithPos(pos Pos, err error) *Error

WithPos adds positional information to the passed error.

func WithRange Uses

func WithRange(start, end Pos, err error) *Error

WithRange adds range information to the passed error.

func (*Error) At Uses

func (err *Error) At(pos Pos) *Error

At sets a position where error occurred.

func (*Error) Error Uses

func (err *Error) Error() string

Error builds error message for the error.

func (*Error) In Uses

func (err *Error) In(start, end Pos) *Error

In sets start and end positions of the error.

func (*Error) Note Uses

func (err *Error) Note(msg string) *Error

Note stacks the additional message upon current error.

func (*Error) NoteAt Uses

func (err *Error) NoteAt(pos Pos, msg string) *Error

NoteAt stacks the additional message upon current error with position.

func (*Error) Notef Uses

func (err *Error) Notef(format string, args ...interface{}) *Error

Notef stacks the additional formatted message upon current error.

func (*Error) NotefAt Uses

func (err *Error) NotefAt(pos Pos, format string, args ...interface{}) *Error

NotefAt stacks the additional formatted message upon current error with poisition.

func (*Error) PrintToFile Uses

func (err *Error) PrintToFile(f *os.File)

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 Uses

func (err *Error) WriteMessage(w io.Writer)

WriteMessage writes error message to the given writer

type Pos Uses

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.

func (Pos) String Uses

func (p Pos) String() string

String makes a string representation of the position. Format is 'file:line:column'.

type Source Uses

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 Uses

func NewDummySource(code string) *Source

NewDummySource make *Source with passed code. The source is actually does not exist in filesystem (so dummy). This is used for tests.

func NewSourceFromFile Uses

func NewSourceFromFile(file string) (*Source, error)

NewSourceFromFile make *Source object from file path.

func NewSourceFromStdin Uses

func NewSourceFromStdin() (*Source, error)

NewSourceFromStdin make *Source object from stdin. User will need to input source code into stdin.

func (*Source) BaseName Uses

func (src *Source) BaseName() string

BaseName makes a base name from the name of source. If the source does not exist in filesystem, its base name will be 'out'.

func (*Source) String Uses

func (src *Source) String() string

Directories

PathSynopsis
fuzz

Package locerr imports 9 packages (graph) and is imported by 9 packages. Updated 2017-09-04. Refresh now. Tools for package owners.