forked from 4coder-archive/4coder_fleury
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4coder_fleury_lang_metadesk.cpp
335 lines (304 loc) · 10.8 KB
/
4coder_fleury_lang_metadesk.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
struct F4_MD_LexerState
{
String_Const_u8 string;
u8 *at;
u8 *one_past_last;
};
enum F4_MD_TokenSubKind
{
F4_MD_TokenSubKind_Null,
F4_MD_TokenSubKind_Tag,
};
internal F4_LANGUAGE_INDEXFILE(F4_MD_IndexFile)
{
for(;!ctx->done;)
{
Token *name = 0;
if(F4_Index_RequireTokenKind(ctx, TokenBaseKind_Identifier, &name, F4_Index_TokenSkipFlag_SkipWhitespace))
{
if(F4_Index_RequireToken(ctx, S8Lit(":"), F4_Index_TokenSkipFlag_SkipWhitespace))
{
F4_Index_MakeNote(ctx, Ii64(name), F4_Index_NoteKind_Constant, 0);
}
}
else if(F4_Index_RequireTokenKind(ctx, TokenBaseKind_Comment, &name, F4_Index_TokenSkipFlag_SkipWhitespace))
{
F4_Index_ParseComment(ctx, name);
}
else
{
F4_Index_ParseCtx_Inc(ctx, F4_Index_TokenSkipFlag_SkipWhitespace);
}
}
}
internal F4_LANGUAGE_LEXINIT(F4_MD_LexInit)
{
F4_MD_LexerState *state = (F4_MD_LexerState *)state_ptr;
state->string = contents;
state->at = contents.str;
state->one_past_last = contents.str + contents.size;
}
internal b32
F4_MD_CharIsSymbol(u8 c)
{
return (c == '~' || c == '!' || c == '@' || c == '#' || c == '$' ||
c == '%' || c == '^' || c == '&' || c == '*' || c == '(' ||
c == ')' || c == '-' || c == '=' || c == '+' || c == '[' ||
c == ']' || c == '{' || c == '}' || c == ':' || c == ';' ||
c == ',' || c == '<' || c == '.' || c == '>' || c == '/' ||
c == '?' || c == '|' || c == '\\');
}
internal F4_LANGUAGE_LEXFULLINPUT(F4_MD_LexFullInput)
{
b32 result = false;
F4_MD_LexerState state_ = *(F4_MD_LexerState *)state_ptr;
F4_MD_LexerState *state = &state_;
u64 emit_counter = 0;
i64 strmax = (i64)state->string.size;
for(i64 i = (i64)(state->at - state->string.str);
i < strmax && state->at + i < state->one_past_last;)
{
i64 start_i = i;
u8 chr = state->string.str[i];
// NOTE(rjf): Comments
if(i+1 < strmax &&
state->string.str[i] == '/' &&
state->string.str[i+1] == '/')
{
Token token = { i, 1, TokenBaseKind_Comment, 0 };
token.size += 1;
for(i64 j = i+2; j < strmax && state->string.str[j] != '\n'; j += 1, token.size += 1);
token_list_push(arena, list, &token);
i += token.size;
}
// NOTE(rjf): Comments
else if(i+1 < strmax &&
state->string.str[i] == '/' &&
state->string.str[i+1] == '*')
{
Token token = { i, 1, TokenBaseKind_Comment, 0 };
token.size += 1;
for(i64 j = i+2; j+1 < strmax && !(state->string.str[j] == '*' && state->string.str[j+1] == '/'); j += 1, token.size += 1);
token.size += 2;
token_list_push(arena, list, &token);
i += token.size;
}
// NOTE(rjf): Identifier
else if(character_is_alpha(chr))
{
Token token = { i, 1, TokenBaseKind_Identifier, 0 };
for(i64 j = i+1; j < (i64)state->string.size &&
(character_is_alpha_numeric(state->string.str[j]) ||
state->string.str[j] == '_');
j += 1, token.size += 1);
token_list_push(arena, list, &token);
i += token.size;
}
// NOTE(rjf): Whitespace
else if(character_is_whitespace(chr))
{
Token token = { i, 1, TokenBaseKind_Whitespace, 0 };
for(i64 j = i+1; j < (i64)state->string.size &&
character_is_whitespace(state->string.str[j]);
j += 1, token.size += 1);
token_list_push(arena, list, &token);
i += token.size;
}
// NOTE(rjf): Numeric Literal
else if(chr >= '0' && chr <= '9')
{
Token token = { i, 1, TokenBaseKind_LiteralFloat, 0 };
for(i64 j = i+1; j < (i64)state->string.size &&
(character_is_alpha_numeric(state->string.str[j]) ||
state->string.str[j] == '_' ||
state->string.str[j] == '.');
j += 1, token.size += 1);
token_list_push(arena, list, &token);
i += token.size;
}
// NOTE(rjf): Single-Line String Literal
else if(chr == '"')
{
Token token = { i, 1, TokenBaseKind_LiteralString, 0 };
for(i64 j = i+1; j < (i64)state->string.size && state->string.str[j] != '"';
j += 1, token.size += 1);
token.size += 1;
token_list_push(arena, list, &token);
i += token.size;
}
// NOTE(rjf): Single-Line String Literal Marker (Bundle-Of-Tokens)
else if(chr == '`')
{
Token token = { i, 1, TokenBaseKind_LiteralString, 0 };
token_list_push(arena, list, &token);
i += token.size;
}
// NOTE(rjf): Single-Line Char Literal
else if(chr == '\'')
{
Token token = { i, 1, TokenBaseKind_LiteralString, 0 };
for(i64 j = i+1; j < (i64)state->string.size && state->string.str[j] != '\'';
j += 1, token.size += 1);
token.size += 1;
token_list_push(arena, list, &token);
i += token.size;
}
// NOTE(rjf): Multi-line String Literal
else if(i+2 < strmax &&
state->string.str[i] == '"' &&
state->string.str[i+1] == '"' &&
state->string.str[i+2] == '"')
{
Token token = { i, 1, TokenBaseKind_LiteralString, 0 };
for(i64 j = i+1; j+2 < (i64)state->string.size &&
!(state->string.str[j] == '"' &&
state->string.str[j+1] == '"' &&
state->string.str[j+2] == '"');
j += 1, token.size += 1);
token.size += 3;
token_list_push(arena, list, &token);
i += token.size;
}
// NOTE(rjf): Multi-Line String Literal Marker (Bundle-Of-Tokens)
else if(i+2 < strmax &&
state->string.str[i] == '`' &&
state->string.str[i+1] == '`' &&
state->string.str[i+2] == '`')
{
Token token = { i, 3, TokenBaseKind_LiteralString, 0 };
token_list_push(arena, list, &token);
i += token.size;
}
// NOTE(rjf): Multi-line Char Literal
else if(i+2 < strmax &&
state->string.str[i] == '\'' &&
state->string.str[i+1] == '\'' &&
state->string.str[i+2] == '\'')
{
Token token = { i, 1, TokenBaseKind_LiteralString, 0 };
for(i64 j = i+1; j+2 < (i64)state->string.size &&
!(state->string.str[j] == '\'' &&
state->string.str[j+1] == '\'' &&
state->string.str[j+2] == '\'');
j += 1, token.size += 1);
token.size += 3;
token_list_push(arena, list, &token);
i += token.size;
}
// NOTE(rjf): Tags
else if(chr == '@')
{
Token token = { i, 1, TokenBaseKind_Identifier, 0 };
token.sub_kind = F4_MD_TokenSubKind_Tag;
for(i64 j = i+1; j < (i64)state->string.size &&
(character_is_alpha_numeric(state->string.str[j]) ||
state->string.str[j] == '_');
j += 1, token.size += 1);
token_list_push(arena, list, &token);
i += token.size;
}
// NOTE(rjf): Scope-Open
else if(chr == '{')
{
Token token = { i, 1, TokenBaseKind_ScopeOpen, 0 };
token_list_push(arena, list, &token);
i += token.size;
}
// NOTE(rjf): Scope-Close
else if(chr == '}')
{
Token token = { i, 1, TokenBaseKind_ScopeClose, 0 };
token_list_push(arena, list, &token);
i += token.size;
}
// NOTE(rjf): Paren-Open
else if(chr == '(' || chr == '[')
{
Token token = { i, 1, TokenBaseKind_ParentheticalOpen, 0 };
token_list_push(arena, list, &token);
i += token.size;
}
// NOTE(rjf): Scope-Close
else if(chr == ')' || chr == ']')
{
Token token = { i, 1, TokenBaseKind_ParentheticalClose, 0 };
token_list_push(arena, list, &token);
i += token.size;
}
// NOTE(rjf): Statement closes
else if(chr == ',' || chr == ';' || (chr == '-' && i+1 < strmax && state->string.str[i+1] == '>'))
{
Token token = { i, 1, TokenBaseKind_StatementClose, 0 };
token_list_push(arena, list, &token);
i += token.size;
}
// NOTE(rjf): Operators
else if(F4_MD_CharIsSymbol(chr))
{
Token token = { i, 1, TokenBaseKind_Operator, 0 };
token_list_push(arena, list, &token);
i += token.size;
}
// NOTE(rjf): Catch-All
else
{
Token token = {i, 1, TokenBaseKind_LexError, 0 };
token_list_push(arena, list, &token);
i += token.size;
}
if(state->at >= state->one_past_last)
{
goto eof;
}
else if(start_i == i)
{
i += 1;
state->at = state->string.str + i;
}
else
{
state->at = state->string.str + i;
emit_counter += 1;
if(emit_counter >= max)
{
goto end;
}
}
}
// NOTE(rjf): Add EOF
eof:;
{
result = true;
Token token = { (i64)state->string.size, 1, TokenBaseKind_EOF, 0 };
token_list_push(arena, list, &token);
}
end:;
*(F4_MD_LexerState *)state_ptr = *state;
return result;
}
internal F4_LANGUAGE_POSCONTEXT(F4_MD_PosContext)
{
return 0;
}
internal F4_LANGUAGE_HIGHLIGHT(F4_MD_Highlight)
{
Range_i64 visible_range = text_layout_get_visible_range(app, text_layout_id);
i64 first_index = token_index_from_pos(array, visible_range.first);
Token_Iterator_Array it = token_iterator_index(0, array, first_index);
for(;;)
{
Token *token = token_it_read(&it);
if(!token || token->pos >= visible_range.one_past_last)
{
break;
}
if(token->sub_kind == F4_MD_TokenSubKind_Tag)
{
paint_text_color(app, text_layout_id, Ii64(token), F4_ARGBFromID(color_table, fleury_color_index_comment_tag, 0));
}
if(!token_it_inc_all(&it))
{
break;
}
}
}