diff options
-rw-r--r-- | doc/conf.py.in | 2 | ||||
-rw-r--r-- | include/serd/serd.h | 79 | ||||
-rw-r--r-- | meson.build | 1 | ||||
-rw-r--r-- | src/byte_source.c | 27 | ||||
-rw-r--r-- | src/byte_source.h | 23 | ||||
-rw-r--r-- | src/caret.c | 75 | ||||
-rw-r--r-- | src/caret.h | 28 | ||||
-rw-r--r-- | src/reader.c | 30 | ||||
-rw-r--r-- | src/serdi.c | 12 | ||||
-rw-r--r-- | src/world.c | 14 | ||||
-rw-r--r-- | test/meson.build | 1 | ||||
-rw-r--r-- | test/test_caret.c | 60 | ||||
-rw-r--r-- | test/test_free_null.c | 1 | ||||
-rw-r--r-- | test/test_overflow.c | 2 | ||||
-rw-r--r-- | test/test_read_chunk.c | 3 | ||||
-rw-r--r-- | test/test_reader_writer.c | 3 |
16 files changed, 305 insertions, 56 deletions
diff --git a/doc/conf.py.in b/doc/conf.py.in index 4d5bf6b8..5fdf1220 100644 --- a/doc/conf.py.in +++ b/doc/conf.py.in @@ -23,7 +23,7 @@ _opaque = [ "FILE", "SerdByteSinkImpl", "SerdByteSourceImpl", - "SerdCursorImpl", + "SerdCaretImpl", "SerdEnvImpl", "SerdIterImpl", "SerdModelImpl", diff --git a/include/serd/serd.h b/include/serd/serd.h index 820af22c..611b0176 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -827,6 +827,68 @@ serd_node_equals(const SerdNode* SERD_NULLABLE a, /** @} + @defgroup serd_caret Caret + @{ +*/ + +/// The origin of a statement in a text document +typedef struct SerdCaretImpl SerdCaret; + +/** + Create a new caret. + + Note that, to minimise model overhead, the caret does not own the name + node, so `name` must have a longer lifetime than the caret for it to be + valid. That is, serd_caret_name() will return exactly the pointer + `name`, not a copy. + + @param name The name of the document or stream (usually a file URI) + @param line The line number in the document (1-based) + @param col The column number in the document (1-based) + @return A new caret that must be freed with serd_caret_free() +*/ +SERD_API +SerdCaret* SERD_ALLOCATED +serd_caret_new(const SerdNode* SERD_NONNULL name, unsigned line, unsigned col); + +/// Return a copy of `caret` +SERD_API +SerdCaret* SERD_ALLOCATED +serd_caret_copy(const SerdCaret* SERD_NULLABLE caret); + +/// Free `caret` +SERD_API +void +serd_caret_free(SerdCaret* SERD_NULLABLE caret); + +/// Return true iff `lhs` is equal to `rhs` +SERD_PURE_API +bool +serd_caret_equals(const SerdCaret* SERD_NULLABLE lhs, + const SerdCaret* SERD_NULLABLE rhs); + +/** + Return the document name. + + This is typically a file URI, but may be a descriptive string node for + statements that originate from streams. +*/ +SERD_PURE_API +const SerdNode* SERD_NONNULL +serd_caret_name(const SerdCaret* SERD_NONNULL caret); + +/// Return the one-relative line number in the document +SERD_PURE_API +unsigned +serd_caret_line(const SerdCaret* SERD_NONNULL caret); + +/// Return the zero-relative column number in the line +SERD_PURE_API +unsigned +serd_caret_column(const SerdCaret* SERD_NONNULL caret); + +/** + @} @defgroup serd_statement Statement @{ */ @@ -850,12 +912,10 @@ typedef struct SerdWorldImpl SerdWorld; /// An error description typedef struct { - SerdStatus status; ///< Error code - const char* SERD_NULLABLE filename; ///< File with error - unsigned line; ///< Line in file with error or 0 - unsigned col; ///< Column in file with error - const char* SERD_NONNULL fmt; ///< Printf-style format string - va_list* SERD_NONNULL args; ///< Arguments for fmt + SerdStatus status; ///< Error code + const SerdCaret* SERD_NULLABLE caret; ///< File origin of error + const char* SERD_NONNULL fmt; ///< Printf-style format string + va_list* SERD_NONNULL args; ///< Arguments for fmt } SerdError; /** @@ -1189,14 +1249,15 @@ serd_reader_start_stream(SerdReader* SERD_NONNULL reader, SerdReadFunc SERD_NONNULL read_func, SerdStreamErrorFunc SERD_NONNULL error_func, void* SERD_NONNULL stream, - const char* SERD_NULLABLE name, + const SerdNode* SERD_NULLABLE name, size_t page_size); /// Prepare to read from a string SERD_API SerdStatus -serd_reader_start_string(SerdReader* SERD_NONNULL reader, - const char* SERD_NONNULL utf8); +serd_reader_start_string(SerdReader* SERD_NONNULL reader, + const char* SERD_NONNULL utf8, + const SerdNode* SERD_NULLABLE name); /** Read a single "chunk" of data during an incremental read. diff --git a/meson.build b/meson.build index 8b10ac2e..55894241 100644 --- a/meson.build +++ b/meson.build @@ -87,6 +87,7 @@ c_header = files('include/serd/serd.h') sources = [ 'src/byte_source.c', + 'src/caret.c', 'src/env.c', 'src/n3.c', 'src/node.c', diff --git a/src/byte_source.c b/src/byte_source.c index 727c655f..0e6ae2be 100644 --- a/src/byte_source.c +++ b/src/byte_source.c @@ -16,6 +16,7 @@ #include "byte_source.h" +#include "caret.h" #include "system.h" #include "serd/serd.h" @@ -52,11 +53,9 @@ serd_byte_source_open_source(SerdByteSource* const source, const SerdStreamErrorFunc error_func, const SerdStreamCloseFunc close_func, void* const stream, - const char* const name, + const SerdNode* const name, const size_t page_size) { - const Cursor cur = {name, 1, 1}; - memset(source, '\0', sizeof(*source)); source->read_func = read_func; source->error_func = error_func; @@ -64,7 +63,10 @@ serd_byte_source_open_source(SerdByteSource* const source, source->stream = stream; source->page_size = page_size; source->buf_size = page_size; - source->cur = cur; + source->name = serd_node_copy(name); + source->caret.file = source->name; + source->caret.line = 1u; + source->caret.col = 1u; source->from_stream = true; if (page_size > 1) { @@ -93,13 +95,19 @@ serd_byte_source_prepare(SerdByteSource* const source) SerdStatus serd_byte_source_open_string(SerdByteSource* const source, - const char* const utf8) + const char* const utf8, + const SerdNode* const name) { - const Cursor cur = {"(string)", 1, 1}; - memset(source, '\0', sizeof(*source)); - source->cur = cur; - source->read_buf = (const uint8_t*)utf8; + + source->name = + name ? serd_node_copy(name) : serd_new_string(SERD_STRING("string")); + + source->read_buf = (const uint8_t*)utf8; + source->caret.file = source->name; + source->caret.line = 1u; + source->caret.col = 1u; + return SERD_SUCCESS; } @@ -115,6 +123,7 @@ serd_byte_source_close(SerdByteSource* const source) serd_free_aligned(source->file_buf); } + serd_node_free(source->name); memset(source, '\0', sizeof(*source)); return st; } diff --git a/src/byte_source.h b/src/byte_source.h index 69f92295..6469ba95 100644 --- a/src/byte_source.h +++ b/src/byte_source.h @@ -17,6 +17,8 @@ #ifndef SERD_BYTE_SOURCE_H #define SERD_BYTE_SOURCE_H +#include "caret.h" + #include "serd/serd.h" #include <assert.h> @@ -27,19 +29,14 @@ typedef int (*SerdStreamCloseFunc)(void*); typedef struct { - const char* filename; - unsigned line; - unsigned col; -} Cursor; - -typedef struct { SerdReadFunc read_func; ///< Read function (e.g. fread) SerdStreamErrorFunc error_func; ///< Error function (e.g. ferror) SerdStreamCloseFunc close_func; ///< Function for closing stream void* stream; ///< Stream (e.g. FILE) size_t page_size; ///< Number of bytes to read at a time size_t buf_size; ///< Number of bytes in file_buf - Cursor cur; ///< Cursor for error reporting + SerdNode* name; ///< Name of stream (referenced by cur) + SerdCaret caret; ///< Caret for error reporting uint8_t* file_buf; ///< Buffer iff reading pages from a file const uint8_t* read_buf; ///< Pointer to file_buf or read_byte size_t read_head; ///< Offset into read_buf @@ -50,7 +47,9 @@ typedef struct { } SerdByteSource; SerdStatus -serd_byte_source_open_string(SerdByteSource* source, const char* utf8); +serd_byte_source_open_string(SerdByteSource* source, + const char* utf8, + const SerdNode* name); SerdStatus serd_byte_source_open_source(SerdByteSource* source, @@ -58,7 +57,7 @@ serd_byte_source_open_source(SerdByteSource* source, SerdStreamErrorFunc error_func, SerdStreamCloseFunc close_func, void* stream, - const char* name, + const SerdNode* name, size_t page_size); SerdStatus @@ -85,11 +84,11 @@ serd_byte_source_advance(SerdByteSource* source) switch (serd_byte_source_peek(source)) { case '\n': - ++source->cur.line; - source->cur.col = 0; + ++source->caret.line; + source->caret.col = 0; break; default: - ++source->cur.col; + ++source->caret.col; } const bool was_eof = source->eof; diff --git a/src/caret.c b/src/caret.c new file mode 100644 index 00000000..2fe92f94 --- /dev/null +++ b/src/caret.c @@ -0,0 +1,75 @@ +/* + Copyright 2018-2020 David Robillard <d@drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "caret.h" + +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> + +SerdCaret* +serd_caret_new(const SerdNode* name, unsigned line, unsigned col) +{ + SerdCaret* caret = (SerdCaret*)malloc(sizeof(SerdCaret)); + + caret->file = name; + caret->line = line; + caret->col = col; + return caret; +} + +SerdCaret* +serd_caret_copy(const SerdCaret* caret) +{ + if (!caret) { + return NULL; + } + + SerdCaret* copy = (SerdCaret*)malloc(sizeof(SerdCaret)); + memcpy(copy, caret, sizeof(SerdCaret)); + return copy; +} + +void +serd_caret_free(SerdCaret* caret) +{ + free(caret); +} + +bool +serd_caret_equals(const SerdCaret* l, const SerdCaret* r) +{ + return (l == r || (l && r && serd_node_equals(l->file, r->file) && + l->line == r->line && l->col == r->col)); +} + +const SerdNode* +serd_caret_name(const SerdCaret* caret) +{ + return caret->file; +} + +unsigned +serd_caret_line(const SerdCaret* caret) +{ + return caret->line; +} + +unsigned +serd_caret_column(const SerdCaret* caret) +{ + return caret->col; +} diff --git a/src/caret.h b/src/caret.h new file mode 100644 index 00000000..3add78a7 --- /dev/null +++ b/src/caret.h @@ -0,0 +1,28 @@ +/* + Copyright 2018-2020 David Robillard <d@drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef SERD_CARET_H +#define SERD_CARET_H + +#include "serd/serd.h" + +struct SerdCaretImpl { + const SerdNode* file; + unsigned line; + unsigned col; +}; + +#endif // SERD_CARET_H diff --git a/src/reader.c b/src/reader.c index 2cdf18c6..9957e55c 100644 --- a/src/reader.c +++ b/src/reader.c @@ -37,8 +37,7 @@ r_err(SerdReader* const reader, const SerdStatus st, const char* const fmt, ...) { va_list args; va_start(args, fmt); - const Cursor* const cur = &reader->source.cur; - const SerdError e = {st, cur->filename, cur->line, cur->col, fmt, &args}; + const SerdError e = {st, &reader->source.caret, fmt, &args}; serd_world_error(reader->world, &e); va_end(args); return st; @@ -246,7 +245,7 @@ serd_reader_start_stream(SerdReader* const reader, const SerdReadFunc read_func, const SerdStreamErrorFunc error_func, void* const stream, - const char* const name, + const SerdNode* const name, const size_t page_size) { return serd_byte_source_open_source( @@ -267,20 +266,25 @@ serd_reader_start_file(SerdReader* reader, const char* uri, bool bulk) return SERD_ERR_UNKNOWN; } - return serd_byte_source_open_source(&reader->source, - bulk ? (SerdReadFunc)fread - : serd_file_read_byte, - (SerdStreamErrorFunc)ferror, - (SerdStreamCloseFunc)fclose, - fd, - uri, - bulk ? SERD_PAGE_SIZE : 1); + SerdNode* const name = serd_new_uri(SERD_STRING(uri)); + const SerdStatus st = serd_byte_source_open_source( + &reader->source, + bulk ? (SerdReadFunc)fread : serd_file_read_byte, + (SerdStreamErrorFunc)ferror, + (SerdStreamCloseFunc)fclose, + fd, + name, + bulk ? SERD_PAGE_SIZE : 1u); + serd_node_free(name); + return st; } SerdStatus -serd_reader_start_string(SerdReader* const reader, const char* const utf8) +serd_reader_start_string(SerdReader* const reader, + const char* const utf8, + const SerdNode* const name) { - return serd_byte_source_open_string(&reader->source, utf8); + return serd_byte_source_open_string(&reader->source, utf8, name); } static SerdStatus diff --git a/src/serdi.c b/src/serdi.c index fcddac6c..a94051bc 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -283,15 +283,18 @@ main(int argc, char** argv) serd_writer_chop_blank_prefix(writer, chop_prefix); serd_reader_add_blank_prefix(reader, add_prefix); - SerdStatus st = SERD_SUCCESS; + SerdStatus st = SERD_SUCCESS; + SerdNode* input_name = NULL; if (from_string) { - st = serd_reader_start_string(reader, input); + input_name = serd_new_string(SERD_STRING("string")); + st = serd_reader_start_string(reader, input, input_name); } else if (from_stdin) { - st = serd_reader_start_stream(reader, + input_name = serd_new_string(SERD_STRING("stdin")); + st = serd_reader_start_stream(reader, serd_file_read_byte, (SerdStreamErrorFunc)ferror, stdin, - "(stdin)", + input_name, 1); } else { st = serd_reader_start_file(reader, input, bulk_read); @@ -305,6 +308,7 @@ main(int argc, char** argv) serd_reader_free(reader); serd_writer_finish(writer); serd_writer_free(writer); + serd_node_free(input_name); serd_env_free(env); serd_node_free(base); serd_world_free(world); diff --git a/src/world.c b/src/world.c index 4413f433..4230448a 100644 --- a/src/world.c +++ b/src/world.c @@ -16,6 +16,7 @@ #include "world.h" +#include "caret.h" #include "node.h" #include "serd_config.h" #include "system.h" @@ -58,10 +59,13 @@ 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: "); + fprintf(stderr, "error: "); + if (e->caret) { + fprintf(stderr, + "%s:%u:%u: ", + serd_node_string(e->caret->file), + e->caret->line, + e->caret->col); } vfprintf(stderr, e->fmt, *e->args); } @@ -76,7 +80,7 @@ serd_world_errorf(const SerdWorld* const world, { va_list args; va_start(args, fmt); - const SerdError e = {st, NULL, 0, 0, fmt, &args}; + const SerdError e = {st, NULL, fmt, &args}; serd_world_error(world, &e); va_end(args); return st; diff --git a/test/meson.build b/test/meson.build index 05701f42..692bafe4 100644 --- a/test/meson.build +++ b/test/meson.build @@ -5,6 +5,7 @@ run_test_suite = find_program('run_test_suite.py') wrapper = meson.get_cross_property('exe_wrapper', '') unit_tests = [ + 'caret', 'env', 'free_null', 'node', diff --git a/test/test_caret.c b/test/test_caret.c new file mode 100644 index 00000000..3683a458 --- /dev/null +++ b/test/test_caret.c @@ -0,0 +1,60 @@ +/* + Copyright 2019-2020 David Robillard <d@drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#undef NDEBUG + +#include "serd/serd.h" + +#include <assert.h> +#include <stddef.h> + +int +main(void) +{ + SerdNode* const node = serd_new_string(SERD_STRING("node")); + SerdCaret* const caret = serd_caret_new(node, 46, 2); + + assert(serd_caret_equals(caret, caret)); + assert(serd_caret_name(caret) == node); + assert(serd_caret_line(caret) == 46); + assert(serd_caret_column(caret) == 2); + + SerdCaret* const copy = serd_caret_copy(caret); + + assert(serd_caret_equals(caret, copy)); + assert(!serd_caret_copy(NULL)); + + SerdNode* const other_node = serd_new_string(SERD_STRING("other")); + SerdCaret* const other_file = serd_caret_new(other_node, 46, 2); + SerdCaret* const other_line = serd_caret_new(node, 47, 2); + SerdCaret* const other_col = serd_caret_new(node, 46, 3); + + assert(!serd_caret_equals(caret, other_file)); + assert(!serd_caret_equals(caret, other_line)); + assert(!serd_caret_equals(caret, other_col)); + assert(!serd_caret_equals(caret, NULL)); + assert(!serd_caret_equals(NULL, caret)); + + serd_caret_free(other_col); + serd_caret_free(other_line); + serd_caret_free(other_file); + serd_node_free(other_node); + serd_caret_free(copy); + serd_caret_free(caret); + serd_node_free(node); + + return 0; +} diff --git a/test/test_free_null.c b/test/test_free_null.c index 3977507c..0968f3fa 100644 --- a/test/test_free_null.c +++ b/test/test_free_null.c @@ -30,6 +30,7 @@ main(void) serd_sink_free(NULL); serd_reader_free(NULL); serd_writer_free(NULL); + serd_caret_free(NULL); return 0; } diff --git a/test/test_overflow.c b/test/test_overflow.c index a1f32a42..2d7c2542 100644 --- a/test/test_overflow.c +++ b/test/test_overflow.c @@ -34,7 +34,7 @@ test_size(SerdWorld* const world, SerdReader* const reader = serd_reader_new(world, syntax, sink, stack_size); assert(reader); - serd_reader_start_string(reader, str); + serd_reader_start_string(reader, str, NULL); const SerdStatus st = serd_reader_read_document(reader); serd_reader_free(reader); serd_sink_free(sink); diff --git a/test/test_read_chunk.c b/test/test_read_chunk.c index 602af540..db2c210a 100644 --- a/test/test_read_chunk.c +++ b/test/test_read_chunk.c @@ -98,7 +98,8 @@ main(void) "eg:s2 eg:p1 eg:o1 ;\n" " eg:p2 eg:o2 .\n" "eg:s3 eg:p1 eg:o1 .\n" - "eg:s4 eg:p1 [ eg:p3 eg:o1 ] .\n")); + "eg:s4 eg:p1 [ eg:p3 eg:o1 ] .\n", + NULL)); assert(!serd_reader_read_chunk(reader) && n_prefix == 1); assert(!serd_reader_read_chunk(reader) && n_base == 1); diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c index b741c1ae..9a592c3a 100644 --- a/test/test_reader_writer.c +++ b/test/test_reader_writer.c @@ -167,7 +167,8 @@ test_read_string(void) assert( !serd_reader_start_string(reader, "<http://example.org/s> <http://example.org/p> " - "<http://example.org/o> .")); + "<http://example.org/o> .", + NULL)); assert(!serd_reader_read_document(reader)); assert(n_statements == 1); |