feat(parser): Add C and C++ support to ASTParser
This commit is contained in:
+8
-1
@@ -38,6 +38,8 @@ from pathlib import Path
|
|||||||
from typing import Optional, Any, List, Tuple, Dict
|
from typing import Optional, Any, List, Tuple, Dict
|
||||||
import tree_sitter
|
import tree_sitter
|
||||||
import tree_sitter_python
|
import tree_sitter_python
|
||||||
|
import tree_sitter_cpp
|
||||||
|
import tree_sitter_c
|
||||||
import re
|
import re
|
||||||
|
|
||||||
_ast_cache: Dict[str, Tuple[float, tree_sitter.Tree]] = {}
|
_ast_cache: Dict[str, Tuple[float, tree_sitter.Tree]] = {}
|
||||||
@@ -49,11 +51,16 @@ class ASTParser:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, language: str) -> None:
|
def __init__(self, language: str) -> None:
|
||||||
if language != "python":
|
if language not in ("python", "cpp", "c"):
|
||||||
raise ValueError(f"Language '{language}' not supported yet.")
|
raise ValueError(f"Language '{language}' not supported yet.")
|
||||||
self.language_name = language
|
self.language_name = language
|
||||||
# Load the tree-sitter language grammar
|
# Load the tree-sitter language grammar
|
||||||
|
if language == "python":
|
||||||
self.language = tree_sitter.Language(tree_sitter_python.language())
|
self.language = tree_sitter.Language(tree_sitter_python.language())
|
||||||
|
elif language == "cpp":
|
||||||
|
self.language = tree_sitter.Language(tree_sitter_cpp.language())
|
||||||
|
elif language == "c":
|
||||||
|
self.language = tree_sitter.Language(tree_sitter_c.language())
|
||||||
self.parser = tree_sitter.Parser(self.language)
|
self.parser = tree_sitter.Parser(self.language)
|
||||||
|
|
||||||
def parse(self, code: str) -> tree_sitter.Tree:
|
def parse(self, code: str) -> tree_sitter.Tree:
|
||||||
|
|||||||
@@ -42,6 +42,16 @@ def test_ast_parser_invalid_language() -> None:
|
|||||||
# If it's intended to raise or handle gracefully, test it here.
|
# If it's intended to raise or handle gracefully, test it here.
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def test_ast_parser_cpp_init() -> None:
|
||||||
|
"""Verify that ASTParser can be initialized with 'cpp'."""
|
||||||
|
parser = ASTParser(language="cpp")
|
||||||
|
assert parser.language_name == "cpp"
|
||||||
|
|
||||||
|
def test_ast_parser_c_init() -> None:
|
||||||
|
"""Verify that ASTParser can be initialized with 'c'."""
|
||||||
|
parser = ASTParser(language="c")
|
||||||
|
assert parser.language_name == "c"
|
||||||
|
|
||||||
def test_ast_parser_get_curated_view() -> None:
|
def test_ast_parser_get_curated_view() -> None:
|
||||||
"""Verify that get_curated_view preserves function bodies with @core_logic or # [HOT]."""
|
"""Verify that get_curated_view preserves function bodies with @core_logic or # [HOT]."""
|
||||||
parser = ASTParser(language="python")
|
parser = ASTParser(language="python")
|
||||||
|
|||||||
Reference in New Issue
Block a user