archeus/include/arc/std/lexer.h

121 lines
3.4 KiB
C
Raw Normal View History

#ifndef ARC_STD_LEXER_H_
#define ARC_STD_LEXER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "arc/std/bool.h"
#include "arc/std/string.h"
#include <stdint.h>
/**
* @brief
*/
typedef enum ARC_Lexer_BasicTokens {
LEXER_TOKEN_LAMBDA = 0,
LEXER_TOKEN_COLON = ':',
LEXER_TOKEN_SEMICOLON = ';',
LEXER_TOKEN_COMMA = ',',
LEXER_TOKEN_PERIOD = '.',
LEXER_TOKEN_FORWARD_SLASH = '/',
LEXER_TOKEN_BACK_SLASH = '\\',
LEXER_TOKEN_LEFT_PARENTHESIS = '(',
LEXER_TOKEN_RIGHT_PARENTHESIS = ')',
LEXER_TOKEN_LEFT_CURLY_BRACE = '{',
LEXER_TOKEN_RIGHT_CURLY_BRACE = '}',
} ARC_Lexer_BasicTokens;
/**
* @brief checks to see if a string is a type of token
*
* @param[in/out] string a string to be checked to see if it matches a token,
* this needs to srip the token out for the lexer to avoid an infinite loop
* @param[out] tokenData a place to store token data (like a variable name), can be NULL if not needed
* @param[in] automataData any data that needs to be used for the ARC_Lexer_AutomataFn
*
* @return if a token was successfully found ARC_True, otherwise ARC_False
*/
typedef ARC_Bool (* ARC_Lexer_AutomataFn)(ARC_String **string, ARC_String **tokenData, void *automataData);
/**
* @brief a lexer type
*/
typedef struct ARC_Lexer ARC_Lexer;
/**
* @brief a lexer token type
*/
typedef struct ARC_LexerToken {
uint32_t id;
ARC_Lexer_AutomataFn automataFn;
void *automataData;
//TODO: automataData free callback
} ARC_LexerToken;
/**
* @brief
*
* @param[out] lexer
*/
void ARC_Lexer_Create(ARC_Lexer **lexer);
/**
* @brief
*
* @param[in] lexer ARC_Lexer to free
*/
void ARC_Lexer_Destroy(ARC_Lexer *lexer);
/**
* @brief
*
* @param [in] lexer
* @param [in] token
*/
void ARC_Lexer_RegisterToken(ARC_Lexer *lexer, ARC_LexerToken token);
/**
* @brief
*
* @param[in] lexer
* @param[in] path
*/
void ARC_Lexer_LexFile(ARC_Lexer *lexer, ARC_String *path);
/**
* @brief checks if the first character of string matches the automataData cast as a char
*
* @note this function is a ARC_Lexer_AutomataFn callback
*
* @param[in/out] string a string to be checked to see if it matches a token,
* this needs to srip the token out for the lexer to avoid an infinite loop
* @param[out] tokenData a place to store token data (like a variable name), can be NULL if not needed
* @param[in] automataData any data that needs to be used for the ARC_Lexer_AutomataFn
*
* @return if a token was successfully found ARC_True, otherwise ARC_False
*/
ARC_Bool ARC_Lexer_AutomataMatchCharFn(ARC_String **string, ARC_String **tokenData, void *automataData);
/**
* @brief checks if the substring automataData as an ARC_String matches the first part of string
*
* @note this function is a ARC_Lexer_AutomataFn callback
*
* @param[in/out] string a string to be checked to see if it matches a token,
* this needs to srip the token out for the lexer to avoid an infinite loop
* @param[out] tokenData a place to store token data (like a variable name), can be NULL if not needed
* @param[in] automataData any data that needs to be used for the ARC_Lexer_AutomataFn
*
* @return if a token was successfully found ARC_True, otherwise ARC_False
*/
ARC_Bool ARC_Lexer_AutomataMatchStringFn(ARC_String **string, ARC_String **tokenData, void *automataData);
#ifdef __cplusplus
}
#endif
#endif // !ARC_STD_LEXER_H_