From c311a4b59da70d5f9b790b7a817770bfb49b036a Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 3 May 2023 15:38:58 -0400 Subject: Factor out read_nquads_statement() --- src/n3.c | 96 ++++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 54 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/n3.c b/src/n3.c index 011f57fa..c92059a5 100644 --- a/src/n3.c +++ b/src/n3.c @@ -1723,60 +1723,72 @@ read_turtleTrigDoc(SerdReader* const reader) return SERD_SUCCESS; } -SerdStatus -read_nquadsDoc(SerdReader* const reader) +static SerdStatus +read_nquads_statement(SerdReader* const reader) { - SerdStatus st = SERD_SUCCESS; - while (!reader->source.eof) { - SerdStatementFlags flags = 0; - ReadContext ctx = {0, 0, 0, 0, 0, 0, &flags}; - bool ate_dot = false; - int s_type = 0; + SerdStatus st = SERD_SUCCESS; + SerdStatementFlags flags = 0; + ReadContext ctx = {0, 0, 0, 0, 0, 0, &flags}; + bool ate_dot = false; + int s_type = 0; + + read_ws_star(reader); + if (peek_byte(reader) == EOF) { + return SERD_FAILURE; + } + + if (peek_byte(reader) == '@') { + return r_err( + reader, SERD_ERR_BAD_SYNTAX, "syntax does not support directives\n"); + } + + // subject predicate object + if ((st = read_subject(reader, ctx, &ctx.subject, &s_type)) || + !read_ws_star(reader) || (st = read_IRIREF(reader, &ctx.predicate)) || + !read_ws_star(reader) || + (st = read_object(reader, &ctx, false, &ate_dot))) { + return st; + } + + if (!ate_dot) { // graphLabel? read_ws_star(reader); - if (peek_byte(reader) == EOF) { + switch (peek_byte(reader)) { + case '.': break; + case '_': + TRY(st, read_BLANK_NODE_LABEL(reader, &ctx.graph, &ate_dot)); + break; + default: + TRY(st, read_IRIREF(reader, &ctx.graph)); } - if (peek_byte(reader) == '@') { - return r_err( - reader, SERD_ERR_BAD_SYNTAX, "syntax does not support directives\n"); + // Terminating '.' + read_ws_star(reader); + if (!eat_byte_check(reader, '.')) { + return SERD_ERR_BAD_SYNTAX; } + } - // subject predicate object - if ((st = read_subject(reader, ctx, &ctx.subject, &s_type)) || - !read_ws_star(reader) || (st = read_IRIREF(reader, &ctx.predicate)) || - !read_ws_star(reader) || - (st = read_object(reader, &ctx, false, &ate_dot))) { - return st; - } + TRY(st, emit_statement(reader, ctx, ctx.object, ctx.datatype, ctx.lang)); - if (!ate_dot) { // graphLabel? - read_ws_star(reader); - switch (peek_byte(reader)) { - case '.': - break; - case '_': - TRY(st, read_BLANK_NODE_LABEL(reader, &ctx.graph, &ate_dot)); - break; - default: - TRY(st, read_IRIREF(reader, &ctx.graph)); - } + pop_node(reader, ctx.graph); + pop_node(reader, ctx.lang); + pop_node(reader, ctx.datatype); + pop_node(reader, ctx.object); - // Terminating '.' - read_ws_star(reader); - if (!eat_byte_check(reader, '.')) { - return SERD_ERR_BAD_SYNTAX; - } - } + return SERD_SUCCESS; +} - TRY(st, emit_statement(reader, ctx, ctx.object, ctx.datatype, ctx.lang)); +SerdStatus +read_nquadsDoc(SerdReader* const reader) +{ + SerdStatus st = SERD_SUCCESS; - pop_node(reader, ctx.graph); - pop_node(reader, ctx.lang); - pop_node(reader, ctx.datatype); - pop_node(reader, ctx.object); + while (!reader->source.eof && !st) { + st = read_nquads_statement(reader); } - return SERD_SUCCESS; + + return st; } #if defined(__clang__) && __clang_major__ >= 10 -- cgit v1.2.1