Last active
August 11, 2017 17:55
-
-
Save zurk/a81fa263c2ea64a04a7b7bb96b1fdb40 to your computer and use it in GitHub Desktop.
Offset error demonstration in bblfsh in FUNCTION_DECLARATION role
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
from bblfsh.client import BblfshClient | |
from bblfsh.github.com.bblfsh.sdk.uast.generated_pb2 import DESCRIPTOR | |
def get_role_id(role_name): | |
return DESCRIPTOR.enum_types_by_name["Role"].values_by_name[role_name].number | |
def uast_to_roles(uast, role="SIMPLE_IDENTIFIER"): | |
role_id = get_role_id(role) | |
stack = [uast] | |
while stack: | |
node = stack.pop(0) | |
if role_id in node.roles: | |
yield node | |
stack.extend(node.children) | |
if __name__ == '__main__': | |
filepath = 'file2uast.py' | |
bc = BblfshClient("0.0.0.0:9432") | |
res = bc.parse(filepath, language='Python') | |
funcs = uast_to_roles(res.uast, "FUNCTION_DECLARATION") | |
src = open(filepath, 'rb').read() | |
for func in funcs: | |
print('======== {} ==================='.format(func.token)) | |
print(src[func.start_position.offset:func.end_position.offset+1].decode("utf8")) | |
print() |
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
======== get_role_id =================== | |
get_role_id(role_name): | |
return DESCRIPTOR.enum_types_by_name["Role"].values_by_name[role_name].number | |
======== uast_to_roles =================== | |
uast_to_roles(uast, role="SIMPLE_IDENTIFIER"): | |
role_id = get_role_id(role) | |
stack = [uast] | |
while stack: | |
node = stack.pop(0) | |
if role_id in node.roles: | |
yield node | |
stack.extend(node.children |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment