Started implementing the get_next_token function

This commit is contained in:
2023-06-20 23:37:56 +01:00
parent e4d161d79d
commit 1b56aaa185
4 changed files with 93 additions and 7 deletions

View File

@@ -78,8 +78,11 @@ typedef struct {
} lexer_string_t;
struct lexer {
u64 cursor;
u64 line;
u64 column;
const char *text;
u64 text_length;
lexer_state_t current;
state_stack_t stack;
lexer_string_t keyword;
@@ -124,6 +127,69 @@ lexer_state_t handle_keyword_end(lexer_t *lexer, char input);
// TODO (Abdelrahman): The printf functions in the state handlers are the exit
// points for the tokenisation function. Replace them once ready.
void lexer_init(lexer_t **lexer) {
if (*lexer) {
lexer_free(lexer);
}
*lexer = (lexer_t *)malloc(sizeof(lexer_t));
if (!(*lexer)) {
return;
}
(*lexer)->cursor = 0;
(*lexer)->line = 1;
(*lexer)->column = 0;
(*lexer)->current = LEXER_STATE_START;
(*lexer)->keyword.type = LEXER_STRING_KEYWORD;
(*lexer)->codepoint.type = LEXER_STRING_UNICODE;
(*lexer)->current_string = dstr_with_capacity(STRING_BUF_START_CAPACITY);
if (!((*lexer)->current_string)) {
lexer_free(lexer);
}
}
void lexer_free(lexer_t **lexer) {
if (!(*lexer)) {
return;
}
free(*lexer);
*lexer = NULL;
}
token_t get_next_token(lexer_t *lexer, const char *text) {
if (text != NULL) {
lexer->cursor = 0;
lexer->text = text;
lexer->text_length = strlen(text);
}
char c;
for (; lexer->cursor < lexer->text_length; ++(lexer->cursor)) {
c = lexer->text[lexer->cursor];
if (c == '\n') {
++(lexer->line);
lexer->column = 0;
continue;
}
lexer_state_machine(lexer, c);
// Track the position in the text
++(lexer->column);
if (lexer->current == LEXER_STATE_ERROR) {
}
}
return (token_t){0};
}
bool validate_json(char *json) {
lexer_t lexer = {0};
lexer.line = 1;

View File

@@ -4,6 +4,8 @@
#include <stdlib.h>
#include <string.h>
#define BUFFER_LENGTH 128
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("NO FILE PROVIDED\n");
@@ -28,7 +30,19 @@ int main(int argc, char *argv[]) {
fclose(fp);
printf("\n%35s: %s\n", filename, validate_json(json) ? "VALID" : "INVALID");
lexer_t *lexer = NULL;
lexer_init(&lexer);
if (!lexer) {
return EXIT_FAILURE;
}
get_next_token(lexer, json);
lexer_free(&lexer);
// printf("\n%35s: %s\n", filename, validate_json(json) ? "VALID" :
// "INVALID");
return EXIT_SUCCESS;
}