libabacus/include/lexer.h

109 lines
2.4 KiB
C
Raw Normal View History

2018-02-10 13:52:33 -08:00
#ifndef LIBABACUS_LEXER_H
#define LIBABACUS_LEXER_H
#include "eval.h"
#include "libabacus.h"
/**
* The lexer used for reading
* a string and converting it into
* tokens.
*/
struct lexer {
/**
* The liblex configuration used
* to convert the string into tokens.
*/
eval_config config;
};
2018-02-10 17:09:35 -08:00
/**
* A token that is produced by the lexer.
*/
struct lexer_match {
/**
* The line that this token was found on.
*/
size_t line;
/**
* The first index at which this token's string
* begins.
*/
size_t from;
/**
* The index of the first character that is outside
* this token.
*/
size_t to;
/**
* The index of the beginning of the line on which
* this token is found.
*/
size_t line_from;
/**
* The type of token.
*/
int type;
};
/**
* The various tokens used by the lexer
* in order to tag meaningful sequences
* of characters.
*/
enum lexer_token {
TOKEN_CHAR = 0,
TOKEN_ID,
TOKEN_TRUE,
TOKEN_FALSE,
TOKEN_NUM,
TOKEN_STR,
TOKEN_CHAR_LIT,
TOKEN_KW_FUN,
TOKEN_KW_IF,
TOKEN_KW_ELSE,
TOKEN_KW_WHILE,
TOKEN_KW_DO,
TOKEN_KW_FOR,
TOKEN_KW_RETURN,
TOKEN_OP_INFIX,
TOKEN_OP_POSTFIX,
TOKEN_OP_PREFIX,
TOKEN_LAST
};
2018-02-10 13:52:33 -08:00
typedef struct lexer lexer;
typedef enum lexer_token lexer_token;
2018-02-10 17:09:35 -08:00
typedef struct lexer_match lexer_match;
2018-02-10 13:52:33 -08:00
/**
* Initializes the given lexer,
* placing the default tokens into it.
* @param lexer the lexer to intiailize.
* @return the result of the operation (can be MALLOC on failed allocation.)
*/
libab_result lexer_init(lexer* lexer);
2018-02-10 17:09:35 -08:00
/**
* Turns the given input string into tokens.
* @param lexer the lexer to use to turn the string into tokens.
* @param string the string to turn into tokens.
* @param lex_into the list which should be populated with matches.
* @return the result of the operation.
*/
libab_result lexer_lex(lexer* lexer, const char* string, ll* lext_into);
2018-02-10 13:52:33 -08:00
/**
* Releases the memory associated with the given lexer,
* removing all registered patterns from it.
* @param lexer the lexer to free.
* @return the result of the operation.
*/
libab_result lexer_free(lexer* lexer);
2018-02-10 17:09:35 -08:00
/**
* Function intended to be passed to "foreach" calls
* in libds. lexer_lex allocates matches, and passing this function
* to foreach will free the memory allocated for the matches.
*/
int lexer_foreach_match_free(void* data, va_list args);
2018-02-10 13:52:33 -08:00
#endif