bblfsh

package module
v2.8.9 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2018 License: Apache-2.0 Imports: 11 Imported by: 7

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 -u gopkg.in/bblfsh/client-go.v2/...

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

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 is the same as NewClientContext, but assumes a default timeout for the connection.

func NewClientContext added in v2.7.0

func NewClientContext(ctx context.Context, endpoint string) (*Client, error)

NewClientContext returns a new bblfsh client given a bblfshd endpoint.

func NewClientWithConnection added in v2.2.1

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) NewParseRequestV2 added in v2.7.0

func (c *Client) NewParseRequestV2() *ParseRequestV2

NewParseRequestV2 is a parsing request to get the UAST.

func (*Client) NewSupportedLanguagesRequest added in v2.5.0

func (c *Client) NewSupportedLanguagesRequest() *SupportedLanguagesRequest

NewSupportedLanguagesRequest is a parsing request to get the supported languages.

func (*Client) NewVersionRequest

func (c *Client) NewVersionRequest() *VersionRequest

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

type ErrPartialParse deprecated added in v2.7.0

type ErrPartialParse struct{}

ErrPartialParse is returned when driver was not able to parse the whole source file.

Deprecated: this type is unused

func (ErrPartialParse) Error added in v2.8.9

func (ErrPartialParse) Error() string

type FatalError added in v2.6.0

type FatalError []string

FatalError is returned when response is returned with Fatal status code.

func (FatalError) Error added in v2.6.0

func (e FatalError) Error() string

type Mode added in v2.8.1

type Mode = protocol2.Mode

Mode controls the level of transformation applied to UAST.

func ParseMode added in v2.8.6

func ParseMode(mode string) (Mode, error)

Parse mode parses a UAST mode string to an enum value.

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

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(fp 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) (*protocol1.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 protocol1.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) Mode added in v2.8.4

func (r *ParseRequest) Mode(mode Mode) *ParseRequest

Mode controls the level of transformation applied to UAST.

func (*ParseRequest) ReadFile

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

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

func (*ParseRequest) UAST added in v2.8.4

func (r *ParseRequest) UAST() (nodes.Node, string, error)

UAST is the same as UASTContext, but uses context.Background as a context.

func (*ParseRequest) UASTContext added in v2.8.4

func (r *ParseRequest) UASTContext(ctx context.Context) (nodes.Node, string, error)

UASTContext send the request and returns decoded UAST and the language. If a file contains syntax error, the ErrPartialParse is returned and will contain a partial AST.

type ParseRequestV2 added in v2.7.0

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

ParseRequestV2 is a parsing request to get the UAST.

func (*ParseRequestV2) Content added in v2.7.0

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

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

func (*ParseRequestV2) Do added in v2.7.0

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

func (*ParseRequestV2) DoContext added in v2.7.0

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

func (*ParseRequestV2) Filename added in v2.7.0

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

Filename sets the filename of the content.

func (*ParseRequestV2) Language added in v2.7.0

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

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

func (*ParseRequestV2) Mode added in v2.8.1

func (r *ParseRequestV2) Mode(mode Mode) *ParseRequestV2

Mode controls the level of transformation applied to UAST.

func (*ParseRequestV2) ReadFile added in v2.7.0

func (r *ParseRequestV2) ReadFile(fp string) *ParseRequestV2

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

func (*ParseRequestV2) UAST added in v2.7.0

func (r *ParseRequestV2) UAST() (nodes.Node, string, error)

UAST is the same as UASTContext, but uses context.Background as a context.

func (*ParseRequestV2) UASTContext added in v2.7.0

func (r *ParseRequestV2) UASTContext(ctx context.Context) (nodes.Node, string, error)

UASTContext send the request and returns decoded UAST and the language. If a file contains syntax error, the ErrPartialParse is returned and will contain a partial AST.

type SupportedLanguagesRequest added in v2.5.0

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

SupportedLanguagesRequest is a request to retrieve the supported languages.

func (*SupportedLanguagesRequest) Do added in v2.5.0

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

func (*SupportedLanguagesRequest) DoWithContext added in v2.5.0

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

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) (*protocol1.VersionResponse, error)

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

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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