feat: started project and lexer

This commit is contained in:
Jef Roosens 2024-03-04 12:18:48 +01:00
commit a6c17eff5f
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
8 changed files with 344 additions and 0 deletions

19
include/mrk/common.h Normal file
View file

@ -0,0 +1,19 @@
#ifndef MRK_COMMON
#define MRK_COMMON
#include <stdlib.h>
#define MRK_CALLOC(out, n, size) \
{ \
void *temp = calloc(n, size); \
if (temp == NULL) \
return mrk_err_failed_alloc; \
*out = temp; \
}
typedef enum mrk_err {
mrk_err_ok = 0,
mrk_err_failed_alloc,
} mrk_err;
#endif

51
include/mrk/lexer.h Normal file
View file

@ -0,0 +1,51 @@
#ifndef MRK_LEXER
#define MRK_LEXER
#include <stdbool.h>
#include "mrk/common.h"
typedef struct mrk_lexer mrk_lexer;
typedef enum mrk_lexer_err {
mrk_lexer_err_ok = 0,
mrk_lexer_err_done,
mrk_lexer_err_unexpected_char,
} mrk_lexer_err;
typedef enum mrk_token_type {
mrk_token_type_pound = 0,
} mrk_token_type;
typedef struct mrk_token {
mrk_token_type type;
size_t start;
size_t end;
} mrk_token;
/**
* Initialize a new lexer struct.
*/
mrk_err mrk_lexer_init(mrk_lexer **out);
/**
* Open the buffer with the given lexer struct. `buf` is expected to live for
* the duration of the lexing.
*
* The lexer will run either until `len` characters have been matched, or until
* a nul character has been reached. If `len` is set to 0, only the nul check is
* used to determine the end of the buffer.
*/
void mrk_lexer_open(mrk_lexer *lexer, const char *buf, size_t len);
/**
* Returns whether the lexer is done.
*/
bool mrk_lexer_at_end(const mrk_lexer *lexer);
/**
* Output the next lexed token for the given input.
*/
mrk_lexer_err mrk_lexer_next(mrk_token *out, mrk_lexer *lexer);
#endif