|
1 |
| -# import io |
2 | 1 | # import os
|
3 |
| -# from ..utils import * |
4 | 2 | # from pathlib import Path
|
| 3 | + |
| 4 | +# from multilspy import SyncLanguageServer |
5 | 5 | # from ...entities import *
|
6 | 6 | # from ...graph import Graph
|
7 | 7 | # from typing import Optional
|
|
17 | 17 | # def __init__(self) -> None:
|
18 | 18 | # super().__init__(Language(tsc.language()))
|
19 | 19 |
|
| 20 | +# def get_entity_label(self, node: Node) -> str: |
| 21 | +# if node.type == 'struct_specifier': |
| 22 | +# return "Struct" |
| 23 | +# elif node.type == 'function_definition': |
| 24 | +# return "Function" |
| 25 | +# raise ValueError(f"Unknown entity type: {node.type}") |
| 26 | + |
| 27 | +# def get_entity_name(self, node: Node) -> str: |
| 28 | +# if node.type in ['struct_specifier', 'function_definition']: |
| 29 | +# return node.child_by_field_name('name').text.decode('utf-8') |
| 30 | +# raise ValueError(f"Unknown entity type: {node.type}") |
| 31 | + |
| 32 | +# def get_entity_docstring(self, node: Node) -> Optional[str]: |
| 33 | +# if node.type in ['struct_specifier', 'function_definition']: |
| 34 | +# body = node.child_by_field_name('body') |
| 35 | +# if body.child_count > 0 and body.children[0].type == 'expression_statement': |
| 36 | +# docstring_node = body.children[0].child(0) |
| 37 | +# return docstring_node.text.decode('utf-8') |
| 38 | +# return None |
| 39 | +# raise ValueError(f"Unknown entity type: {node.type}") |
| 40 | + |
| 41 | +# def get_entity_types(self) -> list[str]: |
| 42 | +# return ['struct_specifier', 'function_definition'] |
| 43 | + |
20 | 44 | # def process_pointer_declaration(self, node: Node) -> tuple[str, int]:
|
21 | 45 | # """
|
22 | 46 | # Processes a pointer declaration node to determine the argument name and pointer count.
|
|
313 | 337 | # # Connect parent to entity
|
314 | 338 | # graph.connect_entities('DEFINES', parent.id, entity.id)
|
315 | 339 |
|
316 |
| -# def first_pass(self, path: Path, graph:Graph) -> None: |
317 |
| -# """ |
318 |
| -# Perform the first pass processing of a C source file or header file. |
319 |
| - |
320 |
| -# Args: |
321 |
| -# path (Path): The path to the C source file or header file. |
322 |
| -# f (io.TextIOWrapper): The file object representing the opened C source file or header file. |
323 |
| -# graph (Graph): The Graph object where entities will be added. |
324 |
| - |
325 |
| -# Returns: |
326 |
| -# None |
327 |
| - |
328 |
| -# Raises: |
329 |
| -# None |
330 |
| - |
331 |
| -# This function processes the specified C source file or header file to extract and add function definitions |
332 |
| -# and struct definitions to the provided graph object. |
333 |
| - |
334 |
| -# - If the file path does not end with '.c' or '.h', it logs a debug message and skips processing. |
335 |
| -# - It creates a File entity representing the file and adds it to the graph. |
336 |
| -# - It parses the file content using a parser instance (`self.parser`). |
337 |
| -# - Function definitions and struct definitions are extracted using Tree-sitter queries. |
338 |
| -# - Each function definition is processed using `self.process_function_definition`. |
339 |
| -# - Each struct definition is processed using `self.process_struct_specifier`. |
340 |
| -# """ |
341 |
| - |
342 |
| -# if path.suffix != '.c' and path.suffix != '.h': |
343 |
| -# logger.debug(f"Skipping none C file {path}") |
344 |
| -# return |
345 |
| - |
346 |
| -# logger.info(f"Processing {path}") |
347 |
| - |
348 |
| -# # Create file entity |
349 |
| -# file = File(os.path.dirname(path), path.name, path.suffix) |
350 |
| -# graph.add_file(file) |
351 |
| - |
352 |
| -# # Parse file |
353 |
| -# source_code = path.read_bytes() |
354 |
| -# tree = self.parser.parse(source_code) |
355 |
| -# try: |
356 |
| -# source_code = source_code.decode('utf-8') |
357 |
| -# except Exception as e: |
358 |
| -# logger.error(f"Failed decoding source code: {e}") |
359 |
| -# source_code = '' |
360 |
| - |
361 |
| -# # Process function definitions |
362 |
| -# query = self.language.query("(function_definition) @function") |
363 |
| -# captures = query.captures(tree.root_node) |
364 |
| -# # captures: {'function': |
365 |
| -# # [<Node type=function_definition, start_point=(0, 0), end_point=(7, 1)>, |
366 |
| -# # <Node type=function_definition, start_point=(15, 0), end_point=(18, 1)> |
367 |
| -# # ] |
368 |
| -# # } |
369 |
| - |
370 |
| -# if 'function' in captures: |
371 |
| -# functions = captures['function'] |
372 |
| -# for node in functions: |
373 |
| -# self.process_function_definition(file, node, path, graph, source_code) |
374 |
| - |
375 |
| -# # Process struct definitions |
376 |
| -# query = self.language.query("(struct_specifier) @struct") |
377 |
| -# captures = query.captures(tree.root_node) |
378 |
| - |
379 |
| -# if 'struct' in captures: |
380 |
| -# structs = captures['struct'] |
381 |
| -# # captures: {'struct': |
382 |
| -# # [ |
383 |
| -# # <Node type=struct_specifier, start_point=(9, 0), end_point=(13, 1)> |
384 |
| -# # ] |
385 |
| -# # } |
386 |
| -# for node in structs: |
387 |
| -# self.process_struct_specifier(file, node, path, graph) |
| 340 | +# def add_symbols(self, entity: Entity) -> None: |
| 341 | +# if entity.node.type == 'struct_specifier': |
| 342 | +# superclasses = entity.node.child_by_field_name("superclasses") |
| 343 | +# if superclasses: |
| 344 | +# base_classes_query = self.language.query("(argument_list (_) @base_class)") |
| 345 | +# base_classes_captures = base_classes_query.captures(superclasses) |
| 346 | +# if 'base_class' in base_classes_captures: |
| 347 | +# for base_class in base_classes_captures['base_class']: |
| 348 | +# entity.add_symbol("base_class", base_class) |
| 349 | +# elif entity.node.type == 'function_definition': |
| 350 | +# query = self.language.query("(call) @reference.call") |
| 351 | +# captures = query.captures(entity.node) |
| 352 | +# if 'reference.call' in captures: |
| 353 | +# for caller in captures['reference.call']: |
| 354 | +# entity.add_symbol("call", caller) |
| 355 | +# query = self.language.query("(typed_parameter type: (_) @parameter)") |
| 356 | +# captures = query.captures(entity.node) |
| 357 | +# if 'parameter' in captures: |
| 358 | +# for parameter in captures['parameter']: |
| 359 | +# entity.add_symbol("parameters", parameter) |
| 360 | +# return_type = entity.node.child_by_field_name('return_type') |
| 361 | +# if return_type: |
| 362 | +# entity.add_symbol("return_type", return_type) |
| 363 | + |
| 364 | +# def resolve_type(self, files: dict[Path, File], lsp: SyncLanguageServer, path: Path, node: Node) -> list[Entity]: |
| 365 | +# res = [] |
| 366 | +# for file, resolved_node in self.resolve(files, lsp, path, node): |
| 367 | +# type_dec = self.find_parent(resolved_node, ['struct_specifier']) |
| 368 | +# res.append(file.entities[type_dec]) |
| 369 | +# return res |
| 370 | + |
| 371 | +# def resolve_method(self, files: dict[Path, File], lsp: SyncLanguageServer, path: Path, node: Node) -> list[Entity]: |
| 372 | +# res = [] |
| 373 | +# for file, resolved_node in self.resolve(files, lsp, path, node): |
| 374 | +# method_dec = self.find_parent(resolved_node, ['function_definition']) |
| 375 | +# if not method_dec: |
| 376 | +# continue |
| 377 | +# if method_dec in file.entities: |
| 378 | +# res.append(file.entities[method_dec]) |
| 379 | +# return res |
| 380 | + |
| 381 | +# def resolve_symbol(self, files: dict[Path, File], lsp: SyncLanguageServer, path: Path, key: str, symbol: Node) -> Entity: |
| 382 | +# if key in ["parameters", "return_type"]: |
| 383 | +# return self.resolve_type(files, lsp, path, symbol) |
| 384 | +# elif key in ["call"]: |
| 385 | +# return self.resolve_method(files, lsp, path, symbol) |
| 386 | +# else: |
| 387 | +# raise ValueError(f"Unknown key {key}") |
388 | 388 |
|
389 | 389 | # def second_pass(self, path: Path, graph: Graph) -> None:
|
390 | 390 | # """
|
|
0 commit comments