progress on modularizing parser paths

This commit is contained in:
Edward R. Gonzalez 2025-03-17 01:09:46 -04:00
parent 790087aa3c
commit 2ed36506b1
4 changed files with 651 additions and 537 deletions

View File

@ -3,8 +3,8 @@
#include "code_serialization.cpp" #include "code_serialization.cpp"
#endif #endif
internal void parser_init(); internal void parser_init(Context* ctx);
internal void parser_deinit(); internal void parser_deinit(Context* ctx);
internal internal
void* fallback_allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ) void* fallback_allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags )
@ -343,7 +343,7 @@ void init(Context* ctx)
} }
define_constants(); define_constants();
parser_init(); parser_init(ctx);
++ context_counter; ++ context_counter;
} }
@ -392,7 +392,7 @@ void deinit(Context* ctx)
while ( left--, left ); while ( left--, left );
array_free( ctx->Fallback_AllocatorBuckets); array_free( ctx->Fallback_AllocatorBuckets);
} }
parser_deinit(); parser_deinit(ctx);
if (_ctx == ctx) if (_ctx == ctx)
_ctx = nullptr; _ctx = nullptr;

View File

@ -10,6 +10,9 @@
ParseInfo wip_parse_str(LexedInfo lexed, ParseOpts* opts) ParseInfo wip_parse_str(LexedInfo lexed, ParseOpts* opts)
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
TokArray toks; TokArray toks;
if (lexed.tokens.Num == 0 && lexed.tokens.Ptr == nullptr) { if (lexed.tokens.Num == 0 && lexed.tokens.Ptr == nullptr) {
check_parse_args(lexed.text); check_parse_args(lexed.text);
@ -23,31 +26,44 @@ ParseInfo wip_parse_str(LexedInfo lexed, ParseOpts* opts)
// TODO(Ed): ParseInfo should be set to the parser context. // TODO(Ed): ParseInfo should be set to the parser context.
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
push_scope();
CodeBody result = parse_global_nspace(_ctx, CT_Global_Body);
parser_pop(& _ctx->parser);
ParseStackNode scope = NullScope;
parser_push(& ctx->parser, & scope);
CodeBody result = parse_global_nspace(ctx,CT_Global_Body);
parser_pop(& ctx->parser);
return info; return info;
} }
CodeClass parse_class( Str def ) CodeClass parse_class( Str def )
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def ); check_parse_args( def );
TokArray toks = lex( def ); TokArray toks = lex( def );
if ( toks.Arr == nullptr ) if ( toks.Arr == nullptr )
return InvalidCode; return InvalidCode;
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
push_scope();
CodeClass result = (CodeClass) parse_class_struct( _ctx, Tok_Decl_Class, parser_not_inplace_def ); ParseStackNode scope = NullScope;
parser_pop(& _ctx->parser); 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; return result;
} }
CodeConstructor parse_constructor( Str def ) CodeConstructor parse_constructor(Str def )
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def ); check_parse_args( def );
TokArray toks = lex( def ); TokArray toks = lex( def );
@ -80,8 +96,8 @@ CodeConstructor parse_constructor( Str def )
break; break;
default : default :
log_failure( "Invalid specifier %s for variable\n%S", spec_to_str( spec ), parser_to_strbuilder(_ctx->parser) ); 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); parser_pop(& ctx->parser);
return InvalidCode; return InvalidCode;
} }
@ -100,28 +116,38 @@ CodeConstructor parse_constructor( Str def )
// <specifiers> ... // <specifiers> ...
} }
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
CodeConstructor result = parser_parse_constructor( specifiers ); CodeConstructor result = parser_parse_constructor(ctx, specifiers);
return result; return result;
} }
CodeDefine parse_define( Str def ) CodeDefine parse_define( Str def )
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def ); check_parse_args( def );
TokArray toks = lex( def ); TokArray toks = lex( def );
if ( toks.Arr == nullptr ) if ( toks.Arr == nullptr )
return InvalidCode; return InvalidCode;
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
push_scope();
CodeDefine result = parser_parse_define(); ParseStackNode scope = NullScope;
parser_pop(& _ctx->parser); parser_push(& ctx->parser, & scope);
CodeDefine result = parser_parse_define(ctx);
parser_pop(& ctx->parser);
return result; return result;
} }
CodeDestructor parse_destructor( Str def ) CodeDestructor parse_destructor( Str def )
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def ); check_parse_args( def );
TokArray toks = lex( def ); TokArray toks = lex( def );
@ -131,210 +157,269 @@ CodeDestructor parse_destructor( Str def )
// TODO(Ed): Destructors can have prefix attributes // TODO(Ed): Destructors can have prefix attributes
// TODO(Ed): Destructors can have virtual // TODO(Ed): Destructors can have virtual
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
CodeDestructor result = parser_parse_destructor(NullCode); CodeDestructor result = parser_parse_destructor(ctx, NullCode);
return result; return result;
} }
CodeEnum parse_enum( Str def ) CodeEnum parse_enum( Str def )
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def ); check_parse_args( def );
ParseStackNode scope = NullScope;
parser_push(& ctx->parser, & scope);
TokArray toks = lex( def ); TokArray toks = lex( def );
if ( toks.Arr == nullptr ) if ( toks.Arr == nullptr )
{ {
parser_pop(& _ctx->parser); parser_pop(& ctx->parser);
return InvalidCode; return InvalidCode;
} }
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
return parser_parse_enum( parser_not_inplace_def); return parser_parse_enum(ctx, parser_not_inplace_def);
} }
CodeBody parse_export_body( Str def ) CodeBody parse_export_body( Str def )
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def ); check_parse_args( def );
TokArray toks = lex( def ); TokArray toks = lex( def );
if ( toks.Arr == nullptr ) if ( toks.Arr == nullptr )
return InvalidCode; return InvalidCode;
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
return parser_parse_export_body(); return parser_parse_export_body(ctx);
} }
CodeExtern parse_extern_link( Str def ) CodeExtern parse_extern_link( Str def )
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def ); check_parse_args( def );
TokArray toks = lex( def ); TokArray toks = lex( def );
if ( toks.Arr == nullptr ) if ( toks.Arr == nullptr )
return InvalidCode; return InvalidCode;
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
return parser_parse_extern_link(); return parser_parse_extern_link(ctx);
} }
CodeFriend parse_friend( Str def ) CodeFriend parse_friend( Str def )
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def ); check_parse_args( def );
TokArray toks = lex( def ); TokArray toks = lex( def );
if ( toks.Arr == nullptr ) if ( toks.Arr == nullptr )
return InvalidCode; return InvalidCode;
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
return parser_parse_friend(); return parser_parse_friend(ctx);
} }
CodeFn parse_function( Str def ) CodeFn parse_function( Str def )
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def ); check_parse_args( def );
TokArray toks = lex( def ); TokArray toks = lex( def );
if ( toks.Arr == nullptr ) if ( toks.Arr == nullptr )
return InvalidCode; return InvalidCode;
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
return (CodeFn) parser_parse_function(); return (CodeFn) parser_parse_function(ctx);
} }
CodeBody parse_global_body( Str def ) CodeBody parse_global_body( Str def )
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def ); check_parse_args( def );
TokArray toks = lex( def ); TokArray toks = lex( def );
if ( toks.Arr == nullptr ) if ( toks.Arr == nullptr )
return InvalidCode; return InvalidCode;
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
push_scope();
CodeBody result = parse_global_nspace(_ctx, CT_Global_Body ); ParseStackNode scope = NullScope;
parser_pop(& _ctx->parser); parser_push(& ctx->parser, & scope);
CodeBody result = parse_global_nspace(ctx, CT_Global_Body );
parser_pop(& ctx->parser);
return result; return result;
} }
CodeNS parse_namespace( Str def ) CodeNS parse_namespace( Str def )
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def ); check_parse_args( def );
TokArray toks = lex( def ); TokArray toks = lex( def );
if ( toks.Arr == nullptr ) if ( toks.Arr == nullptr )
return InvalidCode; return InvalidCode;
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
return parser_parse_namespace(); return parser_parse_namespace(ctx);
} }
CodeOperator parse_operator( Str def ) CodeOperator parse_operator( Str def )
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def ); check_parse_args( def );
TokArray toks = lex( def ); TokArray toks = lex( def );
if ( toks.Arr == nullptr ) if ( toks.Arr == nullptr )
return InvalidCode; return InvalidCode;
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
return (CodeOperator) parser_parse_operator(); return (CodeOperator) parser_parse_operator(ctx);
} }
CodeOpCast parse_operator_cast( Str def ) CodeOpCast parse_operator_cast( Str def )
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def ); check_parse_args( def );
TokArray toks = lex( def ); TokArray toks = lex( def );
if ( toks.Arr == nullptr ) if ( toks.Arr == nullptr )
return InvalidCode; return InvalidCode;
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
return parser_parse_operator_cast(NullCode); return parser_parse_operator_cast(ctx, NullCode);
} }
CodeStruct parse_struct( Str def ) CodeStruct parse_struct( Str def )
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def ); check_parse_args( def );
TokArray toks = lex( def ); TokArray toks = lex( def );
if ( toks.Arr == nullptr ) if ( toks.Arr == nullptr )
return InvalidCode; return InvalidCode;
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
push_scope();
CodeStruct result = (CodeStruct) parse_class_struct( _ctx, Tok_Decl_Struct, parser_not_inplace_def ); ParseStackNode scope = NullScope;
parser_pop(& _ctx->parser); 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; return result;
} }
CodeTemplate parse_template( Str def ) CodeTemplate parse_template( Str def )
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def ); check_parse_args( def );
TokArray toks = lex( def ); TokArray toks = lex( def );
if ( toks.Arr == nullptr ) if ( toks.Arr == nullptr )
return InvalidCode; return InvalidCode;
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
return parser_parse_template(); return parser_parse_template(ctx);
} }
CodeTypename parse_type( Str def ) CodeTypename parse_type( Str def )
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def ); check_parse_args( def );
TokArray toks = lex( def ); TokArray toks = lex( def );
if ( toks.Arr == nullptr ) if ( toks.Arr == nullptr )
return InvalidCode; return InvalidCode;
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
return parser_parse_type( parser_not_from_template, nullptr); return parser_parse_type( ctx, parser_not_from_template, nullptr);
} }
CodeTypedef parse_typedef( Str def ) CodeTypedef parse_typedef( Str def )
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def ); check_parse_args( def );
TokArray toks = lex( def ); TokArray toks = lex( def );
if ( toks.Arr == nullptr ) if ( toks.Arr == nullptr )
return InvalidCode; return InvalidCode;
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
return parser_parse_typedef(); return parser_parse_typedef(ctx);
} }
CodeUnion parse_union( Str def ) CodeUnion parse_union( Str def )
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def ); check_parse_args( def );
TokArray toks = lex( def ); TokArray toks = lex( def );
if ( toks.Arr == nullptr ) if ( toks.Arr == nullptr )
return InvalidCode; return InvalidCode;
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
return parser_parse_union( parser_not_inplace_def); return parser_parse_union(ctx, parser_not_inplace_def);
} }
CodeUsing parse_using( Str def ) CodeUsing parse_using( Str def )
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def ); check_parse_args( def );
TokArray toks = lex( def ); TokArray toks = lex( def );
if ( toks.Arr == nullptr ) if ( toks.Arr == nullptr )
return InvalidCode; return InvalidCode;
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
return parser_parse_using(); return parser_parse_using(ctx);
} }
CodeVar parse_variable( Str def ) CodeVar parse_variable( Str def )
{ {
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def ); check_parse_args( def );
TokArray toks = lex( def ); TokArray toks = lex( def );
if ( toks.Arr == nullptr ) if ( toks.Arr == nullptr )
return InvalidCode; return InvalidCode;
_ctx->parser.Tokens = toks; ctx->parser.Tokens = toks;
return parser_parse_variable(); return parser_parse_variable(ctx);
} }
// Undef helper macros // Undef helper macros

View File

@ -17,7 +17,7 @@ StrBuilder tok_to_strbuilder(Token tok)
return result; return result;
} }
bool lex__eat( TokArray* self, TokType type ); bool lex__eat(Context* ctx, TokArray* self, TokType type );
Token* lex_current(TokArray* self, bool skip_formatting ) Token* lex_current(TokArray* self, bool skip_formatting )
{ {

File diff suppressed because it is too large Load Diff