safetoken

package
v0.16.1 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2024 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package safetoken provides wrappers around methods in go/token, that return errors rather than panicking.

It also provides a central place for workarounds in the underlying packages. The use of this package's functions instead of methods of token.File (such as Offset, Position, and PositionFor) is mandatory throughout the gopls codebase and enforced by a static check.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EndPosition

func EndPosition(fset *token.FileSet, end token.Pos) (_ token.Position)

EndPosition converts an end Pos in the FileSet into a Position.

Call this function only if pos represents the end of a non-empty interval, such as the result of Node.End().

func Line

func Line(f *token.File, pos token.Pos) int

Line returns the line number for the given offset in the given file.

func Offset

func Offset(f *token.File, pos token.Pos) (int, error)

Offset returns f.Offset(pos), but first checks that the file contains the pos.

The definition of "contains" here differs from that of token.File in order to work around a bug in the parser (issue #57490): during error recovery, the parser may create syntax nodes whose computed End position is 1 byte beyond EOF, which would cause token.File.Offset to panic. The workaround is that this function accepts a Pos that is exactly 1 byte beyond EOF and maps it to the EOF offset.

func Offsets

func Offsets(f *token.File, start, end token.Pos) (int, int, error)

Offsets returns Offset(start) and Offset(end).

func Pos

func Pos(f *token.File, offset int) (token.Pos, error)

Pos returns f.Pos(offset), but first checks that the offset is non-negative and not larger than the size of the file.

func Position

func Position(f *token.File, pos token.Pos) token.Position

Position returns the Position for the pos value in the given file.

p must be NoPos, a valid Pos in the range of f, or exactly 1 byte beyond the end of f. (See Offset for explanation.) Any other value causes a panic.

Line directives (//line comments) are ignored.

func StartPosition

func StartPosition(fset *token.FileSet, start token.Pos) (_ token.Position)

StartPosition converts a start Pos in the FileSet into a Position.

Call this function only if start represents the start of a token or parse tree, such as the result of Node.Pos(). If start is the end of an interval, such as Node.End(), call EndPosition instead, as it may need the correction described at Position.

Types

This section is empty.

Jump to

Keyboard shortcuts

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