1#include "api.h"
 2#include "bindings.h"
 3#include <string.h>
 4#include <stdio.h>
 5
 6static void stderr_log(void *payload, TSLogType type, const char *msg)
 7{
 8    bool include_lexing = (bool)payload;
 9    switch (type)
10    {
11    case TSLogTypeParse:
12        fprintf(stderr, "* %s\n", msg);
13        break;
14    case TSLogTypeLex:
15        if (include_lexing)
16            fprintf(stderr, "  %s\n", msg);
17        break;
18    }
19}
20
21TSLogger stderr_logger_new(bool include_lexing)
22{
23    TSLogger result;
24    result.payload = (void *)include_lexing;
25    result.log = stderr_log;
26    return result;
27}
28
29const char *call_callReadFunc(void *payload, uint32_t byte_index, TSPoint position, uint32_t *bytes_read)
30{
31    ParsePayload *p = payload;
32    if (p->previous_content != NULL)
33    {
34        free(p->previous_content);
35    }
36    p->previous_content = callReadFunc(p->read_function_id, byte_index, position, bytes_read);
37    return p->previous_content;
38}
39
40TSTree *call_ts_parser_parse(TSParser *self, const TSTree *old_tree, int read_function_id, TSInputEncoding encoding)
41{
42    ParsePayload payload = {read_function_id, NULL};
43    TSInput input = {&payload, call_callReadFunc, encoding};
44    TSTree *tree = ts_parser_parse(self, old_tree, input);
45    if (payload.previous_content != NULL)
46    {
47        free(payload.previous_content);
48    }
49    return tree;
50}