-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Haskell support added, Bugs fixed in the case of Azure_OpenAI (#28
) 1. In the case of Azure_OpenAI - Some bugs are fixed 2. Added support of Haskell language 3. Added support of Haskell language in text_splitter module of Langchain. PR for the same - langchain-ai/langchain#16191
- Loading branch information
1 parent
4f928c0
commit f6f3907
Showing
8 changed files
with
107 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,4 +107,5 @@ def get_commit_hash(file_path): | |
".yml", | ||
".rst", | ||
".md", | ||
".hs", | ||
] |
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,79 @@ | ||
import tree_sitter | ||
from typing import List, Dict | ||
|
||
|
||
from codeqai.constants import Language | ||
from codeqai.treesitter.treesitter import (Treesitter, | ||
TreesitterMethodNode) | ||
from codeqai.treesitter.treesitter_registry import TreesitterRegistry | ||
|
||
class TreesitterHaskell(Treesitter): | ||
def __init__(self): | ||
super().__init__( | ||
Language.HASKELL, "function", "variable", "comment" | ||
) | ||
|
||
def parse(self, file_bytes: bytes) -> list[TreesitterMethodNode]: | ||
self.tree = self.parser.parse(file_bytes) | ||
result = [] | ||
methods = self._query_all_methods(self.tree.root_node) | ||
for method in methods: | ||
method_name = self._query_method_name(method["method"]) | ||
doc_comment = method["doc_comment"] | ||
source_code = None | ||
if method["method"].type == "signature": | ||
sc = map(lambda x : "\n" + x.text.decode() if x.type == "function" else "", method["method"].children) | ||
source_code = method["method"].text.decode() + "".join(sc) | ||
result.append( | ||
TreesitterMethodNode(method_name, doc_comment, source_code, method["method"]) | ||
) | ||
return result | ||
|
||
def _query_all_methods( | ||
self, | ||
node: tree_sitter.Node, | ||
): | ||
methods: List[Dict[tree_sitter.Node, tree_sitter.Node]] = [] | ||
if node.type == self.method_declaration_identifier: | ||
doc_comment_node = None | ||
if ( | ||
node.prev_named_sibling | ||
and node.prev_named_sibling.type == self.doc_comment_identifier | ||
): | ||
doc_comment_node = node.prev_named_sibling.text.decode() | ||
else: | ||
if node.prev_named_sibling and node.prev_named_sibling.type == "signature": | ||
prev_node = node.prev_named_sibling | ||
if ( | ||
prev_node.prev_named_sibling | ||
and prev_node.prev_named_sibling.type == self.doc_comment_identifier | ||
): | ||
doc_comment_node = prev_node.prev_named_sibling.text.decode() | ||
prev_node.children.append(node) | ||
node = prev_node | ||
methods.append({"method": node, "doc_comment": doc_comment_node}) | ||
else: | ||
for child in node.children: | ||
current = self._query_all_methods(child) | ||
if methods and current: | ||
previous = methods[-1] | ||
if self._query_method_name(previous["method"]) == self._query_method_name(current[0]["method"]): | ||
previous["method"].children.extend(map(lambda x: x["method"], current)) | ||
methods = methods[:-1] | ||
methods.append(previous) | ||
else: | ||
methods.extend(current) | ||
else: | ||
methods.extend(current) | ||
return methods | ||
|
||
def _query_method_name(self, node: tree_sitter.Node): | ||
if node.type == "signature" or node.type == self.method_declaration_identifier: | ||
for child in node.children: | ||
if child.type == self.method_name_identifier: | ||
return child.text.decode() | ||
return None | ||
|
||
|
||
# Register the TreesitterHaskell class in the registry | ||
TreesitterRegistry.register_treesitter(Language.HASKELL, TreesitterHaskell) |
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