bblfsh

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2017 License: Apache-2.0 Imports: 12 Imported by: 0

README

client-go GoDoc Build Status codecov

Babelfish Go client library provides functionality to both connect to the Babelfish server to parse code (obtaining an UAST as a result) and to analyse UASTs with the functionality provided by libuast.

Installation

The recommended way to install client-go is:

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

Example

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

If you don't have a bblfsh server running you can execute it using the following command:

docker run --privileged --rm -it -p 9432:9432 --name bblfsh bblfsh/server

Please read the getting started guide, to learn more about how to use and deploy a bblfsh server.

client, err := bblfsh.NewBblfshClient("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)
}

fmt.Println(res.UAST)
Module {
.  Roles: File
.  Children: {
.  .  0: Import {
.  .  .  Roles: Statement,Declaration,Import
.  .  .  StartPosition: {
.  .  .  .  Offset: 0
.  .  .  .  Line: 1
.  .  .  .  Col: 1
.  .  .  }
.  .  .  Properties: {
.  .  .  .  internalRole: body
.  .  .  }
.  .  .  Children: {
.  .  .  .  0: alias {
.  .  .  .  .  Roles: Import,Pathname,Identifier
.  .  .  .  .  TOKEN "foo"
.  .  .  .  .  Properties: {
.  .  .  .  .  .  asname: <nil>
.  .  .  .  .  .  internalRole: names
.  .  .  .  .  }
.  .  .  .  }
.  .  .  }
.  .  }
.  }
}

License

Apache License 2.0, see LICENSE

Documentation

Overview

Babelfish (https://doc.bblf.sh) Go client library provides functionality to both connect to the Babelfish server 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

func Filter

func Filter(node *uast.Node, xpath string) ([]*uast.Node, error)

Filter takes a `*uast.Node` and a xpath query and filters the tree, returning the list of nodes that satisfy the given query. Filter is thread-safe but not current by an internal global lock.

Types

type BblfshClient

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

BblfshClient holds the public client API to interact with the babelfish server.

func NewBblfshClient

func NewBblfshClient(endpoint string) (*BblfshClient, error)

NewBblfshClient returns a new babelfish client given a server endpoint

func (*BblfshClient) NewParseRequest

func (c *BblfshClient) NewParseRequest() *ParseRequest

type ParseRequest

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

ParseRequest is a placeholder for the parse requests performed by the library

func (*ParseRequest) Content

func (req *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

func (req *ParseRequest) Do() (*protocol.ParseResponse, error)

Do performs the actual parsing by serializaing the request, sending it to the babelfish and waiting for the response (UAST tree).

func (*ParseRequest) DoWithContext

func (req *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 (req *ParseRequest) Encoding(encoding protocol.Encoding) *ParseRequest

Encoding sets the text encoding of the content.

func (*ParseRequest) Filename

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

Filename sets the filename of the content.

func (*ParseRequest) Language

func (req *ParseRequest) Language(lan string) *ParseRequest

Language sets the language of the given source file to parse.

func (*ParseRequest) ReadFile

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

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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