diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2023-11-09 23:19:53 +0100 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2023-11-09 23:19:53 +0100 |
| commit | 1566b6faa8534118c3566188181367cd0868468f (patch) | |
| tree | 1de8d4b369efb5e592685a31088f798a6b63ffa1 /main.c | |
| parent | 349991bf6efe473ab9a5cbdae0a8114d72b997e3 (diff) | |
| download | crep-1566b6faa8534118c3566188181367cd0868468f.tar.gz | |
Added partial matching and introduced threads
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 139 |
1 files changed, 105 insertions, 34 deletions
| @@ -1,11 +1,13 @@ | |||
| 1 | #include <assert.h> | ||
| 1 | #include <stdio.h> | 2 | #include <stdio.h> |
| 2 | #include <stdlib.h> | 3 | #include <stdlib.h> |
| 3 | #include <assert.h> | ||
| 4 | #include <string.h> | 4 | #include <string.h> |
| 5 | #include <pthread.h> | ||
| 5 | 6 | ||
| 6 | #include <tree_sitter/api.h> | 7 | #include <tree_sitter/api.h> |
| 7 | 8 | ||
| 8 | #include "file.h" | 9 | #include "file.h" |
| 10 | #include "list.h" | ||
| 9 | 11 | ||
| 10 | #define DEBUG 1 | 12 | #define DEBUG 1 |
| 11 | 13 | ||
| @@ -30,7 +32,41 @@ const char *extract_value(TSNode captured_node, const char *source_code) { | |||
| 30 | return NULL; | 32 | return NULL; |
| 31 | } | 33 | } |
| 32 | 34 | ||
| 33 | void parse_source_file(const char *file_path, const char *source_code, TSLanguage *language) { | 35 | char* remove_newlines(const char* str) { |
| 36 | size_t length = strlen(str); | ||
| 37 | char* result = (char*)malloc(length + 1); // +1 for the null terminator | ||
| 38 | if (result == NULL) { | ||
| 39 | fprintf(stderr, "Memory allocation failed\n"); | ||
| 40 | exit(1); | ||
| 41 | } | ||
| 42 | |||
| 43 | size_t j = 0; | ||
| 44 | for (size_t i = 0; i < length; i++) { | ||
| 45 | if (str[i] != '\n') { | ||
| 46 | result[j++] = str[i]; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | result[j] = '\0'; | ||
| 51 | return result; | ||
| 52 | } | ||
| 53 | |||
| 54 | struct ThreadArgs { | ||
| 55 | const char* file_path; | ||
| 56 | const char* source_code; | ||
| 57 | TSLanguage* language; | ||
| 58 | const char* cfname; | ||
| 59 | }; | ||
| 60 | |||
| 61 | // void parse_source_file(const char *file_path, const char *source_code, TSLanguage *language, const char *cfname) { | ||
| 62 | void *parse_source_file(void *arg) { | ||
| 63 | struct ThreadArgs* args = (struct ThreadArgs*)arg; | ||
| 64 | |||
| 65 | const char *file_path = args->file_path; | ||
| 66 | const char *source_code = args->source_code; | ||
| 67 | TSLanguage *language = args->language; | ||
| 68 | const char *cfname = args->cfname; | ||
| 69 | |||
| 34 | TSParser *parser = ts_parser_new(); | 70 | TSParser *parser = ts_parser_new(); |
| 35 | ts_parser_set_language(parser, language); | 71 | ts_parser_set_language(parser, language); |
| 36 | 72 | ||
| @@ -55,8 +91,6 @@ void parse_source_file(const char *file_path, const char *source_code, TSLanguag | |||
| 55 | TSQueryCapture capture = match.captures[i]; | 91 | TSQueryCapture capture = match.captures[i]; |
| 56 | TSNode captured_node = capture.node; | 92 | TSNode captured_node = capture.node; |
| 57 | 93 | ||
| 58 | /* fprintf(stderr, "Query: %p, Capture index: %u\n", (void *)query, capture.index); */ | ||
| 59 | |||
| 60 | uint32_t capture_name_length; | 94 | uint32_t capture_name_length; |
| 61 | const char *capture_name = ts_query_capture_name_for_id(query, capture.index, &capture_name_length); | 95 | const char *capture_name = ts_query_capture_name_for_id(query, capture.index, &capture_name_length); |
| 62 | 96 | ||
| @@ -76,7 +110,17 @@ void parse_source_file(const char *file_path, const char *source_code, TSLanguag | |||
| 76 | } | 110 | } |
| 77 | } | 111 | } |
| 78 | 112 | ||
| 79 | printf("%s:%zu\t%s %s %s\n", file_path, fn.lineno, fn.ftype, fn.fname, fn.fparams); | 113 | // Full matching. |
| 114 | /* if (strcmp(fn.fname, cfname) == 0) { */ | ||
| 115 | /* printf("%s:%zu\t%s %s %s\n", file_path, fn.lineno, fn.ftype, fn.fname, fn.fparams); */ | ||
| 116 | /* } */ | ||
| 117 | |||
| 118 | // Substring matching. | ||
| 119 | char *result = strstr(fn.fname, cfname); | ||
| 120 | if (result != NULL) { | ||
| 121 | char *fparams_formatted = remove_newlines(fn.fparams); | ||
| 122 | printf("%s:%zu\t%s %s %s\n", file_path, fn.lineno, fn.ftype, fn.fname, fparams_formatted); | ||
| 123 | } | ||
| 80 | } | 124 | } |
| 81 | } else { | 125 | } else { |
| 82 | if (DEBUG) { | 126 | if (DEBUG) { |
| @@ -88,6 +132,8 @@ void parse_source_file(const char *file_path, const char *source_code, TSLanguag | |||
| 88 | ts_query_delete(query); | 132 | ts_query_delete(query); |
| 89 | ts_tree_delete(tree); | 133 | ts_tree_delete(tree); |
| 90 | ts_parser_delete(parser); | 134 | ts_parser_delete(parser); |
| 135 | |||
| 136 | return NULL; | ||
| 91 | } | 137 | } |
| 92 | 138 | ||
| 93 | const char *get_file_extension(const char *file_path) { | 139 | const char *get_file_extension(const char *file_path) { |
| @@ -98,45 +144,70 @@ const char *get_file_extension(const char *file_path) { | |||
| 98 | return NULL; | 144 | return NULL; |
| 99 | } | 145 | } |
| 100 | 146 | ||
| 101 | int main(void) { | 147 | int main(int argc, char *argv[]) { |
| 102 | const char *file_path = "examples/cmdline.c"; | 148 | if (argc < 2) { |
| 103 | /* const char *file_path = "examples/tabs.py"; */ | 149 | printf("Usage: %s <argument>\n", argv[0]); |
| 104 | const char *extension = get_file_extension(file_path); | 150 | return 1; |
| 151 | } | ||
| 152 | |||
| 153 | char *cfname = argv[1]; | ||
| 105 | 154 | ||
| 106 | TSLanguage *tree_sitter_c(void); | 155 | TSLanguage *tree_sitter_c(void); |
| 107 | TSLanguage *tree_sitter_python(void); | 156 | TSLanguage *tree_sitter_python(void); |
| 108 | 157 | ||
| 109 | struct FileContent source_file = read_entire_file(file_path); | 158 | Node *head = NULL; |
| 110 | if (source_file.content != NULL) { | 159 | list_files_recursively("./examples", &head); |
| 111 | if (DEBUG) { | 160 | int list_size = size_of_file_list(head); |
| 112 | /* fprintf(stdout, "File contents:\n%s\n", source_file.content); */ | 161 | /* pthread_t threads[list_size]; */ |
| 113 | /* fprintf(stdout, "Count of characters: %zu\n", source_file.count); */ | 162 | |
| 114 | } | 163 | printf("size: %d\n", list_size); |
| 115 | 164 | ||
| 116 | if (extension != NULL) { | 165 | Node *current = head; |
| 117 | if (DEBUG) { | 166 | int thread_index = 0; |
| 118 | fprintf(stdout, "File extension: %s\n", extension); | 167 | while (current != NULL) { |
| 119 | } | 168 | const char *file_path = current->file_path; |
| 120 | 169 | const char *extension = get_file_extension(file_path); | |
| 121 | if (strcmp(extension, "c") == 0) { | 170 | struct FileContent source_file = read_entire_file(file_path); |
| 122 | parse_source_file(file_path, source_file.content, tree_sitter_c()); | 171 | |
| 123 | } | 172 | if (source_file.content != NULL) { |
| 124 | 173 | if (extension != NULL) { | |
| 125 | if (strcmp(extension, "py") == 0) { | 174 | if (strcmp(extension, "c") == 0 || strcmp(extension, "h") == 0) { |
| 126 | parse_source_file(file_path, source_file.content, tree_sitter_python()); | 175 | /* parse_source_file(file_path, source_file.content, tree_sitter_c(), cfname); */ |
| 176 | |||
| 177 | struct ThreadArgs thread_args; | ||
| 178 | thread_args.file_path = file_path; | ||
| 179 | thread_args.source_code = source_file.content; | ||
| 180 | thread_args.language = tree_sitter_c(); | ||
| 181 | thread_args.cfname = cfname; | ||
| 182 | |||
| 183 | parse_source_file(&thread_args); | ||
| 184 | |||
| 185 | /* printf("> creating thread #%d\n", thread_index); */ | ||
| 186 | /* if (pthread_create(&threads[thread_index], NULL, parse_source_file, &thread_args) != 0) { */ | ||
| 187 | /* fprintf(stderr, "Error creating thread %d\n", thread_index); */ | ||
| 188 | /* return 1; */ | ||
| 189 | /* } */ | ||
| 190 | } | ||
| 127 | } | 191 | } |
| 192 | free((void *)source_file.content); | ||
| 128 | } else { | 193 | } else { |
| 129 | if (DEBUG) { | 194 | if (DEBUG) { |
| 130 | fprintf(stderr,"No file extension found.\n"); | 195 | fprintf(stderr, "Failed to read file.\n"); |
| 131 | } | 196 | } |
| 132 | } | 197 | } |
| 133 | 198 | current = current->next; | |
| 134 | free((void *)source_file.content); | 199 | thread_index++; |
| 135 | } else { | ||
| 136 | if (DEBUG) { | ||
| 137 | fprintf(stderr, "Failed to read file.\n"); | ||
| 138 | } | ||
| 139 | } | 200 | } |
| 140 | 201 | ||
| 202 | // Collecting threads. | ||
| 203 | /* for (int i = 0; i < list_size; i++) { */ | ||
| 204 | /* printf("> collecting thread #%d\n", thread_index); */ | ||
| 205 | /* if (pthread_join(threads[i], NULL) != 0) { */ | ||
| 206 | /* fprintf(stderr, "Error joining thread %d\n", i); */ | ||
| 207 | /* return 1; */ | ||
| 208 | /* } */ | ||
| 209 | /* } */ | ||
| 210 | |||
| 211 | free_file_list(head); | ||
| 141 | return 0; | 212 | return 0; |
| 142 | } | 213 | } |
