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

Commit 52b1936

Browse files
zurkdennwc
authored andcommitted
Fix classes for backward compatibility
Signed-off-by: Konstantin Slavnov <[email protected]>
1 parent f2a7d7e commit 52b1936

File tree

4 files changed

+53
-51
lines changed

4 files changed

+53
-51
lines changed

bblfsh/__init__.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,4 @@
22
from bblfsh.pyuast import decode, iterator, uast
33
from bblfsh.tree_order import TreeOrder
44
from bblfsh.aliases import *
5-
6-
7-
class RoleSearchException(Exception):
8-
pass
9-
10-
11-
def role_id(rname: str) -> int:
12-
try:
13-
name = DESCRIPTOR.enum_types_by_name["Role"].values_by_name[rname].number
14-
except KeyError:
15-
raise RoleSearchException("Role with name '{}' not found".format(rname))
16-
17-
return name
18-
19-
20-
def role_name(rid: int) -> str:
21-
try:
22-
id_ = DESCRIPTOR.enum_types_by_name["Role"].values_by_number[rid].name
23-
except KeyError:
24-
raise RoleSearchException("Role with ID '{}' not found".format(rid))
25-
26-
return id_
5+
from bblfsh.roles import role_id, role_name

bblfsh/node.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from typing import Union, List, cast, Optional, Any
44

55
from bblfsh.pyuast import Context, NodeExt, IteratorExt, iterator
6+
7+
from bblfsh.roles import role_id
68
from bblfsh.tree_order import TreeOrder
79
from bblfsh.type_aliases import ResultMultiType
810

@@ -14,7 +16,7 @@ class NodeTypedGetException(Exception):
1416
class CompatPosition:
1517
"""
1618
v1 positions were extracted as node.[start|end]_position.[line|col|offset]. To
17-
emulate that, this dictionary will be returned when accesing the old position
19+
emulate that, this dictionary will be returned when accessing the old position
1820
properties and its setters will update the parent Node real position ones.
1921
"""
2022

@@ -56,7 +58,7 @@ def __init__(self, parent: "Node") -> None:
5658
self._par_dict = parent.get_dict()
5759
self._children = self._sync_children()
5860

59-
def _sync_children(self) -> None:
61+
def _sync_children(self) -> List["Node"]:
6062
if "_children" not in self._par_dict:
6163
self._par_dict["_children"] = []
6264
children = self._par_dict["_children"]
@@ -85,7 +87,7 @@ def __len__(self) -> int:
8587
return len(self._children)
8688

8789
def __getitem__(self, idx: Union[int, slice]) -> Any:
88-
return self._children[idx]
90+
return Node(value=self._children[idx])
8991

9092
def __delitem__(self, idx: Union[int, slice]) -> None:
9193
del self._children[idx]
@@ -214,12 +216,12 @@ def _is_dict_list(self, key: str) -> Optional[List]:
214216
return val
215217

216218
@property
217-
def children(self) -> CompatChildren:
219+
def children(self) -> List["Node"]:
218220
return CompatChildren(self)
219221

220222
@property
221223
def token(self) -> str:
222-
return self.get_dict()["@token"]
224+
return self.get_dict().get("@token", "")
223225

224226
@token.setter
225227
def token(self, t: str) -> None:
@@ -228,35 +230,34 @@ def token(self, t: str) -> None:
228230

229231
@property
230232
def roles(self) -> List:
231-
return self.get_dict().get("@role", [])
233+
return [role_id(name) for name in self.get_dict().get("@role", [])]
232234

233235
def _add_position(self) -> None:
234236
d = self.get_dict()
235237
if "@pos" not in d:
236238
d["@pos"] = {
237239
"@type": "uast:Positions",
238-
"start": {
239-
"@type": "uast:Position",
240-
"offset": -1,
241-
"line": -1,
242-
"col": -1,
243-
},
244-
"end": {
245-
"@type": "uast:Position",
246-
"offset": -1,
247-
"line": -1,
248-
"col": -1,
249-
}
240+
"start": Node._get_default_position(),
241+
"end": Node._get_default_position()
250242
}
251243

252244
@property
253245
def start_position(self) -> CompatPosition:
254246
self._add_position()
255-
start = self.get_dict()["@pos"]["start"]
247+
start = self.get_dict()["@pos"].get("start", Node._get_default_position())
256248
return CompatPosition(start)
257249

258250
@property
259251
def end_position(self) -> CompatPosition:
260252
self._add_position()
261-
end = self.get_dict()["@pos"]["end"]
253+
end = self.get_dict()["@pos"].get("start", Node._get_default_position())
262254
return CompatPosition(end)
255+
256+
@staticmethod
257+
def _get_default_position():
258+
return {
259+
"@type": "uast:Position",
260+
"offset": 0,
261+
"line": 0,
262+
"col": 0,
263+
}

bblfsh/roles.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from bblfsh.aliases import DESCRIPTOR
2+
3+
4+
class RoleSearchException(Exception):
5+
pass
6+
7+
8+
def role_id(rname: str) -> int:
9+
try:
10+
name = DESCRIPTOR.enum_types_by_name["Role"].values_by_name[rname.upper()].number
11+
except KeyError:
12+
raise RoleSearchException("Role with name '{}' not found".format(rname))
13+
14+
return name
15+
16+
17+
def role_name(rid: int) -> str:
18+
try:
19+
id_ = DESCRIPTOR.enum_types_by_name["Role"].values_by_number[rid].name
20+
except KeyError:
21+
raise RoleSearchException("Role with ID '{}' not found".format(rid))
22+
23+
return id_.upper()

bblfsh/test_compat.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ def testIteratorPositionOrder(self):
220220
it = iterator(root, TreeOrder.POSITION_ORDER)
221221
self.assertIsNotNone(it)
222222
expanded = [node.internal_type for node in it]
223-
self.assertListEqual(expanded, ['root', 'son1', 'son2_1', 'son1_1',
224-
'son1_2', 'son2_2', 'son2'])
223+
self.assertListEqual(expanded, ['son1', 'son2_1', 'son1_1',
224+
'son1_2', 'son2_2', 'son2', 'root'])
225225

226226
def testFilterInsideIter(self):
227227
root = self._parse_fixture().uast
@@ -288,18 +288,18 @@ def testChildren(self):
288288
n.internal_type = 'root'
289289
c1 = {"@type": "child1"}
290290
n.properties["child1"] = c1
291-
self.assertDictEqual(n.children[0], c1)
291+
self.assertDictEqual(n.children[0].get_dict(), c1)
292292

293293
c2 = {"@type": "child2"}
294294
n.children.append(c2)
295-
self.assertDictEqual(n.children[1], c2)
295+
self.assertDictEqual(n.children[1].get_dict(), c2)
296296
n.children.append(c2)
297-
self.assertDictEqual(n.children[2], c2)
297+
self.assertDictEqual(n.children[2].get_dict(), c2)
298298

299299
l = [{"@type": "list_child1"}, {"@type": "list_child2"}]
300300
n.properties["some_list"] = l
301-
self.assertDictEqual(n.children[3], l[0])
302-
self.assertDictEqual(n.children[4], l[1])
301+
self.assertDictEqual(n.children[3].get_dict(), l[0])
302+
self.assertDictEqual(n.children[4].get_dict(), l[1])
303303

304304
def testChildrenFile(self):
305305
root = self._parse_fixture().uast
@@ -309,8 +309,7 @@ def testChildrenFile(self):
309309
root.children.append(n)
310310
self.assertEqual(len(root.children), 11)
311311
last = root.children[-1]
312-
self.assertDictEqual(last, n.internal_node)
313-
312+
self.assertDictEqual(last.get_dict(), n.internal_node)
314313

315314

316315
if __name__ == "__main__":

0 commit comments

Comments
 (0)