Documentation
¶
Overview ¶
Package parseerror defines errors with byte-accurate source locations.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefinitionError ¶
func DefinitionError(locations DefinitionLocations, source, name string, err error) error
DefinitionError associates err with the source location of a definition.
func LineTable ¶
LineTable returns the byte offset of each line in text.
Example ¶
ExampleLineTable returns line starts for byte-accurate diagnostics.
package main
import (
"fmt"
"gitlab.com/efronlicht/rewrite/parseerror"
)
func main() {
fmt.Println(parseerror.LineTable("first\nsecond"))
}
Output: [0 6]
Types ¶
type Child ¶
type Child struct {
Offset int // relative to parent Error or Child
Err error // actual error. May `Unwrap` to reveal nested errors.
}
Child associates an error with an offset relative to its parent parse error.
type DefinitionKey ¶
DefinitionKey identifies a named definition in a source document.
type DefinitionLocations ¶
type DefinitionLocations = map[DefinitionKey]Location
DefinitionLocations maps definition keys to their source locations.
type Error ¶
Error associates an error with a byte offset in source text.
func New ¶
New associates err with offset in text. For example, New("bad", 1, err) points to the second byte.
Example ¶
ExampleNew attaches a useful location to an ordinary error.
package main
import (
"errors"
"fmt"
"gitlab.com/efronlicht/rewrite/parseerror"
)
func main() {
err := parseerror.New("bad value", 4, errors.New("invalid"))
fmt.Println("line:", err.Line())
fmt.Println("column:", err.Col())
}
Output: line: value column: 5
func NewAt ¶
NewAt associates err with a byte offset in named source text. lineTable should come from LineTable(text), so the error can print its line and column.
type Location ¶
Location identifies a byte offset in source text.
func (Location) Wrap ¶
Wrap associates err with l.
Example ¶
ExampleLocation_Wrap applies a saved source location to a new error.
package main
import (
"errors"
"fmt"
"gitlab.com/efronlicht/rewrite/parseerror"
)
func main() {
location := parseerror.Location{
Filename: "config.uf",
LineTable: parseerror.LineTable("flag -count int"),
Text: "flag -count int",
}
fmt.Println(location.Wrap(errors.New("invalid declaration")))
}
Output: config.uf:1:1 [0x0] invalid declaration