This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
4coder_fleury_index.cpp
executable file
·705 lines (648 loc) · 18.4 KB
/
4coder_fleury_index.cpp
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
global F4_Index_State f4_index = {};
function void
F4_Index_Initialize(void)
{
f4_index.mutex = system_mutex_make();
f4_index.arena = make_arena_system(KB(16));
}
function void
F4_Index_Lock(void)
{
system_mutex_acquire(f4_index.mutex);
}
function void
F4_Index_Unlock(void)
{
system_mutex_release(f4_index.mutex);
}
function u64
_F4_Index_FileHash(Application_Links *app, Buffer_ID id)
{
Scratch_Block scratch(app);
String_Const_u8 unique_name = push_buffer_unique_name(app, scratch, id);
return table_hash_u8(unique_name.str, unique_name.size);
}
function F4_Index_File *
_F4_Index_LookupFile(Application_Links *app, u64 hash, Buffer_ID buffer)
{
F4_Index_File *result = 0;
u64 slot = hash % ArrayCount(f4_index.file_table);
for(F4_Index_File *file = f4_index.file_table[slot]; file; file = file->hash_next)
{
if(file->buffer == buffer)
{
result = file;
break;
}
}
return result;
}
function F4_Index_File *
F4_Index_LookupFile(Application_Links *app, Buffer_ID buffer)
{
return _F4_Index_LookupFile(app, _F4_Index_FileHash(app, buffer), buffer);
}
function F4_Index_File *
F4_Index_LookupOrMakeFile(Application_Links *app, Buffer_ID buffer)
{
F4_Index_File *result = 0;
u64 hash = _F4_Index_FileHash(app, buffer);
u64 slot = hash % ArrayCount(f4_index.file_table);
// NOTE(rjf): Lookup case.
{
result = _F4_Index_LookupFile(app, hash, buffer);
if(result)
{
goto end;
}
}
// NOTE(rjf): Make case.
{
if(f4_index.free_file)
{
result = f4_index.free_file;
f4_index.free_file = f4_index.free_file->hash_next;
memset(result, 0, sizeof(*result));
}
else
{
result = push_array_zero(&f4_index.arena, F4_Index_File, 1);
}
if(result != 0)
{
result->hash_next = f4_index.file_table[slot];
f4_index.file_table[slot] = result;
result->buffer = buffer;
result->arena = make_arena_system(KB(16));
}
}
end:;
return result;
}
function void
F4_Index_EraseFile(Application_Links *app, Buffer_ID id)
{
u64 hash = _F4_Index_FileHash(app, id);
F4_Index_File *file = _F4_Index_LookupFile(app, hash, id);
if(file)
{
u64 slot = hash % ArrayCount(f4_index.file_table);
{
F4_Index_File *prev = 0;
for(F4_Index_File *hash_file = f4_index.file_table[slot]; hash_file; prev = hash_file, hash_file = hash_file->hash_next)
{
if(file == hash_file)
{
if(prev)
{
prev->hash_next = file->hash_next;
}
else
{
f4_index.file_table[slot] = file->hash_next;
}
break;
}
}
}
file->hash_next = f4_index.free_file;
f4_index.free_file = file;
}
}
function void
_F4_Index_FreeNoteTree(F4_Index_Note *note)
{
for(F4_Index_Note *child = note->first_child; child; child = child->next_sibling)
{
_F4_Index_FreeNoteTree(child);
}
F4_Index_Note *prev = note->prev;
F4_Index_Note *next = note->next;
F4_Index_Note *hash_prev = note->hash_prev;
F4_Index_Note *hash_next = note->hash_next;
u64 hash = note->hash;
u64 slot = hash % ArrayCount(f4_index.note_table);
if(prev)
{
prev->next = next;
}
if(next)
{
next->prev = prev;
}
if(prev == 0)
{
if(next)
{
next->hash_prev = hash_prev;
next->hash_next = hash_next;
if(hash_prev)
{
hash_prev->hash_next = next;
}
if(hash_next)
{
hash_next->hash_prev = next;
}
}
else
{
if(hash_prev)
{
hash_prev->hash_next = hash_next;
}
if(hash_next)
{
hash_next->hash_prev = hash_prev;
}
}
if(hash_prev == 0)
{
f4_index.note_table[slot] = next ? next : hash_next;
}
}
}
function void
F4_Index_ClearFile(F4_Index_File *file)
{
if(file)
{
file->generation += 1;
for(F4_Index_Note *note = file->first_note;
note; note = note->next_sibling)
{
_F4_Index_FreeNoteTree(note);
}
linalloc_clear(&file->arena);
file->first_note = file->last_note = 0;
}
}
function F4_Index_Note *
F4_Index_LookupNote(String_Const_u8 string, F4_Index_Note *parent)
{
F4_Index_Note *result = 0;
u64 hash = table_hash_u8(string.str, string.size);
u64 slot = hash % ArrayCount(f4_index.note_table);
for(F4_Index_Note *note = f4_index.note_table[slot]; note; note = note->hash_next)
{
if(note->hash == hash && note->parent == parent)
{
if(string_match(string, note->string))
{
result = note;
break;
}
}
}
return result;
}
function F4_Index_Note *
F4_Index_LookupNote(String_Const_u8 string)
{
return F4_Index_LookupNote(string, 0);
}
function F4_Index_Note *
F4_Index_AllocateNote(void)
{
F4_Index_Note *result = 0;
if(f4_index.free_note)
{
result = f4_index.free_note;
f4_index.free_note = f4_index.free_note->hash_next;
memset(result, 0, sizeof(*result));
}
else
{
result = push_array_zero(&f4_index.arena, F4_Index_Note, 1);
}
return result;
}
function void
F4_Index_InsertNote(F4_Index_ParseCtx *ctx, F4_Index_Note *note, Range_i64 name_range, F4_Index_NoteKind note_kind, F4_Index_NoteFlags note_flags)
{
F4_Index_File *file = ctx->file;
F4_Index_Note *parent = ctx->active_parent;
String_Const_u8 string = F4_Index_StringFromRange(ctx, name_range);
Range_i64 range = name_range;
if(file)
{
u64 hash = table_hash_u8(string.str, string.size);
u64 slot = hash % ArrayCount(f4_index.note_table);
// NOTE(rjf): Push to duplicate chain.
{
F4_Index_Note *list_head = F4_Index_LookupNote(string);
F4_Index_Note *list_tail = list_head;
for(F4_Index_Note *note = list_tail; note; list_tail = note, note = note->next);
if(list_tail != 0)
{
list_tail->next = note;
note->prev = list_tail;
note->hash_next = 0;
note->hash_prev = 0;
}
else
{
note->hash_next = f4_index.note_table[slot];
if(f4_index.note_table[slot])
{
f4_index.note_table[slot]->hash_prev = note;
}
f4_index.note_table[slot] = note;
note->hash_prev = 0;
note->prev = 0;
}
}
note->next = 0;
// NOTE(rjf): Push to tree.
{
note->parent = parent;
if(parent)
{
note->prev_sibling = parent->last_child;
note->next_sibling = 0;
if(parent->last_child == 0)
{
parent->first_child = parent->last_child = note;
}
else
{
parent->last_child->next_sibling = note;
parent->last_child = parent->last_child->next_sibling;
}
}
else
{
note->prev_sibling = file->last_note;
note->next_sibling = 0;
if(file->last_note == 0)
{
file->first_note = file->last_note = note;
}
else
{
file->last_note->next_sibling = note;
file->last_note = file->last_note->next_sibling;
}
}
}
// NOTE(rjf): Fill out data.
{
note->hash = hash;
note->string = push_string_copy(&file->arena, string);
note->kind = note_kind;
note->flags = note_flags;
note->range = range;
note->file = file;
note->file_generation = file->generation;
}
}
}
function F4_Index_Note *
F4_Index_MakeNote(F4_Index_ParseCtx *ctx, Range_i64 name_range, F4_Index_NoteKind note_kind, F4_Index_NoteFlags note_flags)
{
F4_Index_Note *result = F4_Index_AllocateNote();
F4_Index_InsertNote(ctx, result, name_range, note_kind, note_flags);
return result;
}
function void
_F4_Index_Parse(Application_Links *app, F4_Index_File *file, String_Const_u8 string, Token_Array tokens, F4_Language *language)
{
F4_Index_ParseCtx ctx =
{
false,
app,
file,
string,
tokens,
token_iterator_pos(0, &ctx.tokens, 0),
};
if(language != 0)
{
language->IndexFile(&ctx);
}
}
function void
F4_Index_ParseFile(Application_Links *app, F4_Index_File *file, String_Const_u8 string, Token_Array tokens)
{
F4_Index_Lock();
F4_Language *lang = F4_LanguageFromBuffer(app, file->buffer);
_F4_Index_Parse(app, file, string, tokens, lang);
F4_Index_Unlock();
}
function String_Const_u8
F4_Index_StringFromRange(F4_Index_ParseCtx *ctx, Range_i64 range)
{
String_Const_u8 string = string_substring(ctx->string, range);
return string;
}
function String_Const_u8
F4_Index_StringFromToken(F4_Index_ParseCtx *ctx, Token *token)
{
return F4_Index_StringFromRange(ctx, Ii64(token));
}
function F4_Index_Note *
F4_Index_PushParent(F4_Index_ParseCtx *ctx, F4_Index_Note *new_parent)
{
F4_Index_Note *n = ctx->active_parent;
ctx->active_parent = new_parent;
return n;
}
function void
F4_Index_PopParent(F4_Index_ParseCtx *ctx, F4_Index_Note *last_parent)
{
ctx->active_parent = last_parent;
}
function b32
F4_Index_ParseCtx_Inc(F4_Index_ParseCtx *ctx, F4_Index_TokenSkipFlags flags)
{
if(flags & F4_Index_TokenSkipFlag_SkipWhitespace)
{
ctx->done = !token_it_inc_non_whitespace(&ctx->it);
}
else
{
ctx->done = !token_it_inc_all(&ctx->it);
}
return ctx->done;
}
function b32
F4_Index_RequireToken(F4_Index_ParseCtx *ctx, String_Const_u8 string, F4_Index_TokenSkipFlags flags)
{
b32 result = 0;
Token *token = token_it_read(&ctx->it);
if(token)
{
String_Const_u8 token_string =
string_substring(ctx->string, Ii64(token->pos, token->pos+token->size));
if(string_match(string, token_string))
{
result = 1;
}
}
else
{
ctx->done = 1;
}
if(result)
{
F4_Index_ParseCtx_Inc(ctx, flags);
}
return result;
}
function b32
F4_Index_RequireTokenKind(F4_Index_ParseCtx *ctx, Token_Base_Kind kind, Token **token_out, F4_Index_TokenSkipFlags flags)
{
b32 result = 0;
Token *token = token_it_read(&ctx->it);
if(token)
{
if(token->kind == kind)
{
result = 1;
if(token_out)
{
*token_out = token;
}
}
}
else
{
ctx->done = 1;
}if(result)
{
F4_Index_ParseCtx_Inc(ctx, flags);
}
return result;
}
function b32
F4_Index_RequireTokenSubKind(F4_Index_ParseCtx *ctx, int sub_kind, Token **token_out, F4_Index_TokenSkipFlags flags)
{
b32 result = 0;
Token *token = token_it_read(&ctx->it);
if(token)
{
if(token->sub_kind == sub_kind)
{
result = 1;
if(token_out)
{
*token_out = token;
}
}
}
else
{
ctx->done = 1;
}if(result)
{
F4_Index_ParseCtx_Inc(ctx, flags);
}
return result;
}
function b32
F4_Index_PeekToken(F4_Index_ParseCtx *ctx, String_Const_u8 string)
{
b32 result = 0;
Token *token = token_it_read(&ctx->it);
if(token)
{
String_Const_u8 token_string =
string_substring(ctx->string, Ii64(token->pos, token->pos+token->size));
if(string_match(string, token_string))
{
result = 1;
}
}
else
{
ctx->done = 1;
}
return result;
}
function void
F4_Index_ParseComment(F4_Index_ParseCtx *ctx, Token *token)
{
String_Const_u8 string = F4_Index_StringFromToken(ctx, token);
for(u64 i = 0; i < string.size; i += 1)
{
if(string.str[i] == '@')
{
Range_i64 range = Ii64(token);
range.min += i;
F4_Index_MakeNote(ctx, range, F4_Index_NoteKind_CommentTag, 0);
break;
}
else if(i+4 < string.size && string_match(S8Lit("TODO"), string_substring(string, Ii64(i, i + 4))))
{
Range_i64 range = Ii64(token);
range.min += i;
F4_Index_MakeNote(ctx, range, F4_Index_NoteKind_CommentToDo, 0);
}
}
}
function void
F4_Index_SkipSoftTokens(F4_Index_ParseCtx *ctx, b32 preproc)
{
for(;!ctx->done;)
{
Token *token = token_it_read(&ctx->it);
if(preproc)
{
if(!(token->flags & TokenBaseFlag_PreprocessorBody) ||
token->kind == TokenBaseKind_Preprocessor)
{
break;
}
}
else
{
if(token->kind == TokenBaseKind_StatementClose ||
token->kind == TokenBaseKind_ScopeOpen ||
token->kind == TokenBaseKind_ParentheticalOpen)
{
break;
}
}
if(!token_it_inc_non_whitespace(&ctx->it))
{
break;
}
}
}
function void
F4_Index_SkipOpTokens(F4_Index_ParseCtx *ctx)
{
int paren_nest = 0;
for(;!ctx->done;)
{
Token *token = token_it_read(&ctx->it);
if(token->kind == TokenBaseKind_ParentheticalOpen)
{
paren_nest += 1;
}
else if(token->kind == TokenBaseKind_ParentheticalClose)
{
paren_nest -= 1;
if(paren_nest < 0)
{
paren_nest = 0;
}
}
else if(token->kind != TokenBaseKind_Operator && paren_nest == 0)
{
break;
}
F4_Index_ParseCtx_Inc(ctx, F4_Index_TokenSkipFlag_SkipWhitespace);
}
}
function b32
F4_Index_ParsePattern(F4_Index_ParseCtx *ctx, char *fmt, ...)
{
b32 parsed = 1;
F4_Index_ParseCtx ctx_restore = *ctx;
F4_Index_TokenSkipFlags flags = F4_Index_TokenSkipFlag_SkipWhitespace;
va_list args;
va_start(args, fmt);
for(int i = 0; fmt[i];)
{
if(fmt[i] == '%')
{
switch(fmt[i+1])
{
case 't':
{
char *cstring = va_arg(args, char *);
String8 string = SCu8((u8 *)cstring, cstring_length(cstring));
parsed = parsed && F4_Index_RequireToken(ctx, string, flags);
}break;
case 'k':
{
Token_Base_Kind kind = (Token_Base_Kind)va_arg(args, int);
Token **output_token = va_arg(args, Token **);
parsed = parsed && F4_Index_RequireTokenKind(ctx, kind, output_token, flags);
}break;
case 'b':
{
i16 kind = (i16)va_arg(args, int);
Token **output_token = va_arg(args, Token **);
parsed = parsed && F4_Index_RequireTokenSubKind(ctx, kind, output_token, flags);
}break;
case 'n':
{
F4_Index_NoteKind kind = (F4_Index_NoteKind)va_arg(args, int);
F4_Index_Note **output_note = va_arg(args, F4_Index_Note **);
Token *token = 0;
parsed = parsed && F4_Index_RequireTokenKind(ctx, TokenBaseKind_Identifier, &token, flags);
parsed = parsed && !!token;
if (parsed)
{
String8 token_string = F4_Index_StringFromToken(ctx, token);
F4_Index_Note *note = F4_Index_LookupNote(token_string, 0);
b32 kind_match = 0;
for(F4_Index_Note *n = note; n; n = n->next)
{
if(n->kind == kind)
{
kind_match = 1;
note = n;
break;
}
}
if (note && kind_match)
{
*output_note = note;
parsed = 1;
}
else
{
parsed = 0;
}
}
}break;
case 's':
{
F4_Index_SkipSoftTokens(ctx, 0);
}break;
case 'o':
{
F4_Index_SkipOpTokens(ctx);
}break;
default: break;
}
i += 1;
}
else
{
i += 1;
}
}
va_end(args);
if(parsed == 0)
{
*ctx = ctx_restore;
}
return parsed;
}
function void
F4_Index_Tick(Application_Links *app)
{
Scratch_Block scratch(app);
for (Buffer_Modified_Node *node = global_buffer_modified_set.first; node != 0;node = node->next)
{
Temp_Memory_Block temp(scratch);
Buffer_ID buffer_id = node->buffer;
String_Const_u8 contents = push_whole_buffer(app, scratch, buffer_id);
Token_Array tokens = get_token_array_from_buffer(app, buffer_id);
if(tokens.count == 0) { continue; }
F4_Index_Lock();
F4_Index_File *file = F4_Index_LookupOrMakeFile(app, buffer_id);
if(file)
{
ProfileScope(app, "[f] reparse");
F4_Index_ClearFile(file);
F4_Index_ParseFile(app, file, contents, tokens);
}
F4_Index_Unlock();
buffer_clear_layout_cache(app, buffer_id);
}
}