|
| 1 | +# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +"""C/C++ AST extractor using tree-sitter-cpp.""" |
| 4 | + |
| 5 | +from typing import List |
| 6 | + |
| 7 | +from openviking.parse.parsers.code.ast.languages.base import LanguageExtractor |
| 8 | +from openviking.parse.parsers.code.ast.skeleton import ClassSkeleton, CodeSkeleton, FunctionSig |
| 9 | + |
| 10 | + |
| 11 | +def _node_text(node, content_bytes: bytes) -> str: |
| 12 | + return content_bytes[node.start_byte:node.end_byte].decode("utf-8", errors="replace") |
| 13 | + |
| 14 | + |
| 15 | +def _parse_block_comment(raw: str) -> str: |
| 16 | + """Strip /** ... */ markers and leading * from each line.""" |
| 17 | + raw = raw.strip() |
| 18 | + if raw.startswith("/**"): |
| 19 | + raw = raw[3:] |
| 20 | + elif raw.startswith("/*"): |
| 21 | + raw = raw[2:] |
| 22 | + if raw.endswith("*/"): |
| 23 | + raw = raw[:-2] |
| 24 | + lines = [l.strip().lstrip("*").strip() for l in raw.split("\n")] |
| 25 | + return "\n".join(l for l in lines if l).strip() |
| 26 | + |
| 27 | + |
| 28 | +def _preceding_doc(siblings: list, idx: int, content_bytes: bytes) -> str: |
| 29 | + """Return Doxygen block comment immediately before siblings[idx], or ''.""" |
| 30 | + if idx == 0: |
| 31 | + return "" |
| 32 | + prev = siblings[idx - 1] |
| 33 | + if prev.type == "comment": |
| 34 | + return _parse_block_comment(_node_text(prev, content_bytes)) |
| 35 | + return "" |
| 36 | + |
| 37 | + |
| 38 | +def _extract_function_declarator(node, content_bytes: bytes): |
| 39 | + name = "" |
| 40 | + params = "" |
| 41 | + for child in node.children: |
| 42 | + if child.type in ("identifier", "field_identifier") and not name: |
| 43 | + name = _node_text(child, content_bytes) |
| 44 | + elif child.type == "qualified_identifier" and not name: |
| 45 | + name = _node_text(child, content_bytes) |
| 46 | + elif child.type == "function_declarator": |
| 47 | + n, p = _extract_function_declarator(child, content_bytes) |
| 48 | + if n: |
| 49 | + name = n |
| 50 | + if p: |
| 51 | + params = p |
| 52 | + elif child.type == "parameter_list": |
| 53 | + raw = _node_text(child, content_bytes).strip() |
| 54 | + if raw.startswith("(") and raw.endswith(")"): |
| 55 | + raw = raw[1:-1] |
| 56 | + params = raw.strip() |
| 57 | + return name, params |
| 58 | + |
| 59 | + |
| 60 | +def _extract_function(node, content_bytes: bytes, docstring: str = "") -> FunctionSig: |
| 61 | + name = "" |
| 62 | + params = "" |
| 63 | + return_type = "" |
| 64 | + |
| 65 | + for child in node.children: |
| 66 | + if child.type == "function_declarator": |
| 67 | + name, params = _extract_function_declarator(child, content_bytes) |
| 68 | + elif child.type in ("type_specifier", "primitive_type", "type_identifier", |
| 69 | + "qualified_identifier", "auto"): |
| 70 | + if not return_type: |
| 71 | + return_type = _node_text(child, content_bytes) |
| 72 | + elif child.type == "pointer_declarator": |
| 73 | + for sub in child.children: |
| 74 | + if sub.type == "function_declarator": |
| 75 | + name, params = _extract_function_declarator(sub, content_bytes) |
| 76 | + |
| 77 | + return FunctionSig(name=name, params=params, return_type=return_type, docstring=docstring) |
| 78 | + |
| 79 | + |
| 80 | +def _extract_class(node, content_bytes: bytes, docstring: str = "") -> ClassSkeleton: |
| 81 | + name = "" |
| 82 | + bases: List[str] = [] |
| 83 | + body_node = None |
| 84 | + |
| 85 | + for child in node.children: |
| 86 | + if child.type == "type_identifier" and not name: |
| 87 | + name = _node_text(child, content_bytes) |
| 88 | + elif child.type == "base_class_clause": |
| 89 | + for sub in child.children: |
| 90 | + if sub.type == "type_identifier": |
| 91 | + bases.append(_node_text(sub, content_bytes)) |
| 92 | + elif child.type == "field_declaration_list": |
| 93 | + body_node = child |
| 94 | + |
| 95 | + methods: List[FunctionSig] = [] |
| 96 | + if body_node: |
| 97 | + siblings = list(body_node.children) |
| 98 | + for idx, child in enumerate(siblings): |
| 99 | + if child.type == "function_definition": |
| 100 | + doc = _preceding_doc(siblings, idx, content_bytes) |
| 101 | + methods.append(_extract_function(child, content_bytes, docstring=doc)) |
| 102 | + elif child.type in ("declaration", "field_declaration"): |
| 103 | + ret_type = "" |
| 104 | + fn_name = "" |
| 105 | + fn_params = "" |
| 106 | + for sub in child.children: |
| 107 | + if sub.type in ("type_specifier", "primitive_type", "type_identifier", |
| 108 | + "qualified_identifier") and not ret_type: |
| 109 | + ret_type = _node_text(sub, content_bytes) |
| 110 | + elif sub.type == "function_declarator": |
| 111 | + fn_name, fn_params = _extract_function_declarator(sub, content_bytes) |
| 112 | + break |
| 113 | + if fn_name: |
| 114 | + doc = _preceding_doc(siblings, idx, content_bytes) |
| 115 | + methods.append(FunctionSig(name=fn_name, params=fn_params, return_type=ret_type, docstring=doc)) |
| 116 | + |
| 117 | + return ClassSkeleton(name=name, bases=bases, docstring=docstring, methods=methods) |
| 118 | + |
| 119 | + |
| 120 | +class CppExtractor(LanguageExtractor): |
| 121 | + def __init__(self): |
| 122 | + import tree_sitter_cpp as tscpp |
| 123 | + from tree_sitter import Language, Parser |
| 124 | + |
| 125 | + self._language = Language(tscpp.language()) |
| 126 | + self._parser = Parser(self._language) |
| 127 | + |
| 128 | + def extract(self, file_name: str, content: str) -> CodeSkeleton: |
| 129 | + content_bytes = content.encode("utf-8") |
| 130 | + tree = self._parser.parse(content_bytes) |
| 131 | + root = tree.root_node |
| 132 | + |
| 133 | + imports: List[str] = [] |
| 134 | + classes: List[ClassSkeleton] = [] |
| 135 | + functions: List[FunctionSig] = [] |
| 136 | + |
| 137 | + siblings = list(root.children) |
| 138 | + for idx, child in enumerate(siblings): |
| 139 | + if child.type == "preproc_include": |
| 140 | + for sub in child.children: |
| 141 | + if sub.type in ("string_literal", "system_lib_string"): |
| 142 | + raw = _node_text(sub, content_bytes).strip().strip('"<>') |
| 143 | + imports.append(raw) |
| 144 | + elif child.type in ("class_specifier", "struct_specifier"): |
| 145 | + doc = _preceding_doc(siblings, idx, content_bytes) |
| 146 | + classes.append(_extract_class(child, content_bytes, docstring=doc)) |
| 147 | + elif child.type == "function_definition": |
| 148 | + doc = _preceding_doc(siblings, idx, content_bytes) |
| 149 | + functions.append(_extract_function(child, content_bytes, docstring=doc)) |
| 150 | + elif child.type == "namespace_definition": |
| 151 | + for sub in child.children: |
| 152 | + if sub.type == "declaration_list": |
| 153 | + inner = list(sub.children) |
| 154 | + for i2, s2 in enumerate(inner): |
| 155 | + if s2.type in ("class_specifier", "struct_specifier"): |
| 156 | + doc = _preceding_doc(inner, i2, content_bytes) |
| 157 | + classes.append(_extract_class(s2, content_bytes, docstring=doc)) |
| 158 | + elif s2.type == "function_definition": |
| 159 | + doc = _preceding_doc(inner, i2, content_bytes) |
| 160 | + functions.append(_extract_function(s2, content_bytes, docstring=doc)) |
| 161 | + |
| 162 | + return CodeSkeleton( |
| 163 | + file_name=file_name, |
| 164 | + language="C/C++", |
| 165 | + module_doc="", |
| 166 | + imports=imports, |
| 167 | + classes=classes, |
| 168 | + functions=functions, |
| 169 | + ) |
0 commit comments