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 ¶
- func EndPosition(fset *token.FileSet, end token.Pos) (_ token.Position)
- func Line(f *token.File, pos token.Pos) int
- func Offset(f *token.File, pos token.Pos) (int, error)
- func Offsets(f *token.File, start, end token.Pos) (int, int, error)
- func Pos(f *token.File, offset int) (token.Pos, error)
- func Position(f *token.File, pos token.Pos) token.Position
- func StartPosition(fset *token.FileSet, start token.Pos) (_ token.Position)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EndPosition ¶ added in v0.12.0
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 Offset ¶
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 Pos ¶
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 ¶ added in v0.12.0
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 ¶ added in v0.12.0
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.