Started implementing the get_next_token function
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user