From f48f7c088cc1538411a50fbc15a3039bea54f943 Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Tue, 2 Aug 2022 16:58:15 -0400 Subject: [PATCH] Begin prototyping JsonDecode --- src/Json.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/src/Json.c b/src/Json.c index 8823de7..080056b 100644 --- a/src/Json.c +++ b/src/Json.c @@ -29,6 +29,7 @@ #include #include #include +#include struct JsonValue { @@ -44,6 +45,31 @@ struct JsonValue } as; }; +typedef enum JsonToken +{ + TOKEN_UNKNOWN, + TOKEN_COLON, + TOKEN_COMMA, + TOKEN_OBJECT_OPEN, + TOKEN_OBJECT_CLOSE, + TOKEN_ARRAY_OPEN, + TOKEN_ARRAY_CLOSE, + TOKEN_STRING, + TOKEN_INTEGER, + TOKEN_FLOAT, + TOKEN_BOOLEAN, + TOKEN_NULL, + TOKEN_EOF +} JsonToken; + +typedef struct JsonParserState +{ + FILE *stream; + + JsonToken tokenType; + char *token; +} JsonParserState; + JsonType JsonValueType(JsonValue * value) { @@ -386,6 +412,13 @@ JsonDecodeString(FILE * in) while ((c = fgetc(in)) != EOF) { + if (c <= 0x001F) + { + /* Bad byte; these must be escaped */ + free(str); + return NULL; + } + switch (c) { case '"': @@ -615,3 +648,46 @@ JsonFree(HashMap * object) HashMapFree(object); } + +static void +JsonConsumeWhitespace(JsonParserState * state) +{ + int c; + + while (isspace(c = fgetc(state->stream))); + ungetc(c, state->stream); +} + +static void +JsonTokenSeek(JsonParserState * state) +{ + +} + +static int +JsonExpect(JsonParserState * state, JsonToken token) +{ + return state->tokenType == token; +} + +static HashMap * +JsonDecodeObject(JsonParserState * state) +{ + return NULL; +} + +static Array * +JsonDecodeArray(JsonParserState * state) +{ + return NULL; +} + +HashMap * +JsonDecode(FILE * stream) +{ + JsonParserState state; + + state.stream = stream; + + return JsonDecodeObject(&state); +}