Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit d8e2c8f

Browse files
committed
Makes TreeOrder and UastFormat types with implicits
This makes possible, as explained in package.scala file, to use a method f(fmt: UastFormat) as f(0) or f(1) and a method g(fmt: Int) as g(UastBinary) or g(UastYaml) Signed-off-by: ncordon <[email protected]>
1 parent 03e71bc commit d8e2c8f

8 files changed

+105
-60
lines changed

src/main/native/org_bblfsh_client_v2_Context.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/native/org_bblfsh_client_v2_ContextExt.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/native/org_bblfsh_client_v2_libuast_Libuast.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_Context_filter(
824824
return iter;
825825
}
826826

827-
JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_Context_encode(
827+
JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_Context_nativeEncode(
828828
JNIEnv *env, jobject self, jobject jnode, jint fmt) {
829829
UastFormat format = (UastFormat) fmt; // TODO(#107): make it argument
830830

@@ -864,7 +864,7 @@ JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_ContextExt_filter(
864864
return filterUastIterExt(ctx, self, jquery, env);
865865
}
866866

867-
JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_ContextExt_encode(
867+
JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_ContextExt_nativeEncode(
868868
JNIEnv *env, jobject self, jobject node, jint fmt) {
869869
UastFormat format = (UastFormat) fmt;
870870

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

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,25 @@ object BblfshClient {
110110
val DEFAULT_MAX_MSG_SIZE = 100 * 1024 * 1024 // bytes
111111

112112
private val libuast = new Libuast
113-
private val treeOrder = libuast.getTreeOrders
114-
private val uastFormat = libuast.getUastFormats
113+
private val orders = libuast.getTreeOrders
114+
private val formats = libuast.getUastFormats
115115

116-
// Lift tree order as constants
117-
val AnyOrder = treeOrder.AnyOrder
118-
val PreOrder = treeOrder.PreOrder
119-
val PostOrder = treeOrder.PostOrder
120-
val LevelOrder = treeOrder.LevelOrder
121-
val ChildrenOrder = treeOrder.ChildrenOrder
122-
val PositionOrder = treeOrder.PositionOrder
123-
124-
// Lift UAST decoding / encoding formats as constants
125-
val uastBinary = uastFormat.UastBinary
126-
val uastYaml = uastFormat.UastYaml
116+
abstract class UastFormat(val toInt: Int)
117+
abstract class TreeOrder(val toInt: Int)
127118

119+
// Lift tree order as constants
120+
case object UastBinary extends UastFormat(formats.uastBinary)
121+
case object UastYaml extends UastFormat(formats.uastYaml)
122+
123+
// Lift orders from libuast as types
124+
case object AnyOrder extends TreeOrder(orders.anyOrder)
125+
case object PreOrder extends TreeOrder(orders.preOrder)
126+
case object PostOrder extends TreeOrder(orders.postOrder)
127+
case object LevelOrder extends TreeOrder(orders.levelOrder)
128+
case object ChildrenOrder extends TreeOrder(orders.childrenOrder)
129+
case object PositionOrder extends TreeOrder(orders.positionOrder)
130+
131+
/** Creates a BblfshClient with default parameters */
128132
def apply(
129133
host: String, port: Int,
130134
maxMsgSize: Int = DEFAULT_MAX_MSG_SIZE
@@ -137,7 +141,7 @@ object BblfshClient {
137141
*
138142
* Since v2.
139143
*/
140-
def decode(buf: ByteBuffer, fmt: Int): ContextExt = Libuast.synchronized {
144+
def decode(buf: ByteBuffer, fmt: UastFormat): ContextExt = Libuast.synchronized {
141145
if (!buf.isDirect()) {
142146
throw new RuntimeException("Only directly-allocated buffer decoding is supported.")
143147
}
@@ -151,7 +155,7 @@ object BblfshClient {
151155
* Since v2.
152156
*/
153157
def decode(buf: ByteBuffer): ContextExt = Libuast.synchronized {
154-
decode(buf, uastBinary)
158+
decode(buf, UastBinary)
155159
}
156160

157161
/** Enables API: resp.uast.decode() */
@@ -162,7 +166,7 @@ object BblfshClient {
162166
* Always copies memory to a new buffer in Direct mode,
163167
* to be able to pass it to JNI.
164168
*/
165-
def decode(fmt: Int): ContextExt = {
169+
def decode(fmt: UastFormat): ContextExt = {
166170
val bufDirectCopy = ByteBuffer.allocateDirect(buf.size)
167171
buf.copyTo(bufDirectCopy)
168172
val result = BblfshClient.decode(bufDirectCopy, fmt)
@@ -181,14 +185,14 @@ object BblfshClient {
181185
* Decodes in binary format
182186
*/
183187
def decode(): ContextExt = {
184-
decode(uastBinary)
188+
decode(UastBinary)
185189
}
186190
}
187191

188192
/** Enables API: resp.get() */
189193
implicit class ResponseMethods(val resp: ParseResponse) {
190194
/** Gets the root decoding the tree in binary format */
191-
def get(fmt: Int): JNode = {
195+
def get(fmt: UastFormat): JNode = {
192196
val ctx = resp.uast.decode(fmt)
193197
val node = ctx.root().load()
194198
ctx.dispose()
@@ -197,7 +201,7 @@ object BblfshClient {
197201

198202
/** Gets the root node decoding the tree in binary format */
199203
def get(): JNode = {
200-
get(uastBinary)
204+
get(UastBinary)
201205
}
202206
}
203207

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,49 @@ import org.bblfsh.client.v2.libuast.Libuast.{UastIter, UastIterExt}
1010
* This is equivalent of pyuast.ContextExt API
1111
*/
1212
case class ContextExt(nativeContext: Long) {
13-
import BblfshClient.uastBinary
13+
import BblfshClient.{UastFormat, UastBinary}
14+
1415
// @native def load(): JNode // TODO(bzz): clarify when it's needed VS just .root().load()
1516
@native def root(): NodeExt
1617
@native def filter(query: String): UastIterExt
18+
@native def nativeEncode(n: NodeExt, fmt: Int): ByteBuffer
19+
def encode(n: NodeExt, fmt: UastFormat): ByteBuffer = {
20+
nativeEncode(n, fmt)
21+
}
1722
// encode using binary format
18-
@native def encode(n: NodeExt, fmt: Int): ByteBuffer
1923
def encode(n: NodeExt): ByteBuffer = {
20-
encode(n, uastBinary)
24+
encode(n, UastBinary)
2125
}
2226
@native def dispose()
2327
override def finalize(): Unit = {
2428
this.dispose()
2529
}
2630
}
2731

28-
2932
/**
3033
* Represents JVM-side constructed tree
3134
*
3235
* This is equivalent of pyuast.Context API
3336
*/
3437
case class Context(nativeContext: Long) {
35-
import BblfshClient.uastBinary
38+
import BblfshClient.{UastFormat, UastBinary}
3639

3740
@native def root(): JNode
3841
@native def filter(query: String, node: JNode): UastIter
39-
@native def encode(n: JNode, fmt: Int): ByteBuffer
42+
@native def nativeEncode(n: JNode, fmt: Int): ByteBuffer
43+
def encode(n: JNode, fmt: UastFormat): ByteBuffer = {
44+
nativeEncode(n, fmt)
45+
}
4046
// encode using binary format
4147
def encode(n: JNode): ByteBuffer = {
42-
encode(n, uastBinary)
48+
encode(n, UastBinary)
4349
}
4450
@native def dispose()
4551
override def finalize(): Unit = {
4652
this.dispose()
4753
}
4854
}
55+
4956
object Context {
5057
@native def create(): Long
5158
def apply(): Context = new Context(create())

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ case class NodeExt(ctx: ContextExt, handle: Long) {
2828
* This is equivalent of pyuast.Node API.
2929
*/
3030
sealed abstract class JNode {
31-
import BblfshClient.uastBinary
31+
import BblfshClient.{UastFormat, UastBinary}
3232

33-
def toByteArray(fmt: Int): Array[Byte] = {
33+
def toByteArray(fmt: UastFormat): Array[Byte] = {
3434
val buf = toByteBuffer(fmt)
3535
val arr = new Array[Byte](buf.capacity())
3636
buf.get(arr)
@@ -40,10 +40,10 @@ sealed abstract class JNode {
4040

4141
/** Use binary UAST format */
4242
def toByteArray: Array[Byte] = {
43-
toByteArray(uastBinary)
43+
toByteArray(UastBinary)
4444
}
4545

46-
def toByteBuffer(fmt: Int): ByteBuffer = {
46+
def toByteBuffer(fmt: UastFormat): ByteBuffer = {
4747
val ctx = Context()
4848
val bb = ctx.encode(this, fmt)
4949
ctx.dispose()
@@ -52,7 +52,7 @@ sealed abstract class JNode {
5252

5353
/** Use binary UAST format */
5454
def toByteBuffer: ByteBuffer = {
55-
toByteBuffer(uastBinary)
55+
toByteBuffer(UastBinary)
5656
}
5757

5858
/* Dynamic dispatch is a convenience to be called from JNI */
@@ -87,9 +87,9 @@ sealed abstract class JNode {
8787
}
8888

8989
object JNode {
90-
import BblfshClient.uastBinary
90+
import BblfshClient.{UastFormat, UastBinary}
9191

92-
private def decodeFrom(bytes: ByteBuffer, fmt: Int): JNode = {
92+
private def decodeFrom(bytes: ByteBuffer, fmt: UastFormat): JNode = {
9393
val ctx = BblfshClient.decode(bytes, fmt)
9494
val node = ctx.root().load()
9595
ctx.dispose()
@@ -105,7 +105,7 @@ object JNode {
105105
* @param original UAST encoded in wire format of protocol.v2
106106
* @return JNode of the UAST root
107107
*/
108-
def parseFrom(original: ByteBuffer, fmt: Int): JNode = {
108+
def parseFrom(original: ByteBuffer, fmt: UastFormat): JNode = {
109109
val bufDirect = if (!original.isDirect) {
110110
val bufDirectCopy = ByteBuffer.allocateDirect(original.capacity())
111111
original.rewind()
@@ -121,7 +121,7 @@ object JNode {
121121

122122
/** Parse from a buffer using binary UAST format */
123123
def parseFrom(original: ByteBuffer): JNode = {
124-
parseFrom(original, uastBinary)
124+
parseFrom(original, UastBinary)
125125
}
126126

127127
/**
@@ -133,7 +133,7 @@ object JNode {
133133
* @param bytes UAST encoded in wire format of protocol.v2
134134
* @return JNode of the UAST root
135135
*/
136-
def parseFrom(bytes: Array[Byte], fmt: Int): JNode = {
136+
def parseFrom(bytes: Array[Byte], fmt: UastFormat): JNode = {
137137
val bufDirect = ByteBuffer.allocateDirect(bytes.size)
138138
bufDirect.put(bytes)
139139
bufDirect.flip()
@@ -142,7 +142,7 @@ object JNode {
142142

143143
/** Parse from an array using binary UAST format */
144144
def parseFrom(bytes: Array[Byte]): JNode = {
145-
parseFrom(bytes, uastBinary)
145+
parseFrom(bytes, UastBinary)
146146
}
147147
}
148148

src/main/scala/org/bblfsh/client/v2/libuast/Libuast.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.bblfsh.client.v2.libuast
22

3-
import org.bblfsh.client.v2.{ContextExt, Context, JNode, NodeExt, TreeOrder}
3+
import org.bblfsh.client.v2.{ContextExt, Context, JNode, NodeExt}
44
import org.bblfsh.client.v2.libuast.Libuast.UastIterExt
55

66
import scala.collection.Iterator
@@ -19,17 +19,17 @@ object Libuast {
1919
}
2020

2121
case class UastFormat(
22-
UastBinary: Int,
23-
UastYaml: Int
22+
uastBinary: Int,
23+
uastYaml: Int
2424
)
2525

2626
case class TreeOrder(
27-
AnyOrder: Int,
28-
PreOrder: Int,
29-
PostOrder: Int,
30-
LevelOrder: Int,
31-
ChildrenOrder: Int,
32-
PositionOrder: Int
27+
anyOrder: Int,
28+
preOrder: Int,
29+
postOrder: Int,
30+
levelOrder: Int,
31+
childrenOrder: Int,
32+
positionOrder: Int
3333
)
3434

3535
/**
@@ -156,7 +156,7 @@ class Libuast {
156156
@native def decode(buf: ByteBuffer, fmt: Int): ContextExt
157157

158158
/** Lifts the tree order values from the libuast */
159-
@native def getTreeOrders: TreeOrder
159+
@native def getTreeOrders: Libuast.TreeOrder
160160

161161
/** Lifts the uast decoding / encoding options from the libuast */
162162
@native def getUastFormats: Libuast.UastFormat

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

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,46 @@ package org.bblfsh.client
88
package object v2 {
99
/** Key, Value representation of [[org.bblfsh.client.v2.JObject]] */
1010
type JField = (String, JNode)
11-
/** Aliases for constants coming from libuast
12-
* This is just for convenience, to be able to do
13-
* `import org.bblfsh.client.v2.TreeOrder`
14-
* instead of
15-
* `import org.bblfsh.client.v2.libuast.Libuast.TreeOrder`
11+
12+
import BblfshClient._
13+
14+
/** Allow to use methods
15+
* f(fmt: UastFormat) as f(0) or f(1)
16+
* g(fmt: Int) as g(UastBinary) or g(UastYaml)
17+
*/
18+
implicit def formatToInt(fmt: UastFormat): Int = {
19+
fmt.toInt
20+
}
21+
22+
implicit def intToFormat(x: Int): UastFormat = {
23+
x match {
24+
case UastBinary.toInt => UastBinary
25+
case UastYaml.toInt => UastYaml
26+
case _ =>
27+
println("warning: not valid numeric format, using UastBinary")
28+
UastBinary
29+
}
30+
}
31+
32+
/** Allow to use methods
33+
* f(order: UastOrder) as f(0), f(1), f(2), ...
34+
* g(order: Int) as g(AnyOrder), g(PreOrder), g(PostOrder)
1635
*/
17-
type UastFormat = libuast.Libuast.UastFormat
18-
type TreeOrder = libuast.Libuast.TreeOrder
36+
implicit def orderToInt(order: TreeOrder): Int = {
37+
order.toInt
38+
}
39+
40+
implicit def intToOrder(x: Int): TreeOrder = {
41+
x match {
42+
case AnyOrder.toInt => AnyOrder
43+
case PreOrder.toInt => PreOrder
44+
case PostOrder.toInt => PostOrder
45+
case LevelOrder.toInt => LevelOrder
46+
case ChildrenOrder.toInt => ChildrenOrder
47+
case PositionOrder.toInt => PositionOrder
48+
case _ =>
49+
println("warning: not valid numeric order, using AnyOrder")
50+
AnyOrder
51+
}
52+
}
1953
}

0 commit comments

Comments
 (0)