2024-08-28 20:04:18 -06:00
|
|
|
#include "arc/std/lexer.h"
|
|
|
|
|
|
|
|
|
|
#include "arc/std/bool.h"
|
|
|
|
|
#include "arc/std/string.h"
|
|
|
|
|
#include "arc/std/vector.h"
|
|
|
|
|
#include "arc/std/io.h"
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
struct ARC_Lexer {
|
|
|
|
|
ARC_Vector *tokens;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void ARC_Lexer_Create(ARC_Lexer **lexer){
|
|
|
|
|
*lexer = (ARC_Lexer *)malloc(sizeof(ARC_Lexer));
|
|
|
|
|
|
|
|
|
|
//TODO: add compare and delete callbacks
|
|
|
|
|
ARC_Vector_Create(&(*lexer)->tokens, NULL, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ARC_Lexer_Destroy(ARC_Lexer *lexer){
|
|
|
|
|
ARC_Vector_Destroy(lexer->tokens);
|
|
|
|
|
|
|
|
|
|
free(lexer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ARC_Lexer_RegisterToken(ARC_Lexer *lexer, ARC_LexerToken token){
|
|
|
|
|
ARC_LexerToken *storedToken = (ARC_LexerToken *)malloc(sizeof(ARC_LexerToken));
|
|
|
|
|
*storedToken = token;
|
|
|
|
|
ARC_Vector_Add(lexer->tokens, storedToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ARC_Lexer_LexFile(ARC_Lexer *lexer, ARC_String *path){
|
|
|
|
|
ARC_String *data;
|
|
|
|
|
ARC_IO_FileToStr(path, &data);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(data != NULL){
|
|
|
|
|
ARC_String_Destroy(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ARC_Bool ARC_Lexer_AutomataMatchCharFn(ARC_String **string, ARC_String **tokenData, void *automataData){
|
|
|
|
|
*tokenData = NULL;
|
|
|
|
|
|
|
|
|
|
if((*string)->data[0] == *(char *)automataData){
|
|
|
|
|
if((*string)->length == 1){
|
|
|
|
|
ARC_String_Destroy(*string);
|
|
|
|
|
*string = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ARC_String_ReplaceWithSubstring(string, 1, (*string)->length - 1);
|
|
|
|
|
return ARC_True;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ARC_False;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ARC_Bool ARC_Lexer_AutomataMatchStringFn(ARC_String **string, ARC_String **tokenData, void *automataData){
|
|
|
|
|
*tokenData = NULL;
|
|
|
|
|
|
|
|
|
|
ARC_String *automataDataString = (ARC_String *)automataData;
|
|
|
|
|
if(ARC_String_Equals(*string, automataDataString)){
|
|
|
|
|
if((*string)->length == automataDataString->length){
|
|
|
|
|
ARC_String_Destroy(*string);
|
|
|
|
|
*string = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ARC_String_ReplaceWithSubstring(string, automataDataString->length, (*string)->length - automataDataString->length);
|
|
|
|
|
return ARC_True;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ARC_False;
|
|
|
|
|
}
|