Support pointer asterisks syntax

This commit is contained in:
Doyle 2019-02-23 14:04:35 +11:00
parent 2acaebaf77
commit a44603d3ca
2 changed files with 75 additions and 57 deletions

View File

@ -103,6 +103,7 @@ struct DqnInspect_StructMember
int type_len; int type_len;
char const *name; char const *name;
int name_len; int name_len;
int array_dimensions; // > 0 means array
DqnInspect_StructMemberMetadata const *metadata; DqnInspect_StructMemberMetadata const *metadata;
int metadata_len; int metadata_len;
@ -396,8 +397,7 @@ struct CPPVariableDecl
{ {
StringLiteral type; StringLiteral type;
StringLiteral name; StringLiteral name;
b32 is_array; int array_dimensions;
int array_len;
}; };
struct CPPTokeniser struct CPPTokeniser
@ -765,6 +765,35 @@ void ParseCPPEnum(CPPTokeniser *tokeniser)
} }
} }
int ConsumeAsterisks(CPPTokeniser *tokeniser)
{
int result = 0;
for (CPPToken peek_token = CPPTokeniser_PeekToken(tokeniser);
peek_token.type == CPPTokenType::Asterisks;
++result)
{
CPPTokeniser_NextToken(tokeniser);
peek_token = CPPTokeniser_PeekToken(tokeniser);
}
return result;
}
b32 ConsumeConstIdentifier(CPPTokeniser *tokeniser)
{
b32 result = false;
for (CPPToken peek_token = CPPTokeniser_PeekToken(tokeniser);
IsIdentifierToken(peek_token, STR_LITERAL("const"));
)
{
result = true;
CPPTokeniser_NextToken(tokeniser);
peek_token = CPPTokeniser_PeekToken(tokeniser);
}
return result;
}
void ParseCPPStruct(CPPTokeniser *tokeniser) void ParseCPPStruct(CPPTokeniser *tokeniser)
{ {
CPPToken token = CPPTokeniser_NextToken(tokeniser); CPPToken token = CPPTokeniser_NextToken(tokeniser);
@ -793,32 +822,23 @@ void ParseCPPStruct(CPPTokeniser *tokeniser)
{ {
if (token.type == CPPTokenType::Identifier) if (token.type == CPPTokenType::Identifier)
{ {
if (IsIdentifierToken(token, STR_LITERAL("const"))) ConsumeConstIdentifier(tokeniser);
token = CPPTokeniser_NextToken(tokeniser); CPPToken const variable_type = token;
for (int total_asterisks_count = 0;;)
#if 0
int asterisks_count = 0;
for (;; ++asterisks_count)
{ {
Token peek_token = CPPTokeniser_PeekToken(tokeniser); total_asterisks_count = ConsumeAsterisks(tokeniser);
if (peek_token == CPPTokenType::Asterisks) CPPTokeniser_NextToken(tokeniser); if (ConsumeConstIdentifier(tokeniser))
else break; {
total_asterisks_count += ConsumeAsterisks(tokeniser);
ConsumeConstIdentifier(tokeniser);
} }
#endif
CPPToken peek_token = CPPTokeniser_PeekToken(tokeniser); CPPToken peek_token = CPPTokeniser_PeekToken(tokeniser);
if (peek_token.type == CPPTokenType::Identifier)
{
CPPToken const variable_type = token;
CPPToken variable_name = peek_token; CPPToken variable_name = peek_token;
if (IsIdentifierToken(variable_name, STR_LITERAL("const"))) if (variable_name.type != CPPTokenType::Identifier)
{ break;
token = CPPTokeniser_NextToken(tokeniser);
variable_name = CPPTokeniser_PeekToken(tokeniser);
}
for (;;) // Allocate A Member Declaration
{
auto *link = MEM_ARENA_ALLOC_STRUCT(&global_main_arena, CPPDeclLinkedList<CPPVariableDecl>); auto *link = MEM_ARENA_ALLOC_STRUCT(&global_main_arena, CPPDeclLinkedList<CPPVariableDecl>);
*link = {}; *link = {};
if (!link_iterator) struct_members = link; // Set struct_members to first linked list entry if (!link_iterator) struct_members = link; // Set struct_members to first linked list entry
@ -827,6 +847,7 @@ void ParseCPPStruct(CPPTokeniser *tokeniser)
link->value.type = StringLiteral(variable_type.str, variable_type.len); link->value.type = StringLiteral(variable_type.str, variable_type.len);
link->value.name = StringLiteral(variable_name.str, variable_name.len); link->value.name = StringLiteral(variable_name.str, variable_name.len);
link->value.array_dimensions = total_asterisks_count;
CPPTokeniser_NextToken(tokeniser); CPPTokeniser_NextToken(tokeniser);
peek_token = CPPTokeniser_PeekToken(tokeniser); peek_token = CPPTokeniser_PeekToken(tokeniser);
@ -839,9 +860,6 @@ void ParseCPPStruct(CPPTokeniser *tokeniser)
if (peek_token.type == CPPTokenType::Comma) if (peek_token.type == CPPTokenType::Comma)
{ {
CPPTokeniser_NextToken(tokeniser); CPPTokeniser_NextToken(tokeniser);
variable_name = CPPTokeniser_PeekToken(tokeniser);
if (!ExpectToken(variable_name, CPPTokenType::Identifier))
return;
} }
else else
{ {
@ -851,7 +869,6 @@ void ParseCPPStruct(CPPTokeniser *tokeniser)
} }
} }
} }
}
// TODO(doyle): Don't support anonymous/nameless structs yet // TODO(doyle): Don't support anonymous/nameless structs yet
if (name.len == 0) if (name.len == 0)
@ -909,6 +926,7 @@ void ParseCPPStruct(CPPTokeniser *tokeniser)
CPPTokeniser_SprintfToFile(tokeniser, "STR_AND_LEN(\"%.*s\"),\n", decl->type.len, decl->type.str); CPPTokeniser_SprintfToFile(tokeniser, "STR_AND_LEN(\"%.*s\"),\n", decl->type.len, decl->type.str);
CPPTokeniser_SprintfToFile(tokeniser, "STR_AND_LEN(\"%.*s\"),\n", decl->name.len, decl->name.str); CPPTokeniser_SprintfToFile(tokeniser, "STR_AND_LEN(\"%.*s\"),\n", decl->name.len, decl->name.str);
CPPTokeniser_SprintfToFile(tokeniser, "%d, // array_dimensions\n", decl->array_dimensions);
if (member->metadata_array.len <= 0) CPPTokeniser_SprintfToFile(tokeniser, "nullptr, // metadata\n"); if (member->metadata_array.len <= 0) CPPTokeniser_SprintfToFile(tokeniser, "nullptr, // metadata\n");
else CPPTokeniser_SprintfToFile(tokeniser, "DqnInspect_%.*s_%.*s_StructMemberMetadata,\n", name.len, name.str, decl->name.len, decl->name.str); else CPPTokeniser_SprintfToFile(tokeniser, "DqnInspect_%.*s_%.*s_StructMemberMetadata,\n", name.len, name.str, decl->name.len, decl->name.str);

View File

@ -28,10 +28,7 @@ DQN_INSPECT struct OpenGLState
{ {
// #if 0 // #if 0
// #endif // #endif
const int c; void *win32_handle;
int const d;
// void *win32_handle;
int ebo DQN_INSPECT_META(DisplayName = "Element Buffer Object"), vbo, vao DQN_INSPECT_META(DisplayName = "Vertex Array Object", OpenGLVersion = "330"); int ebo DQN_INSPECT_META(DisplayName = "Element Buffer Object"), vbo, vao DQN_INSPECT_META(DisplayName = "Vertex Array Object", OpenGLVersion = "330");
// u32 shaders[(int)OpenGLShader::Count]; // u32 shaders[(int)OpenGLShader::Count];
V4 draw_color DQN_INSPECT_META(DisplayName = "HelloWorld"); V4 draw_color DQN_INSPECT_META(DisplayName = "HelloWorld");
@ -41,8 +38,11 @@ DQN_INSPECT struct OpenGLState
// FixedArray<Mat4, 32> camera_matrixes; // FixedArray<Mat4, 32> camera_matrixes;
int draw_call_count; int draw_call_count;
// const int *const a; const int *const a;
// int const *const b; int const *const b, c, const *const d, *const e;
// int *const e; const int f;
// int const* f; int const g;
int *const h;
int const* i;
int *********const j, k, ******l, const *m;
}; };