diff options
author | David Robillard <d@drobilla.net> | 2018-06-03 22:15:53 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-10-27 13:13:58 +0100 |
commit | 6a91bfca72fc2cfd7ba1002174475d71e35b2969 (patch) | |
tree | d1689b03faf96f58ac4c7ca9081a600491d09a02 | |
parent | 93e54363f5ae251acee94051f77305f60f0158c8 (diff) | |
download | serd-6a91bfca72fc2cfd7ba1002174475d71e35b2969.tar.gz serd-6a91bfca72fc2cfd7ba1002174475d71e35b2969.tar.bz2 serd-6a91bfca72fc2cfd7ba1002174475d71e35b2969.zip |
Add SerdCursor to public API
-rw-r--r-- | serd/serd.h | 85 | ||||
-rw-r--r-- | src/byte_source.c | 23 | ||||
-rw-r--r-- | src/byte_source.h | 17 | ||||
-rw-r--r-- | src/cursor.c | 75 | ||||
-rw-r--r-- | src/cursor.h | 28 | ||||
-rw-r--r-- | src/reader.c | 18 | ||||
-rw-r--r-- | src/serdi.c | 19 | ||||
-rw-r--r-- | src/world.c | 14 | ||||
-rw-r--r-- | tests/cursor_test.c | 59 | ||||
-rw-r--r-- | tests/free_null_test.c | 1 | ||||
-rw-r--r-- | tests/read_chunk_test.c | 3 | ||||
-rw-r--r-- | tests/serd_test.c | 3 | ||||
-rw-r--r-- | wscript | 3 |
13 files changed, 301 insertions, 47 deletions
diff --git a/serd/serd.h b/serd/serd.h index 1f30065b..1cc68f82 100644 --- a/serd/serd.h +++ b/serd/serd.h @@ -72,6 +72,11 @@ typedef struct SerdWorldImpl SerdWorld; typedef struct SerdStatementImpl SerdStatement; /** + Cursor, the origin of a statement in a document. +*/ +typedef struct SerdCursorImpl SerdCursor; + +/** Environment. Represents the state required to resolve a CURIE or relative URI, e.g. the @@ -258,12 +263,10 @@ typedef struct { An error description. */ typedef struct { - SerdStatus status; /**< Error code */ - const char* filename; /**< File where error was encountered, or NULL */ - unsigned line; /**< Line where error was encountered, or 0 */ - unsigned col; /**< Column where error was encountered */ - const char* fmt; /**< Message format string (printf style) */ - va_list* args; /**< Arguments for fmt */ + SerdStatus status; /**< Error code */ + const SerdCursor* cursor; /**< Origin of error, or NULL */ + const char* fmt; /**< Message format string (printf style) */ + va_list* args; /**< Arguments for fmt */ } SerdError; /** @@ -1110,7 +1113,7 @@ serd_reader_start_stream(SerdReader* reader, SerdReadFunc read_func, SerdStreamErrorFunc error_func, void* stream, - const char* name, + const SerdNode* name, size_t page_size); /** @@ -1118,7 +1121,9 @@ serd_reader_start_stream(SerdReader* reader, */ SERD_API SerdStatus -serd_reader_start_string(SerdReader* reader, const char* utf8); +serd_reader_start_string(SerdReader* reader, + const char* utf8, + const SerdNode* name); /** Read a single "chunk" of data during an incremental read. @@ -1301,6 +1306,70 @@ serd_statement_graph(const SerdStatement* statement); /** @} + @name Cursor + @{ +*/ + +/** + Create a new cursor + + Note that, to minimise model overhead, the cursor does not own the name + node, so `name` must have a longer lifetime than the cursor for it to be + valid. That is, serd_cursor_name() will return exactly the pointer + `name`, not a copy. For cursors from models, this is the lifetime of the + model. For user-created cursors, the simplest way to handle this is to use + `SerdNodes`. + + @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 cursor that must be freed with serd_cursor_free() +*/ +SERD_API +SerdCursor* +serd_cursor_new(const SerdNode* name, unsigned line, unsigned col); + +/// Return a copy of `cursor` +SERD_API +SerdCursor* +serd_cursor_copy(const SerdCursor* cursor); + +/// Free `cursor` +SERD_API +void +serd_cursor_free(SerdCursor* cursor); + +/// Return true iff `lhs` is equal to `rhs` +SERD_API +bool +serd_cursor_equals(const SerdCursor* lhs, const SerdCursor* 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_API +const SerdNode* +serd_cursor_name(const SerdCursor* cursor); + +/** + Return the one-relative line number in the document. +*/ +SERD_API +unsigned +serd_cursor_line(const SerdCursor* cursor); + +/** + Return the zero-relative column number in the line. +*/ +SERD_API +unsigned +serd_cursor_column(const SerdCursor* cursor); + +/** + @} @} */ diff --git a/src/byte_source.c b/src/byte_source.c index fce0fadb..0cfdc0bd 100644 --- a/src/byte_source.c +++ b/src/byte_source.c @@ -49,11 +49,9 @@ 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) { - const Cursor cur = { name, 1, 1 }; - memset(source, '\0', sizeof(*source)); source->read_func = read_func; source->error_func = error_func; @@ -61,9 +59,12 @@ serd_byte_source_open_source(SerdByteSource* source, source->stream = stream; source->page_size = page_size; source->buf_size = page_size; - source->cur = cur; + source->name = serd_node_copy(name); source->from_stream = true; + const SerdCursor cur = { source->name, 1, 1 }; + source->cur = cur; + if (page_size > 1) { source->file_buf = (uint8_t*)serd_bufalloc(page_size); source->read_buf = source->file_buf; @@ -90,13 +91,18 @@ serd_byte_source_prepare(SerdByteSource* source) } SerdStatus -serd_byte_source_open_string(SerdByteSource* source, const char* utf8) +serd_byte_source_open_string(SerdByteSource* source, + const char* utf8, + const SerdNode* name) { - const Cursor cur = { "(string)", 1, 1 }; - memset(source, '\0', sizeof(*source)); - source->cur = cur; + + source->name = name ? serd_node_copy(name) : serd_new_string("string"); source->read_buf = (const uint8_t*)utf8; + + const SerdCursor cur = {source->name, 1, 1}; + source->cur = cur; + return SERD_SUCCESS; } @@ -111,6 +117,7 @@ serd_byte_source_close(SerdByteSource* source) if (source->page_size > 1) { free(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 680fd1c5..6da9df5d 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 "cursor.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) + SerdCursor cur; ///< Cursor 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 diff --git a/src/cursor.c b/src/cursor.c new file mode 100644 index 00000000..0ee84446 --- /dev/null +++ b/src/cursor.c @@ -0,0 +1,75 @@ +/* + Copyright 2018-2020 David Robillard <http://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 "cursor.h" + +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> + +SerdCursor* +serd_cursor_new(const SerdNode* name, unsigned line, unsigned col) +{ + SerdCursor* cursor = (SerdCursor*)malloc(sizeof(SerdCursor)); + + cursor->file = name; + cursor->line = line; + cursor->col = col; + return cursor; +} + +SerdCursor* +serd_cursor_copy(const SerdCursor* cursor) +{ + if (!cursor) { + return NULL; + } + + SerdCursor* copy = (SerdCursor*)malloc(sizeof(SerdCursor)); + memcpy(copy, cursor, sizeof(SerdCursor)); + return copy; +} + +void +serd_cursor_free(SerdCursor* cursor) +{ + free(cursor); +} + +bool +serd_cursor_equals(const SerdCursor* l, const SerdCursor* r) +{ + return (l == r || (l && r && serd_node_equals(l->file, r->file) && + l->line == r->line && l->col == r->col)); +} + +const SerdNode* +serd_cursor_name(const SerdCursor* cursor) +{ + return cursor->file; +} + +unsigned +serd_cursor_line(const SerdCursor* cursor) +{ + return cursor->line; +} + +unsigned +serd_cursor_column(const SerdCursor* cursor) +{ + return cursor->col; +} diff --git a/src/cursor.h b/src/cursor.h new file mode 100644 index 00000000..015615bd --- /dev/null +++ b/src/cursor.h @@ -0,0 +1,28 @@ +/* + Copyright 2018-2020 David Robillard <http://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_CURSOR_H +#define SERD_CURSOR_H + +#include "serd/serd.h" + +struct SerdCursorImpl { + const SerdNode* file; + unsigned line; + unsigned col; +}; + +#endif // SERD_CURSOR_H diff --git a/src/reader.c b/src/reader.c index 1bca81ac..e57f5bd9 100644 --- a/src/reader.c +++ b/src/reader.c @@ -34,8 +34,7 @@ r_err(SerdReader* reader, SerdStatus st, const char* 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.cur, fmt, &args }; serd_world_error(reader->world, &e); va_end(args); return st; @@ -244,7 +243,7 @@ serd_reader_start_stream(SerdReader* reader, SerdReadFunc read_func, SerdStreamErrorFunc error_func, void* stream, - const char* name, + const SerdNode* name, size_t page_size) { return serd_byte_source_open_source( @@ -265,20 +264,25 @@ serd_reader_start_file(SerdReader* reader, const char* uri, bool bulk) return SERD_ERR_UNKNOWN; } - return serd_byte_source_open_source( + SerdNode* const name = serd_new_uri(uri); + const SerdStatus st = serd_byte_source_open_source( &reader->source, bulk ? (SerdReadFunc)fread : serd_file_read_byte, (SerdStreamErrorFunc)ferror, (SerdStreamCloseFunc)fclose, fd, - uri, + name, bulk ? SERD_PAGE_SIZE : 1); + serd_node_free(name); + return st; } SerdStatus -serd_reader_start_string(SerdReader* reader, const char* utf8) +serd_reader_start_string(SerdReader* reader, + const char* utf8, + const SerdNode* 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 14eb04c5..fc3efc04 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -246,16 +246,19 @@ main(int argc, char** argv) serd_reader_add_blank_prefix(reader, add_prefix); serd_node_free(root); - 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("string"); + st = serd_reader_start_string(reader, input, input_name); } else if (from_stdin) { - st = serd_reader_start_stream(reader, - serd_file_read_byte, - (SerdStreamErrorFunc)ferror, - stdin, - "(stdin)", - 1); + input_name = serd_new_string("stdin"); + st = serd_reader_start_stream(reader, + serd_file_read_byte, + (SerdStreamErrorFunc)ferror, + stdin, + input_name, + 1); } else { st = serd_reader_start_file(reader, input, bulk_read); } diff --git a/src/world.c b/src/world.c index a99bff8a..3d632f05 100644 --- a/src/world.c +++ b/src/world.c @@ -18,6 +18,7 @@ #include "world.h" +#include "cursor.h" #include "node.h" #include "serd_config.h" @@ -55,10 +56,13 @@ serd_world_error(const SerdWorld* world, const SerdError* e) if (world->error_sink) { world->error_sink(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->cursor) { + fprintf(stderr, + "%s:%u:%u: ", + serd_node_string(e->cursor->file), + e->cursor->line, + e->cursor->col); } vfprintf(stderr, e->fmt, *e->args); } @@ -70,7 +74,7 @@ serd_world_errorf(const SerdWorld* world, SerdStatus st, const char* fmt, ...) { 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/tests/cursor_test.c b/tests/cursor_test.c new file mode 100644 index 00000000..d9e174ff --- /dev/null +++ b/tests/cursor_test.c @@ -0,0 +1,59 @@ +/* + Copyright 2019-2020 David Robillard <http://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("node"); + SerdCursor* const cursor = serd_cursor_new(node, 46, 2); + + assert(serd_cursor_name(cursor) == node); + assert(serd_cursor_line(cursor) == 46); + assert(serd_cursor_column(cursor) == 2); + + SerdCursor* const copy = serd_cursor_copy(cursor); + + assert(serd_cursor_equals(cursor, copy)); + assert(!serd_cursor_copy(NULL)); + + SerdNode* const other_node = serd_new_string("other"); + SerdCursor* const other_file = serd_cursor_new(other_node, 46, 2); + SerdCursor* const other_line = serd_cursor_new(node, 47, 2); + SerdCursor* const other_col = serd_cursor_new(node, 46, 3); + + assert(!serd_cursor_equals(cursor, other_file)); + assert(!serd_cursor_equals(cursor, other_line)); + assert(!serd_cursor_equals(cursor, other_col)); + assert(!serd_cursor_equals(cursor, NULL)); + assert(!serd_cursor_equals(NULL, cursor)); + + serd_cursor_free(other_col); + serd_cursor_free(other_line); + serd_cursor_free(other_file); + serd_node_free(other_node); + serd_cursor_free(copy); + serd_cursor_free(cursor); + serd_node_free(node); + + return 0; +} diff --git a/tests/free_null_test.c b/tests/free_null_test.c index 08f2b513..40f72ba7 100644 --- a/tests/free_null_test.c +++ b/tests/free_null_test.c @@ -29,6 +29,7 @@ main(void) serd_env_free(NULL); serd_reader_free(NULL); serd_writer_free(NULL); + serd_cursor_free(NULL); return 0; } diff --git a/tests/read_chunk_test.c b/tests/read_chunk_test.c index 7abd865f..6efdc37f 100644 --- a/tests/read_chunk_test.c +++ b/tests/read_chunk_test.c @@ -88,7 +88,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/tests/serd_test.c b/tests/serd_test.c index a05606a3..786c92f3 100644 --- a/tests/serd_test.c +++ b/tests/serd_test.c @@ -214,7 +214,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(rt->n_statements == 1); @@ -145,6 +145,7 @@ lib_headers = ['src/reader.h'] lib_source = ['src/base64.c', 'src/byte_source.c', + 'src/cursor.c', 'src/env.c', 'src/n3.c', 'src/node.c', @@ -216,6 +217,7 @@ def build(bld): # Test programs for prog in [('serdi_static', 'src/serdi.c'), + ('cursor_test', 'tests/cursor_test.c'), ('env_test', 'tests/env_test.c'), ('free_null_test', 'tests/free_null_test.c'), ('read_chunk_test', 'tests/read_chunk_test.c'), @@ -540,6 +542,7 @@ def test(tst): srcdir = tst.path.abspath() with tst.group('Unit') as check: + check(['./cursor_test']) check(['./env_test']) check(['./free_null_test']) check(['./read_chunk_test']) |