Files
zig/parser.h
Motiejus Jakštys 5bd533d40c 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>
2026-02-11 14:40:33 +00:00

45 lines
921 B
C

// parser.h
#ifndef _ZIG0_PARSE_H__
#define _ZIG0_PARSE_H__
#include "ast.h"
#include "common.h"
#include <setjmp.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
typedef struct {
const char* source;
uint32_t source_len;
TokenizerTag* token_tags;
AstIndex* token_starts;
uint32_t tokens_len;
AstTokenIndex tok_i;
AstNodeList nodes;
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);
#endif