This repository has been archived by the owner on Feb 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.h
334 lines (298 loc) · 9.94 KB
/
nodes.h
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
enum nodeTypes {
root_node = '/',
literal_node = 'L',
ignore_node = 'I',
comment_node = '-',
ext_node = '<', /* Encloses an extension tag */
name_node = 'N', /* Tag name or part name */
attr_node = 'a', /* Tag attributes */
inner_node = '.', /* Tag contents, optional */
end_name_node = 'e', /* > or /> closing a name node. Missing in Preprocessor_DOM */
close_node = '>', /* Closing tag, optional */
heading_node = 'h', /* Used when working with a heading candidate */
h1_node = 'i',
h2_node = 'j',
h3_node = 'k',
h4_node = 'l',
h5_node = 'm',
h6_node = 'n',
brace_node = '{', /* Used when we still don't know its identity (template/tplarg) */
bracket_node = '[',
template_node = 't',
tplarg_node = 'p',
title_node = 'T',
part_node = '|',
value_node = 'v',
closebrace_node = '}',
};
/* May contain childs: root_node, ext_node, name_node, heading_node (h?_node), template_node, tplarg_node, title_node, part_node, value_node */
struct str_ref {
char const* string;
int length;
bool allocated;
};
extern inline void str_ref_free(struct str_ref* str) {
if ( str->allocated ) {
efree( (char*)str->string);
}
}
struct node {
enum nodeTypes type;
char flags;
int nextSibling;
int contentLength;
/* Relevant only for nodes with childs */
int index; /* index inside nodeString (preprocess) / space of children read (expand) */
struct node* parent;
/* Used for headings */
int commentEnd;
int visualEnd; /* Point where the last text ends (ie. without spaces, comments...) */
/* Used for brace and bracket nodes */
int count;
/* Used for template parts */
int eqpos; /* Name nodes */
int argIndex; /* Brace nodes */
/* Compact me: Move the last three blocks into an union */
union {
struct {
struct str_ref expanded;
struct str_ref name, attr, inner, close;
} ext_data;
};
};
struct literalNode {
int from;
int len;
};
#define UNKNOWN_NODE_LEN -1
#define DEFINE_NODE_STRING() char* nodeString = NULL; \
int nodeStringLen = 0; /* Length used of nodeString. Initialised to 1 for a \0 terminator */ \
struct literalNode currentLiteral = { 0, 0 }; \
int storedLength = 0; /* Length of text already stored in the nodes */ \
struct node* parentNode = NULL; \
addNodeWithTags(root_node, 0);
#define NODE_LEN 16 /* Length of a serialized node */
/**
* Adds a node of the specified type to the nodeString
* @param nodeType enum nodeTypes: Type of the node to add.
* @param txt char*: Text pointer. Must be 'text'
* @param offset int: Offset from txt to copy from
* @param length int: Length to copy from 'from'. -1 to copy until the end of the string.
*/
#define addNodeWithText(nodeType,txt,offset,length) \
do { \
int mylen = length; \
assert( txt == text ); \
if ( currentLiteral.len && nodeType != literal_node ) { \
storeNodeWithText(literal_node, currentLiteral.from, currentLiteral.len); \
currentLiteral.len = 0; \
} \
if ( -1 == mylen ) { \
mylen = text_len - offset; \
} \
\
storeNodeWithText(nodeType,offset,mylen); \
} while (0)
/**
* Like addNodeWithText() but doesn't commit the literals
*/
#define storeNodeWithText(nodeType,offset,length) \
do { \
assert(storedLength == offset); \
struct node tmpnode; \
tmpnode.type = nodeType; \
tmpnode.flags = 0; \
tmpnode.nextSibling = 0; \
tmpnode.contentLength = length; \
\
ALLOC_NODESTRING(); \
serializeNode(nodeString + nodeStringLen, &tmpnode); \
nodeStringLen += NODE_LEN; \
storedLength += length; \
} while (0);
/**
* Records the passed literal inside currentLiteral
* Adjacent literal nodes are stored inside of a single node.
*/
#define addLiteral(literalText,offset,length) \
do { \
int my_len = length; \
assert( literalText == text ); \
if ( my_len == -1 ) { \
my_len = text_len - offset; \
} \
if ( currentLiteral.len ) { \
assert( currentLiteral.from + currentLiteral.len == (offset) ); \
} else { \
currentLiteral.from = (offset); \
} \
currentLiteral.len += my_len; \
assert( (length) >= 0 ); \
} while (0)
/**
* Adds a node which contains other tags
* @param nodeType enum nodeTypes: Type of the node.
* @param charsToSkip int: Number of characters that 'belong' to this node. Used to skip characters
*/
#define addNodeWithTags(nodeType, charsToSkip) \
do { \
struct node* tmpnode; \
if ( currentLiteral.len ) { \
storeNodeWithText(literal_node, currentLiteral.from, currentLiteral.len); \
currentLiteral.len = 0; \
} \
\
tmpnode = alloc_node(); \
tmpnode->type = nodeType; \
tmpnode->flags = 0; \
tmpnode->nextSibling = UNKNOWN_NODE_LEN; \
tmpnode->contentLength = charsToSkip; \
tmpnode->index = nodeStringLen; \
tmpnode->count = 0; \
tmpnode->parent = parentNode; \
tmpnode->commentEnd = -1; \
tmpnode->eqpos = -1; \
parentNode = tmpnode; \
\
ALLOC_NODESTRING(); \
serializeNode(nodeString + nodeStringLen, tmpnode); \
nodeStringLen += NODE_LEN; \
storedLength += charsToSkip; \
} while(0)
#define closeNode(nodeType) \
do { \
struct node* tmpnode = parentNode; \
assert( nodeType == tmpnode->type ); \
if ( currentLiteral.len ) { \
storeNodeWithText(literal_node, currentLiteral.from, currentLiteral.len); \
currentLiteral.len = 0; \
} \
tmpnode->nextSibling = nodeStringLen - tmpnode->index - NODE_LEN; \
serializeNode( nodeString + tmpnode->index, tmpnode ); \
parentNode = parentNode->parent; \
free_node( tmpnode ); \
} while (0)
#define alloc_node() emalloc( sizeof(struct node) )
#define free_node(x) efree(x)
#define ALLOC_NODESTRING() \
do { \
nodeString = erealloc(nodeString, nodeStringLen + NODE_LEN + 1); \
assert( nodeString ); \
} while(0)
/**
* Serializes a node into string.
* The caller must ensure that there are at least NODE_LEN bytes
* available from pointer, and NODE_LEN + 1 writable.
*/
static void serializeNode(char* pointer, struct node* node) {
int c;
pointer[0] = node->type;
pointer[1] = '0' + node->flags;
assert( node->nextSibling < (1 << 24) );
if ( node->nextSibling == UNKNOWN_NODE_LEN ) {
pointer[2] = pointer[3] = pointer[4] = pointer[5] = pointer[6] = pointer[7] = '?';
} else {
sprintf(&pointer[2], "%06x", node->nextSibling);
}
c = pointer[16];
snprintf(&pointer[8], 9, "%08x", node->contentLength);
pointer[16] = c;
}
static inline int hex2dec(char val) {
switch (val) {
case '0'...'9':
return val - '0';
case 'a'...'f':
return val - 'a' + 10;
}
assert(0);
}
/**
* Get the nextSibling value from a node serialized at pointer.
* The nextSibling is a hexadecimal value in bytes 2-7, forming a
* value in the range [0,0x01000000).
*/
static inline int getNextSibling(const char* pointer) {
assert( pointer[2] != '?' );
return ( ( ( ( ( hex2dec(pointer[2]) << 4 ) | hex2dec(pointer[3]) ) << 4 | hex2dec(pointer[4]) ) << 4 | hex2dec(pointer[5]) ) << 4 | hex2dec(pointer[6]) ) << 4 | hex2dec(pointer[7]);
}
/**
* Get the contentLength value from a node serialized at pointer.
* The contentLength is a hexadecimal value in bytes 8-15.
*/
static inline int getContentLength(const char* pointer) {
return ( ( ( ( ( ( ( hex2dec(pointer[8]) << 4 ) | hex2dec(pointer[9]) ) << 4 | hex2dec(pointer[10]) ) << 4 | hex2dec(pointer[11]) ) << 4 | hex2dec(pointer[12]) ) << 4 | hex2dec(pointer[13]) ) << 4 | hex2dec(pointer[14]) ) << 4 | hex2dec(pointer[15]);
}
/**
* Get the output string that would result if the close is not found.
*
* TODO: Reduce space by collapsing nodes here.
*/
static struct node* breakSyntax( struct node* node, char * const nodeString, int *nodeStringLen ) {
struct node* parent;
/* Note we cannot coalesce with a previous literal node since it
* may be our nephew, instead of our sister (we could ask our
* parent, though)
*/
if ( node->type == bracket_node ) {
node->type = literal_node;
node->nextSibling = 0;
serializeNode( nodeString + node->index, node );
parent = node->parent;
free_node( node );
return parent;
} else if ( node->type == brace_node ) {
/* Literalize this node and its children (title, part, part\name, part\value) */
node->type = literal_node;
node->nextSibling = 0;
serializeNode( nodeString + node->index, node );
int writepos = node->index + NODE_LEN;
int readpos = node->index + NODE_LEN;
int nextSibling = getNextSibling( nodeString + readpos );
readpos += NODE_LEN;
/* Move up the title contents */
if ( nextSibling ) {
memmove( nodeString + writepos, nodeString + readpos, nextSibling );
readpos += nextSibling;
writepos += nextSibling;
}
/* Go for part nodes */
while ( readpos < *nodeStringLen ) {
assert( nodeString[readpos] == part_node ); /* <part> node */
assert( !memcmp( nodeString + readpos + 8, "00000001", 8 ) ); /* Part nodes contain exactly one character (the pipe) */
node->contentLength = 1;
serializeNode( nodeString + writepos, node );
readpos += NODE_LEN;
writepos += NODE_LEN;
assert( readpos < *nodeStringLen ); /* All part nodes contain one name node inside */
int nameChildren = getNextSibling( nodeString + readpos ); /* <name> */
readpos += NODE_LEN;
if ( nameChildren ) {
memmove( nodeString + writepos, nodeString + readpos, nameChildren );
readpos += nameChildren;
writepos += nameChildren;
}
if (readpos >= *nodeStringLen) break; /* It may be the case for eg. {{Foo|Bar */
if ( nodeString[readpos] == literal_node ) { /* if this template had a name, there will be an equal inside a literal node */
assert( !memcmp( nodeString + readpos, "L000000000000001", NODE_LEN ) );
memmove( nodeString + writepos, nodeString + readpos, NODE_LEN );
readpos += NODE_LEN;
writepos += NODE_LEN;
}
int valueChildren = getNextSibling( nodeString + readpos ); /* <value> */
assert( nodeString[readpos] == value_node ); /* <value> node */
readpos += NODE_LEN;
if ( valueChildren ) {
memmove( nodeString + writepos, nodeString + readpos, valueChildren );
readpos += valueChildren;
writepos += valueChildren;
}
}
*nodeStringLen = writepos;
parent = node->parent;
free_node( node );
return parent;
} else {
assert( 0 );
}
}