forked from 4coder-archive/4coder_fleury
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4coder_fleury_lang.cpp
138 lines (127 loc) · 4.76 KB
/
4coder_fleury_lang.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
global F4_Language_State f4_langs = {};
internal F4_Language *
F4_LanguageFromString(String_Const_u8 name)
{
F4_Language *result = 0;
if(f4_langs.initialized)
{
u64 hash = table_hash_u8(name.str, name.size);
u64 slot = hash % ArrayCount(f4_langs.language_table);
for(F4_Language *l = f4_langs.language_table[slot]; l; l = l->next)
{
if(l->hash == hash && string_match(l->name, name))
{
result = l;
break;
}
}
}
return result;
}
#define F4_RegisterLanguage(name, IndexFile, LexInit, LexFullInput, PosContext, Highlight, lex_state_type) _F4_RegisterLanguage(name, IndexFile, (F4_Language_LexInit *)LexInit, (F4_Language_LexFullInput *)LexFullInput, (F4_Language_PosContext *)PosContext, (F4_Language_Highlight *)Highlight, sizeof(lex_state_type))
internal void
_F4_RegisterLanguage(String_Const_u8 name,
F4_Language_IndexFile *IndexFile,
F4_Language_LexInit *LexInit,
F4_Language_LexFullInput *LexFullInput,
F4_Language_PosContext *PosContext,
F4_Language_Highlight *Highlight,
u64 lex_state_size)
{
if(f4_langs.initialized == 0)
{
f4_langs.initialized = 1;
f4_langs.arena = make_arena_system(KB(16));
}
F4_Language *language = 0;
u64 hash = table_hash_u8(name.str, name.size);
u64 slot = hash % ArrayCount(f4_langs.language_table);
for(F4_Language *l = f4_langs.language_table[slot]; l; l = l->next)
{
if(l->hash == hash && string_match(l->name, name))
{
language = l;
break;
}
}
if(language == 0)
{
language = push_array(&f4_langs.arena, F4_Language, 1);
language->next = f4_langs.language_table[slot];
f4_langs.language_table[slot] = language;
language->hash = hash;
language->name = push_string_copy(&f4_langs.arena, name);
language->lex_state_size = lex_state_size;
language->IndexFile = IndexFile;
language->LexInit = LexInit;
language->LexFullInput = LexFullInput;
language->PosContext = PosContext;
language->Highlight = Highlight;
}
}
internal F4_Language *
F4_LanguageFromBuffer(Application_Links *app, Buffer_ID buffer)
{
F4_Language *language = 0;
Scratch_Block scratch(app);
String_Const_u8 file_name = push_buffer_file_name(app, scratch, buffer);
String_Const_u8 extension = string_file_extension(file_name);
language = F4_LanguageFromString(extension);
return language;
}
internal void
F4_Language_PosContext_PushData(Arena *arena,
F4_Language_PosContextData **first_ptr,
F4_Language_PosContextData **last_ptr,
F4_Index_Note *note,
Token *query,
int arg_index)
{
F4_Language_PosContextData *first = *first_ptr;
F4_Language_PosContextData *last = *last_ptr;
F4_Language_PosContextData *func = push_array_zero(arena, F4_Language_PosContextData, 1);
func->relevant_note = note;
func->query_token = query;
func->argument_index = arg_index;
if(last == 0)
{
first = last = func;
}
else
{
last->next = func;
last = last->next;
}
*first_ptr = first;
*last_ptr = last;
}
internal void
F4_Language_PosContext_PushData_Call(Arena *arena,
F4_Language_PosContextData **first_ptr,
F4_Language_PosContextData **last_ptr,
String_Const_u8 string, int param_idx)
{
F4_Language_PosContext_PushData(arena, first_ptr, last_ptr, F4_Index_LookupNote(string, 0), 0, param_idx);
}
internal void
F4_Language_PosContext_PushData_Dot(Arena *arena,
F4_Language_PosContextData **first_ptr,
F4_Language_PosContextData **last_ptr,
String_Const_u8 string, Token *query)
{
F4_Language_PosContext_PushData(arena, first_ptr, last_ptr, F4_Index_LookupNote(string, 0), query, 0);
}
internal Token_List
F4_Language_LexFullInput_NoBreaks(Application_Links *app, F4_Language *language, Arena *arena, String_Const_u8 text)
{
Token_List list = {};
if(language != 0)
{
Scratch_Block scratch(app, arena);
void *state = push_array_zero(scratch, u8, language->lex_state_size);
language->LexInit(state, text);
language->LexFullInput(arena, &list, state, max_u64);
}
return list;
}
#include "4coder_fleury_lang_list.h"