aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-11-25 18:08:54 +0100
committerDavid Robillard <d@drobilla.net>2019-01-05 17:12:38 +0100
commitd4e594b0825391db76d72fbf092451d86ebe66c2 (patch)
tree948e8d2fc87b7275fe9b54715123d36cb6b4353c
parentd493841ad1ae145db53b04d4d8eeb21fe6a2a438 (diff)
downloadserd-d4e594b0825391db76d72fbf092451d86ebe66c2.tar.gz
serd-d4e594b0825391db76d72fbf092451d86ebe66c2.tar.bz2
serd-d4e594b0825391db76d72fbf092451d86ebe66c2.zip
Remove reader status and handle errors more precisely
-rw-r--r--src/byte_source.h5
-rw-r--r--src/n3.c53
-rw-r--r--src/reader.c13
-rw-r--r--src/reader.h8
4 files changed, 36 insertions, 43 deletions
diff --git a/src/byte_source.h b/src/byte_source.h
index f88fa6c9..4dfff70f 100644
--- a/src/byte_source.h
+++ b/src/byte_source.h
@@ -87,13 +87,14 @@ serd_byte_source_advance(SerdByteSource* source)
}
if (source->from_stream) {
- source->eof = false;
if (source->page_size > 1) {
if (++source->read_head == source->page_size) {
+ source->eof = false;
st = serd_byte_source_page(source);
}
} else {
if (!source->read_func(&source->read_byte, 1, 1, source->stream)) {
+ source->eof = true;
st = source->error_func(source->stream) ? SERD_ERR_UNKNOWN
: SERD_FAILURE;
}
@@ -102,7 +103,7 @@ serd_byte_source_advance(SerdByteSource* source)
++source->read_head; // Move to next character in string
}
- return source->eof ? SERD_FAILURE : st;
+ return st ? st : source->eof ? SERD_FAILURE : SERD_SUCCESS;
}
#endif // SERD_BYTE_SOURCE_H
diff --git a/src/n3.c b/src/n3.c
index 85fff66a..bb617d8c 100644
--- a/src/n3.c
+++ b/src/n3.c
@@ -197,9 +197,10 @@ read_utf8_character(SerdReader* reader, SerdNode* dest, uint8_t c)
uint32_t size;
uint8_t bytes[4];
SerdStatus st = read_utf8_bytes(reader, bytes, &size, c);
- if (st) {
- push_bytes(reader, dest, replacement_char, 3);
+ if (st && reader->strict) {
return st;
+ } else if (st) {
+ return push_bytes(reader, dest, replacement_char, 3);
}
return push_bytes(reader, dest, bytes, size);
@@ -211,9 +212,10 @@ read_utf8_code(SerdReader* reader, SerdNode* dest, uint32_t* code, uint8_t c)
uint32_t size;
uint8_t bytes[4] = { 0, 0, 0, 0 };
SerdStatus st = read_utf8_bytes(reader, bytes, &size, c);
- if (st) {
- push_bytes(reader, dest, replacement_char, 3);
+ if (st && reader->strict) {
return st;
+ } else if (st) {
+ return push_bytes(reader, dest, replacement_char, 3);
}
if (!(st = push_bytes(reader, dest, bytes, size))) {
@@ -301,7 +303,7 @@ static SerdStatus
read_STRING_LITERAL_LONG(SerdReader* reader, SerdNode* dest, uint8_t q)
{
SerdStatus st = SERD_SUCCESS;
- while (!reader->status) {
+ while (!st) {
const uint8_t c = peek_byte(reader);
if (c == '\\') {
eat_byte_safe(reader, c);
@@ -314,6 +316,9 @@ read_STRING_LITERAL_LONG(SerdReader* reader, SerdNode* dest, uint8_t q)
peek_byte(reader));
return SERD_ERR_BAD_SYNTAX;
}
+ } else if (reader->source.eof) {
+ st = r_err(reader, SERD_ERR_BAD_SYNTAX,
+ "unexpected end of file\n");
} else if (c == q) {
eat_byte_safe(reader, q);
const uint8_t q2 = eat_byte_safe(reader, peek_byte(reader));
@@ -330,6 +335,7 @@ read_STRING_LITERAL_LONG(SerdReader* reader, SerdNode* dest, uint8_t q)
reader, dest, &dest->flags, eat_byte_safe(reader, c));
}
}
+
return st;
}
@@ -339,24 +345,25 @@ static SerdStatus
read_STRING_LITERAL(SerdReader* reader, SerdNode* dest, uint8_t q)
{
SerdStatus st = SERD_SUCCESS;
- while (!reader->status) {
+ while (!st) {
const uint8_t c = peek_byte(reader);
uint32_t code = 0;
switch (c) {
case '\n': case '\r':
- r_err(reader, SERD_ERR_BAD_SYNTAX, "line end in short string\n");
- return SERD_ERR_BAD_SYNTAX;
+ st = r_err(reader, SERD_ERR_BAD_SYNTAX,
+ "line end in short string\n");
+ break;
case '\\':
eat_byte_safe(reader, c);
if ((st = read_ECHAR(reader, dest, &dest->flags)) &&
(st = read_UCHAR(reader, dest, &code))) {
- return r_err(reader, st,
- "invalid escape `\\%c'\n", peek_byte(reader));
+ st = r_err(reader, st,
+ "invalid escape `\\%c'\n", peek_byte(reader));
}
break;
default:
if (c == q) {
- eat_byte_check(reader, q);
+ eat_byte_safe(reader, q);
return SERD_SUCCESS;
} else {
st = read_character(
@@ -365,8 +372,7 @@ read_STRING_LITERAL(SerdReader* reader, SerdNode* dest, uint8_t q)
}
}
- return st ? st
- : eat_byte_check(reader, q) ? SERD_SUCCESS : SERD_ERR_BAD_SYNTAX;
+ return st;
}
static SerdStatus
@@ -669,10 +675,7 @@ read_IRIREF(SerdReader* reader, SerdNode** dest)
} else if (!(c & 0x80)) {
push_byte(reader, *dest, c);
} else if (read_utf8_character(reader, *dest, c)) {
- if (reader->strict) {
- return SERD_ERR_BAD_SYNTAX;
- }
- st = SERD_FAILURE;
+ st = reader->strict ? SERD_ERR_BAD_SYNTAX : SERD_FAILURE;
}
}
}
@@ -1450,8 +1453,6 @@ read_n3_statement(SerdReader* reader)
ctx.subject = subj = 0;
TRY(st, read_wrappedGraph(reader, &ctx));
read_ws_star(reader);
- } else if (!ctx.subject) {
- st = r_err(reader, SERD_ERR_BAD_SYNTAX, "bad subject\n");
} else if ((st = read_triples(reader, ctx, &ate_dot))) {
if (st == SERD_FAILURE && s_type == '[') {
return SERD_SUCCESS;
@@ -1486,23 +1487,22 @@ read_turtleTrigDoc(SerdReader* reader)
const size_t orig_stack_size = reader->stack.size;
const SerdStatus st = read_n3_statement(reader);
if (st > SERD_FAILURE) {
- if (reader->strict) {
+ if (reader->strict || reader->source.eof) {
serd_stack_pop_to(&reader->stack, orig_stack_size);
return st;
}
skip_until(reader, '\n');
- reader->status = SERD_SUCCESS;
}
serd_stack_pop_to(&reader->stack, orig_stack_size);
}
- return reader->status;
+ return SERD_SUCCESS;
}
SerdStatus
read_nquadsDoc(SerdReader* reader)
{
SerdStatus st = SERD_SUCCESS;
- while (!reader->source.eof) {
+ while (!st && !reader->source.eof) {
const size_t orig_stack_size = reader->stack.size;
SerdStatementFlags flags = 0;
@@ -1547,11 +1547,8 @@ read_nquadsDoc(SerdReader* reader)
}
}
- if (emit_statement(reader, ctx, ctx.object)) {
- break;
- }
-
+ st = emit_statement(reader, ctx, ctx.object);
serd_stack_pop_to(&reader->stack, orig_stack_size);
}
- return reader->status;
+ return st;
}
diff --git a/src/reader.c b/src/reader.c
index ab6b3611..6113a208 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -88,7 +88,6 @@ push_node_padded(SerdReader* reader, size_t maxlen,
&reader->stack, sizeof(SerdNode) + maxlen + 1, sizeof(SerdNode));
if (!mem) {
- reader->status = SERD_ERR_OVERFLOW;
return NULL;
}
@@ -286,15 +285,15 @@ serd_reader_start_string(SerdReader* reader,
static SerdStatus
serd_reader_prepare(SerdReader* reader)
{
- reader->status = serd_byte_source_prepare(&reader->source);
- if (reader->status == SERD_SUCCESS) {
- reader->status = skip_bom(reader);
- } else if (reader->status == SERD_FAILURE) {
+ SerdStatus st = serd_byte_source_prepare(&reader->source);
+ if (st == SERD_SUCCESS) {
+ st = skip_bom(reader);
+ } else if (st == SERD_FAILURE) {
reader->source.eof = true;
} else {
- r_err(reader, reader->status, "read error: %s\n", strerror(errno));
+ r_err(reader, st, "read error: %s\n", strerror(errno));
}
- return reader->status;
+ return st;
}
SerdStatus
diff --git a/src/reader.h b/src/reader.h
index 8c0efe3a..f7413ce8 100644
--- a/src/reader.h
+++ b/src/reader.h
@@ -55,7 +55,6 @@ struct SerdReaderImpl {
SerdStack stack;
SerdSyntax syntax;
unsigned next_id;
- SerdStatus status;
uint8_t* buf;
char* bprefix;
size_t bprefix_len;
@@ -99,11 +98,8 @@ peek_byte(SerdReader* reader)
static inline uint8_t
eat_byte(SerdReader* reader)
{
- const uint8_t c = peek_byte(reader);
- const SerdStatus st = serd_byte_source_advance(&reader->source);
- if (st) {
- reader->status = st;
- }
+ const uint8_t c = peek_byte(reader);
+ serd_byte_source_advance(&reader->source);
return c;
}