Slightly better debug output

This commit is contained in:
Eric Fischer
2014-02-05 17:21:19 -08:00
parent 3e9bb4567e
commit 558c47c498
+51 -24
View File
@@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include <stdarg.h>
typedef enum json_type { typedef enum json_type {
JSON_STRING, JSON_NUMBER, JSON_ARRAY, JSON_HASH, JSON_NULL, JSON_TRUE, JSON_FALSE, JSON_STRING, JSON_NUMBER, JSON_ARRAY, JSON_HASH, JSON_NULL, JSON_TRUE, JSON_FALSE,
@@ -27,6 +28,8 @@ typedef struct json_object {
json_hash *hash; json_hash *hash;
} json_object; } json_object;
void json_print(json_object *j);
static json_object *add_object(json_type type, json_object *parent) { static json_object *add_object(json_type type, json_object *parent) {
json_object *o = malloc(sizeof(struct json_object)); json_object *o = malloc(sizeof(struct json_object));
o->type = type; o->type = type;
@@ -57,8 +60,11 @@ static json_object *add_object(json_type type, json_object *parent) {
return o; return o;
} }
static void json_error(char *s) { static void json_error(char *s, ...) {
fprintf(stderr, "%s\n", s); va_list ap;
va_start(ap, s);
vfprintf(stderr, s, ap);
va_end(ap);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@@ -112,7 +118,7 @@ json_object *json_parse(FILE *f, json_object *current) {
return json_parse(f, add_object(JSON_ARRAY, current)); return json_parse(f, add_object(JSON_ARRAY, current));
} else if (c == ']') { } else if (c == ']') {
if (current->parent == NULL || current->parent->type != JSON_ARRAY) { if (current->parent == NULL || current->parent->type != JSON_ARRAY) {
json_error("] without ["); json_error("] without [\n");
} }
return current->parent; return current->parent;
@@ -122,7 +128,7 @@ json_object *json_parse(FILE *f, json_object *current) {
return json_parse(f, add_object(JSON_HASH, current)); return json_parse(f, add_object(JSON_HASH, current));
} else if (c == '}') { } else if (c == '}') {
if (current->parent == NULL || current->parent->type != JSON_HASH) { if (current->parent == NULL || current->parent->type != JSON_HASH) {
json_error("} without {"); json_error("} without {\n");
} }
return current->parent; return current->parent;
@@ -130,7 +136,7 @@ json_object *json_parse(FILE *f, json_object *current) {
if (c == 'n') { if (c == 'n') {
if (getc(f) != 'u' || getc(f) != 'l' || getc(f) != 'l') { if (getc(f) != 'u' || getc(f) != 'l' || getc(f) != 'l') {
json_error("misspelled null"); json_error("misspelled null\n");
} }
return add_object(JSON_NULL, current); return add_object(JSON_NULL, current);
@@ -138,7 +144,7 @@ json_object *json_parse(FILE *f, json_object *current) {
if (c == 't') { if (c == 't') {
if (getc(f) != 'r' || getc(f) != 'u' || getc(f) != 'e') { if (getc(f) != 'r' || getc(f) != 'u' || getc(f) != 'e') {
json_error("misspelled true"); json_error("misspelled true\n");
} }
return add_object(JSON_TRUE, current); return add_object(JSON_TRUE, current);
@@ -146,7 +152,7 @@ json_object *json_parse(FILE *f, json_object *current) {
if (c == 'f') { if (c == 'f') {
if (getc(f) != 'a' || getc(f) != 'l' || getc(f) != 's' || getc(f) != 'e') { if (getc(f) != 'a' || getc(f) != 'l' || getc(f) != 's' || getc(f) != 'e') {
json_error("misspelled false"); json_error("misspelled false\n");
} }
return add_object(JSON_FALSE, current); return add_object(JSON_FALSE, current);
@@ -154,23 +160,23 @@ json_object *json_parse(FILE *f, json_object *current) {
if (c == ',') { if (c == ',') {
if (current->parent == NULL || if (current->parent == NULL ||
(current->parent->type != JSON_ARRAY || (current->parent->type != JSON_ARRAY &&
current->parent->type != JSON_HASH)) { current->parent->type != JSON_HASH)) {
json_error(", not in array or hash"); json_error(", not in array or hash: %d\n", current->parent->type);
} }
return json_parse(f, current); return json_parse(f, current->parent);
} }
if (c == ':') { if (c == ':') {
if (current->parent == NULL || current->parent->type != JSON_HASH) { if (current->parent == NULL || current->parent->type != JSON_HASH) {
json_error(": not in hash"); json_error(": not in hash: %d\n", current->parent);
} }
if (current->parent->hash == NULL || current->parent->hash->value != NULL) { if (current->parent->hash == NULL || current->parent->hash->value != NULL) {
json_error(": without key"); json_error(": without key: %d\n", current->parent);
} }
return json_parse(f, current); return json_parse(f, current->parent);
} }
if (c == '-' || (c >= '0' && c <= '9')) { if (c == '-' || (c >= '0' && c <= '9')) {
@@ -250,7 +256,7 @@ json_object *json_parse(FILE *f, json_object *current) {
} else if (c == 'u') { } else if (c == 'u') {
/* XXX */ /* XXX */
} else { } else {
json_error("unknown string escape"); json_error("unknown string escape %c\n", c);
} }
} else { } else {
string_append(&val, c); string_append(&val, c);
@@ -263,26 +269,45 @@ json_object *json_parse(FILE *f, json_object *current) {
return s; return s;
} }
json_error("unrecognized character"); json_error("unrecognized character %c\n", c);
} }
void json_print(json_object *j) { void json_print(json_object *j) {
if (j->type == JSON_STRING) { if (j == NULL) {
printf("string: %s\n", j->string); printf("NULL");
} else if (j->type == JSON_STRING) {
printf("\"%s\"", j->string);
} else if (j->type == JSON_NUMBER) { } else if (j->type == JSON_NUMBER) {
printf("number: %f\n", j->number); printf("%f", j->number);
} else if (j->type == JSON_NULL) { } else if (j->type == JSON_NULL) {
printf("null\n"); printf("null");
} else if (j->type == JSON_TRUE) { } else if (j->type == JSON_TRUE) {
printf("true\n"); printf("true");
} else if (j->type == JSON_FALSE) { } else if (j->type == JSON_FALSE) {
printf("false\n"); printf("false");
} else if (j->type == JSON_HASH) { } else if (j->type == JSON_HASH) {
printf("hash\n"); printf("{");
json_hash *h = j->hash;
while (h != NULL) {
json_print(h->key);
printf(":");
json_print(h->value);
printf(",");
h = h->next;
}
printf("}");
} else if (j->type == JSON_ARRAY) { } else if (j->type == JSON_ARRAY) {
printf("array\n"); printf("[");
json_array *a = j->array;
while (a != NULL) {
json_print(a->object);
printf(",");
a = a->next;
}
printf("]");
} else { } else {
printf("what type? %d\n", j->type); printf("what type? %d", j->type);
} }
} }
@@ -290,10 +315,12 @@ int main() {
json_object *j = NULL; json_object *j = NULL;
while ((j = json_parse(stdin, j)) != NULL) { while ((j = json_parse(stdin, j)) != NULL) {
#if 0
if (j->parent != NULL) { if (j->parent != NULL) {
printf("parent: "); printf("parent: ");
json_print(j->parent); json_print(j->parent);
} }
#endif
json_print(j); json_print(j);
printf("\n"); printf("\n");
} }