aboutsummaryrefslogtreecommitdiffstats
path: root/src/writer.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-10-21 20:57:33 +0200
committerDavid Robillard <d@drobilla.net>2020-10-27 13:13:58 +0100
commitcd0f6e585a02b26eb42e760fe8fa8b70612cc009 (patch)
treeacff51f98ab8216baa7055bcb6c52354ad7b9e42 /src/writer.c
parent2c70b584b6b110a85f0a7b1491b40c0de37b7f93 (diff)
downloadserd-cd0f6e585a02b26eb42e760fe8fa8b70612cc009.tar.gz
serd-cd0f6e585a02b26eb42e760fe8fa8b70612cc009.tar.bz2
serd-cd0f6e585a02b26eb42e760fe8fa8b70612cc009.zip
Simplify streaming API and improve pretty printing
This removes the obligation from the caller to correctly maintain flags to describe the current anonymous context, instead making the writer handle this itself as much as possible. Flags remain for the cases the writer can not infer from context: the start of anonymous subject and object nodes.
Diffstat (limited to 'src/writer.c')
-rw-r--r--src/writer.c353
1 files changed, 210 insertions, 143 deletions
diff --git a/src/writer.c b/src/writer.c
index 14793112..132cf028 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -31,57 +31,75 @@
#include <stdlib.h>
#include <string.h>
-typedef struct {
- SerdNode* graph;
- SerdNode* subject;
- SerdNode* predicate;
+typedef enum {
+ CTX_NAMED, ///< Normal non-anonymous context
+ CTX_BLANK, ///< Anonymous blank node
+ CTX_LIST ///< Anonymous list
+} ContextType;
+
+typedef struct
+{
+ ContextType type;
+ SerdNode* graph;
+ SerdNode* subject;
+ SerdNode* predicate;
+ bool indented_object;
} WriteContext;
-static const WriteContext WRITE_CONTEXT_NULL = { NULL, NULL, NULL };
+static const WriteContext WRITE_CONTEXT_NULL = {CTX_NAMED,
+ NULL,
+ NULL,
+ NULL,
+ false};
typedef enum {
- SEP_NONE,
+ SEP_NONE, ///< Placeholder for nodes or nothing
SEP_END_S, ///< End of a subject ('.')
SEP_END_P, ///< End of a predicate (';')
SEP_END_O, ///< End of an object (',')
SEP_S_P, ///< Between a subject and predicate (whitespace)
SEP_P_O, ///< Between a predicate and object (whitespace)
SEP_ANON_BEGIN, ///< Start of anonymous node ('[')
+ SEP_ANON_S_P, ///< Between start of anonymous node and predicate
SEP_ANON_END, ///< End of anonymous node (']')
SEP_LIST_BEGIN, ///< Start of list ('(')
SEP_LIST_SEP, ///< List separator (whitespace)
SEP_LIST_END, ///< End of list (')')
SEP_GRAPH_BEGIN, ///< Start of graph ('{')
SEP_GRAPH_END, ///< End of graph ('}')
- SEP_URI_BEGIN, ///< URI start quote ('<')
- SEP_URI_END ///< URI end quote ('>')
} Sep;
+typedef uint32_t SepMask; ///< Bitfield of separator flags
+
+#define SEP_ALL ((SepMask)-1)
+#define M(s) (1U << (s))
+
typedef struct {
- const char* str; ///< Sep string
- uint8_t len; ///< Length of sep string
- uint8_t space_before; ///< Newline before sep
- uint8_t space_after_node; ///< Newline after sep if after node
- uint8_t space_after_sep; ///< Newline after sep if after sep
+ const char* str; ///< Sep string
+ size_t len; ///< Length of sep string
+ int indent; ///< Indent delta
+ SepMask pre_space_after; ///< Leading space if after given seps
+ SepMask pre_line_after; ///< Leading newline if after given seps
+ SepMask post_line_after; ///< Trailing newline if after given seps
} SepRule;
static const SepRule rules[] = {
- { NULL, 0, 0, 0, 0 },
- { " .\n\n", 4, 0, 0, 0 },
- { " ;", 2, 0, 1, 1 },
- { " ,", 2, 0, 1, 0 },
- { NULL, 0, 0, 1, 0 },
- { " ", 1, 0, 0, 0 },
- { "[", 1, 0, 1, 1 },
- { "]", 1, 1, 0, 0 },
- { "(", 1, 0, 0, 0 },
- { NULL, 0, 0, 1, 0 },
- { ")", 1, 1, 0, 0 },
- { " {", 2, 0, 1, 1 },
- { " }", 2, 0, 1, 1 },
- { "<", 1, 0, 0, 0 },
- { ">", 1, 0, 0, 0 },
- { "\n", 1, 0, 1, 0 }
+ {"", 0, +0, SEP_NONE, SEP_NONE, SEP_NONE},
+ {".\n", 2, -1, SEP_ALL, SEP_NONE, SEP_NONE},
+ {";", 1, +0, SEP_ALL, SEP_NONE, SEP_ALL},
+ {",", 1, +0, SEP_ALL, SEP_NONE, ~(M(SEP_ANON_END) | M(SEP_LIST_END))},
+ {"", 0, +1, SEP_NONE, SEP_NONE, SEP_ALL},
+ {" ", 1, +0, SEP_NONE, SEP_NONE, SEP_NONE},
+ {"[", 1, +1, M(SEP_END_O), SEP_NONE, SEP_NONE},
+ {"", 0, +0, SEP_NONE, SEP_ALL, SEP_NONE},
+ {"]", 1, -1, SEP_NONE, ~M(SEP_ANON_BEGIN), SEP_NONE},
+ {"(", 1, +1, M(SEP_END_O), SEP_NONE, SEP_ALL},
+ {"", 0, +0, SEP_NONE, SEP_ALL, SEP_NONE},
+ {")", 1, -1, SEP_NONE, SEP_ALL, SEP_NONE},
+ {"{", 1, +1, SEP_ALL, SEP_NONE, SEP_NONE},
+ {"}", 1, -1, SEP_NONE, SEP_NONE, SEP_ALL},
+ {"<", 1, +0, SEP_NONE, SEP_NONE, SEP_NONE},
+ {">", 1, +0, SEP_NONE, SEP_NONE, SEP_NONE},
};
struct SerdWriterImpl {
@@ -98,8 +116,6 @@ struct SerdWriterImpl {
SerdErrorSink error_sink;
void* error_handle;
WriteContext context;
- SerdNode* list_subj;
- unsigned list_depth;
unsigned indent;
char* bprefix;
size_t bprefix_len;
@@ -129,12 +145,40 @@ supports_abbrev(const SerdWriter* writer)
return writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG;
}
-static inline WriteContext*
-anon_stack_top(SerdWriter* writer)
+static SerdStatus
+free_context(SerdWriter* writer)
+{
+ serd_node_free(writer->context.graph);
+ serd_node_free(writer->context.subject);
+ serd_node_free(writer->context.predicate);
+ return SERD_SUCCESS;
+}
+
+static inline void
+push_context(SerdWriter* writer, const WriteContext new_context)
+{
+ WriteContext* top = (WriteContext*)serd_stack_push(&writer->anon_stack,
+ sizeof(WriteContext));
+
+ *top = writer->context;
+ writer->context = new_context;
+}
+
+static inline void
+pop_context(SerdWriter* writer)
{
assert(!serd_stack_is_empty(&writer->anon_stack));
- return (WriteContext*)(writer->anon_stack.buf
- + writer->anon_stack.size - sizeof(WriteContext));
+
+ if (writer->context.indented_object && writer->indent > 0) {
+ --writer->indent;
+ }
+
+ free_context(writer);
+ writer->context =
+ *(WriteContext*)(writer->anon_stack.buf + writer->anon_stack.size -
+ sizeof(WriteContext));
+
+ serd_stack_pop(&writer->anon_stack, sizeof(WriteContext));
}
static inline SerdNode*
@@ -377,19 +421,37 @@ static bool
write_sep(SerdWriter* writer, const Sep sep)
{
const SepRule* rule = &rules[sep];
- if (rule->space_before) {
+
+ // Adjust indent, but tolerate if it would become negative
+ writer->indent =
+ ((rule->indent >= 0 || writer->indent >= (unsigned)-rule->indent)
+ ? writer->indent + rule->indent
+ : 0);
+
+ // Write newline or space before separator if necessary
+ if (rule->pre_line_after & (1u << writer->last_sep)) {
write_newline(writer);
+ } else if (rule->pre_space_after & (1u << writer->last_sep)) {
+ sink(" ", 1, writer);
}
- if (rule->str) {
+
+ // Write actual separator string
+ if (rule->len > 0) {
sink(rule->str, rule->len, writer);
}
- if ((writer->last_sep && rule->space_after_sep) ||
- (!writer->last_sep && rule->space_after_node)) {
+
+ // Write newline after separator if necessary
+ if (rule->post_line_after & (1u << writer->last_sep)) {
write_newline(writer);
- } else if (writer->last_sep && rule->space_after_node) {
- sink(" ", 1, writer);
+ writer->last_sep = SEP_NONE;
+ } else {
+ writer->last_sep = sep;
+ }
+
+ if (sep == SEP_END_S) {
+ writer->indent = 0;
}
- writer->last_sep = sep;
+
return true;
}
@@ -405,19 +467,11 @@ reset_context(SerdWriter* writer, bool graph)
if (writer->context.predicate) {
memset(writer->context.predicate, 0, sizeof(SerdNode));
}
+ writer->context.indented_object = false;
writer->empty = false;
return SERD_SUCCESS;
}
-static SerdStatus
-free_context(SerdWriter* writer)
-{
- serd_node_free(writer->context.graph);
- serd_node_free(writer->context.subject);
- serd_node_free(writer->context.predicate);
- return SERD_SUCCESS;
-}
-
static bool
is_inline_start(const SerdWriter* writer,
SerdField field,
@@ -433,6 +487,8 @@ write_literal(SerdWriter* writer,
const SerdNode* node,
SerdStatementFlags flags)
{
+ writer->last_sep = SEP_NONE;
+
const SerdNode* datatype = serd_node_datatype(node);
const SerdNode* lang = serd_node_language(node);
const char* node_str = serd_node_string(node);
@@ -493,19 +549,19 @@ write_uri_node(SerdWriter* const writer,
const SerdField field,
const SerdStatementFlags flags)
{
- const SerdNode* prefix = NULL;
- SerdStringView suffix = {NULL, 0};
-
if (is_inline_start(writer, field, flags)) {
- ++writer->indent;
write_sep(writer, SEP_ANON_BEGIN);
- sink("== ", 3, writer);
+ sink(" == ", 4, writer);
}
- const char* node_str = serd_node_string(node);
- const bool has_scheme = serd_uri_string_has_scheme(node_str);
- if (field == SERD_PREDICATE && supports_abbrev(writer)
- && serd_node_equals(node, writer->world->rdf_type)) {
+ writer->last_sep = SEP_NONE;
+
+ const SerdNode* prefix = NULL;
+ SerdStringView suffix = {NULL, 0};
+ const char* node_str = serd_node_string(node);
+ const bool has_scheme = serd_uri_string_has_scheme(node_str);
+ if (field == SERD_PREDICATE && supports_abbrev(writer) &&
+ serd_node_equals(node, writer->world->rdf_type)) {
return sink("a", 1, writer) == 1;
} else if (supports_abbrev(writer) &&
serd_node_equals(node, writer->world->rdf_nil)) {
@@ -521,7 +577,7 @@ write_uri_node(SerdWriter* const writer,
return true;
}
- write_sep(writer, SEP_URI_BEGIN);
+ sink("<", 1, writer);
if (!(writer->style & SERD_STYLE_UNRESOLVED) &&
serd_env_base_uri(writer->env)) {
const SerdURI* base_uri = serd_env_get_parsed_base_uri(writer->env);
@@ -541,10 +597,9 @@ write_uri_node(SerdWriter* const writer,
} else {
write_uri_from_node(writer, node);
}
- write_sep(writer, SEP_URI_END);
+ sink(">", 1, writer);
if (is_inline_start(writer, field, flags)) {
sink(" ;", 2, writer);
- write_newline(writer);
}
return true;
}
@@ -555,7 +610,7 @@ write_curie(SerdWriter* const writer,
const SerdField field,
const SerdStatementFlags flags)
{
- const char* node_str = serd_node_string(node);
+ writer->last_sep = SEP_NONE;
SerdStringView prefix = {NULL, 0};
SerdStringView suffix = {NULL, 0};
@@ -568,25 +623,23 @@ write_curie(SerdWriter* const writer,
serd_world_errorf(writer->world,
st,
"undefined namespace prefix `%s'\n",
- node_str);
+ serd_node_string(node));
return false;
}
- write_sep(writer, SEP_URI_BEGIN);
+ sink("<", 1, writer);
write_uri(writer, prefix.buf, prefix.len);
write_uri(writer, suffix.buf, suffix.len);
- write_sep(writer, SEP_URI_END);
+ sink(">", 1, writer);
break;
case SERD_TURTLE:
case SERD_TRIG:
if (is_inline_start(writer, field, flags)) {
- ++writer->indent;
write_sep(writer, SEP_ANON_BEGIN);
- sink("== ", 3, writer);
+ sink(" == ", 4, writer);
}
- write_lname(writer, node_str, node->n_bytes);
+ write_lname(writer, serd_node_string(node), node->n_bytes);
if (is_inline_start(writer, field, flags)) {
sink(" ;", 2, writer);
- write_newline(writer);
}
}
return true;
@@ -601,20 +654,12 @@ write_blank(SerdWriter* const writer,
const char* node_str = serd_node_string(node);
if (supports_abbrev(writer)) {
if (is_inline_start(writer, field, flags)) {
- ++writer->indent;
return write_sep(writer, SEP_ANON_BEGIN);
- } else if (field == SERD_SUBJECT && (flags & SERD_LIST_S_BEGIN)) {
- assert(writer->list_depth == 0);
- serd_node_set(&writer->list_subj, node);
- ++writer->list_depth;
- ++writer->indent;
+ } else if ((field == SERD_SUBJECT && (flags & SERD_LIST_S_BEGIN)) ||
+ (field == SERD_OBJECT && (flags & SERD_LIST_O_BEGIN))) {
return write_sep(writer, SEP_LIST_BEGIN);
- } else if (field == SERD_OBJECT && (flags & SERD_LIST_O_BEGIN)) {
- ++writer->indent;
- ++writer->list_depth;
- return write_sep(writer, SEP_LIST_BEGIN);
- } else if ((field == SERD_SUBJECT && (flags & SERD_EMPTY_S)) ||
- (field == SERD_OBJECT && (flags & SERD_EMPTY_O))) {
+ } else if (field == SERD_SUBJECT && (flags & SERD_EMPTY_S)) {
+ writer->last_sep = SEP_NONE; // Treat "[]" like anode
return sink("[]", 2, writer) == 2;
}
}
@@ -629,6 +674,7 @@ write_blank(SerdWriter* const writer,
sink(node_str, node->n_bytes, writer);
}
+ writer->last_sep = SEP_NONE;
return true;
}
@@ -638,23 +684,18 @@ write_node(SerdWriter* writer,
const SerdField field,
SerdStatementFlags flags)
{
- bool ret = false;
switch (node->type) {
case SERD_LITERAL:
- ret = write_literal(writer, node, flags);
- break;
+ return write_literal(writer, node, flags);
case SERD_URI:
- ret = write_uri_node(writer, node, field, flags);
- break;
+ return write_uri_node(writer, node, field, flags);
case SERD_CURIE:
- ret = write_curie(writer, node, field, flags);
- break;
+ return write_curie(writer, node, field, flags);
case SERD_BLANK:
- ret = write_blank(writer, node, field, flags);
- break;
+ return write_blank(writer, node, field, flags);
}
- writer->last_sep = SEP_NONE;
- return ret;
+
+ return false;
}
static inline bool
@@ -678,13 +719,16 @@ write_list_obj(SerdWriter* writer,
const SerdNode* object)
{
if (serd_node_equals(object, writer->world->rdf_nil)) {
- --writer->indent;
write_sep(writer, SEP_LIST_END);
return true;
- } else if (serd_node_equals(predicate, writer->world->rdf_first)) {
- write_sep(writer, SEP_LIST_SEP);
+ }
+
+ if (serd_node_equals(predicate, writer->world->rdf_first)) {
write_node(writer, object, SERD_OBJECT, flags);
+ } else {
+ write_sep(writer, SEP_LIST_SEP);
}
+
return false;
}
@@ -693,6 +737,11 @@ serd_writer_write_statement(SerdWriter* writer,
SerdStatementFlags flags,
const SerdStatement* statement)
{
+ assert(!((flags & SERD_EMPTY_S) && (flags & SERD_ANON_S_BEGIN)));
+ assert(!((flags & SERD_EMPTY_S) && (flags & SERD_LIST_S_BEGIN)));
+ assert(!((flags & SERD_ANON_S_BEGIN) && (flags & SERD_LIST_S_BEGIN)));
+ assert(!((flags & SERD_ANON_O_BEGIN) && (flags & SERD_LIST_O_BEGIN)));
+
const SerdNode* const subject = serd_statement_subject(statement);
const SerdNode* const predicate = serd_statement_predicate(statement);
const SerdNode* const object = serd_statement_object(statement);
@@ -726,47 +775,48 @@ serd_writer_write_statement(SerdWriter* writer,
if ((graph && !serd_node_equals(graph, writer->context.graph)) ||
(!graph && ctx(writer, SERD_GRAPH))) {
- writer->indent = 0;
if (ctx(writer, SERD_SUBJECT)) {
write_sep(writer, SEP_END_S);
}
if (ctx(writer, SERD_GRAPH)) {
write_sep(writer, SEP_GRAPH_END);
}
+ if (!writer->empty) {
+ write_newline(writer); // Blank line between top level items
+ }
reset_context(writer, true);
if (graph) {
TRY(write_node(writer, graph, SERD_GRAPH, flags));
- ++writer->indent;
write_sep(writer, SEP_GRAPH_BEGIN);
serd_node_set(&writer->context.graph, graph);
}
}
- if ((flags & SERD_LIST_CONT)) {
+ if (writer->context.type == CTX_LIST) {
if (write_list_obj(writer, flags, predicate, object)) {
// Reached end of list
- if (--writer->list_depth == 0 && writer->list_subj) {
- reset_context(writer, false);
- serd_node_free(writer->context.subject);
- writer->context.subject = writer->list_subj;
- writer->list_subj = NULL;
- }
+ pop_context(writer);
return SERD_SUCCESS;
}
} else if (serd_node_equals(subject, writer->context.subject)) {
if (serd_node_equals(predicate, writer->context.predicate)) {
// Abbreviate S P
- if (!(flags & SERD_ANON_O_BEGIN)) {
+ if (!writer->context.indented_object &&
+ !(flags & (SERD_ANON_O_BEGIN | SERD_LIST_O_BEGIN))) {
++writer->indent;
+ writer->context.indented_object = true;
}
+
write_sep(writer, SEP_END_O);
write_node(writer, object, SERD_OBJECT, flags);
- if (!(flags & SERD_ANON_O_BEGIN)) {
- --writer->indent;
- }
} else {
// Abbreviate S
+ if (writer->context.indented_object && writer->indent > 0) {
+ --writer->indent;
+ writer->context.indented_object = false;
+ }
+
Sep sep = ctx(writer, SERD_PREDICATE) ? SEP_END_P : SEP_S_P;
write_sep(writer, sep);
write_pred(writer, flags, predicate);
@@ -774,22 +824,29 @@ serd_writer_write_statement(SerdWriter* writer,
}
} else {
// No abbreviation
- if (ctx(writer, SERD_SUBJECT)) {
- assert(writer->indent > 0);
+ if (writer->context.indented_object && writer->indent > 0) {
--writer->indent;
- if (serd_stack_is_empty(&writer->anon_stack)) {
- write_sep(writer, SEP_END_S);
+ writer->context.indented_object = false;
+ }
+
+ if (serd_stack_is_empty(&writer->anon_stack)) {
+ if (ctx(writer, SERD_SUBJECT)) {
+ write_sep(writer, SEP_END_S); // Terminate last subject
+ }
+ if (!writer->empty) {
+ write_newline(writer); // Blank line between top level items
}
- } else if (!writer->empty) {
- write_sep(writer, SEP_S_P);
}
- if (!(flags & SERD_ANON_CONT)) {
+ if (serd_stack_is_empty(&writer->anon_stack)) {
write_node(writer, subject, SERD_SUBJECT, flags);
- ++writer->indent;
- write_sep(writer, SEP_S_P);
+ if (!(flags & (SERD_ANON_S_BEGIN | SERD_LIST_S_BEGIN))) {
+ write_sep(writer, SEP_S_P);
+ } else if (flags & SERD_ANON_S_BEGIN) {
+ write_sep(writer, SEP_ANON_S_P);
+ }
} else {
- ++writer->indent;
+ write_sep(writer, SEP_ANON_S_P);
}
reset_context(writer, false);
@@ -802,20 +859,33 @@ serd_writer_write_statement(SerdWriter* writer,
write_node(writer, object, SERD_OBJECT, flags);
}
- if (flags & (SERD_ANON_S_BEGIN|SERD_ANON_O_BEGIN)) {
- WriteContext* ctx = (WriteContext*)serd_stack_push(
- &writer->anon_stack, sizeof(WriteContext));
- if (!ctx) {
- return SERD_ERR_OVERFLOW;
- }
+ if (flags & SERD_LIST_S_BEGIN) {
+ const WriteContext ctx = {CTX_LIST,
+ serd_node_copy(graph),
+ serd_node_copy(subject),
+ NULL,
+ false};
+ push_context(writer, ctx);
+ } if (flags & SERD_LIST_O_BEGIN) {
+ const WriteContext ctx = {CTX_LIST,
+ serd_node_copy(graph),
+ serd_node_copy(object),
+ NULL,
+ false};
+ push_context(writer, ctx);
+ }
- *ctx = writer->context;
- WriteContext new_context = {
- serd_node_copy(graph), serd_node_copy(subject), NULL };
- if ((flags & SERD_ANON_S_BEGIN)) {
- new_context.predicate = serd_node_copy(predicate);
- }
- writer->context = new_context;
+ if (flags & (SERD_ANON_S_BEGIN | SERD_ANON_O_BEGIN)) {
+ const bool is_list = flags & (SERD_LIST_S_BEGIN | SERD_LIST_O_BEGIN);
+ const bool is_subject = flags & SERD_ANON_S_BEGIN;
+
+ const WriteContext ctx = {is_list ? CTX_LIST : CTX_BLANK,
+ serd_node_copy(graph),
+ serd_node_copy(subject),
+ is_subject ? serd_node_copy(predicate) : NULL,
+ false};
+
+ push_context(writer, ctx);
} else {
serd_node_set(&writer->context.graph, graph);
serd_node_set(&writer->context.subject, subject);
@@ -831,21 +901,19 @@ serd_writer_end_anon(SerdWriter* writer,
{
if (writer->syntax == SERD_NTRIPLES || writer->syntax == SERD_NQUADS) {
return SERD_SUCCESS;
- }
- if (serd_stack_is_empty(&writer->anon_stack) || writer->indent == 0) {
+ } else if (serd_stack_is_empty(&writer->anon_stack)) {
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);
- 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);
+ pop_context(writer);
+
+ if (serd_node_equals(node, writer->context.subject)) {
+ // Now-finished anonymous node is the new subject with no other context
memset(writer->context.predicate, 0, sizeof(SerdNode));
}
+
return SERD_SUCCESS;
}
@@ -884,7 +952,6 @@ serd_writer_new(SerdWorld* world,
writer->write_func = write_func;
writer->stream = stream;
writer->context = context;
- writer->list_subj = NULL;
writer->empty = true;
writer->iface.handle = writer;