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"
#endif
internal void parser_init();
internal void parser_deinit();
internal void parser_init(Context* ctx);
internal void parser_deinit(Context* ctx);
internal
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();
parser_init();
parser_init(ctx);
++ context_counter;
}
@ -392,7 +392,7 @@ void deinit(Context* ctx)
while ( left--, left );
array_free( ctx->Fallback_AllocatorBuckets);
}
parser_deinit();
parser_deinit(ctx);
if (_ctx == ctx)
_ctx = nullptr;

View File

@ -10,6 +10,9 @@
ParseInfo wip_parse_str(LexedInfo lexed, ParseOpts* opts)
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
TokArray toks;
if (lexed.tokens.Num == 0 && lexed.tokens.Ptr == nullptr) {
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.
_ctx->parser.Tokens = toks;
push_scope();
CodeBody result = parse_global_nspace(_ctx, CT_Global_Body);
parser_pop(& _ctx->parser);
ctx->parser.Tokens = toks;
ParseStackNode scope = NullScope;
parser_push(& ctx->parser, & scope);
CodeBody result = parse_global_nspace(ctx,CT_Global_Body);
parser_pop(& ctx->parser);
return info;
}
CodeClass parse_class( Str def )
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
_ctx->parser.Tokens = toks;
push_scope();
CodeClass result = (CodeClass) parse_class_struct( _ctx, Tok_Decl_Class, parser_not_inplace_def );
parser_pop(& _ctx->parser);
ctx->parser.Tokens = toks;
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;
}
CodeConstructor parse_constructor( Str def )
CodeConstructor parse_constructor(Str def )
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def );
TokArray toks = lex( def );
@ -80,8 +96,8 @@ 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) );
parser_pop(& _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);
return InvalidCode;
}
@ -100,28 +116,38 @@ CodeConstructor parse_constructor( Str def )
// <specifiers> ...
}
_ctx->parser.Tokens = toks;
CodeConstructor result = parser_parse_constructor( specifiers );
ctx->parser.Tokens = toks;
CodeConstructor result = parser_parse_constructor(ctx, specifiers);
return result;
}
CodeDefine parse_define( Str def )
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
_ctx->parser.Tokens = toks;
push_scope();
CodeDefine result = parser_parse_define();
parser_pop(& _ctx->parser);
ctx->parser.Tokens = toks;
ParseStackNode scope = NullScope;
parser_push(& ctx->parser, & scope);
CodeDefine result = parser_parse_define(ctx);
parser_pop(& ctx->parser);
return result;
}
CodeDestructor parse_destructor( Str def )
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( 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 virtual
_ctx->parser.Tokens = toks;
CodeDestructor result = parser_parse_destructor(NullCode);
ctx->parser.Tokens = toks;
CodeDestructor result = parser_parse_destructor(ctx, NullCode);
return result;
}
CodeEnum parse_enum( Str def )
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def );
ParseStackNode scope = NullScope;
parser_push(& ctx->parser, & scope);
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
{
parser_pop(& _ctx->parser);
parser_pop(& ctx->parser);
return InvalidCode;
}
_ctx->parser.Tokens = toks;
return parser_parse_enum( parser_not_inplace_def);
ctx->parser.Tokens = toks;
return parser_parse_enum(ctx, parser_not_inplace_def);
}
CodeBody parse_export_body( Str def )
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
_ctx->parser.Tokens = toks;
return parser_parse_export_body();
ctx->parser.Tokens = toks;
return parser_parse_export_body(ctx);
}
CodeExtern parse_extern_link( Str def )
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
_ctx->parser.Tokens = toks;
return parser_parse_extern_link();
ctx->parser.Tokens = toks;
return parser_parse_extern_link(ctx);
}
CodeFriend parse_friend( Str def )
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
_ctx->parser.Tokens = toks;
return parser_parse_friend();
ctx->parser.Tokens = toks;
return parser_parse_friend(ctx);
}
CodeFn parse_function( Str def )
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
_ctx->parser.Tokens = toks;
return (CodeFn) parser_parse_function();
ctx->parser.Tokens = toks;
return (CodeFn) parser_parse_function(ctx);
}
CodeBody parse_global_body( Str def )
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
_ctx->parser.Tokens = toks;
push_scope();
CodeBody result = parse_global_nspace(_ctx, CT_Global_Body );
parser_pop(& _ctx->parser);
ctx->parser.Tokens = toks;
ParseStackNode scope = NullScope;
parser_push(& ctx->parser, & scope);
CodeBody result = parse_global_nspace(ctx, CT_Global_Body );
parser_pop(& ctx->parser);
return result;
}
CodeNS parse_namespace( Str def )
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
_ctx->parser.Tokens = toks;
return parser_parse_namespace();
ctx->parser.Tokens = toks;
return parser_parse_namespace(ctx);
}
CodeOperator parse_operator( Str def )
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
_ctx->parser.Tokens = toks;
return (CodeOperator) parser_parse_operator();
ctx->parser.Tokens = toks;
return (CodeOperator) parser_parse_operator(ctx);
}
CodeOpCast parse_operator_cast( Str def )
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
_ctx->parser.Tokens = toks;
return parser_parse_operator_cast(NullCode);
ctx->parser.Tokens = toks;
return parser_parse_operator_cast(ctx, NullCode);
}
CodeStruct parse_struct( Str def )
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
_ctx->parser.Tokens = toks;
push_scope();
CodeStruct result = (CodeStruct) parse_class_struct( _ctx, Tok_Decl_Struct, parser_not_inplace_def );
parser_pop(& _ctx->parser);
ctx->parser.Tokens = toks;
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;
}
CodeTemplate parse_template( Str def )
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
_ctx->parser.Tokens = toks;
return parser_parse_template();
ctx->parser.Tokens = toks;
return parser_parse_template(ctx);
}
CodeTypename parse_type( Str def )
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
_ctx->parser.Tokens = toks;
return parser_parse_type( parser_not_from_template, nullptr);
ctx->parser.Tokens = toks;
return parser_parse_type( ctx, parser_not_from_template, nullptr);
}
CodeTypedef parse_typedef( Str def )
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
_ctx->parser.Tokens = toks;
return parser_parse_typedef();
ctx->parser.Tokens = toks;
return parser_parse_typedef(ctx);
}
CodeUnion parse_union( Str def )
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
_ctx->parser.Tokens = toks;
return parser_parse_union( parser_not_inplace_def);
ctx->parser.Tokens = toks;
return parser_parse_union(ctx, parser_not_inplace_def);
}
CodeUsing parse_using( Str def )
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
_ctx->parser.Tokens = toks;
return parser_parse_using();
ctx->parser.Tokens = toks;
return parser_parse_using(ctx);
}
CodeVar parse_variable( Str def )
{
// TODO(Ed): Lift this.
Context* ctx = _ctx;
check_parse_args( def );
TokArray toks = lex( def );
if ( toks.Arr == nullptr )
return InvalidCode;
_ctx->parser.Tokens = toks;
return parser_parse_variable();
ctx->parser.Tokens = toks;
return parser_parse_variable(ctx);
}
// Undef helper macros

View File

@ -17,7 +17,7 @@ StrBuilder tok_to_strbuilder(Token tok)
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 )
{

File diff suppressed because it is too large Load Diff