ArKanjo 0.2
A tool for find code duplicated functions in codebases
Loading...
Searching...
No Matches
tree_sitter_parser.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <filesystem>
5#include <functional>
6
7#include <tree_sitter/api.h>
8
11
12namespace fs = std::filesystem;
13
15 static bool is_function_empty(TSNode body);
16
17 static std::string detect_language(const std::string& path);
18
19 static std::string get_full_signature(TSNode func_node, const std::string& source);
20
21 // Extracts the function name from a Tree-sitter function node across multiple languages.
22 //
23 // Supports different AST structures depending on the language:
24 // - C/C++: uses "declarator" field (function_definition)
25 // - Rust: uses "name" field (function_item)
26 // - Fallback: recursively searches for an identifier-like node
27 //
28 // This abstraction allows the same FunctionBreaker pipeline to work across
29 // multiple Tree-sitter grammars without hardcoding per-language parsers.
30 //
31 static std::string get_function_name(TSNode func_node, const std::string& source);
32
33 static TSNode get_body(TSNode node);
34
35 static void collect_functions(
36 TSNode node, const std::string& source, const fs::path& relative_path,
37 const std::shared_ptr<TSTree>& tree,
38 std::function<void(const FunctionData&)> callback);
39
40 public:
41 static void process_file(
42 const fs::path& file_path, const fs::path& relative_path, const std::string& source_code,
43 std::function<void(const FunctionData&)> callback);
44
45 explicit TreeSitterParser() = default;
46};
static void process_file(const fs::path &file_path, const fs::path &relative_path, const std::string &source_code, std::function< void(const FunctionData &)> callback)
TreeSitterParser()=default