goq

package module
v0.0.0-...-ab6d2be Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2017 License: MIT Imports: 6 Imported by: 1

README

goq Go Documentation Travis

goq is a Go package to access nodes of Go's AST by queries.

See usage and example in GoDoc.

NOTE: This package is experimental and may make backward-incompatible changes.

Install

Use go get:

$ go get github.com/tenntenn/goq

Usage

All usage are described in GoDoc.

const src = `package main
func main() {
	n := 10
	println(n)
}`

func run() error {
	fset := token.NewFileSet()
	f, err := parser.ParseFile(fset, "main.go", src, 0)
	if err != nil {
		return err
	}

	config := &types.Config{
		Importer: importer.Default(),
	}

	info := &types.Info{
		Defs: map[*ast.Ident]types.Object{},
		Uses: map[*ast.Ident]types.Object{},
	}

	files := []*ast.File{f}
	if _, err := config.Check("main", fset, files, info); err != nil {
		return err
	}

	results := goq.New(fset, info, files).Query(&Int{})
	for _, r := range results {
		fmt.Println(r.Object, "at", fset.Pos(r.Node.Pos()))
	}

	return nil
}

func main() {
	if err := run(); err != nil {
		fmt.Fprintln(os.Stderr, "Error:", err)
		os.Exit(1)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSet

func NewSet(len *optional.Int) *optional.Set

func NewTupple

func NewTupple(len *optional.Int) *optional.Tuple

func NodePath

func NodePath(root, node ast.Node) []ast.Node

Types

type Array

type Array struct {
	// Elem is type of the elements.
	Elem Query
	// Len is length of the array.
	Len *optional.Int64
}

Array is a query for array objects.

func (*Array) Match

func (q *Array) Match(v interface{}) bool

Match implements Query.Match.

type AssignStmtNode

type AssignStmtNode struct {
	Lhs *optional.Set
	Tok *token.Token
	Rhs *optional.Set
}

func (*AssignStmtNode) Match

func (q *AssignStmtNode) Match(v interface{}) bool

type Bool

type Bool struct{}

Bool is a query for integer objects.

func (*Bool) Match

func (q *Bool) Match(v interface{}) bool

Match implements Query.Match.

type Chan

type Chan struct {
	// Elem is type of send or receive values.
	Elem Query
	// Dir is direction of the chan.
	Dir *optional.Int
}

Chan is an query for chan objects.

func (*Chan) Match

func (q *Chan) Match(v interface{}) bool

Match implements Query.Match.

type Complex

type Complex struct {
	// Size is size of the float value.
	Size *optional.Int
}

Complex is a query for float objects.

func (*Complex) Match

func (q *Complex) Match(v interface{}) bool

Match implements Query.Match.

type Float

type Float struct {
	// Size is size of the float value.
	Size *optional.Int
}

Float is a query for float objects.

func (*Float) Exec

func (q *Float) Exec(o types.Object) bool

Exec implements Query.Exec.

func (*Float) Match

func (q *Float) Match(v interface{}) bool

Match implements Query.Match.

type Func

type Func struct {
	// Name is name of the function.
	Name *pattern.Pattern
	// FullName is full name of the function.
	FullName *pattern.Pattern
	// Exported is whether the function is exported or not.
	Exported *optional.Bool
	// Signature is query of signature.
	Signature *Signature
}

Func is a query for function objects.

func (*Func) Match

func (q *Func) Match(v interface{}) bool

Exec implements Query.Exec.

type Goq

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

func New

func New(fset *token.FileSet, files []*ast.File, info *types.Info) *Goq

func (*Goq) Query

func (gq *Goq) Query(q Query) Results

type Int

type Int struct {
	// Size is size of the integer value.
	Size *optional.Int
	// Unsigned means whether the value is unsigned or not.
	Unsigned *optional.Bool
}

Int is a query for integer objects.

func (*Int) Match

func (q *Int) Match(v interface{}) bool

Match implements Query.Match.

type Interface

type Interface struct {
	Empty     *optional.Bool
	Methods   *optional.Set
	Embeddeds *optional.Set
}

func (*Interface) Match

func (q *Interface) Match(v interface{}) bool

Match implements Query.Match.

type Named

type Named struct {
	Underlying Query
	Methods    *optional.Tuple
	TypeName   *TypeName
}

func (*Named) Match

func (q *Named) Match(v interface{}) bool

Match implemets Query.Match.

type Node

type Node struct {
	Files []*ast.File
	Type  reflect.Type
	Path  []Query
}

func (*Node) Match

func (q *Node) Match(v interface{}) bool

type Package

type Package struct {
	Path    *pattern.Pattern
	Name    *pattern.Pattern
	Imports *optional.Set
}

func (*Package) Match

func (q *Package) Match(v interface{}) bool

Match implements Query.Match.

type Query

type Query interface {
	Match(v interface{}) bool
}

Query is an query to search objects.

func And

func And(qs ...Query) Query

And concats queries into a query which Match returns true when all queries Match return true.

func Not

func Not(q Query) Query

Not return a query which Match returns true if given query's Match return false.

func Or

func Or(qs ...Query) Query

Or concats queries into a query which Match returns false when all queries Match return false.

func QueryFunc

func QueryFunc(f func(v interface{}) bool) Query

QueryFunc returns a Query which uses f.

type Result

type Result struct {
	Goq          *Goq
	Node         ast.Node
	Object       types.Object
	TypeAndValue types.TypeAndValue
}

type Results

type Results []*Result

func (Results) Filter

func (rs Results) Filter(q Query) Results

type Signature

type Signature struct {
	// Recv is type of the receiver.
	Recv *Var
	// Params are parameters of the function.
	Params *optional.Tuple
	// Results are results of the function.
	Results *optional.Tuple
	// Variadic is whether the function uses variadic parameters or not.
	Variadic *optional.Bool
}

func (*Signature) Match

func (tm *Signature) Match(v interface{}) bool

Match implements Query.Match.

type Slice

type Slice struct {
	Elem Query
}

func (*Slice) Match

func (q *Slice) Match(v interface{}) bool

Match implements Query.Match.

type String

type String struct{}

String is a query for integer objects.

func (*String) Match

func (q *String) Match(v interface{}) bool

Match implements Query.Match.

type Struct

type Struct struct {
	NumFields *optional.Int
	Fields    []*Var
}

func (*Struct) Match

func (q *Struct) Match(v interface{}) bool

type Type

type Type struct {
	Identical  types.Type
	Implements types.Type
	Default    Query
	Underlying Query
}

func (*Type) Match

func (q *Type) Match(v interface{}) bool

Match implemets Query.Match.

type TypeName

type TypeName struct {
	Exported *optional.Bool
	Name     *pattern.Pattern
	Pkg      *Package
}

func (*TypeName) Match

func (q *TypeName) Match(v interface{}) bool

Match implements Query.Match

type Var

type Var struct {
	IsField   *optional.Bool
	Name      *pattern.Pattern
	Exported  *optional.Bool
	Anonymous *optional.Bool
	Type      Query
}

func (*Var) Match

func (q *Var) Match(v interface{}) bool

Match implements Query.Match.

Jump to

Keyboard shortcuts

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