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
|
#include "parse.h"
#include <stdbool.h>
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "log.h"
enum ParsePhase {
PHASE_COUNT,
PHASE_BUILD,
};
struct ParseCtx {
char * const str;
char *cursor;
struct Tree *tree;
size_t tree_cursor;
size_t *chunks;
enum ParsePhase phase;
};
static uint64_t max(uint64_t a, uint64_t b) {
return a > b ? a : b;
}
static bool alnum(struct ParseCtx *ctx) {
return isalnum(ctx->cursor[0]);
}
static int read_STag(struct ParseCtx *ctx, bool *self_close, size_t nodeId) {
// Record start position of the tag
if (ctx->phase == PHASE_BUILD) {
ctx->chunks[nodeId] = ctx->cursor - ctx->str;
}
if(*ctx->cursor != '<') return 1;
ctx->cursor++;
while(isalnum(*ctx->cursor) || *ctx->cursor == ' ' || *ctx->cursor == '"' || *ctx->cursor == '=') ctx->cursor++;
// Check for self-closing tag
if(*ctx->cursor == '/') {
*self_close = true;
ctx->cursor++;
}
if(*ctx->cursor != '>') return 1;
ctx->cursor++;
return 0;
}
static int read_Element(struct ParseCtx *ctx, size_t nodeId);
static int read_Content(struct ParseCtx *ctx, size_t nodeId) {
int err;
uint64_t children = 0;
while(true) {
if(ctx->cursor[0] == '<' && ctx->cursor[1] != '/') {
size_t childId = ctx->tree_cursor++;
err = read_Element(ctx, childId);
if(err != 0) return err;
if(ctx->phase == PHASE_BUILD)
*imat_nid(ctx->tree->adj, children, nodeId) = childId+1;
children++;
} else if(alnum(ctx)) {
ctx->cursor++;
} else {
break;
}
}
if(ctx->phase == PHASE_COUNT)
ctx->tree->adj.stride = max(ctx->tree->adj.stride, children);
if(ctx->phase == PHASE_BUILD) {
for(size_t i = children; i < ctx->tree->adj.stride; i++) {
*imat_nid(ctx->tree->adj, i, nodeId) = 0;
}
}
return 0;
}
static int read_ETag(struct ParseCtx *ctx) {
if(*ctx->cursor != '<') return 1;
ctx->cursor++;
if(*ctx->cursor != '/') return 1;
ctx->cursor++;
while(isalnum(*ctx->cursor)) ctx->cursor++;
if(*ctx->cursor != '>') return 1;
ctx->cursor++;
return 0;
}
static int read_Element(struct ParseCtx *ctx, size_t nodeId) {
int err;
bool self_close = false;
err = read_STag(ctx, &self_close, nodeId);
if(err != 0) return err;
if(!self_close) {
err = read_Content(ctx, nodeId);
if(err != 0) return err;
err = read_ETag(ctx);
if(err != 0) return err;
} else if(ctx->phase == PHASE_BUILD) {
for(size_t i = 0; i < ctx->tree->adj.stride; i++)
*imat_nid(ctx->tree->adj, i, nodeId) = 0;
}
if(ctx->phase == PHASE_COUNT) ctx->tree->len++;
return 0;
}
// This follows a 2 phase approach. First we size out the tree to figure out how
// much data we are going to store. Then we allocate the space based on that
// count pass before then doing a second pass in the BUILD phase which fill in
// those data structures.
int parse_string(char *str, struct Tree *tree, size_t **chunks) {
assert(chunks != NULL); // Chunks parameter is required
tree->adj.stride = 1;
tree->len = 0;
struct ParseCtx ctx = {
.str = str,
.cursor = str,
.tree = tree,
.tree_cursor = 1,
.phase = PHASE_COUNT,
};
int err;
err = read_Element(&ctx, 0);
if(err != 0) {
log("Parse error at %ld", ctx.cursor - ctx.str);
return err;
}
tree->adj.data = malloc(tree->len * tree->adj.stride * sizeof(*tree->adj.data));
// Allocate memory for chunks
*chunks = malloc(tree->len * sizeof(size_t));
if (*chunks == NULL) {
free(tree->adj.data);
return 1;
}
// Reset the context for the next phase
ctx.phase = PHASE_BUILD;
ctx.cursor = str;
ctx.tree_cursor = 1;
ctx.chunks = *chunks;
err = read_Element(&ctx, 0);
// The second phase we assume the tree is valid since it passed the first phase
assert(err == 0);
return 0;
}
|