parsego

package
v0.17.1 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: BSD-3-Clause Imports: 17 Imported by: 0

Documentation

Overview

The parsego package defines the File type, a wrapper around a go/ast.File that is useful for answering LSP queries. Notably, it bundles the *token.File and *protocol.Mapper necessary for token.Pos locations to and from UTF-16 LSP positions.

Run `go generate` to update resolver.go from GOROOT.

Index

Constants

View Source
const (
	// Header specifies that the main package declaration and imports are needed.
	// This is the mode used when attempting to examine the package graph structure.
	Header = parser.AllErrors | parser.ParseComments | parser.ImportsOnly | parser.SkipObjectResolution

	// Full specifies the full AST is needed.
	// This is used for files of direct interest where the entire contents must
	// be considered.
	Full = parser.AllErrors | parser.ParseComments | parser.SkipObjectResolution
)

Common parse modes; these should be reused wherever possible to increase cache hits.

Variables

This section is empty.

Functions

This section is empty.

Types

type File

type File struct {
	URI  protocol.DocumentURI
	Mode parser.Mode

	// File is the file resulting from parsing. It is always non-nil.
	//
	// Clients must not access the AST's legacy ast.Object-related
	// fields without first ensuring that [File.Resolve] was
	// already called.
	File *ast.File
	Tok  *token.File
	// Source code used to build the AST. It may be different from the
	// actual content of the file if we have fixed the AST.
	Src []byte

	Mapper   *protocol.Mapper // may map fixed Src, not file content
	ParseErr scanner.ErrorList
	// contains filtered or unexported fields
}

A File contains the results of parsing a Go file.

func Parse

func Parse(ctx context.Context, fset *token.FileSet, uri protocol.DocumentURI, src []byte, mode parser.Mode, purgeFuncBodies bool) (res *File, fixes []fixType)

Parse parses a buffer of Go source, repairing the tree if necessary.

The provided ctx is used only for logging.

func (*File) CheckNode added in v0.17.0

func (pgf *File) CheckNode(node ast.Node)

CheckNode asserts that the Node's positions are valid w.r.t. pgf.Tok.

func (*File) CheckPos added in v0.17.0

func (pgf *File) CheckPos(pos token.Pos)

CheckPos asserts that the position is valid w.r.t. pgf.Tok.

func (*File) Fixed

func (pgf *File) Fixed() bool

Fixed reports whether p was "Fixed", meaning that its source or positions may not correlate with the original file.

func (*File) NodeLocation

func (pgf *File) NodeLocation(node ast.Node) (protocol.Location, error)

NodeLocation returns a protocol Location for the ast.Node interval in this file.

func (*File) NodeMappedRange

func (pgf *File) NodeMappedRange(node ast.Node) (protocol.MappedRange, error)

NodeMappedRange returns a MappedRange for the ast.Node interval in this file. A MappedRange can be converted to any other form.

func (*File) NodeOffsets added in v0.17.0

func (pgf *File) NodeOffsets(node ast.Node) (start int, end int, _ error)

NodeOffsets returns offsets for the ast.Node.

func (*File) NodeRange

func (pgf *File) NodeRange(node ast.Node) (protocol.Range, error)

NodeRange returns a protocol Range for the ast.Node interval in this file.

func (*File) PosLocation

func (pgf *File) PosLocation(start, end token.Pos) (protocol.Location, error)

PosLocation returns a protocol Location for the token.Pos interval in this file.

func (*File) PosMappedRange

func (pgf *File) PosMappedRange(start, end token.Pos) (protocol.MappedRange, error)

PosMappedRange returns a MappedRange for the token.Pos interval in this file. A MappedRange can be converted to any other form.

func (*File) PosRange

func (pgf *File) PosRange(start, end token.Pos) (protocol.Range, error)

PosRange returns a protocol Range for the token.Pos interval in this file.

func (*File) PositionPos

func (pgf *File) PositionPos(p protocol.Position) (token.Pos, error)

PositionPos returns the token.Pos of protocol position p within the file.

func (*File) RangePos

func (pgf *File) RangePos(r protocol.Range) (token.Pos, token.Pos, error)

RangePos parses a protocol Range back into the go/token domain.

func (*File) Resolve added in v0.17.0

func (pgf *File) Resolve()

Resolve lazily resolves ast.Ident.Objects in the enclosed syntax tree.

Resolve must be called before accessing any of:

  • pgf.File.Scope
  • pgf.File.Unresolved
  • Ident.Obj, for any Ident in pgf.File

func (*File) String added in v0.16.0

func (pgf *File) String() string

Jump to

Keyboard shortcuts

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