Compare commits

...

3 Commits

8 changed files with 274 additions and 253 deletions

View File

@ -78,9 +78,6 @@ struct Context
StringTable StrCache;
// TODO(Ed): This needs to be just handled by a parser context
Array(Token) Lexer_Tokens;
// TODO(Ed): Active parse context vs a parse result need to be separated conceptually
ParseContext parser;
@ -111,7 +108,7 @@ inline
void logger_fmt(Context* ctx, LogLevel level, char const* fmt, ...)
{
local_persist thread_local
PrintF_Buffer buf = struct_zero(PrintF_Buffer);
PrintF_Buffer buf = struct_zero_init();
va_list va;
va_start(va, fmt);
@ -356,13 +353,13 @@ forceinline CodeBody def_union_body ( s32 num, Code* codes )
struct ParseStackNode
{
ParseStackNode* Prev;
ParseStackNode* prev;
TokenSlice Tokens;
Token* Start;
Str Name; // The name of the AST node (if parsed)
Str ProcName; // The name of the procedure
Code CodeRel; // Relevant AST node
TokenSlice tokens;
Token* start;
Str name; // The name of the AST node (if parsed)
Str proc_name; // The name of the procedure
Code code_rel; // Relevant AST node
// TODO(Ed): When an error occurs, the parse stack is not released and instead the scope is left dangling.
};

View File

@ -22,6 +22,7 @@ ParseInfo wip_parse_str(LexedInfo lexed, ParseOpts* opts)
// TODO(Ed): ParseInfo should be set to the parser context.
ctx->parser = struct_zero(ParseContext);
ctx->parser.tokens = lexed.tokens;
ParseStackNode scope = NullScope;
@ -40,17 +41,16 @@ CodeClass parse_class( Str def )
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
ctx->parser = struct_zero(ParseContext);
ctx->parser.Tokens = toks;
LexedInfo lexed = lex(ctx, def);
ctx->parser.tokens = lexed.tokens;
if ( ctx->parser.tokens.ptr == nullptr )
return InvalidCode;
ParseStackNode scope = NullScope;
parser_push(& ctx->parser, & scope);
CodeClass result = (CodeClass) parse_class_struct( ctx, Tok_Decl_Class, parser_not_inplace_def );
parser_pop(& ctx->parser);
return result;
}
@ -62,10 +62,16 @@ CodeConstructor parse_constructor(Str def )
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
ctx->parser = struct_zero(ParseContext);
LexedInfo lexed = lex(ctx, def);
ctx->parser.tokens = lexed.tokens;
if ( ctx->parser.tokens.ptr == nullptr )
return InvalidCode;
ParseStackNode scope = NullScope;
parser_push(& ctx->parser, & scope);
// TODO(Ed): Constructors can have prefix attributes
CodeSpecifiers specifiers = NullCode;
@ -92,7 +98,7 @@ CodeConstructor parse_constructor(Str def )
break;
default :
log_failure( "Invalid specifier %s for variable\n%S", spec_to_str( spec ), parser_to_strbuilder(ctx->parser, ctx->Allocator_Temp) );
log_failure( "Invalid specifier %s for variable\n%S", spec_to_str( spec ), parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
@ -106,14 +112,13 @@ CodeConstructor parse_constructor(Str def )
eat( currtok.Type );
}
if ( NumSpecifiers )
{
if ( NumSpecifiers ) {
specifiers = def_specifiers_arr( NumSpecifiers, specs_found );
// <specifiers> ...
}
ctx->parser.Tokens = toks;
CodeConstructor result = parser_parse_constructor(ctx, specifiers);
parser_pop(& ctx->parser);
return result;
}
@ -124,17 +129,16 @@ CodeDefine parse_define( Str def )
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
ctx->parser = struct_zero(ParseContext);
ctx->parser.Tokens = toks;
LexedInfo lexed = lex(ctx, def);
ctx->parser.tokens = lexed.tokens;
if ( ctx->parser.tokens.ptr == nullptr )
return InvalidCode;
ParseStackNode scope = NullScope;
parser_push(& ctx->parser, & scope);
CodeDefine result = parser_parse_define(ctx);
parser_pop(& ctx->parser);
return result;
}
@ -146,14 +150,16 @@ CodeDestructor parse_destructor( Str def )
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
ctx->parser = struct_zero(ParseContext);
LexedInfo lexed = lex(ctx, def);
ctx->parser.tokens = lexed.tokens;
if ( ctx->parser.tokens.ptr == nullptr )
return InvalidCode;
// TODO(Ed): Destructors can have prefix attributes
// TODO(Ed): Destructors can have virtual
ctx->parser.Tokens = toks;
CodeDestructor result = parser_parse_destructor(ctx, NullCode);
return result;
}
@ -165,17 +171,14 @@ CodeEnum parse_enum( Str def )
check_parse_args( def );
ParseStackNode scope = NullScope;
parser_push(& ctx->parser, & scope);
ctx->parser = struct_zero(ParseContext);
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
{
parser_pop(& ctx->parser);
LexedInfo lexed = lex(ctx, def);
ctx->parser.tokens = lexed.tokens;
if ( ctx->parser.tokens.ptr == nullptr ) {
return InvalidCode;
}
ctx->parser.Tokens = toks;
return parser_parse_enum(ctx, parser_not_inplace_def);
}
@ -186,11 +189,13 @@ CodeBody parse_export_body( Str def )
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
ctx->parser = struct_zero(ParseContext);
LexedInfo lexed = lex(ctx, def);
ctx->parser.tokens = lexed.tokens;
if ( ctx->parser.tokens.ptr == nullptr )
return InvalidCode;
ctx->parser.Tokens = toks;
return parser_parse_export_body(ctx);
}
@ -201,11 +206,13 @@ CodeExtern parse_extern_link( Str def )
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
ctx->parser = struct_zero(ParseContext);
LexedInfo lexed = lex(ctx, def);
ctx->parser.tokens = lexed.tokens;
if ( ctx->parser.tokens.ptr == nullptr )
return InvalidCode;
ctx->parser.Tokens = toks;
return parser_parse_extern_link(ctx);
}
@ -216,11 +223,13 @@ CodeFriend parse_friend( Str def )
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
ctx->parser = struct_zero(ParseContext);
LexedInfo lexed = lex(ctx, def);
ctx->parser.tokens = lexed.tokens;
if ( ctx->parser.tokens.ptr == nullptr )
return InvalidCode;
ctx->parser.Tokens = toks;
return parser_parse_friend(ctx);
}
@ -231,11 +240,13 @@ CodeFn parse_function( Str def )
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
ctx->parser = struct_zero(ParseContext);
LexedInfo lexed = lex(ctx, def);
ctx->parser.tokens = lexed.tokens;
if ( ctx->parser.tokens.ptr == nullptr )
return InvalidCode;
ctx->parser.Tokens = toks;
return (CodeFn) parser_parse_function(ctx);
}
@ -246,17 +257,16 @@ CodeBody parse_global_body( Str def )
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
ctx->parser = struct_zero(ParseContext);
ctx->parser.Tokens = toks;
LexedInfo lexed = lex(ctx, def);
ctx->parser.tokens = lexed.tokens;
if ( ctx->parser.tokens.ptr == nullptr )
return InvalidCode;
ParseStackNode scope = NullScope;
parser_push(& ctx->parser, & scope);
CodeBody result = parse_global_nspace(ctx, CT_Global_Body );
parser_pop(& ctx->parser);
return result;
}
@ -268,11 +278,13 @@ CodeNS parse_namespace( Str def )
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
ctx->parser = struct_zero(ParseContext);
LexedInfo lexed = lex(ctx, def);
ctx->parser.tokens = lexed.tokens;
if ( ctx->parser.tokens.ptr == nullptr )
return InvalidCode;
ctx->parser.Tokens = toks;
return parser_parse_namespace(ctx);
}
@ -283,11 +295,13 @@ CodeOperator parse_operator( Str def )
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
ctx->parser = struct_zero(ParseContext);
LexedInfo lexed = lex(ctx, def);
ctx->parser.tokens = lexed.tokens;
if ( ctx->parser.tokens.ptr == nullptr )
return InvalidCode;
ctx->parser.Tokens = toks;
return (CodeOperator) parser_parse_operator(ctx);
}
@ -298,11 +312,13 @@ CodeOpCast parse_operator_cast( Str def )
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
ctx->parser = struct_zero(ParseContext);
LexedInfo lexed = lex(ctx, def);
ctx->parser.tokens = lexed.tokens;
if ( ctx->parser.tokens.ptr == nullptr )
return InvalidCode;
ctx->parser.Tokens = toks;
return parser_parse_operator_cast(ctx, NullCode);
}
@ -313,17 +329,16 @@ CodeStruct parse_struct( Str def )
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
ctx->parser = struct_zero(ParseContext);
ctx->parser.Tokens = toks;
LexedInfo lexed = lex(ctx, def);
ctx->parser.tokens = lexed.tokens;
if ( ctx->parser.tokens.ptr == nullptr )
return InvalidCode;
ParseStackNode scope = NullScope;
parser_push(& ctx->parser, & scope);
CodeStruct result = (CodeStruct) parse_class_struct( ctx, Tok_Decl_Struct, parser_not_inplace_def );
parser_pop(& ctx->parser);
return result;
}
@ -335,11 +350,13 @@ CodeTemplate parse_template( Str def )
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
ctx->parser = struct_zero(ParseContext);
LexedInfo lexed = lex(ctx, def);
ctx->parser.tokens = lexed.tokens;
if ( ctx->parser.tokens.ptr == nullptr )
return InvalidCode;
ctx->parser.Tokens = toks;
return parser_parse_template(ctx);
}
@ -350,11 +367,13 @@ CodeTypename parse_type( Str def )
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
ctx->parser = struct_zero(ParseContext);
LexedInfo lexed = lex(ctx, def);
ctx->parser.tokens = lexed.tokens;
if ( ctx->parser.tokens.ptr == nullptr )
return InvalidCode;
ctx->parser.Tokens = toks;
return parser_parse_type( ctx, parser_not_from_template, nullptr);
}
@ -365,11 +384,13 @@ CodeTypedef parse_typedef( Str def )
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
ctx->parser = struct_zero(ParseContext);
LexedInfo lexed = lex(ctx, def);
ctx->parser.tokens = lexed.tokens;
if ( ctx->parser.tokens.ptr == nullptr )
return InvalidCode;
ctx->parser.Tokens = toks;
return parser_parse_typedef(ctx);
}
@ -380,11 +401,13 @@ CodeUnion parse_union( Str def )
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
ctx->parser = struct_zero(ParseContext);
LexedInfo lexed = lex(ctx, def);
ctx->parser.tokens = lexed.tokens;
if ( ctx->parser.tokens.ptr == nullptr )
return InvalidCode;
ctx->parser.Tokens = toks;
return parser_parse_union(ctx, parser_not_inplace_def);
}
@ -395,11 +418,13 @@ CodeUsing parse_using( Str def )
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
ctx->parser = struct_zero(ParseContext);
LexedInfo lexed = lex(ctx, def);
ctx->parser.tokens = lexed.tokens;
if ( ctx->parser.tokens.ptr == nullptr )
return InvalidCode;
ctx->parser.Tokens = toks;
return parser_parse_using(ctx);
}
@ -410,11 +435,13 @@ CodeVar parse_variable( Str def )
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
ctx->parser = struct_zero(ParseContext);
LexedInfo lexed = lex(ctx, def);
ctx->parser.tokens = lexed.tokens;
if ( ctx->parser.tokens.ptr == nullptr )
return InvalidCode;
ctx->parser.Tokens = toks;
return parser_parse_variable(ctx);
}

View File

@ -179,7 +179,7 @@ Code untyped_token_fmt( s32 num_tokens, char const* fmt, ... )
Code untyped_toks( TokenSlice tokens )
{
if ( tokens.Num == 0 ) {
if ( tokens.num == 0 ) {
log_failure( "untyped_toks: empty token slice" );
return InvalidCode;
}

View File

@ -23,10 +23,10 @@ Token* lex_current(ParseContext* self, bool skip_formatting )
{
if ( skip_formatting )
{
while ( self->tokens[self->token_id].Type == Tok_NewLine || self->tokens[self->token_id].Type == Tok_Comment )
while ( self->tokens.ptr[self->token_id].Type == Tok_NewLine || self->tokens.ptr[self->token_id].Type == Tok_Comment )
self->token_id++;
}
return & self->tokens[self->token_id];
return & self->tokens.ptr[self->token_id];
}
Token* lex_peek(ParseContext const* self, bool skip_formatting)
@ -34,12 +34,12 @@ Token* lex_peek(ParseContext const* self, bool skip_formatting)
s32 idx = self->token_id;
if ( skip_formatting )
{
while ( self->tokens[idx].Type == Tok_NewLine )
while ( self->tokens.ptr[idx].Type == Tok_NewLine )
idx++;
return & self->tokens[idx];
return & self->tokens.ptr[idx];
}
return & self->tokens[idx];
return & self->tokens.ptr[idx];
}
Token* lex_previous(ParseContext const* self, bool skip_formatting)
@ -47,12 +47,12 @@ Token* lex_previous(ParseContext const* self, bool skip_formatting)
s32 idx = self->token_id;
if ( skip_formatting )
{
while ( self->tokens[idx].Type == Tok_NewLine )
while ( self->tokens.ptr[idx].Type == Tok_NewLine )
idx --;
return & self->tokens[idx];
return & self->tokens.ptr[idx];
}
return & self->tokens[idx - 1];
return & self->tokens.ptr[idx - 1];
}
Token* lex_next(ParseContext const* self, bool skip_formatting)
@ -60,12 +60,12 @@ Token* lex_next(ParseContext const* self, bool skip_formatting)
s32 idx = self->token_id;
if ( skip_formatting )
{
while ( self->tokens[idx].Type == Tok_NewLine )
while ( self->tokens.ptr[idx].Type == Tok_NewLine )
idx++;
return & self->tokens[idx + 1];
return & self->tokens.ptr[idx + 1];
}
return & self->tokens[idx + 1];
return & self->tokens.ptr[idx + 1];
}
enum
@ -137,7 +137,7 @@ s32 lex_preprocessor_define( LexContext* ctx )
);
// GEN_DEBUG_TRAP();
}
array_append( _ctx->Lexer_Tokens, name );
array_append(ctx->tokens, name);
if ( ctx->left && (* ctx->scanner) == '(' )
{
@ -152,7 +152,7 @@ s32 lex_preprocessor_define( LexContext* ctx )
}
Token opening_paren = { { ctx->scanner, 1 }, Tok_Paren_Open, ctx->line, ctx->column, TF_Preprocess };
array_append( _ctx->Lexer_Tokens, opening_paren );
array_append(ctx->tokens, opening_paren);
move_forward();
Token last_parameter = {};
@ -168,7 +168,7 @@ s32 lex_preprocessor_define( LexContext* ctx )
move_forward();
move_forward();
array_append(_ctx->Lexer_Tokens, parameter);
array_append(ctx->tokens, parameter);
skip_whitespace();
last_parameter = parameter;
@ -202,7 +202,7 @@ s32 lex_preprocessor_define( LexContext* ctx )
move_forward();
parameter.Text.Len++;
}
array_append(_ctx->Lexer_Tokens, parameter);
array_append(ctx->tokens, parameter);
skip_whitespace();
last_parameter = parameter;
}
@ -229,7 +229,7 @@ s32 lex_preprocessor_define( LexContext* ctx )
return Lex_ReturnNull;
}
Token comma = { { ctx->scanner, 1 }, Tok_Comma, ctx->line, ctx->column, TF_Preprocess };
array_append(_ctx->Lexer_Tokens, comma);
array_append(ctx->tokens, comma);
move_forward();
}
@ -243,7 +243,7 @@ s32 lex_preprocessor_define( LexContext* ctx )
return Lex_ReturnNull;
}
Token closing_paren = { { ctx->scanner, 1 }, Tok_Paren_Close, ctx->line, ctx->column, TF_Preprocess };
array_append(_ctx->Lexer_Tokens, closing_paren);
array_append(ctx->tokens, closing_paren);
move_forward();
}
else if ( registered_macro && macro_is_functional( * registered_macro) ) {
@ -268,7 +268,7 @@ s32 lex_preprocessor_directive( LexContext* ctx )
{
char const* hash = ctx->scanner;
Token hash_tok = { { hash, 1 }, Tok_Preprocess_Hash, ctx->line, ctx->column, TF_Preprocess };
array_append( _ctx->Lexer_Tokens, hash_tok );
array_append(ctx->tokens, hash_tok);
move_forward();
skip_whitespace();
@ -344,14 +344,14 @@ s32 lex_preprocessor_directive( LexContext* ctx )
ctx->token.Text.Len = ctx->token.Text.Len + ctx->token.Text.Ptr - hash;
ctx->token.Text.Ptr = hash;
array_append( _ctx->Lexer_Tokens, ctx->token );
array_append(ctx->tokens, ctx->token);
return Lex_Continue; // Skip found token, its all handled here.
}
if ( ctx->token.Type == Tok_Preprocess_Else || ctx->token.Type == Tok_Preprocess_EndIf )
{
ctx->token.Flags |= TF_Preprocess_Cond;
array_append( _ctx->Lexer_Tokens, ctx->token );
array_append(ctx->tokens, ctx->token);
end_line();
return Lex_Continue;
}
@ -360,7 +360,7 @@ s32 lex_preprocessor_directive( LexContext* ctx )
ctx->token.Flags |= TF_Preprocess_Cond;
}
array_append( _ctx->Lexer_Tokens, ctx->token );
array_append(ctx->tokens, ctx->token);
skip_whitespace();
@ -411,7 +411,7 @@ s32 lex_preprocessor_directive( LexContext* ctx )
move_forward();
}
array_append( _ctx->Lexer_Tokens, preprocess_content );
array_append(ctx->tokens, preprocess_content);
return Lex_Continue; // Skip found token, its all handled here.
}
@ -475,14 +475,14 @@ s32 lex_preprocessor_directive( LexContext* ctx )
preprocess_content.Text.Len++;
}
array_append( _ctx->Lexer_Tokens, preprocess_content );
array_append(ctx->tokens, preprocess_content);
return Lex_Continue; // Skip found token, its all handled here.
}
void lex_found_token( LexContext* ctx )
{
if ( ctx->token.Type != Tok_Invalid ) {
array_append( _ctx->Lexer_Tokens, ctx->token );
array_append(ctx->tokens, ctx->token);
return;
}
@ -508,7 +508,7 @@ void lex_found_token( LexContext* ctx )
}
ctx->token.Type = type;
array_append( _ctx->Lexer_Tokens, ctx->token );
array_append(ctx->tokens, ctx->token);
return;
}
if ( ( type <= Tok_Star && type >= Tok_Spec_Alignas)
@ -517,13 +517,13 @@ void lex_found_token( LexContext* ctx )
{
ctx->token.Type = type;
ctx->token.Flags |= TF_Specifier;
array_append( _ctx->Lexer_Tokens, ctx->token );
array_append(ctx->tokens, ctx->token);
return;
}
if ( type != Tok_Invalid )
{
ctx->token.Type = type;
array_append( _ctx->Lexer_Tokens, ctx->token );
array_append(ctx->tokens, ctx->token);
return;
}
@ -561,7 +561,7 @@ void lex_found_token( LexContext* ctx )
ctx->token.Type = Tok_Identifier;
}
array_append( _ctx->Lexer_Tokens, ctx->token );
array_append(ctx->tokens, ctx->token);
}
// TODO(Ed): We should dynamically allocate the lexer's array in Allocator_DyanmicContainers.
@ -571,16 +571,15 @@ void lex_found_token( LexContext* ctx )
neverinline
LexedInfo lex(Context* lib_ctx, Str content)
{
LexedInfo info = struct_zero();
LexedInfo info = struct_zero(LexedInfo);
LexContext c = struct_zero(); LexContext* ctx = & c;
LexContext c = struct_zero(LexContext); LexContext* ctx = & c;
c.content = content;
c.left = content.Len;
c.scanner = content.Ptr;
c.line = 1;
c.column = 1;
Array(Token) tokens = array_init_reserve(Token, lib_ctx->Allocator_DyanmicContainers, lib_ctx->InitSize_LexerTokens );
c.tokens = array_init_reserve(Token, lib_ctx->Allocator_DyanmicContainers, lib_ctx->InitSize_LexerTokens );
// TODO(Ed): Re-implement to new constraints:
// 1. Ability to continue on error
@ -592,18 +591,10 @@ LexedInfo lex(Context* lib_ctx, Str content)
return info;
}
array_clear(_ctx->Lexer_Tokens);
b32 preprocess_args = true;
while (c.left )
{
#if 0
if (Tokens.num()) {
log_fmt("\nLastTok: %SB", Tokens.back().to_strbuilder());
}
#endif
c.token = struct_init(Token) { { c.scanner, 0 }, Tok_Invalid, c.line, c.column, TF_Null };
bool is_define = false;
@ -623,7 +614,7 @@ LexedInfo lex(Context* lib_ctx, Str content)
c.token.Type = Tok_NewLine;
c.token.Text.Len++;
array_append( _ctx->Lexer_Tokens, c.token );
array_append(c.tokens, c.token);
continue;
}
}
@ -662,7 +653,7 @@ LexedInfo lex(Context* lib_ctx, Str content)
c.token.Text.Len++;
move_forward();
array_append( _ctx->Lexer_Tokens, c.token );
array_append(c.tokens, c.token);
}
}
continue;
@ -1118,7 +1109,7 @@ LexedInfo lex(Context* lib_ctx, Str content)
move_forward();
c.token.Text.Len++;
}
array_append( _ctx->Lexer_Tokens, c.token );
array_append(c.tokens, c.token);
continue;
}
else if ( (* ctx->scanner) == '*' )
@ -1154,7 +1145,7 @@ LexedInfo lex(Context* lib_ctx, Str content)
move_forward();
c.token.Text.Len++;
}
array_append( _ctx->Lexer_Tokens, c.token );
array_append(c.tokens, c.token);
// end_line();
continue;
}
@ -1242,14 +1233,14 @@ LexedInfo lex(Context* lib_ctx, Str content)
}
else
{
s32 start = max( 0, array_num(_ctx->Lexer_Tokens) - 100 );
s32 start = max( 0, array_num(c.tokens) - 100 );
log_fmt("\n%d\n", start);
for ( s32 idx = start; idx < array_num(_ctx->Lexer_Tokens); idx++ )
for ( s32 idx = start; idx < array_num(c.tokens); idx++ )
{
log_fmt( "Token %d Type: %s : %.*s\n"
, idx
, toktype_to_str( _ctx->Lexer_Tokens[ idx ].Type ).Ptr
, _ctx->Lexer_Tokens[ idx ].Text.Len, _ctx->Lexer_Tokens[ idx ].Text.Ptr
, toktype_to_str( c.tokens[ idx ].Type ).Ptr
, c.tokens[ idx ].Text.Len, c.tokens[ idx ].Text.Ptr
);
}
@ -1265,7 +1256,7 @@ LexedInfo lex(Context* lib_ctx, Str content)
FoundToken:
{
lex_found_token( ctx );
TokType last_type = array_back(_ctx->Lexer_Tokens)->Type;
TokType last_type = array_back(c.tokens)->Type;
if ( last_type == Tok_Preprocess_Macro_Stmt || last_type == Tok_Preprocess_Macro_Expr )
{
Token thanks_c = { { c.scanner, 0 }, Tok_Invalid, c.line, c.column, TF_Null };
@ -1280,21 +1271,21 @@ LexedInfo lex(Context* lib_ctx, Str content)
c.token.Text.Len++;
move_forward();
array_append( _ctx->Lexer_Tokens, c.token );
array_append(c.tokens, c.token);
continue;
}
}
}
}
if ( array_num(_ctx->Lexer_Tokens) == 0 ) {
if ( array_num(c.tokens) == 0 ) {
log_failure( "Failed to lex any tokens" );
return info;
}
info.messages = c.messages;
info.text = content;
info.tokens = struct_init(TokenSlice) { tokens, scast(s32, array_num(tokens)) };
info.tokens = struct_init(TokenSlice) { pcast(Token*, c.tokens), scast(s32, array_num(c.tokens)) };
return info;
}

View File

@ -13,8 +13,8 @@ constexpr bool lex_skip_formatting = true;
void parser_push( ParseContext* ctx, ParseStackNode* node )
{
node->Prev = ctx->Scope;
ctx->Scope = node;
node->prev = ctx->scope;
ctx->scope = node;
#if 0 && GEN_BUILD_DEBUG
log_fmt("\tEntering parser: %.*s\n", Scope->ProcName.Len, Scope->ProcName.Ptr );
@ -26,19 +26,19 @@ void parser_pop(ParseContext* ctx)
#if 0 && GEN_BUILD_DEBUG
log_fmt("\tPopping parser: %.*s\n", Scope->ProcName.Len, Scope->ProcName.Ptr );
#endif
ctx->Scope = ctx->Scope->Prev;
ctx->scope = ctx->scope->prev;
}
StrBuilder parser_to_strbuilder(ParseContext const* ctx, AllocatorInfo temp)
{
StrBuilder result = strbuilder_make_reserve( temp, kilobytes(4) );
Token scope_start = * ctx->Scope->Start;
Token last_valid = (ctx->token_id >= ctx->tokens.num) ? ctx->tokens[ctx->tokens.num -1] : (* lex_peek(ctx, true));
Token scope_start = * ctx->scope->start;
Token last_valid = (ctx->token_id >= ctx->tokens.num) ? ctx->tokens.ptr[ctx->tokens.num -1] : (* lex_peek(ctx, true));
sptr length = scope_start.Text.Len;
char const* current = scope_start.Text.Ptr + length;
while ( current <= ctx->tokens[ctx->tokens.num - 1].Text.Ptr && (* current) != '\n' && length < 74 )
while ( current <= ctx->tokens.ptr[ctx->tokens.num - 1].Text.Ptr && (* current) != '\n' && length < 74 )
{
current++;
length++;
@ -60,18 +60,18 @@ StrBuilder parser_to_strbuilder(ParseContext const* ctx, AllocatorInfo temp)
else
strbuilder_append_fmt(& result, "\t(%d, %d)\n", last_valid.Line, last_valid.Column );
ParseStackNode* curr_scope = ctx->Scope;
ParseStackNode* curr_scope = ctx->scope;
s32 level = 0;
do
{
if ( curr_scope->Name.Ptr ) {
strbuilder_append_fmt(& result, "\t%d: %s, AST Name: %.*s\n", level, curr_scope->ProcName.Ptr, curr_scope->Name.Len, curr_scope->Name.Ptr );
if ( curr_scope->name.Ptr ) {
strbuilder_append_fmt(& result, "\t%d: %S, AST Name: %S\n", level, curr_scope->proc_name, curr_scope->name );
}
else {
strbuilder_append_fmt(& result, "\t%d: %s\n", level, curr_scope->ProcName.Ptr );
strbuilder_append_fmt(& result, "\t%d: %S\n", level, curr_scope->proc_name );
}
curr_scope = curr_scope->Prev;
curr_scope = curr_scope->prev;
level++;
}
while ( curr_scope );
@ -81,11 +81,11 @@ StrBuilder parser_to_strbuilder(ParseContext const* ctx, AllocatorInfo temp)
bool lex__eat(Context* ctx, ParseContext* parser, TokType type)
{
if ( parser->tokens.num - parser->token_id <= 0 ) {
log_failure( "No tokens left.\n%s", parser_to_strbuilder(parser, ctx->Allocator_Temp) );
log_failure( "No tokens left.\n%SB", parser_to_strbuilder(parser, ctx->Allocator_Temp) );
return false;
}
Token at_idx = parser->tokens[ parser->token_id ];
Token at_idx = parser->tokens.ptr[ parser->token_id ];
if ( ( at_idx.Type == Tok_NewLine && type != Tok_NewLine )
|| ( at_idx.Type == Tok_Comment && type != Tok_Comment ) )
@ -104,9 +104,9 @@ bool lex__eat(Context* ctx, ParseContext* parser, TokType type)
if ( not_accepted )
{
Token tok = * lex_current( parser, lex_skip_formatting );
log_failure( "Parse Error, TokArray::eat, Expected: ' %s ' not ' %.*s ' (%d, %d)`\n%s"
, toktype_to_str(type).Ptr
, at_idx.Text.Len, at_idx.Text.Ptr
log_failure( "Parse Error, TokArray::eat, Expected: ' %S ' not ' %S ' (%d, %d)`\n%SB"
, toktype_to_str(type)
, at_idx.Text
, tok.Line
, tok.Column
, parser_to_strbuilder(parser, ctx->Allocator_Temp)
@ -126,14 +126,11 @@ bool lex__eat(Context* ctx, ParseContext* parser, TokType type)
internal
void parser_init(Context* ctx)
{
ctx->Lexer_Tokens = array_init_reserve(Token, ctx->Allocator_DyanmicContainers, ctx->InitSize_LexerTokens );
}
internal
void parser_deinit(Context* ctx)
{
Array(Token) null_array = { nullptr };
ctx->Lexer_Tokens = null_array;
}
#pragma region Helper Macros
@ -183,7 +180,7 @@ bool _check_parse_args(ParseContext* parser, Str def, char const* func_name )
#if GEN_COMPILER_CPP
# define NullScope { nullptr, {nullptr, 0}, lex_current( & ctx->parser, lex_dont_skip_formatting ), Str{nullptr, 0}, txt( __func__ ), { nullptr} }
#else
# define NullScope (ParseStackNode){ nullptr, {nullptr, 0}, lex_current( & ctx->parser.Tokens, lex_dont_skip_formatting ), (Str){nullptr, 0}, txt( __func__ ), { nullptr} }
# define NullScope (ParseStackNode){ nullptr, {nullptr, 0}, lex_current( & ctx->parser, lex_dont_skip_formatting ), (Str){nullptr, 0}, txt( __func__ ), { nullptr} }
#endif
#pragma endregion Helper Macros
@ -520,14 +517,14 @@ Code parse_array_decl(Context* ctx)
if ( left == 0 )
{
log_failure( "Error, unexpected end of array declaration ( '[]' scope started )\n%s", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Error, unexpected end of array declaration ( '[]' scope started )\n%SB", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
if ( currtok.Type == Tok_BraceSquare_Close )
{
log_failure( "Error, empty array expression in definition\n%s", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Error, empty array expression in definition\n%SB", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
@ -550,14 +547,14 @@ Code parse_array_decl(Context* ctx)
if ( left == 0 )
{
log_failure( "Error, unexpected end of array declaration, expected ]\n%s", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Error, unexpected end of array declaration, expected ]\n%SB", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
if ( currtok.Type != Tok_BraceSquare_Close )
{
log_failure( "%s: Error, expected ] in array declaration, not %s\n%s", toktype_to_str( currtok.Type ), parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "%s: Error, expected ] in array declaration, not %S\n%SB", toktype_to_str( currtok.Type ), parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
@ -697,7 +694,7 @@ internal
Code parse_class_struct(Context* ctx, TokType which, bool inplace_def)
{
if ( which != Tok_Decl_Class && which != Tok_Decl_Struct ) {
log_failure( "Error, expected class or struct, not %s\n%s", toktype_to_str( which ), parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Error, expected class or struct, not %S\n%SB", toktype_to_str( which ), parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
return InvalidCode;
}
@ -725,7 +722,7 @@ Code parse_class_struct(Context* ctx, TokType which, bool inplace_def)
if ( check( Tok_Identifier ) ) {
name = parse_identifier(ctx, nullptr);
ctx->parser.Scope->Name = name.Text;
ctx->parser.scope->name = name.Text;
}
// <ModuleFlags> <class/struct> <Attributes> <Name>
@ -1139,7 +1136,7 @@ CodeBody parse_class_struct_body(Context* ctx, TokType which, Token name)
if ( member == Code_Invalid )
{
log_failure( "Failed to parse member\n%s", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Failed to parse member\n%SB", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
@ -1186,17 +1183,17 @@ Code parse_complicated_definition(Context* ctx, TokType which)
b32 had_paren = false;
for ( ; idx < tokens.num; idx++ )
{
if ( tokens[ idx ].Type == Tok_BraceCurly_Open )
if ( tokens.ptr[ idx ].Type == Tok_BraceCurly_Open )
level++;
if ( tokens[ idx ].Type == Tok_BraceCurly_Close ) {
if ( tokens.ptr[ idx ].Type == Tok_BraceCurly_Close ) {
level--;
had_def = level == 0;
}
b32 found_fn_def = had_def && had_paren;
if ( level == 0 && (tokens[ idx ].Type == Tok_Statement_End || found_fn_def) )
if ( level == 0 && (tokens.ptr[ idx ].Type == Tok_Statement_End || found_fn_def) )
break;
}
@ -1219,23 +1216,23 @@ Code parse_complicated_definition(Context* ctx, TokType which)
return result;
}
Token tok = tokens[ idx - 1 ];
Token tok = tokens.ptr[ idx - 1 ];
if ( tok_is_specifier(tok) && spec_is_trailing( str_to_specifier( tok.Text)) )
{
// <which> <type_identifier>(...) <specifier> ...;
s32 spec_idx = idx - 1;
Token spec = tokens[spec_idx];
Token spec = tokens.ptr[spec_idx];
while ( tok_is_specifier(spec) && spec_is_trailing( str_to_specifier( spec.Text)) )
{
-- spec_idx;
spec = tokens[spec_idx];
spec = tokens.ptr[spec_idx];
}
if ( tokens[spec_idx].Type == Tok_Paren_Close )
if ( tokens.ptr[spec_idx].Type == Tok_Paren_Close )
{
// Forward declaration with trailing specifiers for a procedure
tok = tokens[spec_idx];
tok = tokens.ptr[spec_idx];
Code result = parse_operator_function_or_variable(ctx, false, NullCode, NullCode);
// <Attributes> <Specifiers> <ReturnType/ValueType> <operator <Op>, or Name> ...
@ -1243,13 +1240,13 @@ Code parse_complicated_definition(Context* ctx, TokType which)
return result;
}
log_failure( "Unsupported or bad member definition after %s declaration\n%s", toktype_to_str(which), parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Unsupported or bad member definition after %S declaration\n%SB", toktype_to_str(which), parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
if ( tok.Type == Tok_Identifier )
{
tok = tokens[ idx - 2 ];
tok = tokens.ptr[ idx - 2 ];
bool is_indirection = tok.Type == Tok_Ampersand || tok.Type == Tok_Star;
bool ok_to_parse = false;
@ -1264,21 +1261,21 @@ Code parse_complicated_definition(Context* ctx, TokType which)
// Should be a name right after the type.
Token name = parse_identifier(ctx, nullptr);
ctx->parser.Scope->Name = name.Text;
ctx->parser.scope->name = name.Text;
CodeVar result = parse_variable_after_name(ctx, ModuleFlag_None, NullCode, NullCode, type, name.Text);
parser_pop(& ctx->parser);
return (Code) result;
}
else if ( tok.Type == Tok_Identifier && tokens[ idx - 3 ].Type == which )
else if ( tok.Type == Tok_Identifier && tokens.ptr[ idx - 3 ].Type == which )
{
// Its a variable with type ID using <which> namespace.
// <which> <type_identifier> <identifier>;
ok_to_parse = true;
}
else if ( tok.Type == Tok_Assign_Classifer
&& ( ( tokens[idx - 5].Type == which && tokens[idx - 4].Type == Tok_Decl_Class )
|| ( tokens[idx - 4].Type == which))
&& ( ( tokens.ptr[idx - 5].Type == which && tokens.ptr[idx - 4].Type == Tok_Decl_Class )
|| ( tokens.ptr[idx - 4].Type == which))
)
{
// Its a forward declaration of an enum
@ -1298,7 +1295,7 @@ Code parse_complicated_definition(Context* ctx, TokType which)
if ( ! ok_to_parse )
{
log_failure( "Unsupported or bad member definition after %s declaration\n%s", toktype_to_str(which), parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Unsupported or bad member definition after %S declaration\n%SB", toktype_to_str(which), parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
@ -1310,14 +1307,14 @@ Code parse_complicated_definition(Context* ctx, TokType which)
}
else if ( tok.Type >= Tok_Type_Unsigned && tok.Type <= Tok_Type_MS_W64 )
{
tok = tokens[ idx - 2 ];
tok = tokens.ptr[ idx - 2 ];
if ( tok.Type != Tok_Assign_Classifer
|| ( ( tokens[idx - 5].Type != which && tokens[idx - 4].Type != Tok_Decl_Class )
&& ( tokens[idx - 4].Type != which))
|| ( ( tokens.ptr[idx - 5].Type != which && tokens.ptr[idx - 4].Type != Tok_Decl_Class )
&& ( tokens.ptr[idx - 4].Type != which))
)
{
log_failure( "Unsupported or bad member definition after %s declaration\n%s", toktype_to_str(which), parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Unsupported or bad member definition after %S declaration\n%SB", toktype_to_str(which), parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
@ -1347,7 +1344,7 @@ Code parse_complicated_definition(Context* ctx, TokType which)
}
else
{
log_failure( "Unsupported or bad member definition after %s declaration\n%SB", toktype_to_str(which).Ptr, parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Unsupported or bad member definition after %S declaration\n%SB", toktype_to_str(which).Ptr, parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
@ -1368,7 +1365,7 @@ Code parse_assignment_expression(Context* ctx)
if ( currtok.Type == Tok_Statement_End && currtok.Type != Tok_Comma )
{
log_failure( "Expected expression after assignment operator\n%s", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Expected expression after assignment operator\n%SB", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
@ -1423,7 +1420,7 @@ Code parse_forward_or_definition(Context* ctx, TokType which, bool is_inplace )
default:
log_failure( "Error, wrong token type given to parse_complicated_definition "
"(only supports class, enum, struct, union) \n%s"
"(only supports class, enum, struct, union) \n%SB"
, parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
return InvalidCode;
@ -1558,7 +1555,7 @@ CodeFn parse_function_after_name(Context* ctx
default:
{
log_failure("Body must be either of Function_Body or Untyped type, %s\n%s", code_debug_str(body), parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp));
log_failure("Body must be either of Function_Body or Untyped type, %S\n%SB", code_debug_str(body), parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp));
parser_pop(& ctx->parser);
return InvalidCode;
}
@ -1706,7 +1703,7 @@ CodeBody parse_global_nspace(Context* ctx, CodeType which)
case Tok_Decl_Extern_Linkage:
if ( which == CT_Extern_Linkage_Body )
log_failure( "Nested extern linkage\n%s", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Nested extern linkage\n%SB", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
member = cast(Code, parser_parse_extern_link(ctx));
// extern "..." { ... }
@ -1797,7 +1794,7 @@ CodeBody parse_global_nspace(Context* ctx, CodeType which)
case Tok_Module_Export: {
if ( which == CT_Export_Body )
log_failure( "Nested export declaration\n%s", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Nested export declaration\n%SB", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
member = cast(Code, parser_parse_export_body(ctx));
// export { ... }
@ -1815,7 +1812,7 @@ CodeBody parse_global_nspace(Context* ctx, CodeType which)
{
if ( ! tok_is_attribute(currtok))
{
log_failure("Unbounded macro expression residing in class/struct body\n%S", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp));
log_failure("Unbounded macro expression residing in class/struct body\n%SB", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp));
return InvalidCode;
}
}
@ -1905,12 +1902,12 @@ CodeBody parse_global_nspace(Context* ctx, CodeType which)
for ( ; idx < ctx->parser.tokens.num; idx++ )
{
Token tok = ctx->parser.tokens[ idx ];
Token tok = ctx->parser.tokens.ptr[ idx ];
if ( tok.Type == Tok_Identifier )
{
idx++;
tok = ctx->parser.tokens[ idx ];
tok = ctx->parser.tokens.ptr[ idx ];
if ( tok.Type == Tok_Access_StaticSymbol )
continue;
@ -1978,8 +1975,8 @@ Code parse_global_nspace_constructor_destructor(Context* ctx, CodeSpecifiers spe
TokenSlice tokens = ctx->parser.tokens;
s32 idx = ctx->parser.token_id;
Token nav = tokens[ idx ];
for ( ; idx < tokens.num; idx++, nav = tokens[ idx ] )
Token nav = tokens.ptr[ idx ];
for ( ; idx < tokens.num; idx++, nav = tokens.ptr[ idx ] )
{
if ( nav.Text.Ptr[0] == '<' )
{
@ -2100,7 +2097,7 @@ Token parse_identifier(Context* ctx, bool* possible_member_function)
parser_push(& ctx->parser, & scope );
Token name = currtok;
ctx->parser.Scope->Name = name.Text;
ctx->parser.scope->name = name.Text;
Macro* macro = lookup_macro(currtok.Text);
b32 accept_as_identifier = macro && bitfield_is_set(MacroFlags, macro->Flags, MF_Allow_As_Identifier );
@ -2131,14 +2128,14 @@ Token parse_identifier(Context* ctx, bool* possible_member_function)
Token invalid = NullToken;
if ( left == 0 )
{
log_failure( "Error, unexpected end of static symbol identifier\n%s", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Error, unexpected end of static symbol identifier\n%SB", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return invalid;
}
if ( currtok.Type == Tok_Operator && currtok.Text.Ptr[0] == '~' )
{
bool is_destructor = str_are_equal( ctx->parser.Scope->Prev->ProcName, txt("parser_parse_destructor"));
bool is_destructor = str_are_equal( ctx->parser.scope->prev->proc_name, txt("parser_parse_destructor"));
if (is_destructor)
{
name.Text.Len = ( ( sptr )prevtok.Text.Ptr + prevtok.Text.Len ) - ( sptr )name.Text.Ptr;
@ -2146,7 +2143,7 @@ Token parse_identifier(Context* ctx, bool* possible_member_function)
return name;
}
log_failure( "Error, had a ~ operator after %SB but not a destructor\n%s", toktype_to_str( prevtok.Type ), parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Error, had a ~ operator after %S but not a destructor\n%SB", toktype_to_str( prevtok.Type ), parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return invalid;
}
@ -2158,7 +2155,7 @@ Token parse_identifier(Context* ctx, bool* possible_member_function)
else
{
log_failure( "Found a member function pointer identifier but the parsing context did not expect it\n%s", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Found a member function pointer identifier but the parsing context did not expect it\n%SB", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return invalid;
}
@ -2166,7 +2163,7 @@ Token parse_identifier(Context* ctx, bool* possible_member_function)
if ( currtok.Type != Tok_Identifier )
{
log_failure( "Error, expected static symbol identifier, not %s\n%s", toktype_to_str( currtok.Type ), parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Error, expected static symbol identifier, not %S\n%SB", toktype_to_str( currtok.Type ), parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return invalid;
}
@ -2198,12 +2195,12 @@ CodeInclude parse_include(Context* ctx)
if ( ! check( Tok_String ))
{
log_failure( "Error, expected include string after #include\n%s", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Error, expected include string after #include\n%SB", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
ctx->parser.Scope->Name = currtok.Text;
ctx->parser.scope->name = currtok.Text;
include->Content = cache_str( currtok.Text );
eat( Tok_String );
// #include <Path> or "Path"
@ -2247,12 +2244,12 @@ CodeOperator parse_operator_after_ret_type(Context* ctx
&& currtok.Type != Tok_Ampersand
&& currtok.Type != Tok_Ampersand_DBL )
{
log_failure( "Expected operator after 'operator' keyword\n%s", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Expected operator after 'operator' keyword\n%SB", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
ctx->parser.Scope->Name = currtok.Text;
ctx->parser.scope->name = currtok.Text;
bool was_new_or_delete = false;
@ -2503,7 +2500,7 @@ CodeOperator parse_operator_after_ret_type(Context* ctx
{
if ( op == Op_Invalid )
{
log_failure( "Invalid operator '%s'\n%s", prevtok.Text, parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Invalid operator '%S'\n%SB", prevtok.Text, parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
@ -2513,7 +2510,7 @@ CodeOperator parse_operator_after_ret_type(Context* ctx
if ( op == Op_Invalid )
{
log_failure( "Invalid operator '%s'\n%s", currtok.Text, parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Invalid operator '%S'\n%SB", currtok.Text, parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
@ -2668,7 +2665,7 @@ Code parse_operator_function_or_variable(Context* ctx, bool expects_function, Co
else
{
Token name = parse_identifier(ctx, nullptr);
ctx->parser.Scope->Name = name.Text;
ctx->parser.scope->name = name.Text;
bool detected_capture = check( Tok_Paren_Open );
@ -2719,7 +2716,7 @@ Code parse_operator_function_or_variable(Context* ctx, bool expects_function, Co
else
{
if ( expects_function ) {
log_failure( "Expected function declaration (consteval was used)\n%s", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Expected function declaration (consteval was used)\n%SB", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
@ -2777,12 +2774,12 @@ CodePragma parse_pragma(Context* ctx)
// #pragma
if ( ! check( Tok_Preprocess_Content )) {
log_failure( "Error, expected content after #pragma\n%s", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Error, expected content after #pragma\n%SB", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
ctx->parser.Scope->Name = currtok.Text;
ctx->parser.scope->name = currtok.Text;
pragma->Content = cache_str( currtok.Text );
eat( Tok_Preprocess_Content );
@ -2890,7 +2887,7 @@ CodeParams parse_params(Context* ctx, bool use_template_capture )
Token value_tok = currtok;
if ( currtok.Type == Tok_Comma ) {
log_failure( "Expected value after assignment operator\n%s.", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Expected value after assignment operator\n%SB.", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
@ -2999,7 +2996,7 @@ CodeParams parse_params(Context* ctx, bool use_template_capture )
Token value_tok = currtok;
if ( currtok.Type == Tok_Comma ) {
log_failure( "Expected value after assignment operator\n%s", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Expected value after assignment operator\n%SB", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
@ -3060,7 +3057,7 @@ CodeParams parse_params(Context* ctx, bool use_template_capture )
else
{
if ( ! check( Tok_Operator ) || currtok.Text.Ptr[ 0 ] != '>' ) {
log_failure( "Expected '<' after 'template' keyword\n%s", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Expected '<' after 'template' keyword\n%SB", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
@ -3080,7 +3077,7 @@ CodePreprocessCond parse_preprocess_cond(Context* ctx)
if ( ! tok_is_preprocess_cond(currtok) )
{
log_failure( "Error, expected preprocess conditional\n%s", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Error, expected preprocess conditional\n%SB", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
@ -3093,12 +3090,12 @@ CodePreprocessCond parse_preprocess_cond(Context* ctx)
if ( ! check( Tok_Preprocess_Content ))
{
log_failure( "Error, expected content after #define\n%s", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Error, expected content after #define\n%SB", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
ctx->parser.Scope->Name = currtok.Text;
ctx->parser.scope->name = currtok.Text;
cond->Content = cache_str( currtok.Text );
eat( Tok_Preprocess_Content );
// #<Conditiona> <Content>
@ -3122,7 +3119,7 @@ Code parse_simple_preprocess(Context* ctx, TokType which)
Macro* macro = lookup_macro( full_macro.Text );
if ( which != Tok_Preprocess_Unsupported && macro == nullptr ) {
log_failure("Expected the macro %S to be registered\n%S", full_macro, parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp));
log_failure("Expected the macro %S to be registered\n%SB", full_macro.Text, parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp));
return NullCode;
}
@ -3170,7 +3167,7 @@ Code parse_simple_preprocess(Context* ctx, TokType which)
// <Macro> { <Body> }
// TODO(Ed): Review this?
Str prev_proc = ctx->parser.Scope->Prev->ProcName;
Str prev_proc = ctx->parser.scope->prev->proc_name;
if ( macro->Type == MT_Typename && c_str_compare_len( prev_proc.Ptr, "parser_parse_typedef", prev_proc.Len ) != 0 )
{
if ( check( Tok_Statement_End ))
@ -3191,14 +3188,14 @@ Code parse_simple_preprocess(Context* ctx, TokType which)
{
// If the macro is just a macro in the body of an AST it may have a semi-colon for the user to close on purpsoe
// (especially for functional macros)
Str calling_proc = ctx->parser.Scope->Prev->ProcName;
Str calling_proc = ctx->parser.scope->prev->proc_name;
if (str_contains(ctx->parser.Scope->Prev->ProcName, txt("parser_parse_enum")))
if (str_contains(ctx->parser.scope->prev->proc_name, txt("parser_parse_enum")))
{
// Do nothing
goto Leave_Scope_Early;
}
else if (macro && macro->Type == MT_Typename && str_contains(ctx->parser.Scope->Prev->ProcName, txt("parser_parse_typedef")))
else if (macro && macro->Type == MT_Typename && str_contains(ctx->parser.scope->prev->proc_name, txt("parser_parse_typedef")))
{
if ( peektok.Type == Tok_Statement_End )
{
@ -3235,7 +3232,7 @@ Code parse_simple_preprocess(Context* ctx, TokType which)
Leave_Scope_Early:
Code result = untyped_str( full_macro.Text );
ctx->parser.Scope->Name = full_macro.Text;
ctx->parser.scope->name = full_macro.Text;
parser_pop(& ctx->parser);
return result;
@ -3253,7 +3250,7 @@ Code parse_static_assert(Context* ctx)
Token content = currtok;
ctx->parser.Scope->Name = content.Text;
ctx->parser.scope->name = content.Text;
eat( Tok_StaticAssert );
eat( Tok_Paren_Open );
@ -3689,7 +3686,7 @@ CodeDefine parser_parse_define(Context* ctx)
parser_pop(& ctx->parser);
return InvalidCode;
}
ctx->parser.Scope->Name = currtok.Text;
ctx->parser.scope->name = currtok.Text;
define->Name = cache_str( currtok.Text );
eat( Tok_Identifier );
// #define <Name>
@ -3762,8 +3759,8 @@ CodeDestructor parser_parse_destructor(Context* ctx, CodeSpecifiers specifiers)
ParseStackNode scope = NullScope;
parser_push(& ctx->parser, & scope );
bool has_context = ctx->parser.Scope && ctx->parser.Scope->Prev;
bool is_in_global_nspace = has_context && str_are_equal( ctx->parser.Scope->Prev->ProcName, txt("parse_global_nspace") );
bool has_context = ctx->parser.scope && ctx->parser.scope->prev;
bool is_in_global_nspace = has_context && str_are_equal( ctx->parser.scope->prev->proc_name, txt("parse_global_nspace") );
if ( check( Tok_Spec_Virtual ) )
{
@ -3902,7 +3899,7 @@ CodeEnum parser_parse_enum(Context* ctx, bool inplace_def)
if ( check( Tok_Identifier ) )
{
name = currtok;
ctx->parser.Scope->Name = currtok.Text;
ctx->parser.scope->name = currtok.Text;
eat( Tok_Identifier );
}
// enum <class> <Attributes> <Name>
@ -4235,7 +4232,7 @@ CodeFriend parser_parse_friend(Context* ctx)
{
// Name
Token name = parse_identifier(ctx, nullptr);
ctx->parser.Scope->Name = name.Text;
ctx->parser.scope->name = name.Text;
// friend <ReturnType> <Name>
function = parse_function_after_name(ctx, ModuleFlag_None, NullCode, specifiers, type, name );
@ -4347,7 +4344,7 @@ CodeFn parser_parse_function(Context* ctx)
// <export> <Attributes> <Specifiers> <ReturnType>
Token name = parse_identifier(ctx, nullptr);
ctx->parser.Scope->Name = name.Text;
ctx->parser.scope->name = name.Text;
if ( ! tok_is_valid(name) ) {
parser_pop(& ctx->parser);
return InvalidCode;
@ -4371,7 +4368,7 @@ CodeNS parser_parse_namespace(Context* ctx)
// namespace
Token name = parse_identifier(ctx, nullptr);
ctx->parser.Scope->Name = name.Text;
ctx->parser.scope->name = name.Text;
// namespace <Name>
CodeBody body = parse_global_nspace(ctx, CT_Namespace_Body );
@ -4486,7 +4483,7 @@ CodeOpCast parser_parse_operator_cast(Context* ctx, CodeSpecifiers specifiers)
Str scope_name = { type->Name.Ptr, type->Name.Len };
Token scope_name_tok = { scope_name, Tok_Identifier, 0, 0, TF_Null };
ctx->parser.Scope->Name = scope_name_tok.Text;
ctx->parser.scope->name = scope_name_tok.Text;
eat( Tok_Paren_Open );
eat( Tok_Paren_Close );
@ -4685,8 +4682,8 @@ CodeTemplate parser_parse_template(Context* ctx)
}
bool has_context = ctx->parser.Scope && ctx->parser.Scope->Prev;
bool is_in_global_nspace = has_context && str_are_equal( ctx->parser.Scope->Prev->ProcName, txt("parse_global_nspace") );
bool has_context = ctx->parser.scope && ctx->parser.scope->prev;
bool is_in_global_nspace = has_context && str_are_equal( ctx->parser.scope->prev->proc_name, txt("parse_global_nspace") );
// Possible constructor implemented at global file scope.
if (is_in_global_nspace)
{
@ -4846,7 +4843,7 @@ CodeTypename parser_parse_type(Context* ctx, bool from_template, bool* typedef_i
name.Text.Len = ( ( sptr )prevtok.Text.Ptr + prevtok.Text.Len ) - ( sptr )name.Text.Ptr;
// <Attributes> <Specifiers> <class, enum, struct, union> <inplace def>
}
ctx->parser.Scope->Name = name.Text;
ctx->parser.scope->name = name.Text;
// <Attributes> <Specifiers> <class, enum, struct, union> <Name>
}
@ -4903,10 +4900,10 @@ else if ( currtok.Type == Tok_DeclType )
if ( ! from_template )
{
name = parse_identifier(ctx, nullptr);
ctx->parser.Scope->Name = name.Text;
ctx->parser.scope->name = name.Text;
if ( ! tok_is_valid(name) )
{
log_failure( "Error, failed to type signature\n%s", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Error, failed to type signature\n%SB", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
@ -4924,10 +4921,10 @@ else if ( currtok.Type == Tok_DeclType )
else
{
name = parse_identifier(ctx, nullptr);
ctx->parser.Scope->Name = name.Text;
ctx->parser.scope->name = name.Text;
if ( ! tok_is_valid(name) )
{
log_failure( "Error, failed to type signature\n%s", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
log_failure( "Error, failed to type signature\n%SB", parser_to_strbuilder(& ctx->parser, ctx->Allocator_Temp) );
parser_pop(& ctx->parser);
return InvalidCode;
}
@ -4999,8 +4996,8 @@ else if ( currtok.Type == Tok_DeclType )
last_capture = scanner;
}
bool has_context = ctx->parser.Scope && ctx->parser.Scope->Prev;
bool is_for_opcast = has_context && str_are_equal( ctx->parser.Scope->Prev->ProcName, txt("parser_parse_operator_cast") );
bool has_context = ctx->parser.scope && ctx->parser.scope->prev;
bool is_for_opcast = has_context && str_are_equal( ctx->parser.scope->prev->proc_name, txt("parser_parse_operator_cast") );
if ( is_for_opcast && is_function_typename && last_capture )
{
// If we're parsing for an operator cast, having one capture start is not enough
@ -5281,7 +5278,7 @@ CodeTypedef parser_parse_typedef(Context* ctx)
name = currtok;
Code macro = parse_simple_preprocess(ctx, currtok.Type);
name.Text.Len = macro->Content.Len;
ctx->parser.Scope->Name = name.Text;
ctx->parser.scope->name = name.Text;
// <ModuleFalgs> typedef <Preprocessed_Macro>
if ( currtok.Type == Tok_Identifier )
@ -5487,7 +5484,7 @@ CodeUnion parser_parse_union(Context* ctx, bool inplace_def)
if ( check( Tok_Identifier ) )
{
name = currtok.Text;
ctx->parser.Scope->Name = currtok.Text;
ctx->parser.scope->name = currtok.Text;
eat( Tok_Identifier );
}
// <ModuleFlags> union <Attributes> <Name>
@ -5644,7 +5641,7 @@ CodeUsing parser_parse_using(Context* ctx)
}
name = currtok;
ctx->parser.Scope->Name = name.Text;
ctx->parser.scope->name = name.Text;
eat( Tok_Identifier );
// <ModuleFlags> using <namespace> <Name>
@ -5765,10 +5762,10 @@ CodeVar parser_parse_variable(Context* ctx)
if ( cast(Code, type) == Code_Invalid )
return InvalidCode;
ctx->parser.Scope->Name = parse_identifier(ctx, nullptr).Text;
ctx->parser.scope->name = parse_identifier(ctx, nullptr).Text;
// <ModuleFlags> <Attributes> <Specifiers> <ValueType> <Name>
CodeVar result = parse_variable_after_name(ctx, mflags, attributes, specifiers, type, ctx->parser.Scope->Name );
CodeVar result = parse_variable_after_name(ctx, mflags, attributes, specifiers, type, ctx->parser.scope->name );
// Regular : <ModuleFlags> <Attributes> <Specifiers> <ValueType> <Name> = <Value>; <InlineCmt>
// Bitfield : <ModuleFlags> <Attributes> <Specifiers> <ValueType> <Name> : <BitfieldSize> = <Value>; <InlineCmt>

View File

@ -135,6 +135,7 @@ struct LexContext
s32 line;
s32 column;
Token token;
Array(Token) tokens;
};
struct LexedInfo
@ -158,7 +159,7 @@ struct ParseMessage
struct ParseContext
{
ParseMessage* messages;
ParseStackNode* Scope;
ParseStackNode* scope;
// TokArray Tokens;
TokenSlice tokens;
s32 token_id;

View File

@ -310,7 +310,15 @@
# if GEN_COMPILER_CPP
# define struct_zero(type) {}
# else
# define struct_zero(type) {0}
# define struct_zero(type) (type) {0}
# endif
#endif
#ifndef struct_zero_init
# if GEN_COMPILER_CPP
# define struct_zero_init() {}
# else
# define struct_zero_init() {0}
# endif
#endif

View File

@ -320,7 +320,7 @@ inline
StrBuilder strbuilder_fmt_buf(AllocatorInfo allocator, char const* fmt, ...)
{
local_persist thread_local
PrintF_Buffer buf = struct_zero(PrintF_Buffer);
PrintF_Buffer buf = struct_zero_init();
va_list va;
va_start(va, fmt);