-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdom.jai
More file actions
389 lines (318 loc) · 12.1 KB
/
dom.jai
File metadata and controls
389 lines (318 loc) · 12.1 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
Node_Type :: enum {
UNINITIALIZED :: 0;
FIELD :: 1;
OBJECT :: 2;
ARRAY :: 3;
}
Node :: struct {
parent, next, prev: *Node;
name: string;
node_type: Node_Type;
location: Source_Code_Location;
binding: Any;
using content: union {
value: string;
children: Node_List;
};
}
Node_List :: struct {
first: *Node;
last: *Node;
count: int;
};
for_expansion :: (list: *Node_List, body: Code, flags: For_Flags) #expand {
#if flags & .REVERSE {
`it_index := list.count;
`it := list.last;
while it {
prev := it.prev;
defer { it = prev; it_index -= 1; }
#insert(remove={remove_node(it);}) body;
}
} else {
`it_index := 0;
`it := list.first;
while it {
next := it.next;
defer { it = next; it_index += 1; }
#insert(remove={remove_node(it);}) body;
}
}
}
// ===== Finding Nodes =====
// TODO: add ability to index array nodes?
find_node_by_relative_path :: (node: *Node, path: string) -> *Node, int {
_path := path;
if !_path return node, get_lexical_index(node);
index := 0;
while node != null {
next:, _path = path_next(_path);
if next == ".." {
node = node.parent;
index = get_lexical_index(node);
} else {
node, index = find_child_node_by_name(node, next);
}
if !_path break;
}
return node, index;
}
find_node_by_path :: (parser: *Parser, node: *Node, path: string) -> *Node, int {
_path := path;
if !_path return node, get_lexical_index(node);
search_from_node := node;
is_absolute_path := _path[0] == "/";
if is_absolute_path {
advance(*_path);
search_from_node = parser.root_node;
}
ret, idx := find_node_by_relative_path(search_from_node, _path);
return ret, idx;
}
find_child_node_by_name :: (parent: *Node, name: string) -> (*Node, int) {
if parent.node_type != .OBJECT && parent.node_type != .ARRAY {
return null, -1;
}
for parent.children
if it.name == name
return it, it_index;
return null, -1;
}
// TODO: sort of a leftover from field references, maybe we remove this...
get_lexical_index :: (node: *Node) -> int {
if node == null return -1;
index := 0;
while node {
index += 1;
node = node.prev;
}
return index;
}
// ===== Inserting and Removing Nodes =====
/*
USER WARNING:
If you call any of the below procedures manually, make sure to set the context.allocator to the Parser's pool allocator.
If you don't do this, then the nodes will not be freed when we free the pool, and will be leaked.
Only insert_data_node will properly push the Parser's pool allocator, since this is the only procedure in this section which the user is really expected to call directly.
I have considered just making all of these procedures #scope_module, but I figure it's better to just leave them public and warn the user instead.
*/
// Defines how to handle when an existing node resides at the same path on the DOM that we wish to insert into.
// Be aware that the default insert modes varies based on the procedure.
Node_Insert_Mode :: enum_flags {
RETURN_EXISTING :: 0;
OVERWRITE :: 1;
ALLOW_DUPLICATE;
PREPEND;
}
insert_data_node :: (parser: *Parser, parent: *Node, path: string, binding: Any, insert_mode := Node_Insert_Mode.OVERWRITE) -> *Node {
if binding.type == null {
log("Error: cannot insert data node with null binding.");
return null;
}
push_allocator(get_pool_allocator(*parser.node_pool));
if parent == null parent = parser.root_node;
node := insert_node_with_path(parent, path, insert_mode);
if node == null return node;
// determine node type for inserted data node
node.binding = binding;
io_data := get_io_data(node.binding.type);
if io_data && io_data.serialize_proc {
node.node_type = .FIELD;
} else {
if node.binding.type.type == {
case .INTEGER; #through;
case .FLOAT; #through;
case .STRING; #through;
case .BOOL;
node.node_type = .FIELD;
case .ENUM;
ti_enum := node.binding.type.(*Type_Info_Enum);
if ti_enum.enum_type_flags & .FLAGS {
node.node_type = ifx node.parent && node.parent.binding.value_pointer == node.binding.value_pointer
then .FIELD else .ARRAY;
}
else node.node_type = .FIELD;
case .ARRAY;
// arrays of u8 are serialized as string
// we will probably distinguish this later on u8 vs byte, where byte is serialized using some binary data blob
ti_array := node.binding.type.(*Type_Info_Array);
if ti_array.element_type.runtime_size == 1 {
node.node_type = .FIELD;
}
// indexed arrays and arrays containing struct with defined name member will serialize as gon objects
else if io_data && (.ARRAY_INDEXED & io_data.flags) {
node.node_type = .OBJECT;
}
else if ti_array.element_type.type == .STRUCT {
element_io_data := get_io_data(ti_array.element_type);
if element_io_data && element_io_data.name_member {
node.node_type = .OBJECT;
}
}
else node.node_type = .ARRAY;
case .STRUCT;
node.node_type = ifx io_data && (io_data.flags & .AS_ARRAY)
then .ARRAY else .OBJECT;
}
}
if node.node_type == .UNINITIALIZED {
log("Error: unable to determine node type for data node. (binding.type = %, parent.binding.type = %)", node.binding.type, node.parent.binding.type);
}
// make indirect data bindings for objects and arrays
if node.node_type == .OBJECT || node.node_type == .ARRAY {
if node.binding.type.type == {
case .STRUCT;
iterate_struct_members(node.binding, .SKIP_CONSTANT | .SKIP_PLACE, #code {
insert_data_node(parser, node, it_info.name, it);
});
case .ARRAY;
array := Any_Array.from(node.binding);
element_io_data := get_io_data(array.element_type);
for array {
elem_name: string;
if element_io_data && element_io_data.name_member {
name_member := get_member(it, element_io_data.name_member);
Convert.any_to_any(elem_name, name_member);
}
insert_data_node(parser, node, elem_name, it, .ALLOW_DUPLICATE);
}
}
}
return node;
}
// does the bare minimum to append a node, not even giving it a name
// after the node is appended, caller should initialize it
insert_child_node :: (parent: *Node, prepend := false) -> *Node {
node := New(Node);
node.parent = parent;
parent.children.count += 1;
if prepend {
if parent.children.first != null {
parent.children.first.prev = node;
node.next = parent.children.first;
}
parent.children.first = node;
if parent.children.last == null {
parent.children.last = node;
}
} else {
if parent.children.last != null {
parent.children.last.next = node;
node.prev = parent.children.last;
}
parent.children.last = node;
if parent.children.first == null {
parent.children.first = node;
}
}
return node;
}
get_or_insert_child_node :: (parent: *Node, name: string, insert_mode := Node_Insert_Mode.RETURN_EXISTING) -> *Node {
node: *Node;
if !(insert_mode & .ALLOW_DUPLICATE) {
node = find_child_node_by_name(parent, name);
}
if node == null {
node = insert_child_node(parent, insert_mode & .PREPEND == .PREPEND);
node.name = name;
}
else if insert_mode & .OVERWRITE {
// need to reset only certain fields, so we don't break connections between nodes
node.name = name;
node.node_type = .UNINITIALIZED;
node.binding = Any.{};
node.content = .{};
}
return node;
}
insert_node_with_path :: (parent: *Node, path: string, insert_mode := Node_Insert_Mode.OVERWRITE) -> *Node {
_path := path;
node := parent;
while true {
next:, _path = path_next(_path);
if !_path return get_or_insert_child_node(node, next, insert_mode);
child := find_child_node_by_name(node, next);
if child != null {
if child.node_type != .OBJECT {
log("GON DOM Error: Cannot create a named child node on an array or field node.");
return null;
}
node = child;
continue;
}
node = insert_child_node(node, insert_mode & .PREPEND == .PREPEND);
node.name = next;
node.node_type = .OBJECT;
}
unreachable();
return null;
}
// does not deallocate node, since we use a flat pool for node allocation
remove_node :: (node: *Node) {
node.parent.children.count -= 1;
if node.next != null node.next.prev = node.prev;
if node.prev != null node.prev.next = node.next;
if node.parent.children.first == node node.parent.children.first = node.next;
if node.parent.children.last == node node.parent.children.last = node.prev;
}
// clone_child_nodes_recursive :: (dst: *Node, src: *Node) -> bool {
// for child: src.children {
// node := insert_child_node(dst, false);
// if !clone_node_recursive(node, child) return false;
// }
// return true;
// }
// clone_node_recursive :: (dst: *Node, src: *Node) -> bool {
// dst.name = src.name;
// dst.type = src.type;
// dst.flags = src.flags;
// dst.binding = src.binding;
// dst.content = .{};
// if src.type == {
// case .OBJECT; #through;
// case .ARRAY;
// return clone_child_nodes_recursive(dst, src);
// case .FIELD;
// dst.value = src.value;
// case .UNINITIALIZED;
// return false;
// }
// return true;
// }
// ===== Format Node Path =====
format_node_path :: (node: *Node) -> string {
builder: String_Builder;
init_string_builder(*builder);
format_node_path(*builder, node);
return builder_to_string(*builder);
}
format_node_path :: (builder: *String_Builder, node: *Node) {
if node.parent != null {
format_node_path(builder, node.parent);
append(builder, "/");
}
append(builder, node.name);
}
#scope_module
/*
Node paths follow essentially the same rules as file paths do on Windows.
Instead of using the lexer to incrementally parse path strings, we just use this one procedure which returns the next path token and the remaining string.
I was using the lexer for this previously, but in retrospect that was really dumb.
The procedure does not currently do anything special to validate the content of the string, but perhaps we should?
The caller will also have to check if the path is absolute or relative manually before they start splitting the path.
*/
path_next :: (path: string) -> (next: string, remaining: string, ok: bool) {
found, left, right := split_from_left_by_any(path, "/\\");
if !found return path, "", true;
return left, right, true;
}
// helper function that I may remove if I go back to just flagging the node...
is_indexed_array :: (node: *Node) -> bool {
if node == null
|| node.node_type != .OBJECT
|| node.binding.type == null
|| node.binding.type.type != .ARRAY
return false;
io_data := get_io_data(node.binding.type);
return io_data && (io_data.flags & .ARRAY_INDEXED).(bool);
}