Plan work for get_next_token function

This commit is contained in:
2023-06-20 21:56:59 +01:00
parent 0d289df26f
commit e4d161d79d
2 changed files with 238 additions and 9 deletions

View File

@@ -121,6 +121,9 @@ lexer_state_t handle_false(lexer_t *lexer, char input);
lexer_state_t handle_null(lexer_t *lexer, char input);
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.
bool validate_json(char *json) {
lexer_t lexer = {0};
lexer.line = 1;
@@ -347,8 +350,10 @@ lexer_state_t handle_lexer_start(char input) {
switch (input) {
case '{':
printf("TK_L_BRACE\n");
return LEXER_STATE_OBJECT_START;
case '[':
printf("TK_L_BRACKET\n");
return LEXER_STATE_ARRAY_START;
}
@@ -371,12 +376,14 @@ lexer_state_t handle_collection_end(lexer_t *lexer, char input) {
bool object_end = lexer->current == LEXER_STATE_OBJECT && input == '}';
if (object_end) {
printf("TK_R_BRACE\n");
return LEXER_STATE_OBJECT_END;
}
bool array_end = lexer->current == LEXER_STATE_ARRAY && input == ']';
if (array_end) {
printf("TK_R_BRACKET\n");
return LEXER_STATE_ARRAY_END;
}
@@ -391,6 +398,7 @@ lexer_state_t handle_object(lexer_t *lexer, char input) {
return LEXER_STATE_KEY;
} else if (input == '}') {
printf("TK_R_BRACE\n");
return handle_collection_end(lexer, input);
}
@@ -401,6 +409,7 @@ lexer_state_t handle_array(lexer_t *lexer, char input) {
if (isspace(input)) {
return LEXER_STATE_ARRAY;
} else if (input == ']') {
printf("TK_R_BRACKET\n");
return handle_collection_end(lexer, input);
}
@@ -432,8 +441,10 @@ lexer_state_t handle_value(lexer_t *lexer, char input) {
return LEXER_STATE_DECIMAL;
case '{':
printf("TK_L_BRACE\n");
return LEXER_STATE_OBJECT_START;
case '[':
printf("TK_L_BRACKET\n");
return LEXER_STATE_ARRAY_START;
case 't':
case 'f':
@@ -453,6 +464,7 @@ lexer_state_t handle_string(lexer_t *lexer, char input) {
return LEXER_STATE_ESCAPE_SEQUENCE;
case '"':
printf("TK_STRING: %s\n", dstr_to_cstr(lexer->current_string));
return LEXER_STATE_STRING_END;
}
@@ -473,12 +485,14 @@ lexer_state_t handle_string_end(lexer_t *lexer, char input) {
bool key_end = lexer->current == LEXER_STATE_KEY && input == ':';
if (key_end) {
printf("TK_COLON\n");
return LEXER_STATE_VALUE;
}
bool value_end = lexer->current == LEXER_STATE_VALUE && input == ',';
if (value_end) {
printf("TK_COMMA\n");
return lexer->stack.stack[lexer->stack.size - 1];
}
@@ -545,10 +559,12 @@ lexer_state_t handle_number(lexer_t *lexer, char input) {
return LEXER_STATE_FRACTION;
} else if (input == '}' || input == ']') {
printf("TK_NUMBER: %s\n", dstr_to_cstr(lexer->current_string));
dstr_clear(lexer->current_string);
return handle_collection_end(lexer, input);
} else if (input == ',') {
printf("TK_NUMBER: %s\n", dstr_to_cstr(lexer->current_string));
dstr_clear(lexer->current_string);
return lexer->stack.stack[lexer->stack.size - 1];
@@ -565,6 +581,7 @@ lexer_state_t handle_fraction(lexer_t *lexer, char input) {
return LEXER_STATE_FRACTION;
} else if (input == '}' || input == ']') {
printf("TK_NUMBER: %s\n", dstr_to_cstr(lexer->current_string));
dstr_clear(lexer->current_string);
return handle_collection_end(lexer, input);
@@ -573,6 +590,7 @@ lexer_state_t handle_fraction(lexer_t *lexer, char input) {
return LEXER_STATE_EXPONENT;
} else if (input == ',') {
printf("TK_NUMBER: %s\n", dstr_to_cstr(lexer->current_string));
dstr_clear(lexer->current_string);
return lexer->stack.stack[lexer->stack.size - 1];
@@ -611,10 +629,12 @@ lexer_state_t handle_power(lexer_t *lexer, char input) {
return LEXER_STATE_POWER;
} else if (input == '}' || input == ']') {
printf("TK_NUMBER: %s\n", dstr_to_cstr(lexer->current_string));
dstr_clear(lexer->current_string);
return handle_collection_end(lexer, input);
} else if (input == ',') {
printf("TK_NUMBER: %s\n", dstr_to_cstr(lexer->current_string));
dstr_clear(lexer->current_string);
return lexer->stack.stack[lexer->stack.size - 1];
@@ -626,6 +646,7 @@ lexer_state_t handle_power(lexer_t *lexer, char input) {
}
lexer_state_t handle_number_end(lexer_t *lexer, char input) {
printf("TK_NUMBER: %s\n", dstr_to_cstr(lexer->current_string));
dstr_clear(lexer->current_string);
if (isspace(input)) {
@@ -715,6 +736,7 @@ lexer_state_t handle_null(lexer_t *lexer, char input) {
}
lexer_state_t handle_keyword_end(lexer_t *lexer, char input) {
printf("TK_KEYWORD: %s\n", lexer->keyword.keyword.str);
clear_lex_str(&(lexer->keyword));
if (isspace(input)) {