bblfsh

package module
v2.4.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2018 License: Apache-2.0 Imports: 6 Imported by: 0

README

client-go GoDoc Build Status Build status codecov

Babelfish Go client library provides functionality to both connecting to the Babelfish server for parsing code (obtaining an UAST as a result) and for analysing UASTs with the functionality provided by libuast.

Installation

The recommended way to install client-go is:

go get -d -u gopkg.in/bblfsh/client-go.v2/...
cd $GOPATH/src/gopkg.in/bblfsh/client-go.v2
make dependencies

Windows build is supported, provided by you have make and curl in your %PATH%. It is also possible to link against custom libuast on Windows, read WINDOWS.md.

Example

This small example illustrates how to retrieve the UAST from a small Python script.

If you don't have a bblfsh server installed, please read the getting started guide, to learn more about how to use and deploy a bblfsh server.

Go to thequick start to discover how to run Babelfish with Docker.

client, err := bblfsh.NewClient("0.0.0.0:9432")
if err != nil {
    panic(err)
}

python := "import foo"

res, err := client.NewParseRequest().Language("python").Content(python).Do()
if err != nil {
    panic(err)
}

query := "//*[@roleImport]"
nodes, _ := tools.Filter(res.UAST, query)
for _, n := range nodes {
    fmt.Println(n)
}
Import {
.  Roles: Import,Declaration,Statement
.  StartPosition: {
.  .  Offset: 0
.  .  Line: 1
.  .  Col: 1
.  }
.  Properties: {
.  .  internalRole: body
.  }
.  Children: {
.  .  0: alias {
.  .  .  Roles: Import,Pathname,Identifier
.  .  .  TOKEN "foo"
.  .  .  Properties: {
.  .  .  .  asname: <nil>
.  .  .  .  internalRole: names
.  .  .  }
.  .  }
.  }
}

alias {
.  Roles: Import,Pathname,Identifier
.  TOKEN "foo"
.  Properties: {
.  .  asname: <nil>
.  .  internalRole: names
.  }
}

iter, err := tools.NewIterator(res.UAST)
if err != nil {
    panic(err)
}
defer iter.Dispose()

for node := range iter.Iterate() {
    fmt.Println(node)
}

// For XPath expressions returning a boolean/numeric/string value, you must
// use the right typed Filter function:

boolres, err := FilterBool(res.UAST, "boolean(//*[@strtOffset or @endOffset])")
strres, err := FilterString(res.UAST, "name(//*[1])")
numres, err := FilterNumber(res.UAST, "count(//*)")

Please read the Babelfish clients guide section to learn more about babelfish clients and their query language.

License

Apache License 2.0, see LICENSE

Documentation

Overview

Babelfish (https://doc.bblf.sh) Go client library provides functionality to both connect to the bblfsh daemon to parse code (obtaining an UAST as a result) and to analyse UASTs with the functionality provided by libuast.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	*grpc.ClientConn
	// contains filtered or unexported fields
}

Client holds the public client API to interact with the bblfsh daemon.

func NewClient

func NewClient(endpoint string) (*Client, error)

NewClient returns a new bblfsh client given a bblfshd endpoint.

func NewClientWithConnection

func NewClientWithConnection(conn *grpc.ClientConn) (*Client, error)

NewClientWithConnection returns a new bblfsh client given a grpc connection.

func (*Client) NewNativeParseRequest

func (c *Client) NewNativeParseRequest() *NativeParseRequest

NewNativeParseRequest is a parsing request to get the AST.

func (*Client) NewParseRequest

func (c *Client) NewParseRequest() *ParseRequest

NewParseRequest is a parsing request to get the UAST.

func (*Client) NewVersionRequest

func (c *Client) NewVersionRequest() *VersionRequest

NewVersionRequest is a parsing request to get the version of the server.

type NativeParseRequest

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

NativeParseRequest is a parsing request to get the AST.

func (*NativeParseRequest) Content

func (r *NativeParseRequest) Content(content string) *NativeParseRequest

Content sets the content of the parse request. It should be the source code that wants to be parsed.

func (*NativeParseRequest) Do

Do performs the actual parsing by serializing the request, sending it to bblfsd and waiting for the response.

func (*NativeParseRequest) DoWithContext

DoWithContext does the same as Do(), but sopporting cancellation by the use of Go contexts.

func (*NativeParseRequest) Encoding

func (r *NativeParseRequest) Encoding(encoding protocol.Encoding) *NativeParseRequest

Encoding sets the text encoding of the content.

func (*NativeParseRequest) Filename

func (r *NativeParseRequest) Filename(filename string) *NativeParseRequest

Filename sets the filename of the content.

func (*NativeParseRequest) Language

func (r *NativeParseRequest) Language(language string) *NativeParseRequest

Language sets the language of the given source file to parse. if missing will be guess from the filename and the content.

func (*NativeParseRequest) ReadFile

func (r *NativeParseRequest) ReadFile(filepath string) *NativeParseRequest

ReadFile loads a file given a local path and sets the content and the filename of the request.

type ParseRequest

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

ParseRequest is a parsing request to get the UAST.

func (*ParseRequest) Content

func (r *ParseRequest) Content(content string) *ParseRequest

Content sets the content of the parse request. It should be the source code that wants to be parsed.

func (*ParseRequest) Do

Do performs the actual parsing by serializing the request, sending it to bblfshd and waiting for the response.

func (*ParseRequest) DoWithContext

func (r *ParseRequest) DoWithContext(ctx context.Context) (*protocol.ParseResponse, error)

DoWithContext does the same as Do(), but sopporting cancellation by the use of Go contexts.

func (*ParseRequest) Encoding

func (r *ParseRequest) Encoding(encoding protocol.Encoding) *ParseRequest

Encoding sets the text encoding of the content.

func (*ParseRequest) Filename

func (r *ParseRequest) Filename(filename string) *ParseRequest

Filename sets the filename of the content.

func (*ParseRequest) Language

func (r *ParseRequest) Language(language string) *ParseRequest

Language sets the language of the given source file to parse. if missing will be guess from the filename and the content.

func (*ParseRequest) ReadFile

func (r *ParseRequest) ReadFile(filepath string) *ParseRequest

ReadFile loads a file given a local path and sets the content and the filename of the request.

type VersionRequest

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

VersionRequest is a request to retrieve the version of the server.

func (*VersionRequest) Do

Do performs the actual parsing by serializing the request, sending it to bblfsd and waiting for the response.

func (*VersionRequest) DoWithContext

func (r *VersionRequest) DoWithContext(ctx context.Context) (*protocol.VersionResponse, error)

DoWithContext does the same as Do(), but sopporting cancellation by the use of Go contexts.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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