Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
jsonpointer_position.h
1#ifndef SOURCEMETA_CORE_JSONPOINTER_POSITION_H_
2#define SOURCEMETA_CORE_JSONPOINTER_POSITION_H_
3
4#ifndef SOURCEMETA_CORE_JSONPOINTER_EXPORT
5#include <sourcemeta/core/jsonpointer_export.h>
6#endif
7
8#include <sourcemeta/core/json.h>
9
10#include <sourcemeta/core/jsonpointer_pointer.h>
11
12#include <cstddef> // std::size_t
13#include <cstdint> // std::uint64_t
14#include <optional> // std::optional
15#include <tuple> // std::tuple
16#include <unordered_map> // std::unordered_map
17#include <vector> // std::vector
18
19namespace sourcemeta::core {
20
51class SOURCEMETA_CORE_JSONPOINTER_EXPORT PointerPositionTracker {
52public:
56 using Position =
57 std::tuple<std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t>;
58 auto operator()(const JSON::ParsePhase phase, const JSON::Type,
59 const std::uint64_t line, const std::uint64_t column,
60 const JSON::ParseContext context, const std::size_t index,
61 const JSON::String &property) -> void;
63 [[nodiscard]] auto get(const Pointer &pointer) const
64 -> std::optional<Position>;
66 [[nodiscard]] auto size() const -> std::size_t;
68 [[nodiscard]] auto to_json() const -> JSON;
69
70private:
71 struct Event {
72 JSON::ParsePhase phase;
73 JSON::ParseContext context;
74 std::size_t index;
75 const JSON::String *property;
76 std::uint64_t line;
77 std::uint64_t column;
78 };
79
80 struct TrieNode {
81 std::optional<Position> position;
82 std::unordered_map<std::size_t, std::size_t> index_children;
83 std::unordered_map<std::string_view, std::size_t> property_children;
84 };
85
86 auto ensure_index() const -> void;
87
88// Exporting symbols that depends on the standard C++ library is considered
89// safe.
90// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN
91#if defined(_MSC_VER)
92#pragma warning(disable : 4251)
93#endif
94 std::vector<Event> events;
95 mutable bool indexed{false};
96 mutable std::vector<TrieNode> trie;
97#if defined(_MSC_VER)
98#pragma warning(default : 4251)
99#endif
100};
101
102} // namespace sourcemeta::core
103
104#endif
std::basic_string< Char, CharTraits, Allocator< Char > > String
The string type used by the JSON document.
Definition json_value.h:52
Type
The different types of a JSON instance.
Definition json_value.h:69
ParsePhase
The parsing phase of a JSON document.
Definition json_value.h:60
ParseContext
The context type for parse callbacks.
Definition json_value.h:92
Definition json_value.h:39
auto size() const -> std::size_t
Get the number of tracked positions.
auto get(const Pointer &pointer) const -> std::optional< Position >
Get the position of the value at a given pointer.
auto to_json() const -> JSON
Serialise the tracked positions as a JSON document.
std::tuple< std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t > Position
The start and end line and column numbers of a value.
Definition jsonpointer_position.h:56
GenericPointer< JSON::String, PropertyHashJSON< JSON::String > > Pointer
The pointer type used to look up positions.
Definition jsonpointer_position.h:54
Definition jsonpointer_pointer.h:25
Definition jsonpointer_position.h:51