cli-env-project/lexer.h
2023-02-06 15:48:02 +01:00

67 lines
1.5 KiB
C

#ifndef LEXER_H
#define LEXER_H
#include <stdbool.h>
#include <stdlib.h>
typedef enum {
TokenTypeInvalid,
TokenTypeEof,
TokenTypePlus,
TokenTypeLParen,
TokenTypeRParen,
TokenTypeInt,
TokenTypeId,
} TokenType;
const char* token_type_to_string(TokenType type);
typedef struct {
TokenType type;
size_t index, length;
int line, column;
} Token;
void token_to_string(char* out, size_t out_size, Token token);
typedef struct {
size_t index;
int line, column;
} LexerPosition;
typedef struct {
const char* text;
size_t length;
LexerPosition position;
} Lexer;
void lexer_construct(Lexer* self, const char* text, size_t length);
Token lexer_next(Lexer* self);
bool lexer_done(const Lexer* self);
void lexer_step(Lexer* self);
char lexer_current(const Lexer* self);
LexerPosition lexer_position(const Lexer* self);
Token lexer_token_from(const Lexer* self, TokenType type, LexerPosition start);
void lexer_skip_whitespace(Lexer* self);
Token lexer_make_int_token(Lexer* self);
Token lexer_make_id_token(Lexer* self);
Token lexer_make_single_char_token(Lexer* self, TokenType type);
Token lexer_make_static_token(Lexer* self);
Token lexer_make_token(Lexer* self);
typedef struct {
Lexer* lexer;
Token current;
} TokenIterator;
void token_iterator(TokenIterator* self, Lexer* lexer);
Token token_step(TokenIterator* self);
Token token_current(TokenIterator* self);
bool is_whitespace_char(char c);
bool is_int_char(char c);
bool is_id_char(char c);
#endif