summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 23:45:20 +0100
committerMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 23:45:20 +0100
commit8ab1da7853f6dd309f2d3677ca109737f929ab4a (patch)
tree8afa3fafa23badd9b99aca51fce05a15ee7c2f48 /main.c
parentdcacc00e3750300617ba6e16eb346713f91a783a (diff)
downloadcrep-8ab1da7853f6dd309f2d3677ca109737f929ab4a.tar.gz
Add PHP support
Diffstat (limited to 'main.c')
-rw-r--r--main.c44
1 files changed, 32 insertions, 12 deletions
diff --git a/main.c b/main.c
index 4ff4ab5..6567e82 100644
--- a/main.c
+++ b/main.c
@@ -23,9 +23,14 @@
#include "queries/c.h"
#include "queries/python.h"
+#include "queries/php.h"
#define DEBUG 0
+TSLanguage *tree_sitter_c(void);
+TSLanguage *tree_sitter_python(void);
+TSLanguage *tree_sitter_php(void);
+
typedef struct {
const char *fname;
const char *ftype;
@@ -95,6 +100,15 @@ void parse_source_file(void *arg) {
TSTree *tree =
ts_parser_parse_string(parser, NULL, source_code, strlen(source_code));
+ if (tree == NULL) {
+ if (DEBUG) {
+ fprintf(stderr, "Parsing failed for file: %s\n", file_path);
+ }
+ ts_parser_delete(parser);
+ free((void *)source_code);
+ free(args);
+ return;
+ }
TSNode root_node = ts_tree_root_node(tree);
const char *query_string = args->query_string;
@@ -104,13 +118,23 @@ void parse_source_file(void *arg) {
TSQueryError error_type;
TSQuery *query = ts_query_new(language, query_string, query_len, &error_offset, &error_type);
+ if (query == NULL) {
+ if (DEBUG) {
+ printf("Query creation failed at offset %u with error type %d\n", error_offset, error_type);
+ }
+ ts_tree_delete(tree);
+ ts_parser_delete(parser);
+ free((void *)source_code);
+ free(args);
+ return;
+ }
+
TSQueryCursor *query_cursor = ts_query_cursor_new();
ts_query_cursor_exec(query_cursor, query, root_node);
- if (query != NULL) {
- TSQueryMatch match;
- while (ts_query_cursor_next_match(query_cursor, &match)) {
- Function fn = {0};
+ TSQueryMatch match;
+ while (ts_query_cursor_next_match(query_cursor, &match)) {
+ Function fn = {0};
for (unsigned i = 0; i < match.capture_count; i++) {
TSQueryCapture capture = match.captures[i];
@@ -152,11 +176,6 @@ void parse_source_file(void *arg) {
free((void *)fn.ftype);
free((void *)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);
@@ -185,9 +204,6 @@ int main(int argc, char *argv[]) {
const char *cfname = argv[1];
char *directory = (argc > 2) ? argv[2] : ".";
- TSLanguage *tree_sitter_c(void);
- TSLanguage *tree_sitter_python(void);
-
Node *head = NULL;
list_files_recursively(directory, &head);
int list_size = size_of_file_list(head);
@@ -220,6 +236,10 @@ int main(int argc, char *argv[]) {
lang = tree_sitter_python();
query_string = (const char *)query_python;
query_len = query_python_len;
+ } else if (strcmp(extension, "php") == 0) {
+ lang = tree_sitter_php();
+ query_string = (const char *)query_php;
+ query_len = query_php_len;
}
}