diff options
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 40 |
1 files changed, 30 insertions, 10 deletions
| @@ -21,6 +21,9 @@ | |||
| 21 | #include "list.h" | 21 | #include "list.h" |
| 22 | #include "tpool.h" | 22 | #include "tpool.h" |
| 23 | 23 | ||
| 24 | #include "queries/c.h" | ||
| 25 | #include "queries/python.h" | ||
| 26 | |||
| 24 | #define DEBUG 0 | 27 | #define DEBUG 0 |
| 25 | 28 | ||
| 26 | typedef struct { | 29 | typedef struct { |
| @@ -72,6 +75,8 @@ struct ThreadArgs { | |||
| 72 | const char *file_path; | 75 | const char *file_path; |
| 73 | const char *source_code; | 76 | const char *source_code; |
| 74 | TSLanguage *language; | 77 | TSLanguage *language; |
| 78 | const char *query_string; | ||
| 79 | uint32_t query_len; | ||
| 75 | const char *cfname; | 80 | const char *cfname; |
| 76 | }; | 81 | }; |
| 77 | 82 | ||
| @@ -92,15 +97,12 @@ void parse_source_file(void *arg) { | |||
| 92 | ts_parser_parse_string(parser, NULL, source_code, strlen(source_code)); | 97 | ts_parser_parse_string(parser, NULL, source_code, strlen(source_code)); |
| 93 | TSNode root_node = ts_tree_root_node(tree); | 98 | TSNode root_node = ts_tree_root_node(tree); |
| 94 | 99 | ||
| 95 | const char *query_string = | 100 | const char *query_string = args->query_string; |
| 96 | "(function_definition type: (_) @ftype declarator: (function_declarator declarator: (identifier) @fname parameters: (parameter_list) @fparams))" | 101 | uint32_t query_len = args->query_len; |
| 97 | "(function_definition type: (_) @ftype declarator: (pointer_declarator declarator: (function_declarator declarator: (identifier) @fname parameters: (parameter_list) @fparams)))" | ||
| 98 | "(declaration type: (_) @ftype declarator: (function_declarator declarator: (identifier) @fname parameters: (parameter_list) @fparams))" | ||
| 99 | "(declaration type: (_) @ftype declarator: (pointer_declarator declarator: (function_declarator declarator: (identifier) @fname parameters: (parameter_list) @fparams)))"; | ||
| 100 | 102 | ||
| 101 | uint32_t error_offset; | 103 | uint32_t error_offset; |
| 102 | TSQueryError error_type; | 104 | TSQueryError error_type; |
| 103 | TSQuery *query = ts_query_new(language, query_string, strlen(query_string), &error_offset, &error_type); | 105 | TSQuery *query = ts_query_new(language, query_string, query_len, &error_offset, &error_type); |
| 104 | 106 | ||
| 105 | TSQueryCursor *query_cursor = ts_query_cursor_new(); | 107 | TSQueryCursor *query_cursor = ts_query_cursor_new(); |
| 106 | ts_query_cursor_exec(query_cursor, query, root_node); | 108 | ts_query_cursor_exec(query_cursor, query, root_node); |
| @@ -122,7 +124,7 @@ void parse_source_file(void *arg) { | |||
| 122 | fn.fname = extract_value(captured_node, source_code); | 124 | fn.fname = extract_value(captured_node, source_code); |
| 123 | 125 | ||
| 124 | TSPoint start_point = ts_node_start_point(captured_node); | 126 | TSPoint start_point = ts_node_start_point(captured_node); |
| 125 | fn.lineno = start_point.row; | 127 | fn.lineno = start_point.row + 1; |
| 126 | } | 128 | } |
| 127 | 129 | ||
| 128 | if (strcmp(capture_name, "ftype") == 0) { | 130 | if (strcmp(capture_name, "ftype") == 0) { |
| @@ -140,7 +142,7 @@ void parse_source_file(void *arg) { | |||
| 140 | char *result = strstr(fn.fname, cfname); | 142 | char *result = strstr(fn.fname, cfname); |
| 141 | if (result != NULL) { | 143 | if (result != NULL) { |
| 142 | char *fparams_formatted = remove_newlines(fn.fparams); | 144 | char *fparams_formatted = remove_newlines(fn.fparams); |
| 143 | printf("%s:%zu:\t%s %s %s\n", file_path, fn.lineno, fn.ftype, fn.fname, fparams_formatted); | 145 | printf("%s:%zu: %s %s %s\n", file_path, fn.lineno, fn.ftype ? fn.ftype : "", fn.fname, fparams_formatted ? fparams_formatted : ""); |
| 144 | free(fparams_formatted); | 146 | free(fparams_formatted); |
| 145 | } | 147 | } |
| 146 | } | 148 | } |
| @@ -205,7 +207,23 @@ int main(int argc, char *argv[]) { | |||
| 205 | const char *file_path = current->file_path; | 207 | const char *file_path = current->file_path; |
| 206 | const char *extension = get_file_extension(file_path); | 208 | const char *extension = get_file_extension(file_path); |
| 207 | 209 | ||
| 208 | if (extension != NULL && (strcmp(extension, "c") == 0 || strcmp(extension, "h") == 0)) { | 210 | TSLanguage *lang = NULL; |
| 211 | const char *query_string = NULL; | ||
| 212 | uint32_t query_len = 0; | ||
| 213 | |||
| 214 | if (extension != NULL) { | ||
| 215 | if (strcmp(extension, "c") == 0 || strcmp(extension, "h") == 0) { | ||
| 216 | lang = tree_sitter_c(); | ||
| 217 | query_string = (const char *)query_c; | ||
| 218 | query_len = query_c_len; | ||
| 219 | } else if (strcmp(extension, "py") == 0) { | ||
| 220 | lang = tree_sitter_python(); | ||
| 221 | query_string = (const char *)query_python; | ||
| 222 | query_len = query_python_len; | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | if (lang != NULL && query_string != NULL) { | ||
| 209 | struct FileContent source_file = read_entire_file(file_path); | 227 | struct FileContent source_file = read_entire_file(file_path); |
| 210 | if (source_file.content != NULL) { | 228 | if (source_file.content != NULL) { |
| 211 | struct ThreadArgs *thread_args = malloc(sizeof(struct ThreadArgs)); | 229 | struct ThreadArgs *thread_args = malloc(sizeof(struct ThreadArgs)); |
| @@ -217,7 +235,9 @@ int main(int argc, char *argv[]) { | |||
| 217 | 235 | ||
| 218 | thread_args->file_path = file_path; | 236 | thread_args->file_path = file_path; |
| 219 | thread_args->source_code = source_file.content; | 237 | thread_args->source_code = source_file.content; |
| 220 | thread_args->language = tree_sitter_c(); | 238 | thread_args->language = lang; |
| 239 | thread_args->query_string = query_string; | ||
| 240 | thread_args->query_len = query_len; | ||
| 221 | thread_args->cfname = cfname; | 241 | thread_args->cfname = cfname; |
| 222 | 242 | ||
| 223 | tp_add_job(pool, (thread_func_t)parse_source_file, thread_args); | 243 | tp_add_job(pool, (thread_func_t)parse_source_file, thread_args); |
