diff options
author | David Robillard <d@drobilla.net> | 2022-10-23 22:11:35 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 18:49:07 -0500 |
commit | 38f85ad8c53c618033e3d0b7cb1fe782c38cf0a3 (patch) | |
tree | 8f5f1e7be38d98c9e354a67bdf7b94a3e35c3a60 /src | |
parent | 3b89559e4c08202bee228e25ea42a4371e333c9b (diff) | |
download | serd-38f85ad8c53c618033e3d0b7cb1fe782c38cf0a3.tar.gz serd-38f85ad8c53c618033e3d0b7cb1fe782c38cf0a3.tar.bz2 serd-38f85ad8c53c618033e3d0b7cb1fe782c38cf0a3.zip |
Add SerdCaret
Diffstat (limited to 'src')
-rw-r--r-- | src/byte_source.c | 45 | ||||
-rw-r--r-- | src/byte_source.h | 25 | ||||
-rw-r--r-- | src/caret.c | 69 | ||||
-rw-r--r-- | src/caret.h | 15 | ||||
-rw-r--r-- | src/reader.c | 31 | ||||
-rw-r--r-- | src/serdi.c | 12 | ||||
-rw-r--r-- | src/world.c | 18 |
7 files changed, 163 insertions, 52 deletions
diff --git a/src/byte_source.c b/src/byte_source.c index d6510eb9..8e44d891 100644 --- a/src/byte_source.c +++ b/src/byte_source.c @@ -5,6 +5,9 @@ #include "system.h" +#include "serd/node.h" +#include "serd/string_view.h" + #include <stdbool.h> #include <stdint.h> #include <string.h> @@ -37,20 +40,21 @@ 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; - source->close_func = close_func; - source->stream = stream; - source->page_size = page_size; - source->buf_size = page_size; - source->cur = cur; - source->from_stream = true; + source->read_func = read_func; + source->error_func = error_func; + source->close_func = close_func; + source->stream = stream; + source->page_size = page_size; + source->buf_size = page_size; + source->name = serd_node_copy(name); + source->caret.document = source->name; + source->caret.line = 1U; + source->caret.col = 1U; + source->from_stream = true; if (page_size > 1) { source->file_buf = (uint8_t*)serd_allocate_buffer(page_size); @@ -82,14 +86,20 @@ 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->page_size = 1; - source->cur = cur; - source->read_buf = (const uint8_t*)utf8; + + source->name = + name ? serd_node_copy(name) : serd_new_string(serd_string("string")); + + source->page_size = 1U; + source->read_buf = (const uint8_t*)utf8; + source->caret.document = source->name; + source->caret.line = 1U; + source->caret.col = 1U; + return SERD_SUCCESS; } @@ -105,6 +115,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 e2697bd2..cc6bc840 100644 --- a/src/byte_source.h +++ b/src/byte_source.h @@ -4,7 +4,11 @@ #ifndef SERD_SRC_BYTE_SOURCE_H #define SERD_SRC_BYTE_SOURCE_H +#include "caret.h" // IWYU pragma: keep + #include "serd/attributes.h" +#include "serd/caret.h" +#include "serd/node.h" #include "serd/status.h" #include "serd/stream.h" @@ -16,19 +20,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 @@ -39,7 +38,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, @@ -47,7 +48,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 @@ -73,11 +74,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..1b0de444 --- /dev/null +++ b/src/caret.c @@ -0,0 +1,69 @@ +// Copyright 2018-2020 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#include "caret.h" + +#include "serd/caret.h" + +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> + +SerdCaret* +serd_caret_new(const SerdNode* const document, + const unsigned line, + const unsigned column) +{ + SerdCaret* caret = (SerdCaret*)malloc(sizeof(SerdCaret)); + + if (caret) { + caret->document = document; + caret->line = line; + caret->col = column; + } + + return caret; +} + +SerdCaret* +serd_caret_copy(const SerdCaret* const caret) +{ + if (!caret) { + return NULL; + } + + SerdCaret* copy = (SerdCaret*)malloc(sizeof(SerdCaret)); + memcpy(copy, caret, sizeof(SerdCaret)); + return copy; +} + +void +serd_caret_free(SerdCaret* const caret) +{ + free(caret); +} + +bool +serd_caret_equals(const SerdCaret* const l, const SerdCaret* const r) +{ + return (l == r || (l && r && serd_node_equals(l->document, r->document) && + l->line == r->line && l->col == r->col)); +} + +const SerdNode* +serd_caret_document(const SerdCaret* const caret) +{ + return caret->document; +} + +unsigned +serd_caret_line(const SerdCaret* const caret) +{ + return caret->line; +} + +unsigned +serd_caret_column(const SerdCaret* const caret) +{ + return caret->col; +} diff --git a/src/caret.h b/src/caret.h new file mode 100644 index 00000000..85b4b4b1 --- /dev/null +++ b/src/caret.h @@ -0,0 +1,15 @@ +// Copyright 2018-2020 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#ifndef SERD_SRC_CARET_H +#define SERD_SRC_CARET_H + +#include "serd/node.h" + +struct SerdCaretImpl { + const SerdNode* document; + unsigned line; + unsigned col; +}; + +#endif // SERD_SRC_CARET_H diff --git a/src/reader.c b/src/reader.c index 2ef3b66a..a9c483c0 100644 --- a/src/reader.c +++ b/src/reader.c @@ -11,6 +11,7 @@ #include "world.h" #include "serd/stream.h" +#include "serd/string_view.h" #include "serd/uri.h" #include <errno.h> @@ -27,8 +28,7 @@ r_err(SerdReader* const reader, const SerdStatus st, const char* const fmt, ...) { va_list args; // NOLINT(cppcoreguidelines-init-variables) 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; @@ -230,7 +230,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( @@ -251,20 +251,25 @@ serd_reader_start_file(SerdReader* reader, const char* uri, bool bulk) return SERD_BAD_STREAM; } - 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 4649f57a..0932348c 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -298,15 +298,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); @@ -320,6 +323,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 ececd646..21cdffab 100644 --- a/src/world.c +++ b/src/world.c @@ -3,10 +3,13 @@ #include "world.h" +#include "caret.h" #include "node.h" #include "serd_config.h" #include "system.h" +#include "serd/node.h" + #include "serd/string_view.h" #if defined(USE_POSIX_FADVISE) @@ -45,10 +48,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->document), + e->caret->line, + e->caret->col); } vfprintf(stderr, e->fmt, *e->args); } @@ -64,7 +70,7 @@ serd_world_verrorf(const SerdWorld* const world, va_list args_copy; va_copy(args_copy, args); - const SerdError e = {st, NULL, 0, 0, fmt, &args_copy}; + const SerdError e = {st, NULL, fmt, &args_copy}; serd_world_error(world, &e); va_end(args_copy); return st; @@ -78,7 +84,7 @@ serd_world_errorf(const SerdWorld* const world, { va_list args; // NOLINT(cppcoreguidelines-init-variables) 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; |