From c025ebc29d17e40e08aaa50250fbea6839d539c9 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Tue, 5 May 2026 18:42:53 -0400 Subject: [PATCH] feat(parser): Add C and C++ support to ASTParser --- src/file_cache.py | 11 +++++++++-- tests/test_ast_parser.py | 10 ++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/file_cache.py b/src/file_cache.py index 656940f..d79b723 100644 --- a/src/file_cache.py +++ b/src/file_cache.py @@ -38,6 +38,8 @@ from pathlib import Path from typing import Optional, Any, List, Tuple, Dict import tree_sitter import tree_sitter_python +import tree_sitter_cpp +import tree_sitter_c import re _ast_cache: Dict[str, Tuple[float, tree_sitter.Tree]] = {} @@ -49,11 +51,16 @@ class ASTParser: """ def __init__(self, language: str) -> None: - if language != "python": + if language not in ("python", "cpp", "c"): raise ValueError(f"Language '{language}' not supported yet.") self.language_name = language # Load the tree-sitter language grammar - self.language = tree_sitter.Language(tree_sitter_python.language()) + if language == "python": + 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) def parse(self, code: str) -> tree_sitter.Tree: diff --git a/tests/test_ast_parser.py b/tests/test_ast_parser.py index 8e072d6..1359163 100644 --- a/tests/test_ast_parser.py +++ b/tests/test_ast_parser.py @@ -42,6 +42,16 @@ def test_ast_parser_invalid_language() -> None: # If it's intended to raise or handle gracefully, test it here. 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: """Verify that get_curated_view preserves function bodies with @core_logic or # [HOT].""" parser = ASTParser(language="python")