parser: replace fprintf+longjmp with fail(), add forward declarations

Introduce a fail(p, "msg") inline function that stores the error message
in a buffer and longjmps, replacing ~52 fprintf(stderr,...)+longjmp pairs.
The error message is propagated through Ast.err_msg so callers can decide
whether/how to display it. Also add forward declarations for all static
functions and move PtrModifiers typedef to the type definitions section.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-11 13:20:10 +00:00
parent cd07751d13
commit 5bd533d40c
4 changed files with 164 additions and 154 deletions

View File

@@ -7,6 +7,7 @@
#include <setjmp.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
typedef struct {
const char* source;
@@ -22,8 +23,20 @@ typedef struct {
AstNodeIndexSlice extra_data;
AstNodeIndexSlice scratch;
jmp_buf error_jmp;
char* err_buf;
} Parser;
#define PARSE_ERR_BUF_SIZE 200
_Noreturn static inline void fail(Parser* p, const char* msg) {
size_t len = strlen(msg);
if (len >= PARSE_ERR_BUF_SIZE)
len = PARSE_ERR_BUF_SIZE - 1;
memcpy(p->err_buf, msg, len);
p->err_buf[len] = '\0';
longjmp(p->error_jmp, 1);
}
Parser* parserInit(const char* source, uint32_t len);
void parserDeinit(Parser* parser);
void parseRoot(Parser* parser);