summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--file.c70
-rw-r--r--file.h4
-rw-r--r--main.c192
3 files changed, 133 insertions, 133 deletions
diff --git a/file.c b/file.c
index 2b82a07..d05681d 100644
--- a/file.c
+++ b/file.c
@@ -4,41 +4,41 @@
#include "file.h"
struct FileContent read_entire_file(const char *file_path) {
- struct FileContent file_data;
+ struct FileContent file_data;
+ file_data.content = NULL;
+ file_data.count = 0;
+
+ FILE *file = fopen(file_path, "rb");
+ if (file == NULL) {
+ perror("Error opening file");
+ return file_data;
+ }
+
+ fseek(file, 0, SEEK_END);
+ long file_size = ftell(file);
+ fseek(file, 0, SEEK_SET);
+
+ if (file_size == -1) {
+ perror("Error getting file size");
+ return file_data;
+ }
+
+ file_data.content = (const char *)malloc(file_size);
+ if (file_data.content == NULL) {
+ perror("Error allocating memory");
+ return file_data;
+ }
+
+ size_t bytes_read = fread((void *)file_data.content, 1, file_size, file);
+ if (bytes_read != (size_t)file_size) {
+ perror("Error reading file");
+ free((void *)file_data.content);
file_data.content = NULL;
- file_data.count = 0;
-
- FILE *file = fopen(file_path, "rb");
- if (file == NULL) {
- perror("Error opening file");
- return file_data;
- }
-
- fseek(file, 0, SEEK_END);
- long file_size = ftell(file);
- fseek(file, 0, SEEK_SET);
-
- if (file_size == -1) {
- perror("Error getting file size");
- return file_data;
- }
-
- file_data.content = (const char *)malloc(file_size);
- if (file_data.content == NULL) {
- perror("Error allocating memory");
- return file_data;
- }
-
- size_t bytes_read = fread((void *)file_data.content, 1, file_size, file);
- if (bytes_read != (size_t)file_size) {
- perror("Error reading file");
- free((void *)file_data.content);
- file_data.content = NULL;
- return file_data;
- }
-
- file_data.count = bytes_read;
-
- fclose(file);
return file_data;
+ }
+
+ file_data.count = bytes_read;
+
+ fclose(file);
+ return file_data;
}
diff --git a/file.h b/file.h
index 8b5cacb..31a6e7d 100644
--- a/file.h
+++ b/file.h
@@ -4,8 +4,8 @@
#include <stdio.h>
struct FileContent {
- const char *content;
- size_t count;
+ const char *content;
+ size_t count;
};
struct FileContent read_entire_file(const char *file_path);
diff --git a/main.c b/main.c
index 8486605..8e09859 100644
--- a/main.c
+++ b/main.c
@@ -10,133 +10,133 @@
#define DEBUG 1
typedef struct {
- const char *fname;
- const char *ftype;
- const char *fparams;
- size_t lineno;
+ const char *fname;
+ const char *ftype;
+ const char *fparams;
+ size_t lineno;
} Function;
const char *extract_value(TSNode captured_node, const char *source_code) {
- size_t start = ts_node_start_byte(captured_node);
- size_t end = ts_node_end_byte(captured_node);
- size_t length = end - start;
- char *buffer = malloc(length + 1); // +1 for the null terminator
-
- if (buffer != NULL) {
- snprintf(buffer, length + 1, "%.*s", (int)length, &source_code[start]);
- return buffer;
- }
+ size_t start = ts_node_start_byte(captured_node);
+ size_t end = ts_node_end_byte(captured_node);
+ size_t length = end - start;
+ char *buffer = malloc(length + 1); // +1 for the null terminator
+
+ if (buffer != NULL) {
+ snprintf(buffer, length + 1, "%.*s", (int)length, &source_code[start]);
+ return buffer;
+ }
- return NULL;
+ return NULL;
}
void parse_source_file(const char *file_path, const char *source_code, TSLanguage *language) {
- TSParser *parser = ts_parser_new();
- ts_parser_set_language(parser, language);
-
- TSTree *tree = ts_parser_parse_string(parser, NULL, source_code, strlen(source_code));
- TSNode root_node = ts_tree_root_node(tree);
+ TSParser *parser = ts_parser_new();
+ ts_parser_set_language(parser, language);
- const char *query_string = "(function_definition type: (primitive_type) @ftype declarator: (function_declarator declarator: (identifier) @fname parameters: (parameter_list) @fparams))";
+ TSTree *tree = ts_parser_parse_string(parser, NULL, source_code, strlen(source_code));
+ TSNode root_node = ts_tree_root_node(tree);
- uint32_t error_offset;
- TSQueryError error_type;
- TSQuery *query = ts_query_new(language, query_string, strlen(query_string), &error_offset, &error_type);
+ const char *query_string = "(function_definition type: (primitive_type) @ftype declarator: (function_declarator declarator: (identifier) @fname parameters: (parameter_list) @fparams))";
- TSQueryCursor *query_cursor = ts_query_cursor_new();
- ts_query_cursor_exec(query_cursor, query, root_node);
+ uint32_t error_offset;
+ TSQueryError error_type;
+ TSQuery *query = ts_query_new(language, query_string, strlen(query_string), &error_offset, &error_type);
- if (query != NULL) {
- TSQueryMatch match;
- while (ts_query_cursor_next_match(query_cursor, &match)) {
- Function fn = {0};
+ TSQueryCursor *query_cursor = ts_query_cursor_new();
+ ts_query_cursor_exec(query_cursor, query, root_node);
- for (unsigned i = 0; i < match.capture_count; i++) {
- TSQueryCapture capture = match.captures[i];
- TSNode captured_node = capture.node;
+ if (query != NULL) {
+ TSQueryMatch match;
+ while (ts_query_cursor_next_match(query_cursor, &match)) {
+ Function fn = {0};
- /* fprintf(stderr, "Query: %p, Capture index: %u\n", (void *)query, capture.index); */
+ for (unsigned i = 0; i < match.capture_count; i++) {
+ TSQueryCapture capture = match.captures[i];
+ TSNode captured_node = capture.node;
- uint32_t capture_name_length;
- const char *capture_name = ts_query_capture_name_for_id(query, capture.index, &capture_name_length);
+ /* fprintf(stderr, "Query: %p, Capture index: %u\n", (void *)query, capture.index); */
- if (strcmp(capture_name, "fname") == 0) {
- fn.fname = extract_value(captured_node, source_code);
+ uint32_t capture_name_length;
+ const char *capture_name = ts_query_capture_name_for_id(query, capture.index, &capture_name_length);
- TSPoint start_point = ts_node_start_point(captured_node);
- fn.lineno = start_point.row;
- }
+ if (strcmp(capture_name, "fname") == 0) {
+ fn.fname = extract_value(captured_node, source_code);
- if (strcmp(capture_name, "ftype") == 0) {
- fn.ftype = extract_value(captured_node, source_code);
- }
-
- if (strcmp(capture_name, "fparams") == 0) {
- fn.fparams = extract_value(captured_node, source_code);
- }
- }
+ TSPoint start_point = ts_node_start_point(captured_node);
+ fn.lineno = start_point.row;
+ }
- printf("%s:%zu\t%s %s %s\n", file_path, fn.lineno, fn.ftype, fn.fname, fn.fparams);
+ if (strcmp(capture_name, "ftype") == 0) {
+ fn.ftype = extract_value(captured_node, source_code);
}
- } else {
- if (DEBUG) {
- printf("Query creation failed at offset %u with error type %d\n", error_offset, error_type);
+
+ if (strcmp(capture_name, "fparams") == 0) {
+ fn.fparams = extract_value(captured_node, source_code);
}
+ }
+
+ printf("%s:%zu\t%s %s %s\n", file_path, fn.lineno, fn.ftype, fn.fname, fn.fparams);
}
+ } else {
+ if (DEBUG) {
+ printf("Query creation failed at offset %u with error type %d\n", error_offset, error_type);
+ }
+ }
- ts_query_cursor_delete(query_cursor);
- ts_query_delete(query);
- ts_tree_delete(tree);
- ts_parser_delete(parser);
+ ts_query_cursor_delete(query_cursor);
+ ts_query_delete(query);
+ ts_tree_delete(tree);
+ ts_parser_delete(parser);
}
const char *get_file_extension(const char *file_path) {
- const char *extension = strrchr(file_path, '.');
- if (extension != NULL) {
- return extension + 1;
- }
- return NULL;
+ const char *extension = strrchr(file_path, '.');
+ if (extension != NULL) {
+ return extension + 1;
+ }
+ return NULL;
}
int main(void) {
- const char *file_path = "examples/cmdline.c";
- /* const char *file_path = "examples/tabs.py"; */
- const char *extension = get_file_extension(file_path);
-
- TSLanguage *tree_sitter_c(void);
- TSLanguage *tree_sitter_python(void);
-
- struct FileContent source_file = read_entire_file(file_path);
- if (source_file.content != NULL) {
- if (DEBUG) {
- /* fprintf(stdout, "File contents:\n%s\n", source_file.content); */
- /* fprintf(stdout, "Count of characters: %zu\n", source_file.count); */
- }
+ const char *file_path = "examples/cmdline.c";
+ /* const char *file_path = "examples/tabs.py"; */
+ const char *extension = get_file_extension(file_path);
+
+ TSLanguage *tree_sitter_c(void);
+ TSLanguage *tree_sitter_python(void);
+
+ struct FileContent source_file = read_entire_file(file_path);
+ if (source_file.content != NULL) {
+ if (DEBUG) {
+ /* fprintf(stdout, "File contents:\n%s\n", source_file.content); */
+ /* fprintf(stdout, "Count of characters: %zu\n", source_file.count); */
+ }
- if (extension != NULL) {
- if (DEBUG) {
- fprintf(stdout, "File extension: %s\n", extension);
- }
-
- if (strcmp(extension, "c") == 0) {
- parse_source_file(file_path, source_file.content, tree_sitter_c());
- }
-
- if (strcmp(extension, "py") == 0) {
- parse_source_file(file_path, source_file.content, tree_sitter_python());
- }
- } else {
- if (DEBUG) {
- fprintf(stderr,"No file extension found.\n");
- }
- }
+ if (extension != NULL) {
+ if (DEBUG) {
+ fprintf(stdout, "File extension: %s\n", extension);
+ }
- free((void *)source_file.content);
+ if (strcmp(extension, "c") == 0) {
+ parse_source_file(file_path, source_file.content, tree_sitter_c());
+ }
+
+ if (strcmp(extension, "py") == 0) {
+ parse_source_file(file_path, source_file.content, tree_sitter_python());
+ }
} else {
- if (DEBUG) {
- fprintf(stderr, "Failed to read file.\n");
- }
+ if (DEBUG) {
+ fprintf(stderr,"No file extension found.\n");
+ }
+ }
+
+ free((void *)source_file.content);
+ } else {
+ if (DEBUG) {
+ fprintf(stderr, "Failed to read file.\n");
}
+ }
- return 0;
+ return 0;
}