|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import tree_sitter_c |
| 4 | +from tree_sitter import Language, Node, Parser |
| 5 | + |
| 6 | +from server.parser.base import CodeSymbol, _node_text |
| 7 | + |
| 8 | +C_LANGUAGE = Language(tree_sitter_c.language()) |
| 9 | + |
| 10 | + |
| 11 | +def _docstring(node: Node, source: bytes) -> str | None: |
| 12 | + prev = node.prev_sibling |
| 13 | + while prev is not None: |
| 14 | + if prev.type == "comment": |
| 15 | + text = _node_text(prev, source) |
| 16 | + if text.startswith("/**") or text.startswith("///"): |
| 17 | + return text |
| 18 | + break |
| 19 | + if prev.type in ("\n", " "): |
| 20 | + prev = prev.prev_sibling |
| 21 | + continue |
| 22 | + break |
| 23 | + return None |
| 24 | + |
| 25 | + |
| 26 | +def _function_name(declarator: Node, source: bytes) -> str | None: |
| 27 | + """Drill through pointer/parenthesized declarators to the bare identifier.""" |
| 28 | + while declarator is not None: |
| 29 | + if declarator.type == "function_declarator": |
| 30 | + inner = declarator.child_by_field_name("declarator") |
| 31 | + if inner is None: |
| 32 | + return None |
| 33 | + declarator = inner |
| 34 | + continue |
| 35 | + if declarator.type in ("identifier", "field_identifier"): |
| 36 | + return _node_text(declarator, source) |
| 37 | + if declarator.type == "destructor_name": |
| 38 | + for child in declarator.children: |
| 39 | + if child.type == "identifier": |
| 40 | + return "~" + _node_text(child, source) |
| 41 | + return None |
| 42 | + # pointer_declarator, parenthesized_declarator, … |
| 43 | + inner = declarator.child_by_field_name("declarator") |
| 44 | + if inner is None: |
| 45 | + return None |
| 46 | + declarator = inner |
| 47 | + return None |
| 48 | + |
| 49 | + |
| 50 | +def _signature(node: Node, source: bytes) -> str: |
| 51 | + body = node.child_by_field_name("body") |
| 52 | + if body: |
| 53 | + return ( |
| 54 | + source[node.start_byte : body.start_byte] |
| 55 | + .decode("utf-8", errors="replace") |
| 56 | + .strip() |
| 57 | + ) |
| 58 | + return _node_text(node, source).split("{", 1)[0].split(";", 1)[0].strip() |
| 59 | + |
| 60 | + |
| 61 | +def _parse_function( |
| 62 | + node: Node, |
| 63 | + source: bytes, |
| 64 | + file_path: str, |
| 65 | + language: str, |
| 66 | + parent_name: str | None = None, |
| 67 | + package: str | None = None, |
| 68 | +) -> CodeSymbol | None: |
| 69 | + declarator = node.child_by_field_name("declarator") |
| 70 | + if declarator is None: |
| 71 | + return None |
| 72 | + name = _function_name(declarator, source) |
| 73 | + if name is None: |
| 74 | + return None |
| 75 | + |
| 76 | + sym_type = "method" if parent_name else "function" |
| 77 | + |
| 78 | + return CodeSymbol( |
| 79 | + name=name, |
| 80 | + symbol_type=sym_type, |
| 81 | + language=language, |
| 82 | + source=_node_text(node, source), |
| 83 | + file_path=file_path, |
| 84 | + start_line=node.start_point[0] + 1, |
| 85 | + end_line=node.end_point[0] + 1, |
| 86 | + parent_name=parent_name, |
| 87 | + package=package, |
| 88 | + signature=_signature(node, source), |
| 89 | + docstring=_docstring(node, source), |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +def _parse_typedef( |
| 94 | + node: Node, source: bytes, file_path: str, language: str |
| 95 | +) -> CodeSymbol | None: |
| 96 | + declarator = node.child_by_field_name("declarator") |
| 97 | + if declarator is None: |
| 98 | + return None |
| 99 | + name = _function_name(declarator, source) |
| 100 | + if name is None: |
| 101 | + # typedef sometimes has identifier directly |
| 102 | + for child in node.children: |
| 103 | + if child.type == "type_identifier": |
| 104 | + name = _node_text(child, source) |
| 105 | + break |
| 106 | + if not name: |
| 107 | + return None |
| 108 | + |
| 109 | + return CodeSymbol( |
| 110 | + name=name, |
| 111 | + symbol_type="type", |
| 112 | + language=language, |
| 113 | + source=_node_text(node, source), |
| 114 | + file_path=file_path, |
| 115 | + start_line=node.start_point[0] + 1, |
| 116 | + end_line=node.end_point[0] + 1, |
| 117 | + signature=_node_text(node, source).split(";", 1)[0].strip(), |
| 118 | + docstring=_docstring(node, source), |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +_TYPED_NAMED_NODE = { |
| 123 | + "struct_specifier": "struct", |
| 124 | + "enum_specifier": "enum", |
| 125 | + "union_specifier": "union", |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | +def _parse_named_type( |
| 130 | + node: Node, source: bytes, file_path: str, language: str |
| 131 | +) -> CodeSymbol | None: |
| 132 | + name_node = node.child_by_field_name("name") |
| 133 | + if name_node is None: |
| 134 | + return None |
| 135 | + return CodeSymbol( |
| 136 | + name=_node_text(name_node, source), |
| 137 | + symbol_type=_TYPED_NAMED_NODE[node.type], |
| 138 | + language=language, |
| 139 | + source=_node_text(node, source), |
| 140 | + file_path=file_path, |
| 141 | + start_line=node.start_point[0] + 1, |
| 142 | + end_line=node.end_point[0] + 1, |
| 143 | + signature=_node_text(node, source).split("{", 1)[0].strip(), |
| 144 | + docstring=_docstring(node, source), |
| 145 | + ) |
| 146 | + |
| 147 | + |
| 148 | +def _walk_c( |
| 149 | + container: Node, |
| 150 | + source: bytes, |
| 151 | + file_path: str, |
| 152 | + symbols: list[CodeSymbol], |
| 153 | +) -> None: |
| 154 | + for child in container.children: |
| 155 | + if child.type == "function_definition": |
| 156 | + sym = _parse_function(child, source, file_path, "c") |
| 157 | + if sym: |
| 158 | + symbols.append(sym) |
| 159 | + elif child.type in _TYPED_NAMED_NODE: |
| 160 | + sym = _parse_named_type(child, source, file_path, "c") |
| 161 | + if sym: |
| 162 | + symbols.append(sym) |
| 163 | + elif child.type == "type_definition": |
| 164 | + sym = _parse_typedef(child, source, file_path, "c") |
| 165 | + if sym: |
| 166 | + symbols.append(sym) |
| 167 | + |
| 168 | + |
| 169 | +class CParser: |
| 170 | + def __init__(self) -> None: |
| 171 | + self._parser = Parser(C_LANGUAGE) |
| 172 | + |
| 173 | + def supported_extensions(self) -> list[str]: |
| 174 | + return [".c", ".h"] |
| 175 | + |
| 176 | + def language(self) -> str: |
| 177 | + return "c" |
| 178 | + |
| 179 | + def parse_file(self, source: bytes, file_path: str) -> list[CodeSymbol]: |
| 180 | + tree = self._parser.parse(source) |
| 181 | + symbols: list[CodeSymbol] = [] |
| 182 | + _walk_c(tree.root_node, source, file_path, symbols) |
| 183 | + return symbols |
0 commit comments