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 /include/serd | |
parent | 3b89559e4c08202bee228e25ea42a4371e333c9b (diff) | |
download | serd-38f85ad8c53c618033e3d0b7cb1fe782c38cf0a3.tar.gz serd-38f85ad8c53c618033e3d0b7cb1fe782c38cf0a3.tar.bz2 serd-38f85ad8c53c618033e3d0b7cb1fe782c38cf0a3.zip |
Add SerdCaret
Diffstat (limited to 'include/serd')
-rw-r--r-- | include/serd/caret.h | 77 | ||||
-rw-r--r-- | include/serd/error.h | 11 | ||||
-rw-r--r-- | include/serd/reader.h | 8 | ||||
-rw-r--r-- | include/serd/serd.h | 1 |
4 files changed, 88 insertions, 9 deletions
diff --git a/include/serd/caret.h b/include/serd/caret.h new file mode 100644 index 00000000..2ed77412 --- /dev/null +++ b/include/serd/caret.h @@ -0,0 +1,77 @@ +// Copyright 2011-2022 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#ifndef SERD_CARET_H +#define SERD_CARET_H + +#include "serd/attributes.h" +#include "serd/node.h" + +#include <stdbool.h> + +SERD_BEGIN_DECLS + +/** + @defgroup serd_caret Caret + @ingroup serd_data + @{ +*/ + +/// 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 document + node, so `document` must have a longer lifetime than the caret for it to be + valid. That is, serd_caret_document() will return exactly the pointer + `document`, not a copy. + + @param document The document or the caret refers to (usually a file URI) + @param line The line number in the document (1-based) + @param column 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 document, + unsigned line, + unsigned column); + +/// 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 URI or 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_document(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); + +/** + @} +*/ + +SERD_END_DECLS + +#endif // SERD_CARET_H diff --git a/include/serd/error.h b/include/serd/error.h index 7be1d372..7495ce3b 100644 --- a/include/serd/error.h +++ b/include/serd/error.h @@ -5,6 +5,7 @@ #define SERD_ERROR_H #include "serd/attributes.h" +#include "serd/caret.h" #include "serd/status.h" #include <stdarg.h> @@ -19,12 +20,10 @@ SERD_BEGIN_DECLS /// 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; /** diff --git a/include/serd/reader.h b/include/serd/reader.h index aedc5cb3..cc19a9b8 100644 --- a/include/serd/reader.h +++ b/include/serd/reader.h @@ -5,6 +5,7 @@ #define SERD_READER_H #include "serd/attributes.h" +#include "serd/node.h" #include "serd/sink.h" #include "serd/status.h" #include "serd/stream.h" @@ -73,13 +74,14 @@ 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/include/serd/serd.h b/include/serd/serd.h index eb33d329..8de151c6 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -57,6 +57,7 @@ @{ */ +#include "serd/caret.h" #include "serd/node.h" #include "serd/statement.h" #include "serd/uri.h" |