parseerror

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2026 License: BSD-3-Clause Imports: 4 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func LineTable(text string) []int

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.

func (*Child) Error

func (c *Child) Error() string

Error renders the child offset and indents nested child errors.

func (*Child) Unwrap

func (c *Child) Unwrap() error

Unwrap returns the child error.

type DefinitionKey

type DefinitionKey struct {
	Source string
	Name   string
}

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

type Error struct {
	Filename string
	LineNo   int
	LineOff  int
	Offset   int
	Text     string
	Err      error
}

Error associates an error with a byte offset in source text.

func New

func New(text string, offset int, err error) *Error

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

func NewAt(filename, text string, lineTable []int, offset int, err error) *Error

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.

func (*Error) Col

func (e *Error) Col() int

Col returns the one-based byte column of the error.

func (*Error) Error

func (e *Error) Error() string

Error formats the source location and wrapped error.

func (*Error) Line

func (e *Error) Line() string

Line returns the source line containing the error.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the error attached to the source location.

type Location

type Location struct {
	Filename  string
	LineTable []int
	Offset    int
	Text      string
}

Location identifies a byte offset in source text.

func (Location) Wrap

func (l Location) Wrap(err error) *Error

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL