diff --git a/include/lexer/lexer.h b/include/lexer/lexer.h new file mode 100644 index 0000000..3f680d6 --- /dev/null +++ b/include/lexer/lexer.h @@ -0,0 +1,40 @@ +#ifndef LEXER_STATES_H +#define LEXER_STATES_H + +#include "aliases.h" +#include + +#define VALID_JSON true +#define INVALID_JSON false + +typedef enum { + TK_L_BRACE, + TK_R_BRACE, + TK_L_BRACKET, + TK_R_BRACKET, + TK_COLON, + TK_COMMA, + TK_NULL, + TK_TRUE, + TK_FALSE, + TK_STRING_LITERAL, + TK_INTEGER_LITERAL, + TK_DOUBLE_LITERAL, +} token_type_t; + +typedef union { + void *no_val; + i64 num_int; + f64 num_frac; +} token_value_t; + +typedef struct { + token_type_t type; + token_value_t value; +} token_t; + +typedef struct lexer lexer_t; + +bool validate_json(char *json); + +#endif // !LEXER_STATES_H diff --git a/include/lexer/lexer_states.h b/include/lexer/lexer_states.h deleted file mode 100644 index bc2bc00..0000000 --- a/include/lexer/lexer_states.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef LEXER_STATES_H -#define LEXER_STATES_H - -#include - -#define VALID_JSON true -#define INVALID_JSON false - -typedef struct lexer lexer_t; - -bool validate_json(char *json); - -#endif // !LEXER_STATES_H diff --git a/src/lexer/lexer_states.c b/src/lexer/lexer.c similarity index 97% rename from src/lexer/lexer_states.c rename to src/lexer/lexer.c index c04754c..8791e79 100644 --- a/src/lexer/lexer_states.c +++ b/src/lexer/lexer.c @@ -1,5 +1,6 @@ -#include "lexer_states.h" +#include "lexer.h" #include "aliases.h" +#include "dstring.h" #include #include #include @@ -10,6 +11,7 @@ #define MAX_KEYWORD_LENGTH 5 #define UNICODE_LENGTH 4 #define MAX_STACK_CAPACITY 1024 +#define STRING_BUF_START_CAPACITY 1024 typedef enum { // GENERAL STATES @@ -76,12 +78,13 @@ typedef struct { } lexer_string_t; struct lexer { - lexer_state_t current; - state_stack_t stack; u64 line; u64 column; + lexer_state_t current; + state_stack_t stack; lexer_string_t keyword; lexer_string_t codepoint; + dstr_t *current_string; }; void stack_push(state_stack_t *stack, lexer_state_t value); @@ -123,6 +126,13 @@ bool validate_json(char *json) { 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) { + // TODO (Abdelrahman): This is fine for now, but it doesn't make sense to + // return INVALID_JSON if string allocation fails + return INVALID_JSON; + } for (char *c = json; *c != '\0'; ++c) { // printf("\nINPUT=>%s\n", c);