Doing some initial prep for parser overhaul

This commit is contained in:
Edward R. Gonzalez 2025-02-19 10:50:55 -05:00
parent 844d431e1c
commit 3b81eea688
6 changed files with 70 additions and 36 deletions

View File

@ -407,6 +407,7 @@ struct AST
};
};
StrCached Content; // Attributes, Comment, Execution, Include
// TokenSlice Content; // TODO(Ed): Use a token slice for content
struct {
Specifier ArrSpecs[AST_ArrSpecs_Cap]; // Specifiers
Code NextSpecs; // Specifiers; If ArrSpecs is full, then NextSpecs is used.
@ -422,7 +423,7 @@ struct AST
Code Next;
Code Back;
};
Token* Token; // Reference to starting token, only available if it was derived from parsing.
Token* Token; // Reference to starting token, only available if it was derived from parsing. // TODO(Ed): Change this to a token slice.
Code Parent;
CodeType Type;
// CodeFlag CodeFlags;

View File

@ -391,37 +391,41 @@ forceinline CodeBody def_union_body ( s32 num, Code* codes )
#pragma region Parsing
#if 0
struct StackNode
struct ParseStackNode
{
StackNode* Prev;
ParseStackNode* Prev;
Token Start;
Token Name; // The name of the AST node (if parsed)
Str FailedProc; // The name of the procedure that failed
TokenSlice tokens;
Token* Start;
Str Name; // The name of the AST node (if parsed)
Str ProcName; // The name of the procedure
Code code; // Relevant AST node
// TODO(Ed): When an error occurs, the parse stack is not released and instead the scope is left dangling.
};
// Stack nodes are allocated the error's allocator
struct Error
struct ParseMessage
{
StrBuilder message;
StackNode* context_stack;
ParseMessage* Next;
ParseStackNode* Scope;
Str Log;
LogLevel Level;
};
struct ParseInfo
{
Arena FileMem;
Arena TokMem;
Arena CodeMem;
FileContents FileContent;
Array<Token> Tokens;
Array<Error> Errors;
// Errors are allocated to a dedicated general arena.
ParseMessage* messages;
LexedInfo lexed;
Code result;
};
CodeBody parse_file( Str path );
#endif
struct Opts_parse
{
AllocatorInfo backing_msgs;
AllocatorInfo backing_tokens;
AllocatorInfo backing_ast;
};
ParseInfo wip_parse_str( LexedInfo lexed, Opts_parse opts GEN_PARAM_DEFAULT );
GEN_API CodeClass parse_class ( Str class_def );
GEN_API CodeConstructor parse_constructor ( Str constructor_def );

View File

@ -8,6 +8,29 @@
// Publically Exposed Interface
ParseInfo wip_parse_str(LexedInfo lexed, Opts_parse opts)
{
TokArray toks;
if (lexed.tokens.Num == 0 && lexed.tokens.Ptr == nullptr) {
check_parse_args(lexed.text);
toks = lex(lexed.text);
TokenSlice slice = { toks.Arr, scast(s32, array_num(toks.Arr)) };
lexed.tokens = slice;
}
ParseInfo info = struct_zero(ParseInfo);
info.lexed = lexed;
// TODO(Ed): ParseInfo should be set to the parser context.
_ctx->parser.Tokens = toks;
push_scope();
CodeBody result = parse_global_nspace(CT_Global_Body);
parser_pop(& _ctx->parser);
return info;
}
CodeClass parse_class( Str def )
{
check_parse_args( def );

View File

@ -11,7 +11,7 @@
constexpr bool lex_dont_skip_formatting = false;
constexpr bool lex_skip_formatting = true;
void parser_push( ParseContext* ctx, StackNode* node )
void parser_push( ParseContext* ctx, ParseStackNode* node )
{
node->Prev = ctx->Scope;
ctx->Scope = node;
@ -60,7 +60,7 @@ StrBuilder parser_to_strbuilder(ParseContext ctx)
else
strbuilder_append_fmt(& result, "\t(%d, %d)\n", last_valid.Line, last_valid.Column );
StackNode* curr_scope = ctx.Scope;
ParseStackNode* curr_scope = ctx.Scope;
s32 level = 0;
do
{
@ -181,9 +181,10 @@ bool _check_parse_args( Str def, char const* func_name )
# define check_noskip( Type_ ) ( left && currtok_noskip.Type == Type_ )
# define check( Type_ ) ( left && currtok.Type == Type_ )
// TODO(Ed): Don't do this anymore, we need a better initializer.
# define push_scope() \
Str null_name = {}; \
StackNode scope = { nullptr, lex_current( & _ctx->parser.Tokens, lex_dont_skip_formatting ), null_name, txt( __func__ ) }; \
ParseStackNode scope = { nullptr, {nullptr, 0}, lex_current( & _ctx->parser.Tokens, lex_dont_skip_formatting ), null_name, txt( __func__ ), { nullptr} }; \
parser_push( & _ctx->parser, & scope )
#pragma endregion Helper Macros

View File

@ -97,6 +97,12 @@ struct TokArray
s32 Idx;
};
struct TokenSlice
{
Token* Ptr;
s32 Num;
};
struct LexContext
{
Str content;
@ -108,19 +114,18 @@ struct LexContext
Token token;
};
struct StackNode
struct LexedInfo
{
StackNode* Prev;
Token* Start;
Str Name; // The name of the AST node (if parsed)
Str ProcName; // The name of the procedure
Str text;
TokenSlice tokens;
};
typedef struct ParseStackNode ParseStackNode;
struct ParseContext
{
TokArray Tokens;
StackNode* Scope;
ParseStackNode* Scope;
};
enum MacroType : u16

View File

@ -535,7 +535,7 @@ namespace Lexer_, gen_Lexer_
word LexContext, gen_LexContext
word lex, gen_lex
word StackNode, gen_StackNode
word ParseStackNode, gen_ParseStackNode
word ParseContext, gen_ParseContext
// namespace parse_, gen_parse_