summaryrefslogtreecommitdiff
path: root/src/source_map.c
blob: db01a2187d2d791b8d3c96896d3c2cdbd6c607c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <assert.h>

#include "source_map.h"

cmark_source_map *
source_map_new(cmark_mem *mem)
{
  cmark_source_map *res = (cmark_source_map *) mem->calloc(1, sizeof(cmark_source_map));
  res->mem = mem;
  return res;
}

void
source_map_free(cmark_source_map *self)
{
  cmark_source_extent *tmp;
  for (tmp = self->head; tmp; tmp = source_map_free_extent(self, tmp));
  self->mem->free(self);
}

cmark_source_extent *
source_map_append_extent(cmark_source_map *self, uint64_t start, uint64_t stop, cmark_node *node, cmark_extent_type type)
{
  assert (start <= stop);
  assert (!self->tail || self->tail->stop <= start);

  cmark_source_extent *res = (cmark_source_extent *) self->mem->calloc(1, sizeof(cmark_source_extent));

  res->start = start;
  res->stop = stop;
  res->node = node;
  res->type = type;

  res->next = NULL;
  res->prev = self->tail;

  if (!self->head)
    self->head = res;
  else
    self->tail->next = res;

  self->tail = res;

  return res;
}

cmark_source_extent *
source_map_insert_extent(cmark_source_map *self, cmark_source_extent *previous,
                         uint64_t start, uint64_t stop, cmark_node *node, cmark_extent_type type)
{
  if (start == stop)
    return previous;

  cmark_source_extent *extent = (cmark_source_extent *) self->mem->calloc(1, sizeof(cmark_source_extent));

  extent->start = start;
  extent->stop = stop;
  extent->node = node;
  extent->type = type;
  extent->next = previous->next;
  extent->prev = previous;
  previous->next = extent;

  if (extent->next)
    extent->next->prev = extent;
  else
    self->tail = extent;

  return extent;
}

cmark_source_extent *
source_map_free_extent(cmark_source_map *self, cmark_source_extent *extent)
{
  cmark_source_extent *next = extent->next;

  if (extent->prev)
    extent->prev->next = next;

  if (extent->next)
    extent->next->prev = extent->prev;

  if (extent == self->tail)
    self->tail = extent->prev;

  if (extent == self->head)
    self->head = extent->next;

  if (extent == self->cursor) {
    self->cursor = extent->prev;
  }

  if (extent == self->next_cursor) {
    self->next_cursor = extent->next;
  }

  self->mem->free(extent);

  return next;
}

cmark_source_extent *
source_map_stitch_extent(cmark_source_map *self, cmark_source_extent *extent,
                         cmark_node *node, uint64_t total_length)
{
  cmark_source_extent *next_extent = extent->next;
  cmark_source_extent *res;

  while (next_extent && extent->start == extent->stop) {
    extent = source_map_free_extent(self, extent);
    extent = next_extent;
    next_extent = extent->next;
  }

  if (next_extent) {
    res = source_map_insert_extent(self,
                                   extent,
                                   extent->stop,
                                   extent->next->start,
                                   node,
                                   CMARK_EXTENT_BLANK);
  } else {
    res = source_map_insert_extent(self,
                                   extent,
                                   extent->stop,
                                   total_length,
                                   node,
                                   CMARK_EXTENT_BLANK);
  }

  if (extent->start == extent->stop)
    source_map_free_extent(self, extent);

  return res;
}

cmark_source_extent *
source_map_splice_extent(cmark_source_map *self, uint64_t start, uint64_t stop,
                         cmark_node *node, cmark_extent_type type)
{
  if (!self->next_cursor) {
    self->cursor = source_map_insert_extent(self,
                                            self->cursor,
                                            start + self->cursor_offset,
                                            stop + self->cursor_offset, node, type);

    return self->cursor;
  } else if (start + self->cursor_offset < self->next_cursor->start &&
             stop + self->cursor_offset <= self->next_cursor->start) {
    self->cursor = source_map_insert_extent(self,
                                            self->cursor,
                                            start + self->cursor_offset,
                                            stop + self->cursor_offset, node, type);

    return self->cursor;
  } else if (start + self->cursor_offset < self->next_cursor->start) {
    uint64_t new_start = self->next_cursor->start - self->cursor_offset;

    self->cursor = source_map_insert_extent(self,
                                            self->cursor,
                                            start + self->cursor_offset,
                                            self->next_cursor->start,
                                            node, type);

    if (new_start == stop)
      return self->cursor;

    start = new_start;
  }

  while (self->next_cursor && start + self->cursor_offset >= self->next_cursor->start) {
    self->cursor_offset += self->next_cursor->stop - self->next_cursor->start;
    self->cursor = self->cursor->next;
    self->next_cursor = self->cursor->next;
  }

  return source_map_splice_extent(self, start, stop, node, type);
}

bool
source_map_start_cursor(cmark_source_map *self, cmark_source_extent *cursor)
{
  self->cursor = cursor ? cursor : self->head;

  if (!self->cursor)
    return false;

  self->next_cursor = self->cursor->next;
  self->cursor_offset = self->cursor->stop;

  return true;
}

void
source_map_pretty_print(cmark_source_map *self) {
  cmark_source_extent *tmp;

  for (tmp = self->head; tmp; tmp = tmp->next) {
    printf ("%lu:%lu - %s, %s (%p)\n", tmp->start, tmp->stop,
						cmark_node_get_type_string(tmp->node), 
            cmark_source_extent_get_type_string(tmp),
            (void *) tmp->node);
  }
}

bool
source_map_check(cmark_source_map *self, uint64_t total_length)
{
  uint64_t last_stop = 0;
  cmark_source_extent *tmp;

  for (tmp = self->head; tmp; tmp = tmp->next) {
    if (tmp->start != last_stop) {
      return false;
    } if (tmp->start == tmp->stop)
      return false;
    last_stop = tmp->stop;
  }

  if (last_stop != total_length)
    return false;

  return true;
}


uint64_t
cmark_source_extent_get_start(cmark_source_extent *extent)
{
  return extent->start;
}

uint64_t
cmark_source_extent_get_stop(cmark_source_extent *extent)
{
  return extent->stop;
}

cmark_node *
cmark_source_extent_get_node(cmark_source_extent *extent)
{
  return extent->node;
}

cmark_source_extent *
cmark_source_extent_get_next(cmark_source_extent *extent)
{
  return extent->next;
}

cmark_source_extent *
cmark_source_extent_get_previous(cmark_source_extent *extent)
{
  return extent->prev;
}

cmark_extent_type
cmark_source_extent_get_type(cmark_source_extent *extent)
{
  return extent->type;
}

const char *
cmark_source_extent_get_type_string(cmark_source_extent *extent)
{
  switch (extent->type) {
    case CMARK_EXTENT_NONE:
      return "unknown";
    case CMARK_EXTENT_OPENER:
      return "opener";
    case CMARK_EXTENT_CLOSER:
      return "closer";
    case CMARK_EXTENT_BLANK:
      return "blank";
    case CMARK_EXTENT_CONTENT:
      return "content";
    case CMARK_EXTENT_PUNCTUATION:
      return "punctuation";
    case CMARK_EXTENT_LINK_DESTINATION:
      return "link_destination";
    case CMARK_EXTENT_LINK_TITLE:
      return "link_title";
    case CMARK_EXTENT_LINK_LABEL:
      return "link_label";
    case CMARK_EXTENT_REFERENCE_DESTINATION:
      return "reference_destination";
    case CMARK_EXTENT_REFERENCE_LABEL:
      return "reference_label";
    case CMARK_EXTENT_REFERENCE_TITLE:
      return "reference_title";
  }
  return "unknown";
}