Last active
December 21, 2017 13:06
-
-
Save zurk/f13aa816a7c3220cf700c98c72891ec3 to your computer and use it in GitHub Desktop.
Bblfsh usecases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lib1 | |
import lib2.lib21 | |
import lib3 as lib3_alias | |
from lib4 import lib41 | |
from lib5.lib51 import lib511 | |
from lib6 import lib61 as lib611_alias | |
var1 = None | |
var2 = None | |
class class1: | |
var11 = None | |
def __init__(self): | |
lib1.lib11.lib111.var1111 = None | |
self.var101 = None | |
self.var102, var103 = None, None | |
def func11(self): | |
class1.var111 = None | |
self.var112 = None | |
var113 = None | |
def func12(): | |
def func121(): | |
pass | |
class class2: | |
var21 = None | |
def func21(self): | |
var21 < 0 | |
var21 == 0 | |
def func22(self, arg1): | |
None | |
def func23(): | |
None | |
@decorator_here | |
class class3(class2): | |
class class4: | |
pass | |
def func1(arg1, arg2, arg3): | |
pass | |
func1(var1, var2, *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bblfsh | |
from bblfsh import BblfshClient, role_id, role_name | |
import importlib | |
def node2raw(node, internal_type=True): | |
token = "|%s|" % node.token[:20].replace("\n", "\\n") | |
roles = ", ".join([role_name(k) for k in node.roles]) | |
internal_role = node.internal_type | |
positions = str(node.start_position.line) if node.start_position.line != 0 else "" | |
return [positions, token, internal_role, roles] if internal_type else [positions, token, roles] | |
def uast_roles(uast) -> iter: | |
stack = [(0, uast)] | |
while stack: | |
level, node = stack.pop() | |
yield level, node | |
stack.extend((level+1, c) for c in node.children[::-1]) | |
def rreplace(s, old, new, times): | |
li = s.rsplit(old, times) | |
return new.join(li) | |
def print_table(table): | |
col_size = [max(len(cell) for cell in table_col) for table_col in zip(*table)] | |
for raw in table: | |
print(" ".join(cell.ljust(size) for cell, size in zip(raw, col_size))) | |
def print_uast_structure(uast, internal_type=True): | |
if internal_type: | |
table = [["#", "Token", "Internal Role", "Roles Tree"], ["", "", "", ""]] | |
else: | |
table = [["#", "Token", "Roles Tree"], ["", "", ""]] | |
old_level = 0 | |
for level, node in uast_roles(uast): | |
raw = node2raw(node, internal_type) | |
if level != 0: | |
raw[-1] = "┃ " * (level-1) + "┣ " + raw[-1] | |
if level < old_level: | |
table[-1][-1] = rreplace(table[-1][-1].replace("┣", "┗"), "┃", "┗", old_level-level-1) | |
pass | |
table.append(raw) | |
old_level = level | |
table[-1][-1] = table[-1][-1].replace("┣", "┗").replace("┃", "┗") | |
print_table(table) | |
def print_nodes(nodes): | |
table = [["#", "Token", "Roles"]] | |
for node in nodes: | |
table.append(node2raw(node, False)) | |
print_table(table) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment