Files
c2/v0/token.h
T

87 lines
1.7 KiB
C

/**
* Contains the interface for reading tokens from a file.
*/
#ifndef TOKEN_H
#define TOKEN_H
#include <stddef.h>
/**
* A list of all possible tokens.
*/
typedef enum {
// Keywords
TOKEN_MODULE,
TOKEN_IMPORT,
TOKEN_SEMICOLON,
// Symbols
TOKEN_PARENT_OPEN,
TOKEN_PARENT_CLOSE,
TOKEN_BRACKET_OPEN,
TOKEN_BRACKET_CLOSE,
TOKEN_COMMA,
// Primitives
TOKEN_VOID,
// Variable
TOKEN_IDENTIFIER,
// Others
TOKEN_EOF,
TOKEN_UNKNOWN,
} TokenType;
/**
* Holds additional information about a token.
*/
typedef struct {
/// @brief The textual representation of a token.
/// Note that this is not necessarily null-terminated.
char* text;
/// @brief The entire line of text where the token was found.
char* line_text;
/// @brief The length of the `text` string.
size_t text_length;
/// @brief The length of the `line_text` string.
size_t line_text_length;
/// @brief The actual token.
TokenType token;
/// @brief The line number where the token was found.
int line;
/// @brief The column number where the token was found.
int column;
} Token;
typedef struct TokenStream TokenStream;
/**
* Returns a TokenStream for a text.
*
* @param filename The name of the file to read. This is only used for error reporting.
* @param code The text to read.
* @returns A handle to the TokenStream.
*/
TokenStream* tokenstream_open(const char* filename, const char* code);
/**
* Closes a TokenStream.
* @param ts The TokenStream to close.
*/
void tokenstream_close(TokenStream* ts);
/**
* Gets the next token from the TokenStream.
* @param ts The TokenStream to read from.
* @returns The next token read.
*/
Token tokenstream_next(TokenStream* ts);
#endif