|
| 1 | +# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +"""C# AST extractor using tree-sitter-c-sharp.""" |
| 4 | + |
| 5 | +import re |
| 6 | +from typing import List |
| 7 | + |
| 8 | +from openviking.parse.parsers.code.ast.languages.base import LanguageExtractor |
| 9 | +from openviking.parse.parsers.code.ast.skeleton import ClassSkeleton, CodeSkeleton, FunctionSig |
| 10 | + |
| 11 | + |
| 12 | +def _node_text(node, content_bytes: bytes) -> str: |
| 13 | + return content_bytes[node.start_byte : node.end_byte].decode("utf-8", errors="replace") |
| 14 | + |
| 15 | + |
| 16 | +def _parse_doc_comment(raw: str) -> str: |
| 17 | + """Strip XML doc comment markers (/// or /** */) and extract text from XML tags.""" |
| 18 | + raw = raw.strip() |
| 19 | + if raw.startswith("///"): |
| 20 | + lines = raw.split("\n") |
| 21 | + cleaned = [] |
| 22 | + for line in lines: |
| 23 | + stripped = line.strip() |
| 24 | + if stripped.startswith("///"): |
| 25 | + stripped = stripped[3:].strip() |
| 26 | + if stripped: |
| 27 | + cleaned.append(stripped) |
| 28 | + raw = " ".join(cleaned) |
| 29 | + elif raw.startswith("/**"): |
| 30 | + raw = raw[3:] |
| 31 | + if raw.endswith("*/"): |
| 32 | + raw = raw[:-2] |
| 33 | + lines = [l.strip().lstrip("*").strip() for l in raw.split("\n")] |
| 34 | + raw = "\n".join(l for l in lines if l).strip() |
| 35 | + # Remove XML tags |
| 36 | + raw = re.sub(r"</?[a-zA-Z][a-zA-Z0-9]*(?:\s+[^>]*)?/?>", "", raw) |
| 37 | + # Normalize whitespace |
| 38 | + raw = re.sub(r"\s+", " ", raw).strip() |
| 39 | + return raw |
| 40 | + |
| 41 | + |
| 42 | +def _preceding_doc(siblings: list, idx: int, content_bytes: bytes) -> str: |
| 43 | + """Return XML doc comment immediately before siblings[idx], or ''.""" |
| 44 | + if idx == 0: |
| 45 | + return "" |
| 46 | + comments = [] |
| 47 | + for i in range(idx - 1, -1, -1): |
| 48 | + prev = siblings[i] |
| 49 | + if prev.type == "comment": |
| 50 | + text = _node_text(prev, content_bytes) |
| 51 | + if text.strip().startswith("///") or text.strip().startswith("/**"): |
| 52 | + comments.insert(0, _parse_doc_comment(text)) |
| 53 | + else: |
| 54 | + break |
| 55 | + elif prev.type in ("preprocessor_directive", "nullable_directive"): |
| 56 | + continue |
| 57 | + else: |
| 58 | + break |
| 59 | + return "\n".join(comments) if comments else "" |
| 60 | + |
| 61 | + |
| 62 | +def _extract_method(node, content_bytes: bytes, docstring: str = "") -> FunctionSig: |
| 63 | + name = "" |
| 64 | + params = "" |
| 65 | + return_type = "" |
| 66 | + |
| 67 | + for child in node.children: |
| 68 | + if child.type == "identifier" and not name: |
| 69 | + name = _node_text(child, content_bytes) |
| 70 | + elif child.type == "void_keyword": |
| 71 | + return_type = "void" |
| 72 | + elif child.type in ("predefined_type", "type_identifier", "generic_name"): |
| 73 | + if not return_type: |
| 74 | + return_type = _node_text(child, content_bytes) |
| 75 | + elif child.type == "parameter_list": |
| 76 | + raw = _node_text(child, content_bytes).strip() |
| 77 | + if raw.startswith("(") and raw.endswith(")"): |
| 78 | + raw = raw[1:-1] |
| 79 | + params = raw.strip() |
| 80 | + |
| 81 | + if node.type == "property_declaration": |
| 82 | + for child in node.children: |
| 83 | + if child.type == "accessor_list": |
| 84 | + accessors = [] |
| 85 | + for acc in child.children: |
| 86 | + if acc.type == "accessor_declaration": |
| 87 | + accessor_name = "" |
| 88 | + name_node = acc.child_by_field_name("name") |
| 89 | + if name_node is not None: |
| 90 | + accessor_name = _node_text(name_node, content_bytes).strip() |
| 91 | + else: |
| 92 | + for sub in acc.children: |
| 93 | + if sub.type in ("get", "set", "init"): |
| 94 | + accessor_name = sub.type |
| 95 | + break |
| 96 | + if accessor_name in ("get", "set", "init"): |
| 97 | + accessors.append(accessor_name) |
| 98 | + if accessors: |
| 99 | + params = f"{{ {' '.join(accessors)} }}" |
| 100 | + |
| 101 | + return FunctionSig(name=name, params=params, return_type=return_type, docstring=docstring) |
| 102 | + |
| 103 | + |
| 104 | +def _extract_class(node, content_bytes: bytes, docstring: str = "") -> ClassSkeleton: |
| 105 | + name = "" |
| 106 | + bases: List[str] = [] |
| 107 | + body_node = None |
| 108 | + |
| 109 | + for child in node.children: |
| 110 | + if child.type == "identifier" and not name: |
| 111 | + name = _node_text(child, content_bytes) |
| 112 | + elif child.type == "base_list": |
| 113 | + for sub in child.children: |
| 114 | + if sub.type in ("type_identifier", "identifier"): |
| 115 | + bases.append(_node_text(sub, content_bytes)) |
| 116 | + elif child.type == "declaration_list": |
| 117 | + body_node = child |
| 118 | + |
| 119 | + methods: List[FunctionSig] = [] |
| 120 | + if body_node: |
| 121 | + siblings = list(body_node.children) |
| 122 | + for idx, child in enumerate(siblings): |
| 123 | + if child.type in ("method_declaration", "constructor_declaration"): |
| 124 | + doc = _preceding_doc(siblings, idx, content_bytes) |
| 125 | + methods.append(_extract_method(child, content_bytes, docstring=doc)) |
| 126 | + elif child.type == "property_declaration": |
| 127 | + doc = _preceding_doc(siblings, idx, content_bytes) |
| 128 | + methods.append(_extract_method(child, content_bytes, docstring=doc)) |
| 129 | + |
| 130 | + return ClassSkeleton(name=name, bases=bases, docstring=docstring, methods=methods) |
| 131 | + |
| 132 | + |
| 133 | +class CSharpExtractor(LanguageExtractor): |
| 134 | + def __init__(self): |
| 135 | + import tree_sitter_c_sharp as tscsharp |
| 136 | + from tree_sitter import Language, Parser |
| 137 | + |
| 138 | + self._language = Language(tscsharp.language()) |
| 139 | + self._parser = Parser(self._language) |
| 140 | + |
| 141 | + def extract(self, file_name: str, content: str) -> CodeSkeleton: |
| 142 | + content_bytes = content.encode("utf-8") |
| 143 | + tree = self._parser.parse(content_bytes) |
| 144 | + root = tree.root_node |
| 145 | + |
| 146 | + imports: List[str] = [] |
| 147 | + classes: List[ClassSkeleton] = [] |
| 148 | + functions: List[FunctionSig] = [] |
| 149 | + |
| 150 | + siblings = list(root.children) |
| 151 | + for idx, child in enumerate(siblings): |
| 152 | + if child.type == "using_directive": |
| 153 | + for sub in child.children: |
| 154 | + if sub.type == "identifier": |
| 155 | + imports.append(_node_text(sub, content_bytes)) |
| 156 | + elif sub.type == "qualified_name": |
| 157 | + imports.append(_node_text(sub, content_bytes)) |
| 158 | + elif child.type in ("namespace_declaration", "file_scoped_namespace_declaration"): |
| 159 | + for sub in child.children: |
| 160 | + if sub.type == "declaration_list": |
| 161 | + ns_siblings = list(sub.children) |
| 162 | + for ns_idx, ns_child in enumerate(ns_siblings): |
| 163 | + if ns_child.type in ( |
| 164 | + "class_declaration", |
| 165 | + "interface_declaration", |
| 166 | + "struct_declaration", |
| 167 | + "record_declaration", |
| 168 | + ): |
| 169 | + doc = _preceding_doc(ns_siblings, ns_idx, content_bytes) |
| 170 | + classes.append( |
| 171 | + _extract_class(ns_child, content_bytes, docstring=doc) |
| 172 | + ) |
| 173 | + elif child.type in ( |
| 174 | + "class_declaration", |
| 175 | + "interface_declaration", |
| 176 | + "struct_declaration", |
| 177 | + "record_declaration", |
| 178 | + ): |
| 179 | + doc = _preceding_doc(siblings, idx, content_bytes) |
| 180 | + classes.append(_extract_class(child, content_bytes, docstring=doc)) |
| 181 | + |
| 182 | + return CodeSkeleton( |
| 183 | + file_name=file_name, |
| 184 | + language="C#", |
| 185 | + module_doc="", |
| 186 | + imports=imports, |
| 187 | + classes=classes, |
| 188 | + functions=functions, |
| 189 | + ) |
0 commit comments