/src/CMake/Source/cmJSONState.h
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst or https://cmake.org/licensing for details. */ |
3 | | #pragma once |
4 | | |
5 | | #include "cmConfigure.h" // IWYU pragma: keep |
6 | | |
7 | | #include <cstddef> |
8 | | #include <istream> |
9 | | #include <string> |
10 | | #include <utility> |
11 | | #include <vector> |
12 | | |
13 | | namespace Json { |
14 | | class Value; |
15 | | } |
16 | | |
17 | | class cmJSONState |
18 | | { |
19 | | using Location = struct |
20 | | { |
21 | | int line; |
22 | | int column; |
23 | | }; |
24 | | |
25 | | public: |
26 | | using JsonPair = std::pair<std::string const, Json::Value const*>; |
27 | | |
28 | | enum class StrictMode |
29 | | { |
30 | | Strict, |
31 | | Relaxed, |
32 | | }; |
33 | | |
34 | 33.7k | cmJSONState() = default; |
35 | | cmJSONState(std::string jsonFile, Json::Value* root, |
36 | | StrictMode strictMode = StrictMode::Strict); |
37 | | cmJSONState(std::istream& jsonIStream, Json::Value* root, |
38 | | StrictMode strictMode = StrictMode::Strict); |
39 | | |
40 | | void AddError(std::string const& errMsg); |
41 | | void AddErrorAtValue(std::string const& errMsg, Json::Value const* value); |
42 | | void AddErrorAtOffset(std::string const& errMsg, std::ptrdiff_t offset); |
43 | | std::string GetErrorMessage(bool showContext = true); |
44 | | std::string key(); |
45 | | std::string key_after(std::string const& key); |
46 | | Json::Value const* value_after(std::string const& key); |
47 | | void push_stack(std::string const& key, Json::Value const* value); |
48 | | void pop_stack(); |
49 | | |
50 | | class Error |
51 | | { |
52 | | public: |
53 | | Error(Location loc, std::string errMsg) |
54 | 429k | : location(loc) |
55 | 429k | , message(std::move(errMsg)) {}; |
56 | | Error(std::string errMsg) |
57 | 13.3k | : location({ -1, -1 }) |
58 | 13.3k | , message(std::move(errMsg)) {}; |
59 | 0 | std::string GetErrorMessage() const { return message; } |
60 | 0 | Location GetLocation() const { return location; } |
61 | | |
62 | | private: |
63 | | Location location; |
64 | | std::string message; |
65 | | }; |
66 | | |
67 | | std::vector<JsonPair> parseStack; |
68 | | std::vector<Error> errors; |
69 | | std::string doc; |
70 | | bool allowComments = false; |
71 | | |
72 | | private: |
73 | | void ReadJSONStream(std::istream& jsonIStream, Json::Value* root, |
74 | | StrictMode strictMode); |
75 | | std::string GetJsonContext(Location loc); |
76 | | Location LocateInDocument(ptrdiff_t offset); |
77 | | std::string Filename; |
78 | | }; |