sync/include/sync/lexer.h

131 lines
2.4 KiB
C

// Kyler Olsen
// ZINC Bootstrap compiler
// Lexer Header
// June 2025
#ifndef SYNC_LEXER_H
#define SYNC_LEXER_H
#include <stddef.h>
#include "types.h"
typedef enum {
TOKEN_EOF,
TOKEN_IDENTIFIER,
TOKEN_NUMBER,
TOKEN_OPERATOR,
TOKEN_LPAREN,
TOKEN_RPAREN,
TOKEN_SEMICOLON,
TOKEN_LBRACE,
TOKEN_RBRACE,
TOKEN_LBRACKET,
TOKEN_RBRACKET,
TOKEN_CHARACTER,
TOKEN_STRING,
// Definitions and Declarations
TOKEN_KW_CONST,
TOKEN_KW_ENUM,
TOKEN_KW_FN,
TOKEN_KW_LET,
TOKEN_KW_MUT,
TOKEN_KW_PUBLIC,
TOKEN_KW_STATIC,
TOKEN_KW_STRUCT,
TOKEN_KW_UNION,
// Control Flow
TOKEN_KW_BREAK,
TOKEN_KW_CONTINUE,
TOKEN_KW_DO,
TOKEN_KW_ELSE,
TOKEN_KW_FOR,
TOKEN_KW_IF,
TOKEN_KW_RETURN,
TOKEN_KW_WHILE,
// Values
TOKEN_KW_ERROR,
TOKEN_KW_FALSE,
TOKEN_KW_NONE,
TOKEN_KW_SOME,
TOKEN_KW_TRUE,
TOKEN_KW_VALUE,
// Types
TOKEN_KW_BOOL,
TOKEN_KW_F32,
TOKEN_KW_F64,
TOKEN_KW_I8,
TOKEN_KW_I16,
TOKEN_KW_I32,
TOKEN_KW_I64,
TOKEN_KW_OPTION,
TOKEN_KW_RESULT,
TOKEN_KW_U8,
TOKEN_KW_U16,
TOKEN_KW_U32,
TOKEN_KW_U64,
TOKEN_KW_VOID,
// Modules
TOKEN_KW_AS,
TOKEN_KW_IMPORT,
// Operators
TOKEN_KW_AND,
TOKEN_KW_CAST,
TOKEN_KW_IS,
TOKEN_KW_NOT,
TOKEN_KW_OR,
TOKEN_KW_SIZEOF,
TOKEN_KW_XOR,
} TokenType;
typedef struct {
TokenType type;
const char* start;
size_t length;
FileInfo file_info;
} Token;
typedef struct {
const char* filename;
const char* source;
size_t pos;
size_t column;
size_t line;
} Lexer;
typedef struct TokenResult {
SyncResultType type;
union {
Token result;
SyncError error;
};
struct TokenResult* next;
} TokenResult;
typedef struct {
SyncResultType type;
union {
TokenResult* result;
GeneralError error;
};
} LexerResult;
typedef struct {
size_t length;
Token* tokens;
} TokenArray;
typedef struct {
SyncResultType type;
union {
TokenArray result;
GeneralError error;
};
} TokenArrayResult;
void lexer_init(Lexer* lexer, const char* filename, const char* source);
LexerResult lexical_analysis(Lexer* lexer);
TokenArrayResult token_result_array(TokenResult* result);
void clean_token_result(TokenResult* head);
#endif // SYNC_LEXER_H