CMake Nightly Date Stamp
[kiteware-cmake.git] / Source / cmJSONState.h
blob3223453af38eeb65e172c38bb69b439ae3009773
1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #pragma once
5 #include "cmConfigure.h" // IWYU pragma: keep
7 #include <cstddef>
8 #include <string>
9 #include <utility>
10 #include <vector>
12 #include "cmStringAlgorithms.h"
14 namespace Json {
15 class Value;
18 class cmJSONState
20 using Location = struct
22 int line;
23 int column;
26 public:
27 using JsonPair = std::pair<const std::string, const Json::Value*>;
28 cmJSONState() = default;
29 cmJSONState(const std::string& filename, Json::Value* root);
30 void AddError(std::string const& errMsg);
31 void AddErrorAtValue(std::string const& errMsg, const Json::Value* value);
32 void AddErrorAtOffset(std::string const& errMsg, std::ptrdiff_t offset);
33 std::string GetErrorMessage(bool showContext = true);
34 std::string key();
35 std::string key_after(std::string const& key);
36 const Json::Value* value_after(std::string const& key);
37 void push_stack(std::string const& key, const Json::Value* value);
38 void pop_stack();
40 class Error
42 public:
43 Error(Location loc, std::string errMsg)
44 : location(loc)
45 , message(std::move(errMsg)){};
46 Error(std::string errMsg)
47 : location({ -1, -1 })
48 , message(std::move(errMsg)){};
49 std::string GetErrorMessage() const
51 std::string output = message;
52 if (location.line > 0) {
53 output = cmStrCat("Error: @", location.line, ",", location.column,
54 ": ", output);
56 return output;
58 Location GetLocation() const { return location; }
60 private:
61 Location location;
62 std::string message;
65 std::vector<JsonPair> parseStack;
66 std::vector<Error> errors;
67 std::string doc;
68 bool allowComments = false;
70 private:
71 std::string GetJsonContext(Location loc);
72 Location LocateInDocument(ptrdiff_t offset);