larpcc/parse.h
2026-09-17 00:27:20 +02:00

93 lines
1.6 KiB
C

#ifndef PARSE_H
#define PARSE_H
#include "collection.h"
#include <stddef.h>
// phases:
// https://en.cppreference.com/c/language/translation_phases
// phase 1 - source chars
typedef struct {
char ch;
int line;
int col;
} Char;
typedef struct {
const char* text;
size_t i;
int line;
int col;
} SourceCharIter;
void source_char_iter_init(SourceCharIter* r, const char* text);
Char source_iter_next(SourceCharIter* r);
// phase 2 - logical source lines
typedef struct {
SourceCharIter* source_char_iter;
Char curr;
} LslCharIter;
void lsl_iter_init(LslCharIter* r, SourceCharIter* source_char_iter);
Char lsl_iter_next(LslCharIter* r);
// phase 3 - preprocessor tokens
typedef enum {
Tok_Eof,
Tok_Ident,
Tok_Int,
Tok_Str,
Tok_Char,
Tok_Whitespace,
Tok_HeaderName,
Tok_Eol = '\n', // NOTE: has value 10
Tok_Semicolon = ';',
Tok_LParen = '(',
Tok_RParen = ')',
Tok_LBrace = '{',
Tok_RBrace = '}',
Tok_Hashtag = '#',
} TokTy;
typedef struct {
TokTy ty;
char* text;
int line;
int col;
} Tok;
typedef struct {
LslCharIter* lsl_char_iter;
Char curr;
bool lex_whitespace;
bool lex_headername;
} Lexer;
void lexer_init(Lexer* r, LslCharIter* lsl_char_iter);
Tok lexer_next(Lexer* l);
// phase 4 - preprocessor
typedef struct DefToks DefToks;
typedef struct {
Lexer* lexer;
Tok curr;
DefToks* deftoks;
size_t deftoks_cap;
size_t deftoks_size;
Hashmap defs;
} Preproc;
void preproc_init(Preproc* p, Lexer* lexer);
void preproc_deinit(Preproc* p);
Tok preproc_next(Preproc* p);
#endif