Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

Commit c3e1ab4

Browse files
authored
Merge pull request #12 from Jack-Punter/CppParserFixes
Fix bugs in typedef parsing
2 parents 733d01c + db63bec commit c3e1ab4

File tree

3 files changed

+157
-53
lines changed

3 files changed

+157
-53
lines changed

4coder_fleury_index.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,32 @@ F4_Index_RequireTokenKind(F4_Index_ParseCtx *ctx, Token_Base_Kind kind, Token **
424424
return result;
425425
}
426426

427+
internal b32
428+
F4_Index_RequireTokenSubKind(F4_Index_ParseCtx *ctx, int sub_kind, Token **token_out, F4_Index_TokenSkipFlags flags)
429+
{
430+
b32 result = 0;
431+
Token *token = token_it_read(&ctx->it);
432+
if(token)
433+
{
434+
if(token->sub_kind == sub_kind)
435+
{
436+
result = 1;
437+
if(token_out)
438+
{
439+
*token_out = token;
440+
}
441+
}
442+
}
443+
else
444+
{
445+
ctx->done = 1;
446+
}if(result)
447+
{
448+
F4_Index_ParseCtx_Inc(ctx, flags);
449+
}
450+
return result;
451+
}
452+
427453
internal b32
428454
F4_Index_PeekToken(F4_Index_ParseCtx *ctx, String_Const_u8 string)
429455
{
@@ -527,12 +553,12 @@ F4_Index_ParseComment(F4_Index_ParseCtx *ctx, Token *token)
527553
{
528554
if(string.str[i] == '@')
529555
{
530-
F4_Index_MakeNote(ctx->app, ctx->file, 0, string_substring(string, Ii64(i, string.size-1)), F4_Index_NoteKind_CommentTag, 0, Ii64(token));
556+
F4_Index_MakeNote(ctx->app, ctx->file, 0, string_substring(string, Ii64(i, string.size)), F4_Index_NoteKind_CommentTag, 0, Ii64(token));
531557
break;
532558
}
533559
else if(i+4 < string.size && string_match(S8Lit("TODO"), string_substring(string, Ii64(i, i + 4))))
534560
{
535-
F4_Index_MakeNote(ctx->app, ctx->file, 0, string_substring(string, Ii64(i, string.size-1)), F4_Index_NoteKind_CommentToDo, 0, Ii64(token));
561+
F4_Index_MakeNote(ctx->app, ctx->file, 0, string_substring(string, Ii64(i, string.size)), F4_Index_NoteKind_CommentToDo, 0, Ii64(token));
536562
}
537563
}
538564
}

4coder_fleury_index.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ internal b32 F4_Index_ParseCtx_Inc(F4_Index_ParseCtx *ctx, F4_Index_TokenSkipFla
105105
#define F4_Index_ParseCtx_IncWs(ctx) F4_Index_ParseCtx_Inc(ctx, F4_Index_TokenSkipFlag_SkipWhitespace)
106106
internal b32 F4_Index_RequireToken(F4_Index_ParseCtx *ctx, String_Const_u8 string, F4_Index_TokenSkipFlags flags);
107107
internal b32 F4_Index_RequireTokenKind(F4_Index_ParseCtx *ctx, Token_Base_Kind kind, Token **token_out, F4_Index_TokenSkipFlags flags);
108+
internal b32 F4_Index_RequireTokenSubKind(F4_Index_ParseCtx *ctx, int sub_kind, Token **token_out, F4_Index_TokenSkipFlags flags);
108109
internal b32 F4_Index_PeekToken(F4_Index_ParseCtx *ctx, String_Const_u8 string);
109110
internal b32 F4_Index_PeekTokenKind(F4_Index_ParseCtx *ctx, Token_Base_Kind kind, Token **token_out);
110111
internal b32 F4_Index_PeekTokenSubKind(F4_Index_ParseCtx *ctx, int sub_kind, Token **token_out);

4coder_fleury_lang_cpp.cpp

Lines changed: 128 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11

2+
internal void
3+
F4_CPP_ParseMacroDefinition(F4_Index_ParseCtx *ctx, F4_Index_TokenSkipFlags flags)
4+
{
5+
Token *name = 0;
6+
if(F4_Index_RequireTokenKind(ctx, TokenBaseKind_Identifier, &name, flags))
7+
{
8+
F4_Index_MakeNote(ctx->app, ctx->file, 0, F4_Index_StringFromToken(ctx, name),
9+
F4_Index_NoteKind_Macro, 0, Ii64(name));
10+
}
11+
for(;!ctx->done;)
12+
{
13+
Token *token = token_it_read(&ctx->it);
14+
if(!(token->flags & TokenBaseFlag_PreprocessorBody) ||
15+
token->kind == TokenBaseKind_Preprocessor)
16+
{
17+
break;
18+
}
19+
F4_Index_ParseCtx_IncWs(ctx);
20+
}
21+
}
22+
223
internal b32
324
F4_CPP_SkipParseBody(F4_Index_ParseCtx *ctx)
425
{
@@ -8,7 +29,22 @@ F4_CPP_SkipParseBody(F4_Index_ParseCtx *ctx)
829
for(;!ctx->done;)
930
{
1031
Token *token = token_it_read(&ctx->it);
11-
if(token->sub_kind == TokenCppKind_BraceOp)
32+
Token *name;
33+
if (!token) {
34+
ctx->done = true;
35+
}
36+
// NOTE(jack): Comments and macros can occur inside bodies that we would like to skip,
37+
// and should still be indexed.
38+
else if(F4_Index_PeekTokenKind(ctx, TokenBaseKind_Comment, &name))
39+
{
40+
F4_Index_ParseComment(ctx, name);
41+
}
42+
// NOTE(jack): Macros
43+
else if(F4_Index_RequireTokenSubKind(ctx, TokenCppKind_PPDefine, 0, F4_Index_TokenSkipFlag_SkipWhitespace))
44+
{
45+
F4_CPP_ParseMacroDefinition(ctx, F4_Index_TokenSkipFlag_SkipWhitespace);
46+
}
47+
else if(token->sub_kind == TokenCppKind_BraceOp)
1248
{
1349
nest += 1;
1450
body_found = 1;
@@ -18,11 +54,11 @@ F4_CPP_SkipParseBody(F4_Index_ParseCtx *ctx)
1854
nest -= 1;
1955
if(nest == 0)
2056
{
57+
F4_Index_ParseCtx_Inc(ctx, F4_Index_TokenSkipFlag_SkipWhitespace);
2158
break;
2259
}
2360
}
24-
else
25-
{
61+
else if(body_found == 0) {
2662
break;
2763
}
2864

@@ -68,43 +104,39 @@ internal F4_LANGUAGE_INDEXFILE(F4_CPP_IndexFile)
68104
(F4_Index_PeekToken(ctx, S8Lit("union"))) ||
69105
(F4_Index_PeekToken(ctx, S8Lit("enum")))))
70106
{
71-
//~ NOTE(rjf): Structs
72-
if(scope_nest == 0 &&
73-
F4_Index_RequireToken(ctx, S8Lit("struct"), flags) &&
74-
F4_Index_RequireTokenKind(ctx, TokenBaseKind_Identifier, &name, flags))
107+
//~ NOTE(rjf): Structs and Unions (nested declarations not parsed)
108+
if(F4_Index_RequireTokenSubKind(ctx, TokenCppKind_Struct, &name, flags) ||
109+
F4_Index_RequireTokenSubKind(ctx, TokenCppKind_Union, &name, flags))
75110
{
76111
handled = 1;
77-
F4_Index_NoteFlags note_flags = F4_Index_NoteFlag_ProductType;
112+
F4_Index_NoteFlags note_flags = ((name->sub_kind == TokenCppKind_Struct) ?
113+
F4_Index_NoteFlag_ProductType : F4_Index_NoteFlag_SumType);
114+
115+
b32 name_found = F4_Index_RequireTokenKind(ctx, TokenBaseKind_Identifier, &name, flags);
78116
if(!F4_CPP_SkipParseBody(ctx))
79117
{
80118
note_flags |= F4_Index_NoteFlag_Prototype;
81119
}
82-
F4_Index_MakeNote(ctx->app, ctx->file, 0,
83-
F4_Index_StringFromToken(ctx, name),
84-
F4_Index_NoteKind_Type,
85-
note_flags, Ii64(name));
86-
}
87-
88-
//~ NOTE(rjf): Unions
89-
else if(scope_nest == 0 &&
90-
F4_Index_RequireToken(ctx, S8Lit("union"), flags) &&
91-
F4_Index_RequireTokenKind(ctx, TokenBaseKind_Identifier, &name, flags))
92-
{
93-
handled = 1;
94-
F4_Index_NoteFlags note_flags = F4_Index_NoteFlag_SumType;
95-
if(!F4_CPP_SkipParseBody(ctx))
120+
if (name_found) {
121+
F4_Index_MakeNote(ctx->app, ctx->file, 0,
122+
F4_Index_StringFromToken(ctx, name),
123+
F4_Index_NoteKind_Type,
124+
note_flags, Ii64(name));
125+
}
126+
// NOTE(jack): clear the prototype flag so that the typedef'd name
127+
// is not flagged as a prototype
128+
note_flags &= ~F4_Index_NoteFlag_Prototype;
129+
if (F4_Index_RequireTokenKind(ctx, TokenBaseKind_Identifier, &name, flags))
96130
{
97-
note_flags |= F4_Index_NoteFlag_Prototype;
131+
F4_Index_MakeNote(ctx->app, ctx->file, 0,
132+
F4_Index_StringFromToken(ctx, name),
133+
F4_Index_NoteKind_Type,
134+
note_flags, Ii64(name));
98135
}
99-
F4_Index_MakeNote(ctx->app, ctx->file, 0,
100-
F4_Index_StringFromToken(ctx, name),
101-
F4_Index_NoteKind_Type,
102-
note_flags, Ii64(name));
103136
}
104137

105138
//~ NOTE(rjf): Enums
106-
else if(scope_nest == 0 &&
107-
F4_Index_RequireToken(ctx, string_u8_litexpr("enum"), flags))
139+
else if(F4_Index_RequireToken(ctx, string_u8_litexpr("enum"), flags))
108140
{
109141
handled = 1;
110142
F4_Index_NoteFlags note_flags = 0;
@@ -130,6 +162,48 @@ internal F4_LANGUAGE_INDEXFILE(F4_CPP_IndexFile)
130162

131163
for(;!ctx->done;)
132164
{
165+
if (F4_Index_RequireTokenKind(ctx, TokenBaseKind_Comment, &constant, flags))
166+
{
167+
F4_Index_ParseComment(ctx, constant);
168+
}
169+
// NOTE(jack): Macros
170+
if(F4_Index_RequireTokenSubKind(ctx, TokenCppKind_PPDefine, 0, flags))
171+
{
172+
// NOTE(jack): If the defined token is the enum type name,
173+
// don't note it as an macro
174+
// typedef enum Key
175+
// {
176+
// #define Key(name, str) Key_##name,
177+
// #include "os_key_list.inc"
178+
// #undef Key
179+
// Key_Max
180+
// } Key;
181+
182+
if(!F4_Index_PeekToken(ctx, F4_Index_StringFromToken(ctx, name)))
183+
{
184+
F4_CPP_ParseMacroDefinition(ctx, F4_Index_TokenSkipFlag_SkipWhitespace);
185+
}
186+
else
187+
{
188+
F4_Index_RequireTokenKind(ctx, TokenBaseKind_Identifier, 0, flags);
189+
for(;!ctx->done;)
190+
{
191+
Token *token = token_it_read(&ctx->it);
192+
if(!(token->flags & TokenBaseFlag_PreprocessorBody) ||
193+
token->kind == TokenBaseKind_Preprocessor)
194+
{
195+
break;
196+
}
197+
F4_Index_ParseCtx_IncWs(ctx);
198+
}
199+
}
200+
}
201+
202+
// NOTE(jack): On #undef skip past the identifier so that it isn't noted as a constant
203+
if(F4_Index_RequireTokenSubKind(ctx, TokenCppKind_PPUndef, 0, flags))
204+
{
205+
F4_Index_RequireTokenKind(ctx, TokenBaseKind_Identifier, 0, flags);
206+
}
133207
if(F4_Index_RequireTokenKind(ctx, TokenBaseKind_Identifier, &constant, flags))
134208
{
135209
F4_Index_MakeNote(ctx->app, ctx->file, 0, F4_Index_StringFromToken(ctx, constant), F4_Index_NoteKind_Constant, 0, Ii64(constant));
@@ -139,6 +213,10 @@ internal F4_LANGUAGE_INDEXFILE(F4_CPP_IndexFile)
139213
{
140214
for(;!ctx->done;)
141215
{
216+
if (F4_Index_RequireTokenKind(ctx, TokenBaseKind_Comment, &constant, flags))
217+
{
218+
F4_Index_ParseComment(ctx, constant);
219+
}
142220
if(F4_Index_PeekToken(ctx, S8Lit("}")) ||
143221
F4_Index_PeekToken(ctx, S8Lit(",")))
144222
{
@@ -174,11 +252,23 @@ internal F4_LANGUAGE_INDEXFILE(F4_CPP_IndexFile)
174252
F4_Index_NoteKind_Type,
175253
note_flags, Ii64(name));
176254
}
255+
256+
// NOTE(jack): clear the prototype flag so that the typedef'd name
257+
// is not flagged as a prototype
258+
note_flags &= ~F4_Index_NoteFlag_Prototype;
259+
if (F4_Index_RequireTokenKind(ctx, TokenBaseKind_Identifier, &name, flags))
260+
{
261+
F4_Index_MakeNote(ctx->app, ctx->file, 0,
262+
F4_Index_StringFromToken(ctx, name),
263+
F4_Index_NoteKind_Type,
264+
note_flags, Ii64(name));
265+
}
177266
}
178267

179268
//~ NOTE(rjf): Regular Typedef
180269
else
181270
{
271+
Token *aliased_type_token = token_it_read(&ctx->it);
182272
for(;token_it_inc_all(&ctx->it);)
183273
{
184274
Token *token = token_it_read(&ctx->it);
@@ -198,37 +288,24 @@ internal F4_LANGUAGE_INDEXFILE(F4_CPP_IndexFile)
198288
F4_Index_SeekToken(ctx, S8Lit(";"));
199289
if(name)
200290
{
291+
F4_Index_Note* aliased_type_index_note = F4_Index_LookupNote(F4_Index_StringFromToken(ctx, aliased_type_token));
292+
F4_Index_NoteFlags note_flags = 0;
293+
294+
if (aliased_type_index_note) {
295+
note_flags = aliased_type_index_note->flags;
296+
}
201297
F4_Index_MakeNote(ctx->app, ctx->file, 0,
202298
F4_Index_StringFromToken(ctx, name),
203299
F4_Index_NoteKind_Type,
204-
0, Ii64(name));
300+
note_flags, Ii64(name));
205301
}
206302
}
207-
208303
}
209-
210304
//~ NOTE(rjf): Macros
211-
else if(F4_Index_PeekTokenSubKind(ctx, TokenCppKind_PPDefine, 0))
305+
else if(F4_Index_RequireTokenSubKind(ctx, TokenCppKind_PPDefine, 0, flags))
212306
{
213307
handled = 1;
214-
215-
F4_Index_ParseCtx_Inc(ctx, F4_Index_TokenSkipFlag_SkipWhitespace);
216-
if(F4_Index_RequireTokenKind(ctx, TokenBaseKind_Identifier, &name, flags))
217-
{
218-
F4_Index_MakeNote(ctx->app, ctx->file, 0, F4_Index_StringFromToken(ctx, name),
219-
F4_Index_NoteKind_Macro, 0, Ii64(name));
220-
}
221-
222-
for(;!ctx->done;)
223-
{
224-
Token *token = token_it_read(&ctx->it);
225-
if(!(token->flags & TokenBaseFlag_PreprocessorBody) ||
226-
token->kind == TokenBaseKind_Preprocessor)
227-
{
228-
break;
229-
}
230-
F4_Index_ParseCtx_IncWs(ctx);
231-
}
308+
F4_CPP_ParseMacroDefinition(ctx, flags);
232309
}
233310

234311
//~ NOTE(rjf): Comment Tags

0 commit comments

Comments
 (0)