diff options
author | David Robillard <d@drobilla.net> | 2018-11-25 18:08:54 +0100 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2019-01-05 17:12:38 +0100 |
commit | d4e594b0825391db76d72fbf092451d86ebe66c2 (patch) | |
tree | 948e8d2fc87b7275fe9b54715123d36cb6b4353c /src/n3.c | |
parent | d493841ad1ae145db53b04d4d8eeb21fe6a2a438 (diff) | |
download | serd-d4e594b0825391db76d72fbf092451d86ebe66c2.tar.gz serd-d4e594b0825391db76d72fbf092451d86ebe66c2.tar.bz2 serd-d4e594b0825391db76d72fbf092451d86ebe66c2.zip |
Remove reader status and handle errors more precisely
Diffstat (limited to 'src/n3.c')
-rw-r--r-- | src/n3.c | 53 |
1 files changed, 25 insertions, 28 deletions
@@ -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; } |