Skip to content

Commit e0fca9f

Browse files
authored
Merge pull request bblfsh#111 from bzz/expose-parse-mode
v2: expose parse mode
2 parents 7e551cc + a708f28 commit e0fca9f

File tree

8 files changed

+72
-32
lines changed

8 files changed

+72
-32
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ jobs:
3939
- ./sbt assembly test
4040
after_failure: &failure_logs_anchor
4141
- docker logs bblfsh
42+
- ls hs_* 1> /dev/null 2>&1 && cat hs_*
4243

4344
- &release
4445
name: 'Cross-compile, release & publish to Sonatype'

README.md

+5-10
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,19 @@ API
7979
```scala
8080
import scala.io.Source
8181
import org.bblfsh.client.BblfshClient
82+
import gopkg.in.bblfsh.sdk.v2.protocol.driver.Mode
8283

83-
val client = BblfshClient("0.0.0.0", 9432)
84+
val client = BblfshClient("localhost", 9432)
8485

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

8990
// Full response
90-
println(resp.uast.get)
91+
println(resp.get)
9192

9293
// Filtered response
93-
println(client.filter(resp.uast.get, "//Import[@roleImport]"))
94-
95-
// Filtered responses using XPath functions returning types
96-
// other than NodeLists (Bool, Number, String):
97-
println(client.filterBool(resp.uast.get, "boolean(//*[@strtOffset or @endOffset])"))
98-
println(client.filterString(resp.uast.get, "name(//*[1])"))
99-
println(client.filterNumber(resp.uast.get, "count(//*)"))
94+
println(client.filter(resp.get, "//uast:Import"))
10095
```
10196

10297
Command line:

src/main/scala/org/bblfsh/client/v2/BblfshClient.scala

+14-5
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,22 @@ class BblfshClient(host: String, port: Int, maxMsgSize: Int) {
2929
* @param lang (optional) language to parse, default auto-detect \w enry
3030
* @param timeout (disabled) bblfsh request timeout, seconds
3131
* Right now this does not have any effect in v2.
32+
* @param mode (optional) mode to parse, default to bblfshd 'default' mode
3233
* @return UAST in parse response.
3334
*/
3435
def parseWithOptions(
3536
name: String,
3637
content: String,
3738
lang: String,
38-
timeout: Long
39+
timeout: Long,
40+
mode: Mode
3941
): ParseResponse = {
40-
// FIXME(bzz): make timout work in v2 again
42+
// TODO(#100): make timeout work in v2 again
4143
val req = ParseRequest(
4244
filename = name,
4345
content = content,
44-
language = lang
46+
language = lang,
47+
mode = mode
4548
)
4649
stub.parse(req)
4750
}
@@ -61,7 +64,13 @@ class BblfshClient(host: String, port: Int, maxMsgSize: Int) {
6164
name: String,
6265
content: String,
6366
lang: String = ""
64-
): ParseResponse = parseWithOptions(name, content, lang, DEFAULT_TIMEOUT_SEC)
67+
): ParseResponse = parseWithOptions(name, content, lang, DEFAULT_TIMEOUT_SEC, Mode.DEFAULT_MODE)
68+
69+
def parse(
70+
name: String,
71+
content: String,
72+
mode: Mode
73+
): ParseResponse = parseWithOptions(name, content, "", DEFAULT_TIMEOUT_SEC, mode)
6574

6675
/**
6776
* Parses the given file name and content,
@@ -80,7 +89,7 @@ class BblfshClient(host: String, port: Int, maxMsgSize: Int) {
8089
content: String,
8190
timeout: Long,
8291
lang: String = ""
83-
): ParseResponse = parseWithOptions(name, content, lang, timeout)
92+
): ParseResponse = parseWithOptions(name, content, lang, timeout, Mode.DEFAULT_MODE)
8493

8594
def supportedLanguages(): SupportedLanguagesResponse = {
8695
val req = SupportedLanguagesRequest()

src/main/scala/org/bblfsh/client/v2/ContextExt.scala

-4
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ case class Context(nativeContext: Long) {
3131
@native def root(): JNode
3232
@native def filter(query: String, node: JNode): UastIter
3333
@native def encode(n: JNode): ByteBuffer
34-
3534
@native def dispose()
36-
override def finalize(): Unit = {
37-
this.dispose()
38-
}
3935
}
4036
object Context {
4137
@native def create(): Long
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package org.bblfsh.client
22

33
/** Bblfsh client for protocol v2.
4-
* See https://github.com/bblfsh/sdk/blob/v3.1.0/protocol/driver.proto
5-
*
6-
* The main class to use is [[org.bblfsh.client.v2.BblfshClient]]
7-
*/
4+
* See https://github.com/bblfsh/sdk/blob/v3.1.0/protocol/driver.proto
5+
*
6+
* The main class to use is [[org.bblfsh.client.v2.BblfshClient]]
7+
*/
88
package object v2 {
9-
/** Key, Value representation of [[org.bblfsh.client.v2.JObject]] */
10-
type JField = (String, JNode)
9+
/** Key, Value representation of [[org.bblfsh.client.v2.JObject]] */
10+
type JField = (String, JNode)
1111
}

src/test/scala/org/bblfsh/client/v2/BblfshClientBaseTest.scala

+2
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ class BblfshClientBaseTest extends FlatSpec
2121

2222
override def afterAll {
2323
client.close()
24+
System.runFinalization()
25+
System.gc()
2426
}
2527
}

src/test/scala/org/bblfsh/client/v2/BblfshClientParseTest.scala

+39-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.bblfsh.client.v2
22

33
import java.nio.ByteBuffer
4+
5+
import gopkg.in.bblfsh.sdk.v2.protocol.driver.Mode
6+
47
import scala.io.Source
58

69
class BblfshClientParseTest extends BblfshClientBaseTest {
@@ -12,20 +15,54 @@ class BblfshClientParseTest extends BblfshClientBaseTest {
1215
assert(resp.errors.isEmpty)
1316
}
1417

18+
"Filtering UAST" should "work in Native mode" in {
19+
val fileContent = Source.fromFile(fileName).getLines.mkString("\n")
20+
val resp = client.parse(fileName, fileContent, Mode.NATIVE)
21+
val node = resp.get
22+
23+
val iter = BblfshClient.filter(node, "//SimpleName")
24+
iter.toList should have size (10) // number of Identifiers in the file
25+
iter.close()
26+
}
27+
28+
// TODO(#110) implement value type returns
29+
// "Filtering UAST" should "work for Value types" in {
30+
// val iter = BblfshClient.filterNumber(resp.get, "count(//*)")
31+
// iter.toList should have size (517) // total number of nodes (not the number of results which is 1)
32+
// }
33+
34+
"Filtering UAST" should "work in Annotated mode" in {
35+
val fileContent = Source.fromFile(fileName).getLines.mkString("\n")
36+
val resp = client.parse(fileName, fileContent, Mode.ANNOTATED)
37+
val node = resp.get
38+
39+
val iter = BblfshClient.filter(node, "//SimpleName[@role='Call']")
40+
iter.toList should have size (1) // number of function called in the file
41+
iter.close()
42+
}
43+
44+
"Filtering UAST" should "work in Semantic mode" in {
45+
val fileContent = Source.fromFile(fileName).getLines.mkString("\n")
46+
val resp = client.parse(fileName, fileContent, Mode.SEMANTIC)
47+
val node = resp.get
48+
49+
val iter = BblfshClient.filter(node, "//uast:Identifier[@role='Call']")
50+
iter.toList should have size (1) // number of function called in the file
51+
iter.close()
52+
}
53+
1554
"Decoded UAST after parsing" should "not be NULL" in {
1655
val uast = resp.uast.decode()
1756

1857
assert(uast != null)
1958
assert(uast.nativeContext != 0)
2059

21-
println(uast)
2260
uast.dispose()
2361
}
2462

2563
"Decoded UAST RootNode" should "not be NULL" in {
2664
val uast = resp.uast.decode()
2765
val rootNode: NodeExt = uast.root()
28-
println(rootNode.getClass)
2966

3067
rootNode should not be null
3168
rootNode.ctx should not be (0)
@@ -37,15 +74,11 @@ class BblfshClientParseTest extends BblfshClientBaseTest {
3774
"Encoding UAST to the same ContextExt" should "produce the same bytes" in {
3875
val uastCtx: ContextExt = resp.uast.decode()
3976
val rootNode: NodeExt = uastCtx.root()
40-
println(s"Root node: $rootNode")
4177

4278
val encodedBytes: ByteBuffer = uastCtx.encode(rootNode)
4379

4480
encodedBytes.capacity should be(resp.uast.asReadOnlyByteBuffer.capacity)
4581
encodedBytes shouldEqual resp.uast.asReadOnlyByteBuffer
46-
47-
println(resp.uast.asReadOnlyByteBuffer)
48-
println(encodedBytes)
4982
}
5083

5184
"Encoding java UAST to a new Context" should "produce the same bytes" in {
@@ -62,7 +95,6 @@ class BblfshClientParseTest extends BblfshClientBaseTest {
6295

6396

6497
"Encoding python UAST to a new Context" should "produce the same bytes" in {
65-
val client = BblfshClient("localhost", 9432)
6698
val fileName = "src/test/resources/python_file.py"
6799
val fileContent = Source.fromFile(fileName).getLines.mkString("\n")
68100
val resp = client.parse(fileName, fileContent)

src/test/scala/org/bblfsh/client/v2/FilterManagedTest.scala

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class FilterManagedTest extends FlatSpec
2727
ctx = Context()
2828
}
2929

30+
override def afterAll() = {
31+
System.runFinalization()
32+
System.gc()
33+
}
34+
3035
"XPath filter" should "find all positions under context" in {
3136
val it = ctx.filter("//file", managedRoot)
3237
it.hasNext() should be(true)

0 commit comments

Comments
 (0)