|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | +#include <tree_sitter/api.h> |
| 5 | +#include "tree_sitter/parser.h" |
| 6 | + |
| 7 | +extern const TSLanguage *tree_sitter_markdown(void); |
| 8 | + |
| 9 | +char *escape_double_quotes(const char *type) { |
| 10 | + char dq[] = "\""; |
| 11 | + int result = strcmp(type, dq); |
| 12 | + if (result == 0) { |
| 13 | + return "\\\""; |
| 14 | + } else { |
| 15 | + return type; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +char *escape_back_slash(const char *type) { |
| 20 | + char dq[] = "\\"; |
| 21 | + int result = strcmp(type, dq); |
| 22 | + if (result == 0) { |
| 23 | + return "\\\\"; |
| 24 | + } else { |
| 25 | + return type; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// Function to print an S-expression with line and column information |
| 30 | +void print_sexp_with_position(TSNode node, const TSLanguage *lang, const char *source) { |
| 31 | + TSSymbol symbol = ts_node_symbol(node); |
| 32 | + const char *type = ts_language_symbol_name( lang, symbol); |
| 33 | + |
| 34 | + // Get the start position of the node |
| 35 | + TSPoint start_point = ts_node_start_point(node); |
| 36 | + |
| 37 | + // Get the end position of the node |
| 38 | + TSPoint end_point = ts_node_end_point(node); |
| 39 | + |
| 40 | + char *escaped1 = escape_double_quotes(type); |
| 41 | + char *escaped = escape_back_slash(escaped1); |
| 42 | + |
| 43 | + // Print the S-expression with line and column information |
| 44 | + printf("(\"%s\" \"%d:%d-%d:%d\" ", escaped, start_point.row + 1, start_point.column + 1, end_point.row + 1, end_point.column + 1); |
| 45 | + |
| 46 | + // Recursively print child nodes |
| 47 | + for (uint32_t i = 0, child_count = ts_node_child_count(node); i < child_count; i++) { |
| 48 | + TSNode child = ts_node_child(node, i); |
| 49 | + print_sexp_with_position(child, lang, source); |
| 50 | + } |
| 51 | + |
| 52 | + printf(")"); |
| 53 | +} |
| 54 | + |
| 55 | +int main(int argc, char *argv[]) { |
| 56 | + // Initialize the Tree-sitter library |
| 57 | + TSParser *parser = ts_parser_new(); |
| 58 | + |
| 59 | + // Load the Markdown language |
| 60 | + const TSLanguage *markdown_language = tree_sitter_markdown(); |
| 61 | + |
| 62 | + if (!markdown_language) { |
| 63 | + fprintf(stderr, "Error loading Markdown language\n"); |
| 64 | + return 1; |
| 65 | + } |
| 66 | + |
| 67 | + // Set the parser's language to Markdown |
| 68 | + ts_parser_set_language(parser, markdown_language); |
| 69 | + |
| 70 | + // Read the Markdown input from a file (change this path to your Markdown file) |
| 71 | + /*FILE *input_file = fopen(argv[1], "r");*/ |
| 72 | + /*if (!input_file) {*/ |
| 73 | + /*fprintf(stderr, "Error opening input file\n");*/ |
| 74 | + /*return 1;*/ |
| 75 | + /*}*/ |
| 76 | + |
| 77 | + // Get the size of the input file |
| 78 | + /*fseek(input_file, 0, SEEK_END);*/ |
| 79 | + /*long file_size = ftell(input_file);*/ |
| 80 | + /*rewind(input_file);*/ |
| 81 | + |
| 82 | + // Read the file content into a buffer |
| 83 | + /*char *input_buffer = (char *)malloc(file_size + 1);*/ |
| 84 | + /*fread(input_buffer, 1, file_size, input_file);*/ |
| 85 | + |
| 86 | + char *input_buffer = NULL; |
| 87 | + size_t buffer_size = 0; |
| 88 | + size_t file_size = 0; |
| 89 | + |
| 90 | + while (1) { |
| 91 | + char chunk[1024]; // Read data in 1KB chunks |
| 92 | + |
| 93 | + // Read a chunk of data from stdin |
| 94 | + size_t bytes_read = fread(chunk, 1, sizeof(chunk), stdin); |
| 95 | + |
| 96 | + if (bytes_read == 0) { |
| 97 | + // No more data to read (EOF or error) |
| 98 | + break; |
| 99 | + } |
| 100 | + |
| 101 | + // Resize the buffer to accommodate the new data |
| 102 | + file_size += bytes_read; |
| 103 | + input_buffer = (char *)realloc(input_buffer, file_size); |
| 104 | + |
| 105 | + if (input_buffer == NULL) { |
| 106 | + perror("Memory allocation failed"); |
| 107 | + exit(EXIT_FAILURE); |
| 108 | + } |
| 109 | + |
| 110 | + // Copy the chunk into the buffer |
| 111 | + memcpy(input_buffer + file_size - bytes_read, chunk, bytes_read); |
| 112 | + } |
| 113 | + |
| 114 | + input_buffer[file_size] = '\0'; |
| 115 | + |
| 116 | + // Create a Tree-sitter input document |
| 117 | + // TSInput input = ts_input_from_string(input_buffer); |
| 118 | + |
| 119 | + // Parse the input |
| 120 | + TSTree *tree = ts_parser_parse_string(parser, NULL, input_buffer, file_size); |
| 121 | + |
| 122 | + TSNode root_node = ts_tree_root_node(tree); |
| 123 | + |
| 124 | + // Check if parsing was successful |
| 125 | + if (ts_node_is_null(root_node)) { |
| 126 | + fprintf(stderr, "Parsing failed\n"); |
| 127 | + return 1; |
| 128 | + } |
| 129 | + |
| 130 | + // Perform your desired operations with the parsed tree here |
| 131 | + // For example, you can traverse the tree and analyze the Markdown document's structure. |
| 132 | + const char *root_string = ts_node_string(root_node); |
| 133 | + print_sexp_with_position(root_node, markdown_language, input_buffer); |
| 134 | + |
| 135 | + // Clean up resources |
| 136 | + ts_parser_delete(parser); |
| 137 | + //fclose(input_file); |
| 138 | + free(input_buffer); |
| 139 | + |
| 140 | + return 0; |
| 141 | +} |
| 142 | + |
0 commit comments