249 lines
8 KiB
C
249 lines
8 KiB
C
#ifndef ARC_STD_LEXER_H_
|
|
#define ARC_STD_LEXER_H_
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
#include "arc/std/string.h"
|
|
#include <stdint.h>
|
|
|
|
/**
|
|
* @brief a lexer type
|
|
*/
|
|
typedef struct ARC_Lexer ARC_Lexer;
|
|
|
|
/**
|
|
* @brief a lexer token type
|
|
*/
|
|
typedef struct ARC_LexerToken {
|
|
uint32_t rule;
|
|
ARC_String *data;
|
|
} ARC_LexerToken;
|
|
|
|
/**
|
|
* @brief checks to see if a string is a type of token
|
|
*
|
|
* @note do not set tokenData if this function returns 0, doing so will create a memory leak
|
|
*
|
|
* @param[out] tokenData a place to store token data (like a variable name), can be NULL if not needed
|
|
* @param[in] string a string to be checked to see if it matches a token
|
|
* @param[in] automataData any data that needs to be used for the ARC_Lexer_AutomataFn
|
|
*
|
|
* @return the size of the token found, or 0 if the token was not found
|
|
*/
|
|
typedef uint32_t (* ARC_LexerTokenRule_AutomataFn)(ARC_String **tokenData, ARC_String *string, void *automataData);
|
|
|
|
/**
|
|
* @brief a callback function to clean up ARC_LexerTokenRule's automataData
|
|
*
|
|
* @param automataData the void * automataData to destroy
|
|
*/
|
|
typedef void (* ARC_LexerTokenRule_DestroyAutomataDataFn)(void *automataData);
|
|
|
|
/**
|
|
* @brief a lexer token rule type
|
|
*/
|
|
typedef struct ARC_LexerTokenRule {
|
|
uint32_t id;
|
|
|
|
void *automataData;
|
|
|
|
ARC_LexerTokenRule_AutomataFn automataFn;
|
|
ARC_LexerTokenRule_DestroyAutomataDataFn destroyAutomataDataFn;
|
|
} ARC_LexerTokenRule;
|
|
|
|
/**
|
|
* @brief creates an ARC_Lexer type
|
|
*
|
|
* @param[out] lexer ARC_Lexer to create
|
|
*/
|
|
void ARC_Lexer_Create(ARC_Lexer **lexer);
|
|
|
|
/**
|
|
* @brief destroys an ARC_Lexer type
|
|
*
|
|
* @param[in] lexer ARC_Lexer to free
|
|
*/
|
|
void ARC_Lexer_Destroy(ARC_Lexer *lexer);
|
|
|
|
/**
|
|
* @brief adds a token rule to a lexer
|
|
*
|
|
* @param[in] lexer the lexer to add a token rule to
|
|
* @param[in] tokenRule the token rule to add
|
|
*/
|
|
void ARC_Lexer_RegisterTokenRule(ARC_Lexer *lexer, ARC_LexerTokenRule tokenRule);
|
|
|
|
/**
|
|
* @brief clears all tokens from a lexer (will not clear token rules)
|
|
*
|
|
* @param lexer the lexer to clear tokens from
|
|
*/
|
|
void ARC_Lexer_Clear(ARC_Lexer *lexer);
|
|
|
|
/**
|
|
* @brief creates tokens using a given string with ARC_LexerToken rules
|
|
*
|
|
* @param[in] lexer the lexer to get the ARC_LexerTokens from
|
|
* @param[in/out] data the string to lex, will be freed and set to NULL by the end of this function
|
|
*/
|
|
void ARC_Lexer_LexString(ARC_Lexer *lexer, ARC_String **data);
|
|
|
|
/**
|
|
* @brief reads in and lexs a file
|
|
*
|
|
* @note this function will call ARC_Lexer_LexString, so it's notes are applicable to this function
|
|
*
|
|
* @param[in] lexer the lexer which holds to rules to use
|
|
* @param[in] path path of file to read in and lex
|
|
*/
|
|
void ARC_Lexer_LexFile(ARC_Lexer *lexer, ARC_String *path);
|
|
|
|
/**
|
|
* @brief prints rule id and hex of the function name
|
|
*
|
|
* @note this is mostly used for debugging
|
|
*
|
|
* @param[in] lexer the lexer to print rules from
|
|
*/
|
|
void ARC_Lexer_PrintTokenRules(ARC_Lexer *lexer);
|
|
|
|
/**
|
|
* @brief gets a token at a given index from a lexer
|
|
*
|
|
* @note unless you have a very good reason, you probably don't want to mess with the tokens string.
|
|
* that will probably change the token's string inside the lexer
|
|
*
|
|
* @param[in] lexer the lexer to get the token from
|
|
* @param[in] index the index of the token in the lexer to get
|
|
*
|
|
* @return a token at the lexer index on success, otherwise NULL
|
|
*/
|
|
ARC_LexerToken *ARC_Lexer_GetToken(ARC_Lexer *lexer, uint32_t index);
|
|
|
|
/**
|
|
* @brief gets a token at a given index from a lexer
|
|
*
|
|
* @param[in] lexer the lexer to get the tokens size from
|
|
*
|
|
* @return the size of the token array in a lexer
|
|
*/
|
|
uint32_t ARC_Lexer_GetTokensSize(ARC_Lexer *lexer);
|
|
|
|
/**
|
|
* @brief returns a boolean based on if a lexers rules are continious
|
|
*
|
|
* @param[in] lexer the lexer to check if its ruls are continious
|
|
*
|
|
* @return ARC_True if the set rules are continious
|
|
*/
|
|
ARC_Bool ARC_Lexer_IsContinious(ARC_Lexer *lexer);
|
|
|
|
/**
|
|
* @brief returns a boolean based on if a given id is a stored token rule id
|
|
*
|
|
* @param[in] lexer the lexer to check stored token rule ids
|
|
* @param[in] id the id to check against the token rules
|
|
*
|
|
* @return ARC_True if the id is a rule id, ARC_False otherwise
|
|
*/
|
|
ARC_Bool ARC_Lexer_IsTokenId(ARC_Lexer *lexer, uint32_t id);
|
|
|
|
/**
|
|
* @brief checks if the first character of string matches the automataData cast as a char
|
|
*
|
|
* @note this is intended as a helper callback
|
|
* @note this function is a ARC_Lexer_AutomataFn callback
|
|
*
|
|
* @param[out] tokenData a place to store token data (like a variable name), can be NULL if not needed
|
|
* @param[in] string a string to be checked to see if it matches a token
|
|
* @param[in] automataData any data that needs to be used for the ARC_Lexer_AutomataFn
|
|
*
|
|
* @return the size of the token found, or 0 if the token was not found
|
|
*/
|
|
uint32_t ARC_Lexer_AutomataMatchCharFn(ARC_String **tokenData, ARC_String *string, void *automataData);
|
|
|
|
/**
|
|
* @brief checks if the substring automataData as an ARC_String matches the first part of string
|
|
*
|
|
* @note this is intended as a helper callback
|
|
* @note this function is a ARC_Lexer_AutomataFn callback
|
|
*
|
|
* @param[out] tokenData a place to store token data (like a variable name), can be NULL if not needed
|
|
* @param[in] string a string to be checked to see if it matches a token
|
|
* @param[in] automataData any data that needs to be used for the ARC_Lexer_AutomataFn
|
|
*
|
|
* @return the size of the token found, or 0 if the token was not found
|
|
*/
|
|
uint32_t ARC_Lexer_AutomataMatchStringFn(ARC_String **tokenData, ARC_String *string, void *automataData);
|
|
|
|
/**
|
|
* @brief checks if the first part of string is a character in substring
|
|
*
|
|
* @note this is intended as a helper callback
|
|
* @note this function is a ARC_Lexer_AutomataFn callback
|
|
*
|
|
* @param[out] tokenData a place to store token data (like a variable name), can be NULL if not needed
|
|
* @param[in] string a string to be checked to see if it matches a token
|
|
* @param[in] automataData any data that needs to be used for the ARC_Lexer_AutomataFn
|
|
*
|
|
* @return the size of the token found, or 0 if the token was not found
|
|
*/
|
|
uint32_t ARC_Lexer_AutomataMatchCharInStringFn(ARC_String **tokenData, ARC_String *string, void *automataData);
|
|
|
|
/**
|
|
* @brief creates a ARC_LexerTokenRule with a given id and character
|
|
*
|
|
* @note this is intended as a helper funtion
|
|
*
|
|
* @param[in] id a tokens id (basically the token value)
|
|
* @param[in] character the character to match against
|
|
*
|
|
* @return a token rule based in the id and character
|
|
*/
|
|
ARC_LexerTokenRule ARC_LexerTokenRule_CreateAndReturnMatchCharRule(uint32_t id, char character);
|
|
|
|
/**
|
|
* @brief creates a ARC_LexerTokenRule with a given id and character range
|
|
*
|
|
* @note this is intended as a helper funtion
|
|
*
|
|
* @param[in] id a tokens id (basically the token value)
|
|
* @param[in] start the minimum character value to match against
|
|
* @param[in] end the maxamum character value to match against
|
|
*
|
|
* @return a token rule based in the id and character
|
|
*/
|
|
ARC_LexerTokenRule ARC_LexerTokenRule_CreateAndReturnMatchCharOrBetween(uint32_t id, char start, char end);
|
|
|
|
/**
|
|
* @brief creates a ARC_LexerTokenRule with a given id and string
|
|
*
|
|
* @note this is intended as a helper funtion
|
|
* #note string will not be freed (it will be copied and the copy will be freed)
|
|
*
|
|
* @param[in] id a tokens id (basically the token value)
|
|
* @param[in] character the string to match against, will be copied
|
|
*
|
|
* @return a token rule based in the id and string
|
|
*/
|
|
ARC_LexerTokenRule ARC_LexerTokenRule_CreateAndReturnMatchStringRule(uint32_t id, ARC_String *string);
|
|
|
|
/**
|
|
* @brief creates a ARC_LexerTokenRule with a given id and string
|
|
*
|
|
* @note this is intended as a helper funtion
|
|
* #note string will not be freed (it will be copied and the copy will be freed)
|
|
*
|
|
* @param[in] id a tokens id (basically the token value)
|
|
* @param[in] character the string to match against, will be copied
|
|
*
|
|
* @return a token rule based in the id and string
|
|
*/
|
|
ARC_LexerTokenRule ARC_LexerTokenRule_CreateAndReturnMatchCharInStringRule(uint32_t id, ARC_String *string);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif // !ARC_STD_LEXER_H_
|