diff --git a/include/config.h b/include/config.h new file mode 100644 index 0000000..2a5ce51 --- /dev/null +++ b/include/config.h @@ -0,0 +1,39 @@ + +#ifndef _CONFIG_H +#define _CONFIG_H + +/* + Number of elements in a hashmap + + Hashmaps are implemented as an array + of linked lists. This specifies the + length of the initial array. +*/ +#define OBJ_SIZE 32 + +/* + Length of surrounding context to print + on parser errors. + + Upon parser errors (e.g. syntax errors + in the JSON file), the program outputs + the surrounding context of the error. + E.g: + | Expected ':' at index 123 + | context: + | \n\t\t{ "foo" "bar" },\n\t + | ^ + In the above example, ERROR_CONTEXT_LEN + is set to 20. +*/ +#define ERROR_CONTEXT_LEN 60 + +/* + Error codes + EARLY_EOF - End of file unexpectedly reached + UNEXPECTED_CHAR - Unexpected character encountered +*/ +#define EARLY_EOF 202 +#define UNEXPECTED_CHAR 200 + +#endif diff --git a/include/json_value.h b/include/json_value.h index ea49205..880d2e8 100644 --- a/include/json_value.h +++ b/include/json_value.h @@ -2,10 +2,9 @@ #ifndef _JSON_VALUE_H #define _JSON_VALUE_H +#include "config.h" #include // bool -#define OBJ_SIZE 32 - typedef struct obj_entry { char const* key; struct json_value* val; diff --git a/src/main.c b/src/main.c index 5ce3108..db00c36 100644 --- a/src/main.c +++ b/src/main.c @@ -18,7 +18,7 @@ int main(int argc, char* argv[]) struct json_value x = parse_json_value(fp); - // print_json(x, 1); + print_json(x, 1); json_value_delete(x); diff --git a/src/parse.c b/src/parse.c index b98de26..6f25616 100644 --- a/src/parse.c +++ b/src/parse.c @@ -1,6 +1,7 @@ #include "parse.h" +#include "config.h" #include "json_value.h" #include "util.h" @@ -10,10 +11,6 @@ #include // exit, EXIT_SUCCESS, EXIT_FAILURE #include // strcmp -#define EARLY_EOF 202 -#define MALLOC_DIE 201 -#define UNEXPECTED_CHAR 200 - char* read_string(FILE* fp); obj_t* read_object(FILE* fp); void discard_whitespace(FILE* fp); diff --git a/src/util.c b/src/util.c index 1785bc5..a640707 100644 --- a/src/util.c +++ b/src/util.c @@ -1,5 +1,6 @@ #include "util.h" +#include "config.h" #include // err #include // errno @@ -8,8 +9,6 @@ #include // fprintf #include // malloc, realloc, calloc -#define ERROR_CONTEXT_LEN 80 - void* malloc_or_die(size_t size) { void* result = malloc(size);