-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from alexminnaar/readme
adding more treesitter parsers
- Loading branch information
Showing
16 changed files
with
731 additions
and
226 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from repogpt.parsers.treesitter import TreeSitterParser, FileSummary, SummaryPosition | ||
from tree_sitter import Language, Parser | ||
|
||
|
||
class CppTreeSitterParser(TreeSitterParser): | ||
@staticmethod | ||
def get_file_summary(code: str, file_name: str) -> FileSummary: | ||
|
||
if not TreeSitterParser.loaded: | ||
TreeSitterParser.initialize_treesitter() | ||
|
||
file_summary = FileSummary() | ||
parser = Parser() | ||
parser.set_language(TreeSitterParser.languages['cpp']) | ||
tree = parser.parse(bytes(code, "utf-8")) | ||
|
||
def traverse(node, current_line): | ||
if node.type == 'function_declarator': | ||
for child in node.children: | ||
if child.type == 'identifier' or child.type == 'field_identifier': | ||
function_name = code[child.start_byte: child.end_byte] | ||
file_summary.methods.append( | ||
SummaryPosition(function_name, node.start_point[0], node.end_point[0])) | ||
|
||
if node.type == 'class_specifier': | ||
for child in node.children: | ||
if child.type == 'type_identifier': | ||
class_name = code[child.start_byte: child.end_byte] | ||
file_summary.classes.append( | ||
SummaryPosition(class_name, node.start_point[0], node.end_point[0])) | ||
|
||
for child in node.children: | ||
traverse(child, current_line) | ||
|
||
root_node = tree.root_node | ||
|
||
traverse(root_node, 0) | ||
|
||
# methods and classes are not in order so sort | ||
file_summary.methods = sorted(file_summary.methods, key=lambda x: x.start_line) | ||
file_summary.classes = sorted(file_summary.classes, key=lambda x: x.start_line) | ||
|
||
return file_summary |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from repogpt.parsers.treesitter import TreeSitterParser, FileSummary, SummaryPosition | ||
from tree_sitter import Language, Parser | ||
|
||
|
||
class GoTreeSitterParser(TreeSitterParser): | ||
@staticmethod | ||
def get_file_summary(code: str, file_name:str) -> FileSummary: | ||
|
||
if not TreeSitterParser.loaded: | ||
TreeSitterParser.initialize_treesitter() | ||
|
||
file_summary = FileSummary() | ||
parser = Parser() | ||
parser.set_language(TreeSitterParser.languages['go']) | ||
tree = parser.parse(bytes(code, "utf-8")) | ||
|
||
def traverse(node, current_line): | ||
|
||
if node.type == 'type_spec': | ||
for child in node.children: | ||
if child.type == 'type_identifier': | ||
class_name = code[child.start_byte: child.end_byte] | ||
file_summary.classes.append( | ||
SummaryPosition(class_name, node.start_point[0], node.end_point[0])) | ||
|
||
if node.type == 'method_declaration': | ||
for child in node.children: | ||
if child.type == 'field_identifier': | ||
function_name = code[child.start_byte: child.end_byte] | ||
file_summary.methods.append( | ||
SummaryPosition(function_name, node.start_point[0], node.end_point[0])) | ||
|
||
if node.type == 'function_declaration': | ||
for child in node.children: | ||
if child.type == 'identifier': | ||
function_name = code[child.start_byte: child.end_byte] | ||
file_summary.methods.append( | ||
SummaryPosition(function_name, node.start_point[0], node.end_point[0])) | ||
|
||
for child in node.children: | ||
traverse(child, current_line) | ||
|
||
root_node = tree.root_node | ||
|
||
traverse(root_node, 0) | ||
|
||
# methods and classes are not in order so sort | ||
file_summary.methods = sorted(file_summary.methods, key=lambda x: x.start_line) | ||
file_summary.classes = sorted(file_summary.classes, key=lambda x: x.start_line) | ||
|
||
return file_summary |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from repogpt.parsers.treesitter import TreeSitterParser, FileSummary, SummaryPosition | ||
from tree_sitter import Language, Parser | ||
|
||
|
||
class JavaTreeSitterParser(TreeSitterParser): | ||
@staticmethod | ||
def get_file_summary(code: str, file_name: str) -> FileSummary: | ||
|
||
if not TreeSitterParser.loaded: | ||
TreeSitterParser.initialize_treesitter() | ||
|
||
file_summary = FileSummary() | ||
parser = Parser() | ||
parser.set_language(TreeSitterParser.languages['java']) | ||
tree = parser.parse(bytes(code, "utf-8")) | ||
|
||
def traverse(node, current_line): | ||
if node.type == 'constructor_declaration': | ||
for child in node.children: | ||
if child.type == 'identifier': | ||
function_name = code[child.start_byte: child.end_byte] | ||
file_summary.methods.append( | ||
SummaryPosition(function_name, node.start_point[0], node.end_point[0])) | ||
|
||
if node.type == 'method_declaration': | ||
for child in node.children: | ||
if child.type == 'identifier': | ||
function_name = code[child.start_byte: child.end_byte] | ||
file_summary.methods.append( | ||
SummaryPosition(function_name, node.start_point[0], node.end_point[0])) | ||
|
||
if node.type == 'class_declaration': | ||
for child in node.children: | ||
if child.type == 'identifier': | ||
class_name = code[child.start_byte: child.end_byte] | ||
file_summary.classes.append( | ||
SummaryPosition(class_name, node.start_point[0], node.end_point[0])) | ||
|
||
for child in node.children: | ||
traverse(child, current_line) | ||
|
||
root_node = tree.root_node | ||
|
||
traverse(root_node, 0) | ||
|
||
# methods and classes are not in order so sort | ||
file_summary.methods = sorted(file_summary.methods, key=lambda x: x.start_line) | ||
file_summary.classes = sorted(file_summary.classes, key=lambda x: x.start_line) | ||
|
||
return file_summary |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from repogpt.parsers.treesitter import TreeSitterParser, FileSummary, SummaryPosition | ||
from tree_sitter import Language, Parser | ||
|
||
|
||
class JsTreeSitterParser(TreeSitterParser): | ||
@staticmethod | ||
def get_file_summary(code: str, file_name: str) -> FileSummary: | ||
|
||
if not TreeSitterParser.loaded: | ||
TreeSitterParser.initialize_treesitter() | ||
|
||
file_summary = FileSummary() | ||
parser = Parser() | ||
parser.set_language(TreeSitterParser.languages['cpp']) | ||
tree = parser.parse(bytes(code, "utf-8")) | ||
|
||
def traverse(node, current_line): | ||
if node.type == 'function_declarator': | ||
for child in node.children: | ||
if child.type == 'identifier' or child.type == 'field_identifier': | ||
function_name = code[child.start_byte: child.end_byte] | ||
file_summary.methods.append( | ||
SummaryPosition(function_name, node.start_point[0], node.end_point[0])) | ||
|
||
if node.type == 'class_specifier': | ||
class_name_node = node.children[1] | ||
class_name = code[class_name_node.start_byte: class_name_node.end_byte] | ||
file_summary.classes.append(SummaryPosition(class_name, node.start_point[0], node.end_point[0])) | ||
|
||
for child in node.children: | ||
traverse(child, current_line) | ||
|
||
root_node = tree.root_node | ||
|
||
traverse(root_node, 0) | ||
|
||
# methods and classes are not in order so sort | ||
file_summary.methods = sorted(file_summary.methods, key=lambda x: x.start_line) | ||
file_summary.classes = sorted(file_summary.classes, key=lambda x: x.start_line) | ||
|
||
return file_summary |
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
Oops, something went wrong.