aboutsummaryrefslogtreecommitdiff
path: root/src/load.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/load.c')
-rw-r--r--src/load.c68
1 files changed, 35 insertions, 33 deletions
diff --git a/src/load.c b/src/load.c
index c212489..831eed6 100644
--- a/src/load.c
+++ b/src/load.c
@@ -84,6 +84,7 @@ typedef struct {
/*** error reporting ***/
static void error_set(json_error_t *error, const lex_t *lex,
+ enum json_error_code code,
const char *msg, ...)
{
va_list ap;
@@ -134,7 +135,7 @@ static void error_set(json_error_t *error, const lex_t *lex,
}
}
- jsonp_error_set(error, line, col, pos, "%s", result);
+ jsonp_error_set(error, line, col, pos, code, "%s", result);
}
@@ -213,7 +214,7 @@ static int stream_get(stream_t *stream, json_error_t *error)
out:
stream->state = STREAM_STATE_ERROR;
- error_set(error, stream_to_lex(stream), "unable to decode byte 0x%x", c);
+ error_set(error, stream_to_lex(stream), json_error_invalid_utf8, "unable to decode byte 0x%x", c);
return STREAM_STATE_ERROR;
}
@@ -336,7 +337,7 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
goto out;
else if(c == STREAM_STATE_EOF) {
- error_set(error, lex, "premature end of input");
+ error_set(error, lex, json_error_premature_end_of_input, "premature end of input");
goto out;
}
@@ -344,9 +345,9 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
/* control character */
lex_unget_unsave(lex, c);
if(c == '\n')
- error_set(error, lex, "unexpected newline");
+ error_set(error, lex, json_error_invalid_syntax, "unexpected newline");
else
- error_set(error, lex, "control character 0x%x", c);
+ error_set(error, lex, json_error_invalid_syntax, "control character 0x%x", c);
goto out;
}
@@ -356,7 +357,7 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
c = lex_get_save(lex, error);
for(i = 0; i < 4; i++) {
if(!l_isxdigit(c)) {
- error_set(error, lex, "invalid escape");
+ error_set(error, lex, json_error_invalid_syntax, "invalid escape");
goto out;
}
c = lex_get_save(lex, error);
@@ -366,7 +367,7 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
c == 'f' || c == 'n' || c == 'r' || c == 't')
c = lex_get_save(lex, error);
else {
- error_set(error, lex, "invalid escape");
+ error_set(error, lex, json_error_invalid_syntax, "invalid escape");
goto out;
}
}
@@ -400,7 +401,7 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
value = decode_unicode_escape(p);
if(value < 0) {
- error_set(error, lex, "invalid Unicode escape '%.6s'", p - 1);
+ error_set(error, lex, json_error_invalid_syntax, "invalid Unicode escape '%.6s'", p - 1);
goto out;
}
p += 5;
@@ -410,7 +411,7 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
if(*p == '\\' && *(p + 1) == 'u') {
int32_t value2 = decode_unicode_escape(++p);
if(value2 < 0) {
- error_set(error, lex, "invalid Unicode escape '%.6s'", p - 1);
+ error_set(error, lex, json_error_invalid_syntax, "invalid Unicode escape '%.6s'", p - 1);
goto out;
}
p += 5;
@@ -425,6 +426,7 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
else {
/* invalid second surrogate */
error_set(error, lex,
+ json_error_invalid_syntax,
"invalid Unicode '\\u%04X\\u%04X'",
value, value2);
goto out;
@@ -432,13 +434,13 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
}
else {
/* no second surrogate */
- error_set(error, lex, "invalid Unicode '\\u%04X'",
+ error_set(error, lex, json_error_invalid_syntax, "invalid Unicode '\\u%04X'",
value);
goto out;
}
}
else if(0xDC00 <= value && value <= 0xDFFF) {
- error_set(error, lex, "invalid Unicode '\\u%04X'", value);
+ error_set(error, lex, json_error_invalid_syntax, "invalid Unicode '\\u%04X'", value);
goto out;
}
@@ -526,9 +528,9 @@ static int lex_scan_number(lex_t *lex, int c, json_error_t *error)
intval = json_strtoint(saved_text, &end, 10);
if(errno == ERANGE) {
if(intval < 0)
- error_set(error, lex, "too big negative integer");
+ error_set(error, lex, json_error_numeric_overflow, "too big negative integer");
else
- error_set(error, lex, "too big integer");
+ error_set(error, lex, json_error_numeric_overflow, "too big integer");
goto out;
}
@@ -570,7 +572,7 @@ static int lex_scan_number(lex_t *lex, int c, json_error_t *error)
lex_unget_unsave(lex, c);
if(jsonp_strtod(&lex->saved_text, &doubleval)) {
- error_set(error, lex, "real number overflow");
+ error_set(error, lex, json_error_numeric_overflow, "real number overflow");
goto out;
}
@@ -701,7 +703,7 @@ static json_t *parse_object(lex_t *lex, size_t flags, json_error_t *error)
json_t *value;
if(lex->token != TOKEN_STRING) {
- error_set(error, lex, "string or '}' expected");
+ error_set(error, lex, json_error_invalid_syntax, "string or '}' expected");
goto error;
}
@@ -710,14 +712,14 @@ static json_t *parse_object(lex_t *lex, size_t flags, json_error_t *error)
return NULL;
if (memchr(key, '\0', len)) {
jsonp_free(key);
- error_set(error, lex, "NUL byte in object key not supported");
+ error_set(error, lex, json_error_null_byte_in_key, "NUL byte in object key not supported");
goto error;
}
if(flags & JSON_REJECT_DUPLICATES) {
if(json_object_get(object, key)) {
jsonp_free(key);
- error_set(error, lex, "duplicate object key");
+ error_set(error, lex, json_error_duplicate_key, "duplicate object key");
goto error;
}
}
@@ -725,7 +727,7 @@ static json_t *parse_object(lex_t *lex, size_t flags, json_error_t *error)
lex_scan(lex, error);
if(lex->token != ':') {
jsonp_free(key);
- error_set(error, lex, "':' expected");
+ error_set(error, lex, json_error_invalid_syntax, "':' expected");
goto error;
}
@@ -753,7 +755,7 @@ static json_t *parse_object(lex_t *lex, size_t flags, json_error_t *error)
}
if(lex->token != '}') {
- error_set(error, lex, "'}' expected");
+ error_set(error, lex, json_error_invalid_syntax, "'}' expected");
goto error;
}
@@ -793,7 +795,7 @@ static json_t *parse_array(lex_t *lex, size_t flags, json_error_t *error)
}
if(lex->token != ']') {
- error_set(error, lex, "']' expected");
+ error_set(error, lex, json_error_invalid_syntax, "']' expected");
goto error;
}
@@ -810,7 +812,7 @@ static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error)
lex->depth++;
if(lex->depth > JSON_PARSER_MAX_DEPTH) {
- error_set(error, lex, "maximum parsing depth reached");
+ error_set(error, lex, json_error_stack_overflow, "maximum parsing depth reached");
return NULL;
}
@@ -821,7 +823,7 @@ static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error)
if(!(flags & JSON_ALLOW_NUL)) {
if(memchr(value, '\0', len)) {
- error_set(error, lex, "\\u0000 is not allowed without JSON_ALLOW_NUL");
+ error_set(error, lex, json_error_null_character, "\\u0000 is not allowed without JSON_ALLOW_NUL");
return NULL;
}
}
@@ -865,11 +867,11 @@ static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error)
break;
case TOKEN_INVALID:
- error_set(error, lex, "invalid token");
+ error_set(error, lex, json_error_invalid_syntax, "invalid token");
return NULL;
default:
- error_set(error, lex, "unexpected token");
+ error_set(error, lex, json_error_invalid_syntax, "unexpected token");
return NULL;
}
@@ -889,7 +891,7 @@ static json_t *parse_json(lex_t *lex, size_t flags, json_error_t *error)
lex_scan(lex, error);
if(!(flags & JSON_DECODE_ANY)) {
if(lex->token != '[' && lex->token != '{') {
- error_set(error, lex, "'[' or '{' expected");
+ error_set(error, lex, json_error_invalid_syntax, "'[' or '{' expected");
return NULL;
}
}
@@ -901,7 +903,7 @@ static json_t *parse_json(lex_t *lex, size_t flags, json_error_t *error)
if(!(flags & JSON_DISABLE_EOF_CHECK)) {
lex_scan(lex, error);
if(lex->token != TOKEN_EOF) {
- error_set(error, lex, "end of file expected");
+ error_set(error, lex, json_error_end_of_input_expected, "end of file expected");
json_decref(result);
return NULL;
}
@@ -944,7 +946,7 @@ json_t *json_loads(const char *string, size_t flags, json_error_t *error)
jsonp_error_init(error, "<string>");
if (string == NULL) {
- error_set(error, NULL, "wrong arguments");
+ error_set(error, NULL, json_error_invalid_argument, "wrong arguments");
return NULL;
}
@@ -988,7 +990,7 @@ json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t
jsonp_error_init(error, "<buffer>");
if (buffer == NULL) {
- error_set(error, NULL, "wrong arguments");
+ error_set(error, NULL, json_error_invalid_argument, "wrong arguments");
return NULL;
}
@@ -1019,7 +1021,7 @@ json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)
jsonp_error_init(error, source);
if (input == NULL) {
- error_set(error, NULL, "wrong arguments");
+ error_set(error, NULL, json_error_invalid_argument, "wrong arguments");
return NULL;
}
@@ -1058,7 +1060,7 @@ json_t *json_loadfd(int input, size_t flags, json_error_t *error)
jsonp_error_init(error, source);
if (input < 0) {
- error_set(error, NULL, "wrong arguments");
+ error_set(error, NULL, json_error_invalid_argument, "wrong arguments");
return NULL;
}
@@ -1079,14 +1081,14 @@ json_t *json_load_file(const char *path, size_t flags, json_error_t *error)
jsonp_error_init(error, path);
if (path == NULL) {
- error_set(error, NULL, "wrong arguments");
+ error_set(error, NULL, json_error_invalid_argument, "wrong arguments");
return NULL;
}
fp = fopen(path, "rb");
if(!fp)
{
- error_set(error, NULL, "unable to open %s: %s",
+ error_set(error, NULL, json_error_cannot_open_file, "unable to open %s: %s",
path, strerror(errno));
return NULL;
}
@@ -1139,7 +1141,7 @@ json_t *json_load_callback(json_load_callback_t callback, void *arg, size_t flag
jsonp_error_init(error, "<callback>");
if (callback == NULL) {
- error_set(error, NULL, "wrong arguments");
+ error_set(error, NULL, json_error_invalid_argument, "wrong arguments");
return NULL;
}