diag

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: BSD-2-Clause Imports: 5 Imported by: 5

Documentation

Overview

Package diag contains building blocks for formatting and processing diagnostic information.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Complain

func Complain(w io.Writer, msg string)

Complain prints a message to w in bold and red, adding a trailing newline.

func Complainf

func Complainf(w io.Writer, format string, args ...any)

Complainf is like Complain, but accepts a format string and arguments.

func PackCognateErrors added in v0.19.0

func PackCognateErrors(errs []*Error) error

PackCognateErrors combines multiple instances of Error with with the same Type and Context.Name into one:

  • If called with no errors, it returns nil.

  • If called with one error, it returns that error itself.

  • If called with more than one Error, it returns an error that combines all of them. The returned error also implements Shower, and its Error and Show methods avoid duplicating the type and context name of the constituent errors.

func ShowError

func ShowError(w io.Writer, err error)

ShowError shows an error. It uses the Show method if the error implements Shower, and uses Complain to print the error message otherwise.

Types

type Context

type Context struct {
	Name   string
	Source string
	Ranging
	// contains filtered or unexported fields
}

Context is a range of text in a source code. It is typically used for errors that can be associated with a part of the source code, like parse errors and a traceback entry.

Context values should only be constructed using NewContext.

func NewContext

func NewContext(name, source string, r Ranger) *Context

NewContext creates a new Context.

func (*Context) Show

func (c *Context) Show(indent string) string

Show shows a SourceContext.

func (*Context) ShowCompact

func (c *Context) ShowCompact(indent string) string

ShowCompact shows a Context, with no line break between the culprit range description and relevant source excerpt.

type Error

type Error struct {
	Type    string
	Message string
	Context Context
}

Error represents an error with context that can be showed.

func UnpackCognateErrors added in v0.19.0

func UnpackCognateErrors(err error) []*Error

UnpackCognateErrors returns the constituent Error instances in an error and if it is built from PackCognateErrors. Otherwise it returns nil.

func (*Error) Error

func (e *Error) Error() string

Error returns a plain text representation of the error.

func (*Error) Range

func (e *Error) Range() Ranging

Range returns the range of the error.

func (*Error) Show

func (e *Error) Show(indent string) string

Show shows the error.

type Ranger

type Ranger interface {
	// Range returns the range associated with the value.
	Range() Ranging
}

Ranger wraps the Range method.

type Ranging

type Ranging struct {
	From int
	To   int
}

Ranging represents a range [From, To) within an indexable sequence. Structs can embed Ranging to satisfy the Ranger interface.

Ideally, this type would be called Range. However, doing that means structs embedding this type will have Range as a field instead of a method, thus not implementing the Ranger interface.

func MixedRanging

func MixedRanging(a, b Ranger) Ranging

MixedRanging returns a Ranging from the start position of a to the end position of b.

func PointRanging

func PointRanging(p int) Ranging

PointRanging returns a zero-width Ranging at the given point.

func (Ranging) Range

func (r Ranging) Range() Ranging

Range returns the Ranging itself.

type Shower

type Shower interface {
	// Show takes an indentation string and shows.
	Show(indent string) string
}

Shower wraps the Show function.

Jump to

Keyboard shortcuts

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