ncordon / scala-client

Babelfish Scala client

Home Page:https://doc.bblf.sh/using-babelfish/clients.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Babelfish Scala client Build Status

This is a Scala/JNI implementation of the Babelfish client. It uses ScalaPB for Protobuf/gRPC code generation and libuast for XPath queries.

Status

The latest scala-client v2.x supports the UASTv2 protocol.

Installation

Building from sources

git clone https://github.com/bblfsh/scala-client.git
cd scala-client
./sbt assembly

gRPC/protobuf files are re-generate from src/main/proto on every ./sbt compile and are stored under ./target/src_managed/.

The jar file and the native module are generated in the build/ directory. If you move the jar file to some other path, the native (.so or .dylib) library must be in the same path.

If the build fails because it can't find the jni.h header file, run it with:

./sbt -java-home /usr/lib/jvm/java-8-openjdk-amd64 assembly

Changing the JDK directory to the one right for your system.

For more developer documentation please check our CONTRIBUTING guideline.

Apache Maven

The bblfsh-client package is available thorugh Maven central, so it can be easily added as a dependency in various package management systems. Examples of how to handle it for most common systems are included below; for other systems just look at Maven central's dependency information.

<dependency>
    <groupId>org.bblfsh</groupId>
    <artifactId>bblfsh-client</artifactId>
    <version>${version}</version>
</dependency>

Scala sbt

libraryDependencies += "org.bblfsh" % "bblfsh-client" % version

Dependencies

You need to install the Java SDK and its header files. The command for Debian and derived distributions would be:

sudo apt install openjdk-8 openjdk-8-jdk-headless

Usage

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/bblfshd

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

API

import scala.io.Source
import org.bblfsh.client.v2.BblfshClient, BblfshClient._
import gopkg.in.bblfsh.sdk.v2.protocol.driver.Mode

val client = BblfshClient("localhost", 9432)

val filename = "/path/to/file.py" // client responsible for encoding it to utf-8
val fileContent = Source.fromFile(filename).getLines.mkString("\n")
val resp = client.parse(filename, fileContent, Mode.SEMANTIC)

// Full response
println(resp.get)

// Filtered response
println(client.filter(resp.get, "//uast:Identifier"))

Command line:

java -jar build/bblfsh-client-assembly-*.jar -f <file.py>

or if you want to use a XPath query:

java -jar build/bblfsh-client-assembly-*.jar -f <file.py> -q "//uast:Identifier"

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

License

Apache 2.0

About

Babelfish Scala client

https://doc.bblf.sh/using-babelfish/clients.html

License:Apache License 2.0


Languages

Language:Scala 45.4%Language:C++ 36.5%Language:Shell 18.1%