From 68769abbf1c7b4ef4f03f93f0c50e016c709a167 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 10 May 2018 20:14:24 +0200 Subject: Move error handling to world --- src/reader.c | 3 ++- src/serd_internal.h | 23 ----------------------- src/world.c | 32 ++++++++++++++++++++++++++++++++ src/world.h | 6 ++++++ src/writer.c | 44 +++++++++++++++++--------------------------- 5 files changed, 57 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/reader.c b/src/reader.c index c35f5fbc..7b4e5fed 100644 --- a/src/reader.c +++ b/src/reader.c @@ -21,6 +21,7 @@ #include "serd_internal.h" #include "stack.h" #include "system.h" +#include "world.h" #include #include @@ -38,7 +39,7 @@ r_err(SerdReader* const reader, const SerdStatus st, const char* const fmt, ...) va_start(args, fmt); const Cursor* const cur = &reader->source.cur; const SerdError e = {st, cur->filename, cur->line, cur->col, fmt, &args}; - serd_error(reader->world, &e); + serd_world_error(reader->world, &e); va_end(args); return st; } diff --git a/src/serd_internal.h b/src/serd_internal.h index 1bb2a3bc..91ba509c 100644 --- a/src/serd_internal.h +++ b/src/serd_internal.h @@ -17,12 +17,6 @@ #ifndef SERD_INTERNAL_H #define SERD_INTERNAL_H -#include "world.h" - -#include "serd/serd.h" - -#include - #define NS_XSD "http://www.w3.org/2001/XMLSchema#" #define NS_RDF "http://www.w3.org/1999/02/22-rdf-syntax-ns#" @@ -32,21 +26,4 @@ # define MIN(a, b) (((a) < (b)) ? (a) : (b)) #endif -/* Error reporting */ - -static inline void -serd_error(const SerdWorld* world, const SerdError* e) -{ - if (world->error_func) { - world->error_func(world->error_handle, e); - } else { - if (e->filename) { - fprintf(stderr, "error: %s:%u:%u: ", e->filename, e->line, e->col); - } else { - fprintf(stderr, "error: "); - } - vfprintf(stderr, e->fmt, *e->args); - } -} - #endif // SERD_INTERNAL_H diff --git a/src/world.c b/src/world.c index 5663c182..aa260e37 100644 --- a/src/world.c +++ b/src/world.c @@ -16,8 +16,40 @@ #include "world.h" +#include +#include #include +SerdStatus +serd_world_error(const SerdWorld* const world, const SerdError* const e) +{ + if (world->error_func) { + world->error_func(world->error_handle, e); + } else { + if (e->filename) { + fprintf(stderr, "error: %s:%u:%u: ", e->filename, e->line, e->col); + } else { + fprintf(stderr, "error: "); + } + vfprintf(stderr, e->fmt, *e->args); + } + return e->status; +} + +SerdStatus +serd_world_errorf(const SerdWorld* const world, + const SerdStatus st, + const char* const fmt, + ...) +{ + va_list args; + va_start(args, fmt); + const SerdError e = {st, NULL, 0, 0, fmt, &args}; + serd_world_error(world, &e); + va_end(args); + return st; +} + SerdWorld* serd_world_new(void) { diff --git a/src/world.h b/src/world.h index 3d994df9..08c351fe 100644 --- a/src/world.h +++ b/src/world.h @@ -24,4 +24,10 @@ struct SerdWorldImpl { void* error_handle; }; +SerdStatus +serd_world_error(const SerdWorld* world, const SerdError* e); + +SerdStatus +serd_world_errorf(const SerdWorld* world, SerdStatus st, const char* fmt, ...); + #endif // SERD_WORLD_H diff --git a/src/writer.c b/src/writer.c index 70d22485..6def5f73 100644 --- a/src/writer.c +++ b/src/writer.c @@ -22,11 +22,11 @@ #include "stack.h" #include "string_utils.h" #include "uri_utils.h" +#include "world.h" #include "serd/serd.h" #include -#include #include #include #include @@ -137,32 +137,16 @@ supports_uriref(const SerdWriter* writer) return writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG; } -static void -w_err(SerdWriter* writer, SerdStatus st, const char* fmt, ...) -{ - /* TODO: This results in errors with no file information, which is not - helpful when re-serializing a file (particularly for "undefined - namespace prefix" errors. The statement sink API needs to be changed to - add a Cursor parameter so the source can notify the writer of the - statement origin for better error reporting. */ - - va_list args; - va_start(args, fmt); - const SerdError e = {st, NULL, 0, 0, fmt, &args}; - serd_error(writer->world, &e); - va_end(args); -} - SERD_PURE_FUNC static WriteContext* -anon_stack_top(SerdWriter* writer) +anon_stack_top(SerdWriter* const writer) { assert(!serd_stack_is_empty(&writer->anon_stack)); return (WriteContext*)(writer->anon_stack.buf + writer->anon_stack.size - sizeof(WriteContext)); } -static inline SerdNode* +static SerdNode* ctx(SerdWriter* writer, const Field field) { SerdNode* node = NULL; @@ -192,7 +176,8 @@ write_character(SerdWriter* writer, const uint8_t* utf8, size_t* size) const uint32_t c = parse_utf8_char(utf8, size); switch (*size) { case 0: - w_err(writer, SERD_ERR_BAD_ARG, "invalid UTF-8 start: %X\n", utf8[0]); + serd_world_errorf( + writer->world, SERD_ERR_BAD_ARG, "invalid UTF-8 start: %X\n", utf8[0]); return 0; case 1: snprintf(escape, sizeof(escape), "\\u%04X", utf8[0]); @@ -611,10 +596,10 @@ write_uri_node(SerdWriter* const writer, if (!has_scheme && !supports_uriref(writer) && !serd_env_base_uri(writer->env)) { - w_err(writer, - SERD_ERR_BAD_ARG, - "syntax does not support URI reference <%s>\n", - node_str); + serd_world_errorf(writer->world, + SERD_ERR_BAD_ARG, + "syntax does not support URI reference <%s>\n", + node_str); return false; } @@ -651,7 +636,8 @@ write_curie(SerdWriter* const writer, const SerdNode* const node) case SERD_NTRIPLES: case SERD_NQUADS: if ((st = serd_env_expand(writer->env, node, &prefix, &suffix))) { - w_err(writer, st, "undefined namespace prefix `%s'\n", node_str); + serd_world_errorf( + writer->world, st, "undefined namespace prefix `%s'\n", node_str); return false; } write_sep(writer, SEP_URI_BEGIN); @@ -913,20 +899,24 @@ serd_writer_end_anon(SerdWriter* writer, const SerdNode* node) if (writer->syntax == SERD_NTRIPLES || writer->syntax == SERD_NQUADS) { return SERD_SUCCESS; } + if (serd_stack_is_empty(&writer->anon_stack) || writer->indent == 0) { - w_err(writer, SERD_ERR_UNKNOWN, "unexpected end of anonymous node\n"); - return SERD_ERR_UNKNOWN; + return serd_world_errorf( + writer->world, SERD_ERR_UNKNOWN, "unexpected end of anonymous node\n"); } + --writer->indent; write_sep(writer, SEP_ANON_END); free_context(&writer->context); writer->context = *anon_stack_top(writer); serd_stack_pop(&writer->anon_stack, sizeof(WriteContext)); + const bool is_subject = serd_node_equals(node, writer->context.subject); if (is_subject) { serd_node_set(&writer->context.subject, node); memset(writer->context.predicate, 0, sizeof(SerdNode)); } + return SERD_SUCCESS; } -- cgit v1.2.1