2024-08-27 03:23:29 -06:00
|
|
|
#ifndef ARC_STD_LEXER_H_
|
|
|
|
|
#define ARC_STD_LEXER_H_
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-08-28 20:04:18 -06:00
|
|
|
#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);
|
|
|
|
|
|
2024-08-27 03:23:29 -06:00
|
|
|
/**
|
|
|
|
|
* @brief a lexer type
|
|
|
|
|
*/
|
|
|
|
|
typedef struct ARC_Lexer ARC_Lexer;
|
|
|
|
|
|
2024-08-28 20:04:18 -06:00
|
|
|
/**
|
|
|
|
|
* @brief a lexer token type
|
|
|
|
|
*/
|
|
|
|
|
typedef struct ARC_LexerToken {
|
|
|
|
|
uint32_t id;
|
|
|
|
|
|
|
|
|
|
ARC_Lexer_AutomataFn automataFn;
|
|
|
|
|
void *automataData;
|
|
|
|
|
//TODO: automataData free callback
|
|
|
|
|
} ARC_LexerToken;
|
|
|
|
|
|
2024-08-27 03:23:29 -06:00
|
|
|
/**
|
|
|
|
|
* @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);
|
|
|
|
|
|
2024-08-28 20:04:18 -06:00
|
|
|
/**
|
|
|
|
|
* @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);
|
|
|
|
|
|
|
|
|
|
|
2024-08-27 03:23:29 -06:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif // !ARC_STD_LEXER_H_
|