parseutil

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2017 License: MIT Imports: 12 Imported by: 62

README

go-parse-utils

go-parse-utils is a collection of utilities for parsing code easily.

GoDoc Build Status codecov License Go Report Card

Install
go get gopkg.in/src-d/go-parse-utils.v1
Package AST

PackageAST retrieves the *ast.Package of a package in the given path.

pkg, err := parseutil.PackageAST("github.com/my/project")
Source code importer

The default importer.Importer of the Go standard library scans compiled objects, which can be painful to deal with in code generation, as it requires to go build before running go generate.

This packages provides an implementation of importer.Importer and importer.ImporterFrom that reads directly from source code if the package is in the GOPATH, otherwise (the stdlib, for example) falls back to the default importer in the standard library.

Features:

  • It is safe for concurrent use.
  • Caches packages after they are first imported.
importer := parseutil.NewImporter()
pkg, err := importer.Import("github.com/my/project")
License

MIT, see LICENSE

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultGoPath = GoPath(filepath.SplitList(build.Default.GOPATH))

DefaultGoPath contains the default list of go paths provided either via GOPATH environment variable or the default value.

View Source
var ErrNotInGoPath = fmt.Errorf("parseutil: package is not in any of the go paths")

ErrNotInGoPath is an error returned when a package is not in any of the possible go paths.

View Source
var ErrTooManyPackages = errors.New("more than one package found in a directory")

ErrTooManyPackages is returned when there is more than one package in a directory where there should only be one Go package.

Functions

func PackageAST

func PackageAST(path string) (pkg *ast.Package, err error)

PackageAST returns the AST of the package at the given path.

func PackageTestAST added in v1.1.1

func PackageTestAST(path string) (pkg *ast.Package, err error)

PackageTestAST returns the AST of the test package at the given path.

Types

type FileFilter

type FileFilter func(pkgPath, file string, typ FileType) bool

FileFilter returns true if the given file needs to be kept.

type FileFilters

type FileFilters []FileFilter

FileFilters represents a colection of FileFilter

func (FileFilters) Filter

func (fs FileFilters) Filter(pkgPath string, files []string, typ FileType) (filtered []string)

Filter returns the files passed in files that satisfy all FileFilters.

func (FileFilters) KeepFile

func (fs FileFilters) KeepFile(pkgPath, file string, typ FileType) bool

KeepFile returns true if and only if the file passes all FileFilters.

type FileType

type FileType string

FileType represents the type of go source file type.

const (
	GoFile  FileType = "go"
	CgoFile FileType = "cgo"
)

type GoPath added in v1.1.2

type GoPath []string

GoPath is the collection of all go paths.

func (GoPath) Abs added in v1.1.2

func (gp GoPath) Abs(pkg string) (string, error)

Abs returns the absolute path to a package. The go path in the absolute path that will be returned is the first that contains the given package.

func (GoPath) PathOf added in v1.1.2

func (gp GoPath) PathOf(pkg string) (string, error)

PathOf returns the first go path that contains the given package.

type Importer

type Importer struct {
	// contains filtered or unexported fields
}

Importer is an implementation of `types.Importer` and `types.ImporterFrom` that builds actual source files and not the compiled objects in the pkg directory. It is safe to use it concurrently. A package is cached after building it the first time.

func NewImporter

func NewImporter() *Importer

NewImporter creates a new Importer instance with the default importer of the runtime assigned as the underlying importer.

func (*Importer) GetSourceFiles added in v1.1.1

func (i *Importer) GetSourceFiles(path, srcDir string, filters FileFilters) (string, []string, error)

GetSourceFiles return the go files available in src under path after applying the filters.

func (*Importer) Import

func (i *Importer) Import(path string) (*types.Package, error)

Import returns the imported package for the given import path, or an error if the package couldn't be imported. Two calls to Import with the same path return the same package.

func (*Importer) ImportFrom

func (i *Importer) ImportFrom(path, srcDir string, mode types.ImportMode) (*types.Package, error)

ImportFrom returns the imported package for the given import path when imported by the package in srcDir, or an error if the package couldn't be imported. The mode value must be 0; it is reserved for future use. Two calls to ImportFrom with the same path and srcDir return the same package.

func (*Importer) ImportFromWithFilters

func (i *Importer) ImportFromWithFilters(path, srcDir string, mode types.ImportMode, filters FileFilters) (*types.Package, error)

ImportFromWithFilters works like ImportFrom but filters the source files using the passed FileFilters.

func (*Importer) ImportWithFilters

func (i *Importer) ImportWithFilters(path string, filters FileFilters) (*types.Package, error)

ImportWithFilters works like Import but filtering the source files to parse using the passed FileFilters.

func (*Importer) ParseSourceFiles added in v1.1.1

func (i *Importer) ParseSourceFiles(root string, paths []string) (*types.Package, error)

ParseSourceFiles parses the files in paths under root returning a types.Package and an optional error

Jump to

Keyboard shortcuts

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