-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add c, cpp, csharp, yaml, md, rst support (#7)
Closes #5
- Loading branch information
1 parent
36e1025
commit e0705db
Showing
17 changed files
with
653 additions
and
554 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
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,26 @@ | ||
import tree_sitter | ||
|
||
from codeqai.constants import Language | ||
from codeqai.treesitter.treesitter import Treesitter | ||
from codeqai.treesitter.treesitter_registry import TreesitterRegistry | ||
|
||
|
||
class TreesitterC(Treesitter): | ||
def __init__(self): | ||
super().__init__(Language.C, "function_definition", "identifier", "comment") | ||
|
||
def _query_method_name(self, node: tree_sitter.Node): | ||
if node.type == self.method_declaration_identifier: | ||
for child in node.children: | ||
# if method returns pointer, skip pointer declarator | ||
if child.type == "pointer_declarator": | ||
child = child.children[1] | ||
if child.type == "function_declarator": | ||
for child in child.children: | ||
if child.type == self.method_name_identifier: | ||
return child.text.decode() | ||
return None | ||
|
||
|
||
# Register the TreesitterJava class in the registry | ||
TreesitterRegistry.register_treesitter(Language.C, TreesitterC) |
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,26 @@ | ||
import tree_sitter | ||
|
||
from codeqai.constants import Language | ||
from codeqai.treesitter.treesitter import Treesitter | ||
from codeqai.treesitter.treesitter_registry import TreesitterRegistry | ||
|
||
|
||
class TreesitterCpp(Treesitter): | ||
def __init__(self): | ||
super().__init__(Language.CPP, "function_definition", "identifier", "comment") | ||
|
||
def _query_method_name(self, node: tree_sitter.Node): | ||
if node.type == self.method_declaration_identifier: | ||
for child in node.children: | ||
# if method returns pointer, skip pointer declarator | ||
if child.type == "pointer_declarator": | ||
child = child.children[1] | ||
if child.type == "function_declarator": | ||
for child in child.children: | ||
if child.type == self.method_name_identifier: | ||
return child.text.decode() | ||
return None | ||
|
||
|
||
# Register the TreesitterJava class in the registry | ||
TreesitterRegistry.register_treesitter(Language.CPP, TreesitterCpp) |
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,62 @@ | ||
import tree_sitter | ||
|
||
from codeqai.constants import Language | ||
from codeqai.treesitter.treesitter import Treesitter | ||
from codeqai.treesitter.treesitter_registry import TreesitterRegistry | ||
|
||
|
||
class TreesitterCsharp(Treesitter): | ||
def __init__(self): | ||
super().__init__( | ||
Language.C_SHARP, "method_declaration", "identifier", "comment" | ||
) | ||
|
||
def _query_method_name(self, node: tree_sitter.Node): | ||
first_match = None | ||
if node.type == self.method_declaration_identifier: | ||
for child in node.children: | ||
# if the return type is an object type, then the method name | ||
# is the second match | ||
if child.type == self.method_name_identifier and not first_match: | ||
first_match = child.text.decode() | ||
elif child.type == self.method_name_identifier and first_match: | ||
return child.text.decode() | ||
return first_match | ||
|
||
def _query_all_methods(self, node: tree_sitter.Node): | ||
methods = [] | ||
if node.type == self.method_declaration_identifier: | ||
doc_comment_nodes = [] | ||
if ( | ||
node.prev_named_sibling | ||
and node.prev_named_sibling.type == self.doc_comment_identifier | ||
): | ||
current_doc_comment_node = node.prev_named_sibling | ||
while ( | ||
current_doc_comment_node | ||
and current_doc_comment_node.type == self.doc_comment_identifier | ||
): | ||
doc_comment_nodes.append(current_doc_comment_node.text.decode()) | ||
if current_doc_comment_node.prev_named_sibling: | ||
current_doc_comment_node = ( | ||
current_doc_comment_node.prev_named_sibling | ||
) | ||
else: | ||
current_doc_comment_node = None | ||
|
||
doc_comment_str = "" | ||
doc_comment_nodes.reverse() | ||
for doc_comment_node in doc_comment_nodes: | ||
doc_comment_str += doc_comment_node + "\n" | ||
if doc_comment_str.strip() != "": | ||
methods.append({"method": node, "doc_comment": doc_comment_str.strip()}) | ||
else: | ||
methods.append({"method": node, "doc_comment": None}) | ||
else: | ||
for child in node.children: | ||
methods.extend(self._query_all_methods(child)) | ||
return methods | ||
|
||
|
||
# Register the TreesitterJava class in the registry | ||
TreesitterRegistry.register_treesitter(Language.C_SHARP, TreesitterCsharp) |
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.