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.
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.
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.
Apache License 2.0, see LICENSE