position

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package position implements a struct that describes where a sequence of characters is located in a file.

Index

Constants

This section is empty.

Variables

View Source
var Default = New(0, 1, 1)
View Source
var DefaultLocation = NewLocation("<main>", DefaultSpan)
View Source
var Zero = New(0, 0, 0)
View Source
var ZeroLocation = NewLocation("", ZeroSpan)

Functions

This section is empty.

Types

type Interface

type Interface interface {
	Pos() *Position
	SetPos(*Position)
}

Represents something that can be positioned.

type Location

type Location struct {
	*Span
	FilePath string
	Parent   *Location
}

Describes an arbitrary source position in a particular file. Lines and columns must be > 0.

func JoinLocationOfCollection

func JoinLocationOfCollection[Element LocationInterface](collection []Element) *Location

Join the position of the first element of a collection with the last one.

func JoinLocationOfLastElement

func JoinLocationOfLastElement[Element LocationInterface](left *Location, rightCollection []Element) *Location

Joins the given position with the last element of the given collection.

func LocationOfLastElement

func LocationOfLastElement[Element LocationInterface](collection []Element) *Location

Retrieve the location of the last element of a collection.

func NewLocation

func NewLocation(filename string, span *Span) *Location

Create a new location with a given position.

func NewLocationWithParent

func NewLocationWithParent(filename string, span *Span, parent *Location) *Location

Create a new location with a given position.

func SpliceLocation

func SpliceLocation(target, current *Location, unqoute bool) *Location

func (*Location) Copy

func (l *Location) Copy() *Location

func (*Location) Equal

func (l *Location) Equal(other *Location) bool

func (*Location) HumanString

func (l *Location) HumanString(style bool, colorizer colorizer.Colorizer, accentColor *color.Color) (string, error)

Return a string representation of this error that can be presented to humans.

Reads the content of the file using the OS.

func (*Location) HumanStringWithSource

func (l *Location) HumanStringWithSource(source string, style bool, colorizer colorizer.Colorizer, accentColor *color.Color) (string, error)

Return a string representation of this error that can be presented to humans.

Uses the given source string instead of reading the content of the file.

func (*Location) HumanStringWithSourceMap

func (l *Location) HumanStringWithSourceMap(style bool, colorizer colorizer.Colorizer, accentColor *color.Color, sourceMap map[string]string) (string, error)

Return a string representation of this error that can be presented to humans.

Tries to fetch the source from the provided map (keys are filepaths, values are source strings). If there is no entry for the required filepath it tries to read the file through the OS.

func (*Location) Join

func (left *Location) Join(right *Location) *Location

Join two locations into one. Works properly when the receiver is nil or the argument is nil.

func (*Location) JoinSpan

func (left *Location) JoinSpan(right *Span) *Location

Join two locations into one. Works properly when the receiver is nil or the argument is nil.

func (*Location) RelFilename

func (l *Location) RelFilename() string

Returns a path to the file that is relative to the current working directory. if it's impossible to get the working directory or the file path cannot be transformed into a relative one, the original file path is returned instead.

func (*Location) String

func (l *Location) String() string

String representation of the location.

type LocationInterface

type LocationInterface interface {
	Location() *Location
	SetLocation(*Location)
}

Represents something that contains a location.

type Position

type Position struct {
	ByteOffset int // Index of the first byte of the source code fragment
	Line       int // Source line number where the fragment starts
	Column     int // Source column number where the fragment starts
}

Position describes an arbitrary source code position. Lines and columns must be > 0.

func New

func New(byteOffset, line, column int) *Position

Create a new source position struct.

func (*Position) Equal

func (p *Position) Equal(other *Position) bool

func (*Position) Pos

func (p *Position) Pos() *Position

Retrieve the position, used in interfaces.

func (*Position) SetPos

func (p *Position) SetPos(pos *Position)

func (*Position) String

func (p *Position) String() string

String returns a string formatted like that:

line:column

func (*Position) Valid

func (p *Position) Valid() bool

Check if the position is valid.

type Span

type Span struct {
	StartPos *Position
	EndPos   *Position
}

Represents a span of text in a string/file.

func JoinSpanOfCollection

func JoinSpanOfCollection[Element SpanInterface](collection []Element) *Span

Join the position of the first element of a collection with the last one.

func JoinSpanOfLastElement

func JoinSpanOfLastElement[Element SpanInterface](left *Span, rightCollection []Element) *Span

Joins the given position with the last element of the given collection.

func NewSpan

func NewSpan(startPos, endPos *Position) *Span

Create a new Span.

func NewSpanFromPosition

func NewSpanFromPosition(pos *Position) *Span

Create a new span from a single position.

func SpanOfLastElement

func SpanOfLastElement[Element SpanInterface](collection []Element) *Span

Retrieve the position of the last element of a collection.

func (*Span) Equal

func (s *Span) Equal(other *Span) bool

func (*Span) Join

func (left *Span) Join(right *Span) *Span

Join two spans into one. Works properly when the receiver is nil or the argument is nil.

func (*Span) SetSpan

func (s *Span) SetSpan(other *Span)

func (*Span) Span

func (s *Span) Span() *Span

type SpanInterface

type SpanInterface interface {
	Span() *Span
	SetSpan(*Span)
}

Represents something that contains a span.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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