Begin prototyping JsonDecode

This commit is contained in:
Jordan Bancino 2022-08-02 16:58:15 -04:00
parent 8d77846578
commit f48f7c088c

View file

@ -29,6 +29,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h>
struct JsonValue struct JsonValue
{ {
@ -44,6 +45,31 @@ struct JsonValue
} as; } 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 JsonType
JsonValueType(JsonValue * value) JsonValueType(JsonValue * value)
{ {
@ -386,6 +412,13 @@ JsonDecodeString(FILE * in)
while ((c = fgetc(in)) != EOF) while ((c = fgetc(in)) != EOF)
{ {
if (c <= 0x001F)
{
/* Bad byte; these must be escaped */
free(str);
return NULL;
}
switch (c) switch (c)
{ {
case '"': case '"':
@ -615,3 +648,46 @@ JsonFree(HashMap * object)
HashMapFree(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);
}